summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-04-10 14:53:59 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-04-10 14:55:40 -0400
commit000ceddf7b607c70297d2dbdee4a800423975fc1 (patch)
tree0c4e47baacc13dfe70969d7cad183dcf1abb3bca
parent6c8d11e8e5b3457286fcda75b6516e93f1f12f17 (diff)
build: Factor out vars.mk generation into a script
-rw-r--r--Makefile36
-rwxr-xr-xconfig/vars.sh81
2 files changed, 83 insertions, 34 deletions
diff --git a/Makefile b/Makefile
index 0df7ce5..9117fe9 100644
--- a/Makefile
+++ b/Makefile
@@ -123,7 +123,7 @@ export UBSAN_CFLAGS= -fsanitize=undefined
export TSAN_CPPFLAGS= -DBFS_USE_TARGET_CLONES=0
export TSAN_CFLAGS= -fsanitize=thread
-SAN := ${ASAN}${LSAN}${MSAN}${TSAN}${UBSAN}
+export SAN=${ASAN}${LSAN}${MSAN}${TSAN}${UBSAN}
export SAN_CFLAGS= -fno-sanitize-recover=all
# MSAN and TSAN both need all code to be instrumented
@@ -212,39 +212,7 @@ config: ${MKS}
# Saves the configurable variables
${GEN}/vars.mk:
@${XMKDIR} ${@D}
- @printf 'PREFIX := %s\n' "$$XPREFIX" >$@
- @printf 'MANDIR := %s\n' "$$XMANDIR" >>$@
- @printf 'OS := %s\n' "$$XOS" >>$@
- @printf 'ARCH := %s\n' "$$XARCH" >>$@
- @printf 'CC := %s\n' "$$XCC" >>$@
- @printf 'INSTALL := %s\n' "$$XINSTALL" >>$@
- @printf 'MKDIR := %s\n' "$$XMKDIR" >>$@
- @printf 'RM := %s\n' "$$XRM" >>$@
- @printf '# BFS_CPPFLAGS\nCPPFLAGS := %s\n' "$$BFS_CPPFLAGS" >>$@
- @test "${TSAN}" != y || printf '# TSAN\nCPPFLAGS += %s\n' "$$TSAN_CPPFLAGS" >>$@
- @test "${LINT}" != y || printf '# LINT\nCPPFLAGS += %s\n' "$$LINT_CPPFLAGS" >>$@
- @test "${RELEASE}" != y || printf '# RELEASE\nCPPFLAGS += %s\n' "$$RELEASE_CPPFLAGS" >>$@
- @printf '# CPPFLAGS\nCPPFLAGS += %s\n' "$$XCPPFLAGS" >>$@
- @printf '# EXTRA_CPPFLAGS\nCPPFLAGS += %s\n' "$$EXTRA_CPPFLAGS" >>$@
- @printf '# BFS_CFLAGS\nCFLAGS := %s\n' "$$BFS_CFLAGS" >>$@
- @test "${ASAN}" != y || printf '# ASAN\nCFLAGS += %s\n' "$$ASAN_CFLAGS" >>$@
- @test "${LSAN}" != y || printf '# LSAN\nCFLAGS += %s\n' "$$LSAN_CFLAGS" >>$@
- @test "${MSAN}" != y || printf '# MSAN\nCFLAGS += %s\n' "$$MSAN_CFLAGS" >>$@
- @test "${TSAN}" != y || printf '# TSAN\nCFLAGS += %s\n' "$$TSAN_CFLAGS" >>$@
- @test "${UBSAN}" != y || printf '# UBSAN\nCFLAGS += %s\n' "$$UBSAN_CFLAGS" >>$@
- @case "${SAN}" in *y*) printf '# *SAN\nCFLAGS += %s\n' "$$SAN_CFLAGS" >>$@ ;; esac
- @test "${GCOV}" != y || printf '# GCOV\nCFLAGS += %s\n' "$$GCOV_CFLAGS" >>$@
- @test "${LINT}" != y || printf '# LINT\nCFLAGS += %s\n' "$$LINT_CFLAGS" >>$@
- @test "${RELEASE}" != y || printf '# RELEASE\nCFLAGS += %s\n' "$$RELEASE_CFLAGS" >>$@
- @printf '# CFLAGS\nCFLAGS += %s\n' "$$XCFLAGS" >>$@
- @printf '# EXTRA_CFLAGS\nCFLAGS += %s\n' "$$EXTRA_CFLAGS" >>$@
- @printf '# LDFLAGS\nLDFLAGS := %s\n' "$$XLDFLAGS" >>$@
- @printf '# EXTRA_LDFLAGS\nLDFLAGS += %s\n' "$$EXTRA_LDFLAGS" >>$@
- @printf '# LDLIBS\nLDLIBS := %s\n' "$$XLDLIBS" >>$@
- @printf '# EXTRA_LDLIBS\nLDLIBS += %s\n' "$$EXTRA_LDLIBS" >>$@
- @printf '# BFS_LDLIBS\nLDLIBS += %s\n' "$$BFS_LDLIBS" >>$@
- @printf 'PKGS :=\n' >>$@
- @case "${OS}-${SAN}" in FreeBSD-*y*) printf 'POSTLINK = elfctl -e +noaslr $$@\n' >>$@ ;; esac
+ @config/vars.sh >$@
@cat $@
.PHONY: ${GEN}/vars.mk
diff --git a/config/vars.sh b/config/vars.sh
new file mode 100755
index 0000000..c0fb124
--- /dev/null
+++ b/config/vars.sh
@@ -0,0 +1,81 @@
+#!/usr/bin/env bash
+
+# Copyright © Tavian Barnes <tavianator@tavianator.com>
+# SPDX-License-Identifier: 0BSD
+
+# Writes the saved variables to gen/vars.mk
+
+set -eu
+
+print() {
+ NAME="$1"
+ OP="${2:-:=}"
+
+ if (($# >= 3)); then
+ printf '# %s\n' "${3#X}"
+ declare -n VAR="$3"
+ VALUE="${VAR:-}"
+ else
+ # Try X$NAME, $NAME, ""
+ local -n XVAR="X$NAME"
+ local -n VAR="$NAME"
+ VALUE="${XVAR:-${VAR:-}}"
+ fi
+
+ printf '%s %s %s\n' "$NAME" "$OP" "$VALUE"
+}
+
+cond_flags() {
+ local -n COND="$1"
+
+ if [[ "${COND:-}" == *y* ]]; then
+ print "$2" += "${1}_${2}"
+ fi
+}
+
+print PREFIX
+print MANDIR
+
+print OS
+print ARCH
+
+print CC
+print INSTALL
+print MKDIR
+print RM
+
+print CPPFLAGS := BFS_CPPFLAGS
+cond_flags TSAN CPPFLAGS
+cond_flags LINT CPPFLAGS
+cond_flags RELEASE CPPFLAGS
+print CPPFLAGS += XCPPFLAGS
+print CPPFLAGS += EXTRA_CPPFLAGS
+
+print CFLAGS := BFS_CFLAGS
+cond_flags ASAN CFLAGS
+cond_flags LSAN CFLAGS
+cond_flags MSAN CFLAGS
+cond_flags TSAN CFLAGS
+cond_flags UBSAN CFLAGS
+cond_flags SAN CFLAGS
+cond_flags GCOV CFLAGS
+cond_flags LINT CFLAGS
+cond_flags RELEASE CFLAGS
+print CFLAGS += XCFLAGS
+print CFLAGS += EXTRA_CFLAGS
+
+print LDFLAGS := XLDFLAGS
+print LDFLAGS += EXTRA_LDFLAGS
+
+print LDLIBS := XLDLIBS
+print LDLIBS += EXTRA_LDLIBS
+print LDLIBS += BFS_LDLIBS
+
+print PKGS
+
+# Disable ASLR on FreeBSD when sanitizers are enabled
+case "$XOS-$SAN" in
+ FreeBSD-*y*)
+ printf 'POSTLINK = elfctl -e +noaslr $$@\n'
+ ;;
+esac