Skip to content

Commit 7034123

Browse files
committed
build: Allow ./configure CFLAGS=... to override auto-detected flags
1 parent c8a3dc9 commit 7034123

File tree

6 files changed

+93
-56
lines changed

6 files changed

+93
-56
lines changed

build/auto.mk

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright © Tavian Barnes <[email protected]>
2+
# SPDX-License-Identifier: 0BSD
3+
4+
# Makefile that generates gen/auto.mk
5+
6+
include build/prelude.mk
7+
include gen/vars.mk
8+
include gen/early.mk
9+
include gen/late.mk
10+
include build/exports.mk
11+
12+
# Auto-detected flags
13+
AUTO_FLAGS := \
14+
gen/flags/std.mk \
15+
gen/flags/bind-now.mk \
16+
gen/flags/deps.mk \
17+
gen/flags/pthread.mk \
18+
gen/flags/Wformat.mk \
19+
gen/flags/Wimplicit-fallthrough.mk \
20+
gen/flags/Wimplicit.mk \
21+
gen/flags/Wmissing-decls.mk \
22+
gen/flags/Wmissing-var-decls.mk \
23+
gen/flags/Wshadow.mk \
24+
gen/flags/Wsign-compare.mk \
25+
gen/flags/Wstrict-prototypes.mk \
26+
gen/flags/Wundef-prefix.mk
27+
28+
gen/auto.mk: ${AUTO_FLAGS}
29+
${MSG} "[ GEN] $@"
30+
@printf '# %s\n' "$@" >$@
31+
@cat $^ >>$@
32+
@cat ${^:%=%.log} >gen/flags.log
33+
${VCAT} $@
34+
.PHONY: gen/auto.mk
35+
36+
# Check that the C compiler works at all
37+
cc::
38+
@build/cc.sh -q build/empty.c -o gen/.cc.out; \
39+
ret=$$?; \
40+
build/msg-if.sh "[ CC ] build/empty.c" test $$ret -eq 0; \
41+
exit $$ret
42+
43+
# The short name of the config test
44+
SLUG = ${@:gen/%.mk=%}
45+
# The source file to build
46+
CSRC = build/${SLUG}.c
47+
# The hidden output file name
48+
OUT = ${SLUG:flags/%=gen/flags/.%.out}
49+
50+
${AUTO_FLAGS}: cc
51+
@${MKDIR} ${@D}
52+
@build/flags-if.sh ${CSRC} -o ${OUT} >$@ 2>$@.log; \
53+
build/msg-if.sh "[ CC ] ${SLUG}.c" test $$? -eq 0
54+
.PHONY: ${AUTO_FLAGS}

build/config.mk

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ include build/exports.mk
1010
config: gen/config.mk gen/config.h
1111
.PHONY: config
1212

13+
# All the generated makefile fragments
14+
MKS := \
15+
gen/vars.mk \
16+
gen/early.mk \
17+
gen/auto.mk \
18+
gen/pkgs.mk \
19+
gen/late.mk
20+
1321
# The main configuration file, which includes the others
14-
gen/config.mk: gen/vars.mk gen/flags.mk gen/pkgs.mk
22+
gen/config.mk: ${MKS}
1523
${MSG} "[ GEN] $@"
1624
@printf '# %s\n' "$@" >$@
1725
@printf 'include %s\n' $^ >>$@
@@ -36,16 +44,17 @@ gen/vars.mk::
3644

3745
# Sets the build flags. This depends on vars.mk and uses a recursive make so
3846
# that the default flags can depend on variables like ${OS}.
39-
gen/flags.mk: gen/vars.mk
47+
gen/early.mk gen/late.mk: gen/vars.mk
4048
@+XMAKEFLAGS="$$MAKEFLAGS" ${MAKE} -sf build/flags.mk $@
41-
.PHONY: gen/flags.mk
49+
50+
# Auto-detected build flags
51+
gen/auto.mk: gen/early.mk gen/late.mk
52+
@+XMAKEFLAGS="$$MAKEFLAGS" ${MAKE} -sf build/auto.mk $@
4253

4354
# Auto-detect dependencies and their build flags
44-
gen/pkgs.mk: gen/flags.mk
55+
gen/pkgs.mk: gen/auto.mk
4556
@+XMAKEFLAGS="$$MAKEFLAGS" ${MAKE} -sf build/pkgs.mk $@
46-
.PHONY: gen/pkgs.mk
4757

4858
# Compile-time feature detection
4959
gen/config.h: gen/pkgs.mk
5060
@+XMAKEFLAGS="$$MAKEFLAGS" ${MAKE} -sf build/header.mk $@
51-
.PHONY: gen/config.h

build/exports.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ export XCFLAGS=${_CFLAGS}
1717
export XLDFLAGS=${_LDFLAGS}
1818
export XLDLIBS=${_LDLIBS}
1919

20+
export CONF_CPPFLAGS=${CPPFLAGS} ${EXTRA_CPPFLAGS}
21+
export CONF_CFLAGS=${CFLAGS} ${EXTRA_CFLAGS}
22+
export CONF_LDFLAGS=${LDFLAGS} ${EXTRA_LDFLAGS}
23+
export CONF_LDLIBS=${LDLIBS} ${EXTRA_LDLIBS}
24+
2025
export XNOLIBS=${NOLIBS}

