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 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 10 | # meson_init.sh configures OpenTitan's Meson build system. Rather than calling |
| 11 | # Meson directly, this script should be used instead, since it handles things |
| 12 | # that Meson does not out of the box, such as: |
| 13 | # - The RISC-V toolchain. |
| 14 | # - Idempotence. |
| 15 | # - Building for multiple device platforms. |
| 16 | # |
| 17 | # This script's semantics for creating build directories are as follows: |
| 18 | # - By default, this script will create any missing build directories and |
| 19 | # configure them. It is idempotent: if there is no work to be done, nothing |
| 20 | # will change and this script will exit successfully. |
| 21 | # - Passing -r will reconfigure existing build directories. This should be used |
| 22 | # when editing meson.build files. |
| 23 | # - Passing -f will erase existing build directories. This should be used when |
| 24 | # existing configuration is completely broken, or a clean build is desired |
| 25 | # (neither of these should be necessary). |
| 26 | # - Passing -A will explicitly disable idempotence, and cause the script to exit |
| 27 | # if a build directory already exists. This should be used only in CI, where |
| 28 | # such error-checking is desireable. |
| 29 | # |
| 30 | # Note that only the first option above is actually guaranteed to be idempotent. |
| 31 | |
Miguel Young de la Sota | 6379357 | 2019-11-13 14:18:51 -0600 | [diff] [blame] | 32 | . util/build_consts.sh |
| 33 | |
| 34 | echo "Detected \$REPO_TOP at $REPO_TOP." |
| 35 | echo "Object directory set at $OBJ_DIR." |
| 36 | echo "Binary directory set at $BIN_DIR." |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 37 | echo "OpenTitan version: $OT_VERSION" |
Miguel Young de la Sota | 6379357 | 2019-11-13 14:18:51 -0600 | [diff] [blame] | 38 | echo |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 39 | |
| 40 | function usage() { |
| 41 | cat << USAGE |
| 42 | Configure Meson build targets. |
| 43 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 44 | Usage: $0 [-r|-f|-A|-K] |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 45 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 46 | -f: Force a reconfiguration by removing existing build dirs. |
| 47 | -r: Reconfigure build dirs, if they exist. |
| 48 | -A: Assert that no build dirs exist when running this command. |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 49 | -K: Keep include search paths as generated by Meson. |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 50 | |
| 51 | USAGE |
| 52 | } |
| 53 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 54 | FLAGS_assert=false |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 55 | FLAGS_force=false |
| 56 | FLAGS_reconfigure="" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 57 | FLAGS_keep_includes=false |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 58 | while getopts 'r?:f?:K?:A?' flag; do |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 59 | case "${flag}" in |
| 60 | f) FLAGS_force=true;; |
| 61 | r) FLAGS_reconfigure="--reconfigure";; |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 62 | A) FLAGS_assert=true;; |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 63 | K) FLAGS_keep_includes=true;; |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 64 | ?) usage && exit 1;; |
| 65 | *) usage |
| 66 | error "Unexpected option ${flag}" |
| 67 | ;; |
| 68 | esac |
| 69 | done |
| 70 | |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 71 | if [[ ! -n "$(command -v meson)" ]]; then |
| 72 | echo "Unable to find meson. Please install meson before running this command." >&2 |
| 73 | exit 1 |
| 74 | fi |
| 75 | |
| 76 | if [[ ! -n "$(command -v ninja)" ]]; then |
| 77 | echo "Unable to find ninja. Please install ninja before running this command." >&2 |
| 78 | exit 1 |
| 79 | fi |
| 80 | |
| 81 | if [[ "${FLAGS_force}" == true ]]; then |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 82 | rm -rf "$OBJ_DIR" |
| 83 | rm -rf "$BIN_DIR" |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 84 | fi |
| 85 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 86 | if [[ "${FLAGS_assert}" == true ]]; then |
| 87 | if [[ -e "$OBJ_DIR" ]]; then |
| 88 | echo "Object directory at $OBJ_DIR already exists. Aborting." >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | if [[ -e "$BIN_DIR" ]]; then |
| 92 | echo "Binary directory at $BIN_DIR already exists. Aborting." >&2 |
| 93 | exit 1 |
| 94 | fi |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 95 | fi |
| 96 | |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 97 | readonly DEFAULT_RISCV_TOOLS=/tools/riscv |
| 98 | TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}" |
| 99 | CROSS_FILE=$(mktemp /tmp/toolchain.XXXXXX.txt) |
| 100 | cp toolchain.txt "$CROSS_FILE" |
| 101 | perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$CROSS_FILE" |
| 102 | echo "Set up toolchain file at $CROSS_FILE." >&2 |
| 103 | |
| 104 | # 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] | 105 | # - Absolute paths. |
| 106 | # - Ephemeral build directories. |
| 107 | # |
| 108 | # This function is necessary because Meson does not give adequate |
| 109 | # control over what directories are passed in as -I search directories |
| 110 | # to the C compiler. While Meson does provide |implicit_include_directories|, |
| 111 | # support for this option is poor: empirically, Meson ignores this option for |
| 112 | # some targerts. Doing it as a post-processing step ensures that Meson does |
| 113 | # not allow improper #includes to compile successfully. |
| 114 | function purge_includes() { |
| 115 | if [[ "${FLAGS_keep_includes}" == "true" ]]; then |
| 116 | return |
| 117 | fi |
| 118 | echo "Purging superfluous -I arguments from $1." |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 119 | local ninja_file="$1/build.ninja" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 120 | perl -pi -e 's#-I[^/][^@ ]+ # #g' -- "$ninja_file" |
| 121 | } |
| 122 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 123 | reconf="${FLAGS_reconfigure}" |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 124 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 125 | if [[ ! -d "$OBJ_DIR" ]]; then |
| 126 | echo "Output directory does not exist at $OBJ_DIR; creating." >&2 |
| 127 | mkdir -p "$OBJ_DIR" |
| 128 | reconf="" |
| 129 | elif [[ -z "$reconf" ]]; then |
| 130 | echo "Output directory already exists at $OBJ_DIR; skipping." >&2 |
| 131 | continue |
| 132 | fi |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 133 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 134 | mkdir -p "$DEV_BIN_DIR" |
| 135 | set -x |
| 136 | meson $reconf \ |
| 137 | -Dot_version="$OT_VERSION" \ |
| 138 | -Ddev_bin_dir="$DEV_BIN_DIR" \ |
| 139 | -Dhost_bin_dir="$HOST_BIN_DIR" \ |
| 140 | --cross-file="$CROSS_FILE" \ |
| 141 | "$OBJ_DIR" |
| 142 | { set +x; } 2>/dev/null |
| 143 | purge_includes "$OBJ_DIR" |