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 |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 9 | set -x |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 10 | |
| 11 | readonly BUILD_DIR_PREFIX="build" |
| 12 | readonly TARGET_VERILATOR="verilator" |
| 13 | readonly TARGET_FPGA="fpga" |
| 14 | |
| 15 | function usage() { |
| 16 | cat << USAGE |
| 17 | Configure Meson build targets. |
| 18 | |
| 19 | Usage: $0 [-r|-f] |
| 20 | |
| 21 | -f: Remove build directories before running Meson. |
| 22 | -r: Force reconfiguration of build directories. |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 23 | -K: Keep include search paths as generated by Meson. |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 24 | |
| 25 | USAGE |
| 26 | } |
| 27 | |
| 28 | FLAGS_force=false |
| 29 | FLAGS_reconfigure="" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 30 | FLAGS_keep_includes=false |
| 31 | while getopts 'r?:f?:K?' flag; do |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 32 | case "${flag}" in |
| 33 | f) FLAGS_force=true;; |
| 34 | r) FLAGS_reconfigure="--reconfigure";; |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 35 | K) FLAGS_keep_includes=true;; |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 36 | ?) usage && exit 1;; |
| 37 | *) usage |
| 38 | error "Unexpected option ${flag}" |
| 39 | ;; |
| 40 | esac |
| 41 | done |
| 42 | |
| 43 | if [[ "${FLAGS_force}" == true && -n "${FLAGS_reconfigure}" ]]; then |
| 44 | usage >&2 |
| 45 | echo "Error: -r and -f cannont be used at the same time." >&2 |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
| 49 | if [[ ! -n "$(command -v meson)" ]]; then |
| 50 | echo "Unable to find meson. Please install meson before running this command." >&2 |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | if [[ ! -n "$(command -v ninja)" ]]; then |
| 55 | echo "Unable to find ninja. Please install ninja before running this command." >&2 |
| 56 | exit 1 |
| 57 | fi |
| 58 | |
| 59 | if [[ "${FLAGS_force}" == true ]]; then |
| 60 | for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do |
| 61 | rm -rf "${BUILD_DIR_PREFIX}-${target_suffix}" |
| 62 | done |
| 63 | fi |
| 64 | |
| 65 | if [[ ! -n "${FLAGS_reconfigure}" ]] ; then |
| 66 | for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do |
| 67 | if [[ -d "${BUILD_DIR_PREFIX}-${target_suffix}" ]]; then |
| 68 | usage >&2 |
| 69 | echo "Error: ${BUILD_DIR_PREFIX}-${target_suffix} already exists. " \ |
| 70 | "Remove directory, or rerun $0 with the -r option" >&2 |
| 71 | exit 1 |
| 72 | fi |
| 73 | done |
| 74 | fi |
| 75 | |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 76 | readonly DEFAULT_RISCV_TOOLS=/tools/riscv |
| 77 | TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}" |
| 78 | CROSS_FILE=$(mktemp /tmp/toolchain.XXXXXX.txt) |
| 79 | cp toolchain.txt "$CROSS_FILE" |
| 80 | perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$CROSS_FILE" |
| 81 | echo "Set up toolchain file at $CROSS_FILE." >&2 |
| 82 | |
| 83 | # purge_includes $build_dir deletes any -I command line arguments that are not |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 84 | # - Absolute paths. |
| 85 | # - Ephemeral build directories. |
| 86 | # |
| 87 | # This function is necessary because Meson does not give adequate |
| 88 | # control over what directories are passed in as -I search directories |
| 89 | # to the C compiler. While Meson does provide |implicit_include_directories|, |
| 90 | # support for this option is poor: empirically, Meson ignores this option for |
| 91 | # some targerts. Doing it as a post-processing step ensures that Meson does |
| 92 | # not allow improper #includes to compile successfully. |
| 93 | function purge_includes() { |
| 94 | if [[ "${FLAGS_keep_includes}" == "true" ]]; then |
| 95 | return |
| 96 | fi |
| 97 | echo "Purging superfluous -I arguments from $1." |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 98 | local ninja_file="$1/build.ninja" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 99 | perl -pi -e 's#-I[^/][^@ ]+ # #g' -- "$ninja_file" |
| 100 | } |
| 101 | |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 102 | # configure_meson $target generates a build directory at build-$target. |
| 103 | function configure_meson() { |
| 104 | local target="$1" |
| 105 | local build_dir="${BUILD_DIR_PREFIX}-$target" |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 106 | |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 107 | mkdir -p "$build_dir" |
| 108 | meson ${FLAGS_reconfigure} \ |
| 109 | -Dtarget="$target" \ |
| 110 | --cross-file="$CROSS_FILE" \ |
| 111 | --buildtype=plain \ |
| 112 | "$build_dir" |
| 113 | purge_includes "$build_dir" |
| 114 | } |
| 115 | |
| 116 | configure_meson "${TARGET_VERILATOR}" |
| 117 | configure_meson "${TARGET_FPGA}" |