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 | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 44 | Usage: $0 [-r|-f|-A|-K|-T|-t] |
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. |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 50 | -t FILE: Configure Meson with toolchain configuration FILE |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 51 | -T PATH: Build tock from PATH rather than the remote repository. |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 52 | |
| 53 | USAGE |
| 54 | } |
| 55 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 56 | readonly DEFAULT_RISCV_TOOLS=/tools/riscv |
| 57 | TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}" |
| 58 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 59 | FLAGS_assert=false |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 60 | FLAGS_force=false |
| 61 | FLAGS_reconfigure="" |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 62 | FLAGS_keep_includes=false |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 63 | FLAGS_specified_toolchain_file=false |
Luís Marques | 5849cd1 | 2020-06-26 10:33:55 +0100 | [diff] [blame] | 64 | ARG_toolchain_file="${TOOLCHAIN_PATH}/meson-riscv32-unknown-elf-clang.txt" |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 65 | ARG_tock_dir="" |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 66 | while getopts 'r?:f?:K?:A?:T:t:' flag; do |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 67 | case "${flag}" in |
| 68 | f) FLAGS_force=true;; |
| 69 | r) FLAGS_reconfigure="--reconfigure";; |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 70 | A) FLAGS_assert=true;; |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 71 | K) FLAGS_keep_includes=true;; |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 72 | t) FLAGS_specified_toolchain_file=true |
| 73 | ARG_toolchain_file="${OPTARG}";; |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 74 | T) ARG_tock_dir="${OPTARG}";; |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 75 | ?) usage && exit 1;; |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 76 | :) usage |
| 77 | error "${OPTARG} requires a path argument" |
| 78 | ;; |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 79 | *) usage |
| 80 | error "Unexpected option ${flag}" |
| 81 | ;; |
| 82 | esac |
| 83 | done |
| 84 | |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 85 | if [[ ! -n "$(command -v meson)" ]]; then |
| 86 | echo "Unable to find meson. Please install meson before running this command." >&2 |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | if [[ ! -n "$(command -v ninja)" ]]; then |
| 91 | echo "Unable to find ninja. Please install ninja before running this command." >&2 |
| 92 | exit 1 |
| 93 | fi |
| 94 | |
| 95 | if [[ "${FLAGS_force}" == true ]]; then |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 96 | rm -rf "$OBJ_DIR" |
| 97 | rm -rf "$BIN_DIR" |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 98 | fi |
| 99 | |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 100 | if [[ "${FLAGS_assert}" == true ]]; then |
| 101 | if [[ -e "$OBJ_DIR" ]]; then |
| 102 | echo "Object directory at $OBJ_DIR already exists. Aborting." >&2 |
| 103 | exit 1 |
| 104 | fi |
| 105 | if [[ -e "$BIN_DIR" ]]; then |
| 106 | echo "Binary directory at $BIN_DIR already exists. Aborting." >&2 |
| 107 | exit 1 |
| 108 | fi |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 109 | fi |
| 110 | |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 111 | TOCK_LOCAL=false |
| 112 | if [[ ! -z "${ARG_tock_dir}" ]]; then |
| 113 | TOCK_LOCAL=true |
| 114 | |
| 115 | if [[ ! -d "${ARG_tock_dir}" ]]; then |
| 116 | echo "Referenced Tock directory ${ARG_tock_dir} doesn't exist. Aborting." >&2 |
| 117 | exit 1 |
| 118 | fi |
| 119 | |
| 120 | if [[ ! -f "${TOCK_SYMLINK}" || -L "${TOCK_SYMLINK}" ]]; then |
| 121 | echo "Creating symlink to Tock project at ${ARG_tock_dir}." |
| 122 | ln -sf "${ARG_tock_dir}" "${TOCK_SYMLINK}" |
| 123 | elif [[ -d "${TOCK_SYMLINK}" ]]; then |
| 124 | echo "Tock directory $TOCK_SYMLINK exists in place. Leaving as is." |
| 125 | else |
| 126 | echo "Placing Tock at ${TOCK_SYMLINK} would clobber something. Aborting." >&2 |
| 127 | exit 1 |
| 128 | fi |
| 129 | fi |
| 130 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 131 | if [[ -f "${ARG_toolchain_file}" ]]; then |
| 132 | echo "Using meson toolchain file at $ARG_toolchain_file." >&2 |
| 133 | else |
| 134 | if [[ "${FLAGS_specified_toolchain_file}" == true ]]; then |
| 135 | echo "Unable to find meson toolchain file at $ARG_toolchain_file. Aborting." >&2 |
| 136 | exit 1 |
| 137 | else |
| 138 | cross_file=$(mktemp /tmp/toolchain.XXXXXX.txt) |
| 139 | cp toolchain.txt "$cross_file" |
| 140 | perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$cross_file" |
| 141 | echo "Set up toolchain file at $cross_file." >&2 |
| 142 | ARG_toolchain_file="${cross_file}" |
| 143 | fi |
| 144 | fi |
Miguel Young de la Sota | d258b33 | 2019-10-29 14:05:23 -0500 | [diff] [blame] | 145 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 146 | reconf="${FLAGS_reconfigure}" |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 147 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 148 | if [[ ! -d "$OBJ_DIR" ]]; then |
| 149 | echo "Output directory does not exist at $OBJ_DIR; creating." >&2 |
| 150 | mkdir -p "$OBJ_DIR" |
| 151 | reconf="" |
| 152 | elif [[ -z "$reconf" ]]; then |
| 153 | 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] | 154 | fi |
Miguel Young de la Sota | 4a4946d | 2019-11-21 10:44:18 -0600 | [diff] [blame] | 155 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 156 | mkdir -p "$DEV_BIN_DIR" |
| 157 | set -x |
| 158 | meson $reconf \ |
| 159 | -Dot_version="$OT_VERSION" \ |
| 160 | -Ddev_bin_dir="$DEV_BIN_DIR" \ |
| 161 | -Dhost_bin_dir="$HOST_BIN_DIR" \ |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 162 | -Dtock_local="$TOCK_LOCAL" \ |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 163 | -Dkeep_includes="$FLAGS_keep_includes" \ |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 164 | --cross-file="$ARG_toolchain_file" \ |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 165 | "$OBJ_DIR" |