Skip to content

Commit 3552b79

Browse files
committed
build/flags: Infrastructure to detect compiler flag support
Use it to detect -MD -MP support.
1 parent bd8a345 commit 3552b79

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

build/deps.mk

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ include build/exports.mk
1111
gen/deps.mk::
1212
${MSG} "[ GEN] $@"
1313
@printf '# %s\n' "$@" >$@
14-
@if build/cc.sh -MD -MP build/empty.c -o gen/.deps.out; then \
15-
printf '_CPPFLAGS += -MD -MP\n'; \
16-
fi >>$@ 2>$@.log
17-
${VCAT} $@
1814
@printf -- '-include %s\n' ${OBJS:.o=.d} >>$@
15+
${VCAT} $@

build/flags-if.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
# Copyright © Tavian Barnes <[email protected]>
4+
# SPDX-License-Identifier: 0BSD
5+
6+
# Add flags to a makefile if a build succeeds
7+
8+
set -eu
9+
10+
FLAGS=$(sed -n '\|^///|{s|^/// ||; s|[^=]*= ||; p}' "$1")
11+
12+
if build/cc.sh "$@" $FLAGS; then
13+
sed -n 's|^/// \(.*=.*\)|\1|p' "$1"
14+
else
15+
exit 1
16+
fi

build/flags.mk

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ _LDLIBS := ${LDLIBS} ${EXTRA_LDLIBS} ${_LDLIBS}
103103

104104
include build/exports.mk
105105

106-
gen/flags.mk::
106+
# Conditionally-supported flags
107+
AUTO_FLAGS := \
108+
gen/flags/deps.mk
109+
110+
gen/flags.mk: ${AUTO_FLAGS}
107111
${MSG} "[ GEN] $@"
108112
@printf '# %s\n' "$@" >$@
109113
@printf '_CPPFLAGS := %s\n' "$$XCPPFLAGS" >>$@
@@ -112,4 +116,18 @@ gen/flags.mk::
112116
@printf '_LDLIBS := %s\n' "$$XLDLIBS" >>$@
113117
@printf 'NOLIBS := %s\n' "$$XNOLIBS" >>$@
114118
@test "${OS}-${SAN}" != FreeBSD-y || printf 'POSTLINK = elfctl -e +noaslr $$@\n' >>$@
119+
@cat ${.ALLSRC} >>$@
115120
${VCAT} $@
121+
.PHONY: gen/flags.mk
122+
123+
# The short name of the config test
124+
SLUG = ${@:gen/%.mk=%}
125+
# The source file to build
126+
CSRC = build/${SLUG}.c
127+
# The hidden output file name
128+
OUT = ${SLUG:flags/%=gen/flags/.%.out}
129+
130+
${AUTO_FLAGS}::
131+
@${MKDIR} ${@D}
132+
@build/flags-if.sh ${CSRC} -o ${OUT} >$@ 2>$@.log; \
133+
build/msg-if.sh "[ CC ] ${SLUG}.c" test $$? -eq 0

build/flags/deps.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright © Tavian Barnes <[email protected]>
2+
// SPDX-License-Identifier: 0BSD
3+
4+
/// _CPPFLAGS += -MD -MP
5+
6+
int main(void) {
7+
return 0;
8+
}

0 commit comments

Comments
 (0)