summaryrefslogtreecommitdiffstats
path: root/build
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2025-07-09 09:57:00 -0400
committerTavian Barnes <tavianator@tavianator.com>2025-07-09 10:11:29 -0400
commit6d0240aa07076819cf454d13a21c3e1e0d13c6bf (patch)
tree2fe4cb0c6f84fbabdbdc9bbfa2df714e2a5bc5da /build
parenta69f4fb067ec22b95741e37efe355fb1106fefd2 (diff)
downloadbfs-6d0240aa07076819cf454d13a21c3e1e0d13c6bf.tar.xz
build/flags-if: Support multiple flag groups
Diffstat (limited to 'build')
-rwxr-xr-xbuild/cc.sh21
-rwxr-xr-xbuild/flags-if.sh73
-rw-r--r--build/header.mk2
3 files changed, 65 insertions, 31 deletions
diff --git a/build/cc.sh b/build/cc.sh
index e1d2b0b..b6f38b5 100755
--- a/build/cc.sh
+++ b/build/cc.sh
@@ -9,26 +9,11 @@
set -eu
-QUIET=
+# Without -q, print the executed command for config.log
if [ "$1" = "-q" ]; then
- QUIET=y
shift
-fi
-
-# Source files can specify their own flags with lines like
-#
-# /// _CFLAGS += -Wmissing-variable-declarations
-#
-# which will be added to the makefile on success, or lines like
-#
-# /// -Werror
-#
-# which are just used for the current file.
-EXTRA_FLAGS=$(sed -n '\|^///|{s|^/// ||; s|[^=]*= ||; p;}' "$1")
-
-# Without -q, print the executed command for config.log
-if [ -z "$QUIET" ]; then
+else
set -x
fi
-$XCC $XCPPFLAGS $XCFLAGS $XLDFLAGS "$@" $EXTRA_FLAGS $XLDLIBS
+$XCC $XCPPFLAGS $XCFLAGS $XLDFLAGS "$@" $XLDLIBS
diff --git a/build/flags-if.sh b/build/flags-if.sh
index 81eb345..4cbfbc9 100755
--- a/build/flags-if.sh
+++ b/build/flags-if.sh
@@ -3,26 +3,75 @@
# Copyright © Tavian Barnes <tavianator@tavianator.com>
# SPDX-License-Identifier: 0BSD
-# Add flags to a makefile if a build succeeds
+# Add flags to a makefile if a build succeeds. Source files can specify their
+# own flags with lines like
+#
+# /// _CFLAGS += -Wmissing-variable-declarations
+#
+# which will be added to the makefile on success, or lines like
+#
+# /// -Werror
+#
+# which are just used for the current file. Lines like
+#
+# /// ---
+#
+# separate groups of flags, to try multiple ways to achieve something, e.g.
+#
+# /// CFLAGS += -pthread
+# /// ---
+# /// LDLIBS += -lpthread
set -eu
-build/cc.sh "$@" || exit 1
+# Any new flags we're using
+FLAGS=""
+# Any new makefile lines we're printing
+OUTPUT=""
-# If the build succeeded, print any lines like
-#
-# /// _CFLAGS += -foo
-#
-# (unless they're already set)
-OLD_FLAGS="$XCC $XCPPFLAGS $XCFLAGS $XLDFLAGS $XLDLIBS"
+# Check the existing flags so we don't add duplicates
+OLD_FLAGS=" $XCPPFLAGS $XCFLAGS $XLDFLAGS $XLDLIBS "
+
+add_flag() {
+ if [ "${OLD_FLAGS#* $1 }" = "$OLD_FLAGS" ]; then
+ FLAGS="${FLAGS}${FLAGS:+ }$1"
+ else
+ return 1
+ fi
+}
+
+try_cc() {
+ build/cc.sh "$@" $FLAGS || return $?
+ printf '%s' "$OUTPUT"
+ exit
+}
while IFS="" read -r line; do
case "$line" in
- ///*=*)
- flag="${line#*= }"
- if [ "${OLD_FLAGS#*"$flag"}" = "$OLD_FLAGS" ]; then
- printf '%s\n' "${line#/// }"
+ "/// "*)
+ line="${line#/// }"
+ ;;
+ *)
+ continue
+ ;;
+ esac
+
+ case "$line" in
+ ---)
+ try_cc "$@" || :
+ FLAGS=
+ OUTPUT=
+ ;;
+ *=*)
+ if add_flag "${line#*= }"; then
+ OUTPUT="${OUTPUT}${line}
+"
fi
;;
+ *)
+ add_flag "$line" || :
+ ;;
esac
done <"$1"
+
+try_cc "$@"
diff --git a/build/header.mk b/build/header.mk
index f15829a..723922e 100644
--- a/build/header.mk
+++ b/build/header.mk
@@ -89,5 +89,5 @@ OUT = ${SLUG:has/%=gen/has/.%.out}
${HEADERS}::
@${MKDIR} ${@D}
- @build/define-if.sh ${SLUG} build/cc.sh build/${SLUG}.c -o ${OUT} >$@ 2>$@.log; \
+ @build/define-if.sh ${SLUG} build/flags-if.sh build/${SLUG}.c -o ${OUT} >$@ 2>$@.log; \
build/msg-if.sh "[ CC ] ${SLUG}.c" test $$? -eq 0