blob: fbf6d6100251b7f725fca39fa50b97a67c6cc361 [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
Miguel Young de la Sotad258b332019-10-29 14:05:23 -05009set -x
Miguel Osorio03f2e232019-09-17 19:44:37 -070010
11readonly BUILD_DIR_PREFIX="build"
12readonly TARGET_VERILATOR="verilator"
13readonly TARGET_FPGA="fpga"
14
15function usage() {
16 cat << USAGE
17Configure Meson build targets.
18
19Usage: $0 [-r|-f]
20
21 -f: Remove build directories before running Meson.
22 -r: Force reconfiguration of build directories.
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050023 -K: Keep include search paths as generated by Meson.
Miguel Osorio03f2e232019-09-17 19:44:37 -070024
25USAGE
26}
27
28FLAGS_force=false
29FLAGS_reconfigure=""
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050030FLAGS_keep_includes=false
31while getopts 'r?:f?:K?' flag; do
Miguel Osorio03f2e232019-09-17 19:44:37 -070032 case "${flag}" in
33 f) FLAGS_force=true;;
34 r) FLAGS_reconfigure="--reconfigure";;
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050035 K) FLAGS_keep_includes=true;;
Miguel Osorio03f2e232019-09-17 19:44:37 -070036 ?) usage && exit 1;;
37 *) usage
38 error "Unexpected option ${flag}"
39 ;;
40 esac
41done
42
43if [[ "${FLAGS_force}" == true && -n "${FLAGS_reconfigure}" ]]; then
44 usage >&2
45 echo "Error: -r and -f cannont be used at the same time." >&2
46 exit 1
47fi
48
49if [[ ! -n "$(command -v meson)" ]]; then
50 echo "Unable to find meson. Please install meson before running this command." >&2
51 exit 1
52fi
53
54if [[ ! -n "$(command -v ninja)" ]]; then
55 echo "Unable to find ninja. Please install ninja before running this command." >&2
56 exit 1
57fi
58
59if [[ "${FLAGS_force}" == true ]]; then
60 for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do
61 rm -rf "${BUILD_DIR_PREFIX}-${target_suffix}"
62 done
63fi
64
65if [[ ! -n "${FLAGS_reconfigure}" ]] ; then
66 for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do
67 if [[ -d "${BUILD_DIR_PREFIX}-${target_suffix}" ]]; then
68 usage >&2
69 echo "Error: ${BUILD_DIR_PREFIX}-${target_suffix} already exists. " \
70 "Remove directory, or rerun $0 with the -r option" >&2
71 exit 1
72 fi
73 done
74fi
75
Miguel Young de la Sotad258b332019-10-29 14:05:23 -050076readonly DEFAULT_RISCV_TOOLS=/tools/riscv
77TOOLCHAIN_PATH="${TOOLCHAIN_PATH:-$DEFAULT_RISCV_TOOLS}"
78CROSS_FILE=$(mktemp /tmp/toolchain.XXXXXX.txt)
79cp toolchain.txt "$CROSS_FILE"
80perl -pi -e "s#$DEFAULT_RISCV_TOOLS#$TOOLCHAIN_PATH#g" "$CROSS_FILE"
81echo "Set up toolchain file at $CROSS_FILE." >&2
82
83# purge_includes $build_dir deletes any -I command line arguments that are not
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050084# - Absolute paths.
85# - Ephemeral build directories.
86#
87# This function is necessary because Meson does not give adequate
88# control over what directories are passed in as -I search directories
89# to the C compiler. While Meson does provide |implicit_include_directories|,
90# support for this option is poor: empirically, Meson ignores this option for
91# some targerts. Doing it as a post-processing step ensures that Meson does
92# not allow improper #includes to compile successfully.
93function purge_includes() {
94 if [[ "${FLAGS_keep_includes}" == "true" ]]; then
95 return
96 fi
97 echo "Purging superfluous -I arguments from $1."
Miguel Young de la Sotad258b332019-10-29 14:05:23 -050098 local ninja_file="$1/build.ninja"
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050099 perl -pi -e 's#-I[^/][^@ ]+ # #g' -- "$ninja_file"
100}
101
Miguel Young de la Sotad258b332019-10-29 14:05:23 -0500102# configure_meson $target generates a build directory at build-$target.
103function configure_meson() {
104 local target="$1"
105 local build_dir="${BUILD_DIR_PREFIX}-$target"
Miguel Osorio03f2e232019-09-17 19:44:37 -0700106
Miguel Young de la Sotad258b332019-10-29 14:05:23 -0500107 mkdir -p "$build_dir"
108 meson ${FLAGS_reconfigure} \
109 -Dtarget="$target" \
110 --cross-file="$CROSS_FILE" \
111 --buildtype=plain \
112 "$build_dir"
113 purge_includes "$build_dir"
114}
115
116configure_meson "${TARGET_VERILATOR}"
117configure_meson "${TARGET_FPGA}"