Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 1 | #!/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 | |
| 6 | set -o errexit |
| 7 | set -o pipefail |
| 8 | set -o nounset |
| 9 | |
| 10 | readonly BUILD_DIR_PREFIX="build" |
| 11 | readonly TARGET_VERILATOR="verilator" |
| 12 | readonly TARGET_FPGA="fpga" |
| 13 | |
| 14 | function usage() { |
| 15 | cat << USAGE |
| 16 | Configure Meson build targets. |
| 17 | |
| 18 | Usage: $0 [-r|-f] |
| 19 | |
| 20 | -f: Remove build directories before running Meson. |
| 21 | -r: Force reconfiguration of build directories. |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame^] | 22 | -K: Keep include search paths as generated by Meson. |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 23 | |
| 24 | USAGE |
| 25 | } |
| 26 | |
| 27 | FLAGS_force=false |
| 28 | FLAGS_reconfigure="" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame^] | 29 | FLAGS_keep_includes=false |
| 30 | while getopts 'r?:f?:K?' flag; do |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 31 | case "${flag}" in |
| 32 | f) FLAGS_force=true;; |
| 33 | r) FLAGS_reconfigure="--reconfigure";; |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame^] | 34 | K) FLAGS_keep_includes=true;; |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 35 | ?) usage && exit 1;; |
| 36 | *) usage |
| 37 | error "Unexpected option ${flag}" |
| 38 | ;; |
| 39 | esac |
| 40 | done |
| 41 | |
| 42 | if [[ "${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 |
| 46 | fi |
| 47 | |
| 48 | if [[ ! -n "$(command -v meson)" ]]; then |
| 49 | echo "Unable to find meson. Please install meson before running this command." >&2 |
| 50 | exit 1 |
| 51 | fi |
| 52 | |
| 53 | if [[ ! -n "$(command -v ninja)" ]]; then |
| 54 | echo "Unable to find ninja. Please install ninja before running this command." >&2 |
| 55 | exit 1 |
| 56 | fi |
| 57 | |
| 58 | if [[ "${FLAGS_force}" == true ]]; then |
| 59 | for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do |
| 60 | rm -rf "${BUILD_DIR_PREFIX}-${target_suffix}" |
| 61 | done |
| 62 | fi |
| 63 | |
| 64 | if [[ ! -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 |
| 73 | fi |
| 74 | |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame^] | 75 | # 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. |
| 85 | function 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 Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 94 | meson ${FLAGS_reconfigure} "-Dtarget=${TARGET_VERILATOR}" --cross-file=toolchain.txt \ |
| 95 | --buildtype=plain "${BUILD_DIR_PREFIX}-${TARGET_VERILATOR}" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame^] | 96 | purge_includes ${TARGET_VERILATOR} |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 97 | |
| 98 | meson ${FLAGS_reconfigure} "-Dtarget=${TARGET_FPGA}" --cross-file=toolchain.txt \ |
| 99 | --buildtype=plain "${BUILD_DIR_PREFIX}-${TARGET_FPGA}" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame^] | 100 | purge_includes ${TARGET_FPGA} |