blob: 7f4102b64fadd8e9515af3e585a271bf1e780378 [file] [log] [blame]
#!/bin/bash
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
set -o errexit
set -o pipefail
set -o nounset
. util/build_consts.sh
echo "Detected \$REPO_TOP at $REPO_TOP."
echo "Object directory set at $OBJ_DIR."
echo "Binary directory set at $BIN_DIR."
echo "OpenTitan version: $OT_VERSION"
echo
function usage() {
cat << USAGE
Configure Meson build targets.
Usage: $0 [-r|-f]
-f: Remove build directories before running Meson.
-r: Force reconfiguration of build directories.
-K: Keep include search paths as generated by Meson.
USAGE
}
FLAGS_force=false
FLAGS_reconfigure=""
FLAGS_keep_includes=false
while getopts 'r?:f?:K?' flag; do
case "${flag}" in
f) FLAGS_force=true;;
r) FLAGS_reconfigure="--reconfigure";;
K) FLAGS_keep_includes=true;;
?) usage && exit 1;;
*) usage
error "Unexpected option ${flag}"
;;
esac
done
if [[ "${FLAGS_force}" == true && -n "${FLAGS_reconfigure}" ]]; then
usage >&2
echo "Error: -r and -f cannont be used at the same time." >&2
exit 1
fi
if [[ ! -n "$(command -v meson)" ]]; then
echo "Unable to find meson. Please install meson before running this command." >&2
exit 1
fi
if [[ ! -n "$(command -v ninja)" ]]; then
echo "Unable to find ninja. Please install ninja before running this command." >&2
exit 1
fi
if [[ "${FLAGS_force}" == true ]]; then
rm -rf $BUILD_ROOT/build-*
fi
if [[ ! -n "${FLAGS_reconfigure}" ]] ; then
for platform in "${PLATFORMS[@]}"; do
obj_dir="$(sw_obj_dir "$platform")"
if [[ -d "$obj_dir" ]]; then
usage >&2
echo "Error: $obj_dir already exists. Remove directory, or rerun $0 " \
"with the -r option" >&2
exit 1
fi
done
fi
readonly DEFAULT_RISCV_TOOLS=/tools/riscv
TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}"
CROSS_FILE=$(mktemp /tmp/toolchain.XXXXXX.txt)
cp toolchain.txt "$CROSS_FILE"
perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$CROSS_FILE"
echo "Set up toolchain file at $CROSS_FILE." >&2
# purge_includes $build_dir deletes any -I command line arguments that are not
# - Absolute paths.
# - Ephemeral build directories.
#
# This function is necessary because Meson does not give adequate
# control over what directories are passed in as -I search directories
# to the C compiler. While Meson does provide |implicit_include_directories|,
# support for this option is poor: empirically, Meson ignores this option for
# some targerts. Doing it as a post-processing step ensures that Meson does
# not allow improper #includes to compile successfully.
function purge_includes() {
if [[ "${FLAGS_keep_includes}" == "true" ]]; then
return
fi
echo "Purging superfluous -I arguments from $1."
local ninja_file="$1/build.ninja"
perl -pi -e 's#-I[^/][^@ ]+ # #g' -- "$ninja_file"
}
for platform in ${PLATFORMS[@]}; do
obj_dir="$(sw_obj_dir "$platform")"
bin_dir="$(sw_bin_dir "$platform")"
mkdir -p "$obj_dir"
mkdir -p "$bin_dir"
meson ${FLAGS_reconfigure} \
-Dtarget="$platform" \
-Dot_version="$OT_VERSION" \
-Ddev_bin_dir="$bin_dir" \
-Dhost_bin_dir="$HOST_BIN_DIR" \
--cross-file="$CROSS_FILE" \
--buildtype=plain \
"$obj_dir"
purge_includes "$obj_dir"
done