2023-11-26 21:51:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# Exit immediately if the script encounters a non-zero status.
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# If set, Bash includes filenames beginning with a `.` in the results of
|
|
|
|
# filename expansion. The filenames `.` and `..` must always be matched
|
|
|
|
# explicitly, even if dotglob is set.
|
|
|
|
shopt -s dotglob
|
|
|
|
|
|
|
|
# ============================================================
|
|
|
|
# PROLOGUE
|
|
|
|
# ============================================================
|
|
|
|
|
|
|
|
# Create a new top-level directory as fallback in case $BUILD (defined below)
|
|
|
|
# is ever empty.
|
|
|
|
mkdir -p "/tmp/bs.clang"
|
|
|
|
|
|
|
|
# Create an intermediate build directory. The final step of this script will
|
|
|
|
# copy the content from this directory to $OUT.
|
|
|
|
BUILD=$(mktemp -d -p "/tmp/bs.clang")
|
|
|
|
|
|
|
|
if [ -z "$BUILD" ]; then
|
|
|
|
>&2 echo "Failed to create temp directory."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Deletes the intermediate build directory on exit. We use a concatenation of
|
|
|
|
# the intermediate directory with the basename of the generated temp directory
|
|
|
|
# to ensure we never evaluate to root (i.e. `/`). That should never actually
|
|
|
|
# happen but a good habit to establish nonetheless.
|
|
|
|
function cleanup {
|
2023-12-21 21:54:49 +00:00
|
|
|
yes | rm -r "/tmp/bs.clang/$(basename "$BUILD")"
|
2023-11-27 19:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# ============================================================
|
|
|
|
# BUILD
|
|
|
|
# ============================================================
|
|
|
|
|
|
|
|
# Copy template contents over to the intermediate build directory.
|
|
|
|
cp -r template/* "$BUILD"
|
|
|
|
|
2023-11-28 16:17:09 +00:00
|
|
|
# Explicitly set permissions on all copied files.
|
2023-11-27 19:42:43 +00:00
|
|
|
find "$BUILD" -type f -execdir chmod 644 {} +
|
|
|
|
find "$BUILD" -type d -execdir chmod 755 {} +
|
|
|
|
chmod 755 "$BUILD"/.githooks/pre-commit
|
|
|
|
|
2023-12-21 21:54:49 +00:00
|
|
|
# Create new subdirectories for out-of-source builds. Each should be initialized
|
|
|
|
# with `CMAKE_BUILD_TYPE` set appropriately during configuration, e.g.
|
|
|
|
# `cd build/Debug && cmake -DCMAKE_BUILD_TYPE=Debug ../..`
|
|
|
|
mkdir -p "$BUILD"/build/{Debug,Release}
|
|
|
|
|
|
|
|
# ============================================================
|
|
|
|
# REWRITES
|
|
|
|
# ============================================================
|
|
|
|
|
2024-01-06 18:12:44 +00:00
|
|
|
for file in flake.nix {,src/,docs/,test/}CMakeLists.txt
|
2023-12-21 21:54:49 +00:00
|
|
|
do
|
|
|
|
sed -i "s/<NAME>/$NAME/g" "$BUILD/$file"
|
|
|
|
done
|
|
|
|
|
2023-11-27 19:42:43 +00:00
|
|
|
# ============================================================
|
|
|
|
# EPILOGUE
|
|
|
|
# ============================================================
|
|
|
|
|
|
|
|
# Success! Copy contents to target directory.
|
2023-12-13 16:14:27 +00:00
|
|
|
cp -a "$BUILD"/* "$OUT"
|