blob: 07617d11d4dc9fae02b33b35920bfa446faa5019 [file] [log] [blame]
Miguel Osorio03f2e232019-09-17 19:44:37 -07001#!/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
6set -o errexit
7set -o pipefail
8set -o nounset
9
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -060010# 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 Sota63793572019-11-13 14:18:51 -060032. util/build_consts.sh
33
34echo "Detected \$REPO_TOP at $REPO_TOP."
35echo "Object directory set at $OBJ_DIR."
36echo "Binary directory set at $BIN_DIR."
Miguel Young de la Sotab2ef4832019-11-22 12:55:46 -060037echo "OpenTitan version: $OT_VERSION"
Miguel Young de la Sota63793572019-11-13 14:18:51 -060038echo
Miguel Osorio03f2e232019-09-17 19:44:37 -070039
40function usage() {
41 cat << USAGE
42Configure Meson build targets.
43
Sam Elliott30abf912021-01-04 11:56:13 +000044Usage: $0 [-r|-f|-A|-K|-c] [-T PATH] [-t FILE]
Miguel Osorio03f2e232019-09-17 19:44:37 -070045
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -060046 -A: Assert that no build dirs exist when running this command.
Sam Elliott30abf912021-01-04 11:56:13 +000047 -c: Enable coverage (requires clang).
48 -f: Force a reconfiguration by removing existing build dirs.
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050049 -K: Keep include search paths as generated by Meson.
Sam Elliott30abf912021-01-04 11:56:13 +000050 -r: Reconfigure build dirs, if they exist.
Sam Elliottc09da262020-06-03 11:10:36 +010051 -t FILE: Configure Meson with toolchain configuration FILE
Miguel Osorio03f2e232019-09-17 19:44:37 -070052
53USAGE
54}
55
Sam Elliottc09da262020-06-03 11:10:36 +010056readonly DEFAULT_RISCV_TOOLS=/tools/riscv
57TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}"
58
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -060059FLAGS_assert=false
Miguel Osorio03f2e232019-09-17 19:44:37 -070060FLAGS_force=false
61FLAGS_reconfigure=""
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050062FLAGS_keep_includes=false
Sam Elliottc09da262020-06-03 11:10:36 +010063FLAGS_specified_toolchain_file=false
Alphan Ulusoyd2bbbe12020-08-14 15:39:51 -040064FLAGS_coverage=false
Luís Marques5849cd12020-06-26 10:33:55 +010065ARG_toolchain_file="${TOOLCHAIN_PATH}/meson-riscv32-unknown-elf-clang.txt"
Sam Elliott30abf912021-01-04 11:56:13 +000066# `getopts` usage
67# - The initial colon in the optstring is to suppress the default error
68# handling.
69# - The remaining options are specified in alphabetical order, and the case
70# statement should match this order.
71# - Only options that take an argument should have a following colon.
72# - The case statement contains two additional cases:
73# - when `$flag` = `?`, this is an unexpected option.
74# - when `$flag` = `:`, this is the case that a flag which requires an
75# argument is not provided one. In both cases, `$OPTARG` contains the
76# relevant parsed option.
77# - After option parsing is finished, we `shift` by `$OPTIND - 1` so that the
78# remaining (unprocessed) arguments are in `$@` (and $1, $2, $3 etc.).
79while getopts ':AcfKrt:T:' flag; do
Miguel Osorio03f2e232019-09-17 19:44:37 -070080 case "${flag}" in
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -060081 A) FLAGS_assert=true;;
Sam Elliott30abf912021-01-04 11:56:13 +000082 c) FLAGS_coverage=true;;
83 f) FLAGS_force=true;;
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050084 K) FLAGS_keep_includes=true;;
Sam Elliott30abf912021-01-04 11:56:13 +000085 r) FLAGS_reconfigure="--reconfigure";;
Sam Elliottc09da262020-06-03 11:10:36 +010086 t) FLAGS_specified_toolchain_file=true
87 ARG_toolchain_file="${OPTARG}";;
Sam Elliott30abf912021-01-04 11:56:13 +000088 \?) echo "Unexpected option: -${OPTARG}" >&2
89 usage
90 exit 1
91 ;;
92 :) echo "Option -${OPTARG} requires a path argument" >&2
93 usage
94 exit 1
Jon Flatleya863a282020-03-03 10:49:39 -050095 ;;
Sam Elliott30abf912021-01-04 11:56:13 +000096 *) echo "Internal Error: Unhandled option: -${flag}" >&2
97 exit 1
Miguel Osorio03f2e232019-09-17 19:44:37 -070098 ;;
99 esac
100done
Sam Elliott30abf912021-01-04 11:56:13 +0000101shift $((OPTIND - 1))
102
103# We do not accept additional arguments.
104if [[ "$#" -gt 0 ]]; then
105 echo "Unexpected arguments:" "$@" >&2
106 exit 1
107fi
Miguel Osorio03f2e232019-09-17 19:44:37 -0700108
Miguel Osorio03f2e232019-09-17 19:44:37 -0700109if [[ ! -n "$(command -v meson)" ]]; then
110 echo "Unable to find meson. Please install meson before running this command." >&2
111 exit 1
112fi
113
114if [[ ! -n "$(command -v ninja)" ]]; then
115 echo "Unable to find ninja. Please install ninja before running this command." >&2
116 exit 1
117fi
118
119if [[ "${FLAGS_force}" == true ]]; then
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -0600120 rm -rf "$OBJ_DIR"
121 rm -rf "$BIN_DIR"
Miguel Osorio03f2e232019-09-17 19:44:37 -0700122fi
123
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -0600124if [[ "${FLAGS_assert}" == true ]]; then
125 if [[ -e "$OBJ_DIR" ]]; then
126 echo "Object directory at $OBJ_DIR already exists. Aborting." >&2
127 exit 1
128 fi
129 if [[ -e "$BIN_DIR" ]]; then
130 echo "Binary directory at $BIN_DIR already exists. Aborting." >&2
131 exit 1
132 fi
Miguel Osorio03f2e232019-09-17 19:44:37 -0700133fi
134
Miguel Young de la Sota76526c32020-01-28 10:24:41 -0500135reconf="${FLAGS_reconfigure}"
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -0600136
Miguel Young de la Sota76526c32020-01-28 10:24:41 -0500137if [[ ! -d "$OBJ_DIR" ]]; then
138 echo "Output directory does not exist at $OBJ_DIR; creating." >&2
139 mkdir -p "$OBJ_DIR"
140 reconf=""
141elif [[ -z "$reconf" ]]; then
142 echo "Output directory already exists at $OBJ_DIR; skipping." >&2
Miguel Young de la Sota76526c32020-01-28 10:24:41 -0500143fi
Miguel Young de la Sota4a4946d2019-11-21 10:44:18 -0600144
Sam Elliott36fe3b52020-08-17 17:21:24 +0100145if [[ -f "${ARG_toolchain_file}" ]]; then
146 echo "Using meson toolchain file at $ARG_toolchain_file." >&2
147else
148 if [[ "${FLAGS_specified_toolchain_file}" == true ]]; then
149 echo "Unable to find meson toolchain file at $ARG_toolchain_file. Aborting." >&2
150 exit 1
151 else
152 cross_file="$OBJ_DIR/toolchain-configured.txt"
153 cp toolchain.txt "$cross_file"
154 perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$cross_file"
155 touch -r toolchain.txt "$cross_file"
156 echo "Set up toolchain file at $cross_file." >&2
157 ARG_toolchain_file="${cross_file}"
158 fi
159fi
160
Philipp Wagner525891d2020-10-30 19:01:04 +0000161mkdir -p "$BIN_DIR"
Miguel Young de la Sota76526c32020-01-28 10:24:41 -0500162set -x
163meson $reconf \
164 -Dot_version="$OT_VERSION" \
Philipp Wagner525891d2020-10-30 19:01:04 +0000165 -Dbin_dir="$BIN_DIR" \
Sam Elliott371c79f2020-06-23 20:06:19 +0100166 -Dkeep_includes="$FLAGS_keep_includes" \
Alphan Ulusoyd2bbbe12020-08-14 15:39:51 -0400167 -Dcoverage="$FLAGS_coverage" \
Sam Elliottc09da262020-06-03 11:10:36 +0100168 --cross-file="$ARG_toolchain_file" \
Miguel Young de la Sota76526c32020-01-28 10:24:41 -0500169 "$OBJ_DIR"