Files
Attacks-for-Bombs-1/scripts/_common.sh
T

82 lines
3.4 KiB
Bash
Executable File

# Shared helpers for scripts/*.sh. Sourced, never run directly (hence the
# leading underscore and no executable bit); every script sources it after
# it has cd'd to the project root, because both helpers below run
# `cmake -P cmake/<something>.cmake` relative to that.
#
# There is deliberately no Windows twin of this file: scripts/*.bat each keep
# their own copy of these two small wrappers as labels (:read_config and
# :resolve_build_dir), because sharing labels between .bat files is more
# fragile than repeating them. What the wrappers *ask* is answered by the
# same cmake scripts either way, so no logic is duplicated -- only the glue.
# Reads one value out of Configuration.cmake -- this project's single
# source of truth -- via cmake/PrintConfig.cmake, instead of hardcoding a
# second copy of a default that can silently drift out of sync. See that
# file for exactly what this strips off of its output.
_pmn_read_config() {
cmake "-DPRINT_VARS=$1" -P cmake/PrintConfig.cmake | sed -E 's/^-- [^=]*=//'
}
# _pmn_resolve_build_dir [<name>]
#
# Prints the build directory to use. With a <name> (a preset, or "default")
# that is simply BUILD_OUTPUT_DIR/<name>; without one it is discovered: the
# directory scripts/build.sh built last, else the only one that exists. If
# neither works, the reason goes to stderr and the return status is 1.
#
# The logic itself is cmake/BuildDir.cmake's -- see it for the details and
# the one-line answer protocol parsed here.
_pmn_resolve_build_dir() {
local answer
# run.sh never needed cmake before it had to ask which build directory to
# use, so say what's wrong instead of letting the shell's own "command not
# found" and an empty answer speak for it.
if ! command -v cmake >/dev/null 2>&1; then
echo "error: cmake not found on PATH -- it is needed to work out which build directory to use." >&2
echo " Put it on PATH, or set BUILD_DIR to the directory explicitly, e.g. BUILD_DIR=out/build/<preset>." >&2
return 1
fi
answer="$(cmake "-DPMN_NAME=${1:-}" -P cmake/BuildDir.cmake | sed -E 's/^-- //')"
case "$answer" in
BUILD_DIR=*)
printf '%s\n' "${answer#BUILD_DIR=}"
;;
ERROR=*)
echo "error: ${answer#ERROR=}" >&2
return 1
;;
*)
echo "error: cmake/BuildDir.cmake did not answer as expected: ${answer}" >&2
return 1
;;
esac
}
# _pmn_record_build_dir <name>
#
# Remembers <name> as the build directory built last, so the next
# `scripts/run.sh` / `install.sh` / `docs.sh` without a --preset uses it.
_pmn_record_build_dir() {
cmake "-DPMN_NAME=$1" -DPMN_RECORD=ON -P cmake/BuildDir.cmake >/dev/null
}
# On macOS, Homebrew's llvm formula -- the one that ships clang-tidy -- is
# keg-only: installed, but deliberately not linked into PATH, so a script
# looking for `clang-tidy` can't find it. Add its bin directory to the END of
# PATH (never the front: Apple's clang must stay the one builds use). A
# no-op everywhere else, and when llvm isn't installed.
_pmn_extend_tool_path() {
[ "$(uname -s)" = Darwin ] || return 0
local d
for d in /opt/homebrew/opt/llvm/bin /usr/local/opt/llvm/bin; do
if [ -d "$d" ]; then
case ":$PATH:" in
*":$d:"*) ;;
*) PATH="$PATH:$d" ;;
esac
return 0
fi
done
return 0
}