blob: 34b25b74957fea1aa59c6bfc95cd545b5317d128 [file] [log] [blame]
Miguel Osorio03f2e232019-09-17 19:44:37 -07001#!/bin/bash
2# Copyright lowRISC contributors.
3# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4# SPDX-License-Identifier: Apache-2.0
5
6set -o errexit
7set -o pipefail
8set -o nounset
9
10readonly BUILD_DIR_PREFIX="build"
11readonly TARGET_VERILATOR="verilator"
12readonly TARGET_FPGA="fpga"
13
14function usage() {
15 cat << USAGE
16Configure Meson build targets.
17
18Usage: $0 [-r|-f]
19
20 -f: Remove build directories before running Meson.
21 -r: Force reconfiguration of build directories.
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050022 -K: Keep include search paths as generated by Meson.
Miguel Osorio03f2e232019-09-17 19:44:37 -070023
24USAGE
25}
26
27FLAGS_force=false
28FLAGS_reconfigure=""
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050029FLAGS_keep_includes=false
30while getopts 'r?:f?:K?' flag; do
Miguel Osorio03f2e232019-09-17 19:44:37 -070031 case "${flag}" in
32 f) FLAGS_force=true;;
33 r) FLAGS_reconfigure="--reconfigure";;
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050034 K) FLAGS_keep_includes=true;;
Miguel Osorio03f2e232019-09-17 19:44:37 -070035 ?) usage && exit 1;;
36 *) usage
37 error "Unexpected option ${flag}"
38 ;;
39 esac
40done
41
42if [[ "${FLAGS_force}" == true && -n "${FLAGS_reconfigure}" ]]; then
43 usage >&2
44 echo "Error: -r and -f cannont be used at the same time." >&2
45 exit 1
46fi
47
48if [[ ! -n "$(command -v meson)" ]]; then
49 echo "Unable to find meson. Please install meson before running this command." >&2
50 exit 1
51fi
52
53if [[ ! -n "$(command -v ninja)" ]]; then
54 echo "Unable to find ninja. Please install ninja before running this command." >&2
55 exit 1
56fi
57
58if [[ "${FLAGS_force}" == true ]]; then
59 for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do
60 rm -rf "${BUILD_DIR_PREFIX}-${target_suffix}"
61 done
62fi
63
64if [[ ! -n "${FLAGS_reconfigure}" ]] ; then
65 for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do
66 if [[ -d "${BUILD_DIR_PREFIX}-${target_suffix}" ]]; then
67 usage >&2
68 echo "Error: ${BUILD_DIR_PREFIX}-${target_suffix} already exists. " \
69 "Remove directory, or rerun $0 with the -r option" >&2
70 exit 1
71 fi
72 done
73fi
74
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050075# purge_includes $target deletes any -I command line arguments that are not
76# - Absolute paths.
77# - Ephemeral build directories.
78#
79# This function is necessary because Meson does not give adequate
80# control over what directories are passed in as -I search directories
81# to the C compiler. While Meson does provide |implicit_include_directories|,
82# support for this option is poor: empirically, Meson ignores this option for
83# some targerts. Doing it as a post-processing step ensures that Meson does
84# not allow improper #includes to compile successfully.
85function purge_includes() {
86 if [[ "${FLAGS_keep_includes}" == "true" ]]; then
87 return
88 fi
89 echo "Purging superfluous -I arguments from $1."
90 local ninja_file="${BUILD_DIR_PREFIX}-$1/build.ninja"
91 perl -pi -e 's#-I[^/][^@ ]+ # #g' -- "$ninja_file"
92}
93
Miguel Osorio03f2e232019-09-17 19:44:37 -070094meson ${FLAGS_reconfigure} "-Dtarget=${TARGET_VERILATOR}" --cross-file=toolchain.txt \
95 --buildtype=plain "${BUILD_DIR_PREFIX}-${TARGET_VERILATOR}"
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050096purge_includes ${TARGET_VERILATOR}
Miguel Osorio03f2e232019-09-17 19:44:37 -070097
98meson ${FLAGS_reconfigure} "-Dtarget=${TARGET_FPGA}" --cross-file=toolchain.txt \
99 --buildtype=plain "${BUILD_DIR_PREFIX}-${TARGET_FPGA}"
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -0500100purge_includes ${TARGET_FPGA}