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 | |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 44 | Usage: $0 [-r|-f|-A|-K|-c] [-T PATH] [-t FILE] |
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 | -A: Assert that no build dirs exist when running this command. |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 47 | -c: Enable coverage (requires clang). |
| 48 | -f: Force a reconfiguration by removing existing build dirs. |
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. |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 50 | -r: Reconfigure build dirs, if they exist. |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 51 | -t FILE: Configure Meson with toolchain configuration FILE |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 52 | -T PATH: Build tock from PATH rather than the remote repository. |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 53 | |
| 54 | USAGE |
| 55 | } |
| 56 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 57 | readonly DEFAULT_RISCV_TOOLS=/tools/riscv |
| 58 | TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}" |
| 59 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 60 | FLAGS_assert=false |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 61 | FLAGS_force=false |
| 62 | FLAGS_reconfigure="" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 63 | FLAGS_keep_includes=false |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 64 | FLAGS_specified_toolchain_file=false |
Alphan Ulusoy | d2bbbe1 | 2020-08-14 15:39:51 -0400 | [diff] [blame] | 65 | FLAGS_coverage=false |
Luís Marques | 5849cd1 | 2020-06-26 10:33:55 +0100 | [diff] [blame] | 66 | ARG_toolchain_file="${TOOLCHAIN_PATH}/meson-riscv32-unknown-elf-clang.txt" |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 67 | ARG_tock_dir="" |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 68 | # `getopts` usage |
| 69 | # - The initial colon in the optstring is to suppress the default error |
| 70 | # handling. |
| 71 | # - The remaining options are specified in alphabetical order, and the case |
| 72 | # statement should match this order. |
| 73 | # - Only options that take an argument should have a following colon. |
| 74 | # - The case statement contains two additional cases: |
| 75 | # - when `$flag` = `?`, this is an unexpected option. |
| 76 | # - when `$flag` = `:`, this is the case that a flag which requires an |
| 77 | # argument is not provided one. In both cases, `$OPTARG` contains the |
| 78 | # relevant parsed option. |
| 79 | # - After option parsing is finished, we `shift` by `$OPTIND - 1` so that the |
| 80 | # remaining (unprocessed) arguments are in `$@` (and $1, $2, $3 etc.). |
| 81 | while getopts ':AcfKrt:T:' flag; do |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 82 | case "${flag}" in |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 83 | A) FLAGS_assert=true;; |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 84 | c) FLAGS_coverage=true;; |
| 85 | f) FLAGS_force=true;; |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 86 | K) FLAGS_keep_includes=true;; |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 87 | r) FLAGS_reconfigure="--reconfigure";; |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 88 | t) FLAGS_specified_toolchain_file=true |
| 89 | ARG_toolchain_file="${OPTARG}";; |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 90 | T) ARG_tock_dir="${OPTARG}";; |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 91 | \?) echo "Unexpected option: -${OPTARG}" >&2 |
| 92 | usage |
| 93 | exit 1 |
| 94 | ;; |
| 95 | :) echo "Option -${OPTARG} requires a path argument" >&2 |
| 96 | usage |
| 97 | exit 1 |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 98 | ;; |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 99 | *) echo "Internal Error: Unhandled option: -${flag}" >&2 |
| 100 | exit 1 |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 101 | ;; |
| 102 | esac |
| 103 | done |
Sam Elliott | 30abf91 | 2021-01-04 11:56:13 +0000 | [diff] [blame] | 104 | shift $((OPTIND - 1)) |
| 105 | |
| 106 | # We do not accept additional arguments. |
| 107 | if [[ "$#" -gt 0 ]]; then |
| 108 | echo "Unexpected arguments:" "$@" >&2 |
| 109 | exit 1 |
| 110 | fi |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 111 | |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 112 | if [[ ! -n "$(command -v meson)" ]]; then |
| 113 | echo "Unable to find meson. Please install meson before running this command." >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | if [[ ! -n "$(command -v ninja)" ]]; then |
| 118 | echo "Unable to find ninja. Please install ninja before running this command." >&2 |
| 119 | exit 1 |
| 120 | fi |
| 121 | |
| 122 | if [[ "${FLAGS_force}" == true ]]; then |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 123 | rm -rf "$OBJ_DIR" |
| 124 | rm -rf "$BIN_DIR" |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 125 | fi |
| 126 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 127 | if [[ "${FLAGS_assert}" == true ]]; then |
| 128 | if [[ -e "$OBJ_DIR" ]]; then |
| 129 | echo "Object directory at $OBJ_DIR already exists. Aborting." >&2 |
| 130 | exit 1 |
| 131 | fi |
| 132 | if [[ -e "$BIN_DIR" ]]; then |
| 133 | echo "Binary directory at $BIN_DIR already exists. Aborting." >&2 |
| 134 | exit 1 |
| 135 | fi |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 136 | fi |
| 137 | |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 138 | TOCK_LOCAL=false |
| 139 | if [[ ! -z "${ARG_tock_dir}" ]]; then |
| 140 | TOCK_LOCAL=true |
| 141 | |
| 142 | if [[ ! -d "${ARG_tock_dir}" ]]; then |
| 143 | echo "Referenced Tock directory ${ARG_tock_dir} doesn't exist. Aborting." >&2 |
| 144 | exit 1 |
| 145 | fi |
| 146 | |
| 147 | if [[ ! -f "${TOCK_SYMLINK}" || -L "${TOCK_SYMLINK}" ]]; then |
| 148 | echo "Creating symlink to Tock project at ${ARG_tock_dir}." |
| 149 | ln -sf "${ARG_tock_dir}" "${TOCK_SYMLINK}" |
| 150 | elif [[ -d "${TOCK_SYMLINK}" ]]; then |
| 151 | echo "Tock directory $TOCK_SYMLINK exists in place. Leaving as is." |
| 152 | else |
| 153 | echo "Placing Tock at ${TOCK_SYMLINK} would clobber something. Aborting." >&2 |
| 154 | exit 1 |
| 155 | fi |
| 156 | fi |
| 157 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 158 | reconf="${FLAGS_reconfigure}" |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 159 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 160 | if [[ ! -d "$OBJ_DIR" ]]; then |
| 161 | echo "Output directory does not exist at $OBJ_DIR; creating." >&2 |
| 162 | mkdir -p "$OBJ_DIR" |
| 163 | reconf="" |
| 164 | elif [[ -z "$reconf" ]]; then |
| 165 | echo "Output directory already exists at $OBJ_DIR; skipping." >&2 |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 166 | fi |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 167 | |
Sam Elliott | 36fe3b5 | 2020-08-17 17:21:24 +0100 | [diff] [blame] | 168 | if [[ -f "${ARG_toolchain_file}" ]]; then |
| 169 | echo "Using meson toolchain file at $ARG_toolchain_file." >&2 |
| 170 | else |
| 171 | if [[ "${FLAGS_specified_toolchain_file}" == true ]]; then |
| 172 | echo "Unable to find meson toolchain file at $ARG_toolchain_file. Aborting." >&2 |
| 173 | exit 1 |
| 174 | else |
| 175 | cross_file="$OBJ_DIR/toolchain-configured.txt" |
| 176 | cp toolchain.txt "$cross_file" |
| 177 | perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$cross_file" |
| 178 | touch -r toolchain.txt "$cross_file" |
| 179 | echo "Set up toolchain file at $cross_file." >&2 |
| 180 | ARG_toolchain_file="${cross_file}" |
| 181 | fi |
| 182 | fi |
| 183 | |
Philipp Wagner | 525891d | 2020-10-30 19:01:04 +0000 | [diff] [blame] | 184 | mkdir -p "$BIN_DIR" |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 185 | set -x |
| 186 | meson $reconf \ |
| 187 | -Dot_version="$OT_VERSION" \ |
Philipp Wagner | 525891d | 2020-10-30 19:01:04 +0000 | [diff] [blame] | 188 | -Dbin_dir="$BIN_DIR" \ |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 189 | -Dtock_local="$TOCK_LOCAL" \ |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 190 | -Dkeep_includes="$FLAGS_keep_includes" \ |
Alphan Ulusoy | d2bbbe1 | 2020-08-14 15:39:51 -0400 | [diff] [blame] | 191 | -Dcoverage="$FLAGS_coverage" \ |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 192 | --cross-file="$ARG_toolchain_file" \ |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 193 | "$OBJ_DIR" |