build/flags.mk

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright © Tavian Barnes <[email protected]>
22
# SPDX-License-Identifier: 0BSD
33

4-
# Makefile that generates gen/flags.mk
4+
# Makefile that generates gen/{early,late}.mk
55

66
include build/prelude.mk
77
include gen/vars.mk
@@ -79,32 +79,10 @@ _CFLAGS += ${LTO_CFLAGS,${_LTO}}
7979
# Configurable flags
8080
CFLAGS ?= -g -Wall
8181

82-
# Add the configurable flags last so they can override ours
83-
_CPPFLAGS += ${CPPFLAGS} ${EXTRA_CPPFLAGS}
84-
_CFLAGS += ${CFLAGS} ${EXTRA_CFLAGS}
85-
_LDFLAGS += ${LDFLAGS} ${EXTRA_LDFLAGS}
86-
# (except LDLIBS, as earlier libs override later ones)
87-
_LDLIBS := ${LDLIBS} ${EXTRA_LDLIBS} ${_LDLIBS}
88-
8982
include build/exports.mk
9083

91-
# Conditionally-supported flags
92-
AUTO_FLAGS := \
93-
gen/flags/std.mk \
94-
gen/flags/bind-now.mk \
95-
gen/flags/deps.mk \
96-
gen/flags/pthread.mk \
97-
gen/flags/Wformat.mk \
98-
gen/flags/Wimplicit-fallthrough.mk \
99-
gen/flags/Wimplicit.mk \
100-
gen/flags/Wmissing-decls.mk \
101-
gen/flags/Wmissing-var-decls.mk \
102-
gen/flags/Wshadow.mk \
103-
gen/flags/Wsign-compare.mk \
104-
gen/flags/Wstrict-prototypes.mk \
105-
gen/flags/Wundef-prefix.mk
106-
107-
gen/flags.mk: ${AUTO_FLAGS}
84+
# Saves the internal flags
85+
gen/early.mk::
10886
${MSG} "[ GEN] $@"
10987
@printf '# %s\n' "$@" >$@
11088
@printf '_CPPFLAGS := %s\n' "$$XCPPFLAGS" >>$@
@@ -113,27 +91,14 @@ gen/flags.mk: ${AUTO_FLAGS}
11391
@printf '_LDLIBS := %s\n' "$$XLDLIBS" >>$@
11492
@printf 'NOLIBS := %s\n' "$$XNOLIBS" >>$@
11593
@test "${OS}-${SAN}" != FreeBSD-y || printf 'POSTLINK = elfctl -e +noaslr $$@\n' >>$@
116-
@cat $^ >>$@
117-
@cat ${^:%=%.log} >gen/flags.log
11894
${VCAT} $@
119-
.PHONY: gen/flags.mk
120-
121-
# Check that the C compiler works at all
122-
cc::
123-
@build/cc.sh -q build/empty.c -o gen/.cc.out; \
124-
ret=$$?; \
125-
build/msg-if.sh "[ CC ] build/empty.c" test $$ret -eq 0; \
126-
exit $$ret
127-
128-
# The short name of the config test
129-
SLUG = ${@:gen/%.mk=%}
130-
# The source file to build
131-
CSRC = build/${SLUG}.c
132-
# The hidden output file name
133-
OUT = ${SLUG:flags/%=gen/flags/.%.out}
134-
135-
${AUTO_FLAGS}: cc
136-
@${MKDIR} ${@D}
137-
@build/flags-if.sh ${CSRC} -o ${OUT} >$@ 2>$@.log; \
138-
build/msg-if.sh "[ CC ] ${SLUG}.c" test $$? -eq 0
139-
.PHONY: ${AUTO_FLAGS}
95+
96+
# Save explicit flags from ./configure separately so they can override the rest
97+
gen/late.mk::
98+
${MSG} "[ GEN] $@"
99+
@printf '# %s\n' "$@" >$@
100+
@printf '_CPPFLAGS += %s\n' "$$CONF_CPPFLAGS" >>$@
101+
@printf '_CFLAGS += %s\n' "$$CONF_CFLAGS" >>$@
102+
@printf '_LDFLAGS += %s\n' "$$CONF_LDFLAGS" >>$@
103+
@printf '_LDLIBS := %s $${_LDLIBS}\n' "$$CONF_LDLIBS" >>$@
104+
${VCAT} $@

build/header.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
include build/prelude.mk
77
include gen/vars.mk
8-
include gen/flags.mk
8+
include gen/early.mk
9+
include gen/auto.mk
910
include gen/pkgs.mk
11+
include gen/late.mk
1012
include build/exports.mk
1113

1214
# All header fragments we generate

build/pkgs.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
include build/prelude.mk
77
include gen/vars.mk
8-
include gen/flags.mk
8+
include gen/early.mk
9+
include gen/auto.mk
10+
include gen/late.mk
911
include build/exports.mk
1012

1113
HEADERS := ${ALL_PKGS:%=gen/with/%.h}

0 commit comments

Comments
 (0)