Makefile: allow for combining DEVELOPER=1 and CFLAGS="..."

Ever since the DEVELOPER=1 facility introduced there's been no way to have custom CFLAGS (e.g. CFLAGS="-O0 -g -ggdb3") while still benefiting from the set of warnings and assertions DEVELOPER=1 enables. This is because the semantics of variables in the Makefile are such that the user setting CFLAGS overrides anything we set, including what we're doing in config.mak.dev[1]. So let's introduce a "DEVELOPER_CFLAGS" variable in config.mak.dev and add it to ALL_CFLAGS. Before this the ALL_CFLAGS variable would (basically, there's some nuance we won't go into) be set to: $(CPPFLAGS) [$(CFLAGS) *or* $(CFLAGS) in config.mak.dev] $(BASIC_CFLAGS) $(EXTRA_CPPFLAGS) But will now be: $(DEVELOPER_CFLAGS) $(CPPFLAGS) $(CFLAGS) $(BASIC_CFLAGS) $(EXTRA_CPPFLAGS) The reason for putting DEVELOPER_CFLAGS first is to allow for selectively overriding something DEVELOPER=1 brings in. On both GCC and Clang later settings override earlier ones. E.g. "-Wextra -Wno-extra" will enable no "extra" warnings, but not if those two arguments are reversed. Examples of things that weren't possible before, but are now: # Use -O0 instead of -O2 for less painful debuggng DEVELOPER=1 CFLAGS="-O0 -g" # DEVELOPER=1 plus -Wextra, but disable some of the warnings DEVELOPER=1 DEVOPTS="no-error extra-all" CFLAGS="-O0 -g -Wno-unused-parameter" The reason for the patches leading up to this one re-arranged the various *FLAGS assignments and includes is just for readability. The Makefile supports assignments out of order, e.g.: $ cat Makefile X = $(A) $(B) $(C) A = A B = B include c.mak all: @echo $(X) $ cat c.mak C=C $ make A B C So we could have gotten away with the much smaller change of changing "CFLAGS" in config.mak.dev to "DEVELOPER_CFLAGS" and adding that to ALL_CFLAGS earlier in the Makefile "before" the config.mak.* includes. But I think it's more readable to use variables "after" they're included. 1. https://www.gnu.org/software/make/manual/html_node/Overriding.html Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 22, 2019 at 15:41 UTC 6d5d4b4e9326021f080508126be38ea1beaba6aa
2 files changed +28 -24
Makefile
+6 -2
@@ -479,7 +479,11 @@ all::
479 #
480 # Define DEVELOPER to enable more compiler warnings. Compiler version
481 # and family are auto detected, but could be overridden by defining
482 -# COMPILER_FEATURES (see config.mak.dev)
482 +# COMPILER_FEATURES (see config.mak.dev). You can still set
483 +# CFLAGS="..." in combination with DEVELOPER enables, whether that's
484 +# for tweaking something unrelated (e.g. optimization level), or for
485 +# selectively overriding something DEVELOPER or one of the DEVOPTS
486 +# (see just below) brings in.
487 #
488 # When DEVELOPER is set, DEVOPTS can be used to control compiler
489 # options. This variable contains keywords separated by
@@ -1169,7 +1173,7 @@ ifdef DEVELOPER
1173 include config.mak.dev
1174 endif
1175
1172 -ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
1176 +ALL_CFLAGS = $(DEVELOPER_CFLAGS) $(CPPFLAGS) $(CFLAGS)
1177 ALL_LDFLAGS = $(LDFLAGS)
1178
1179 comma := ,
config.mak.dev
+22 -22
@@ -1,41 +1,41 @@
1 ifeq ($(filter no-error,$(DEVOPTS)),)
2 -CFLAGS += -Werror
2 +DEVELOPER_CFLAGS += -Werror
3 endif
4 ifneq ($(filter pedantic,$(DEVOPTS)),)
5 -CFLAGS += -pedantic
5 +DEVELOPER_CFLAGS += -pedantic
6 # don't warn for each N_ use
7 -CFLAGS += -DUSE_PARENS_AROUND_GETTEXT_N=0
8 -endif
9 -CFLAGS += -Wall
10 -CFLAGS += -Wdeclaration-after-statement
11 -CFLAGS += -Wformat-security
12 -CFLAGS += -Wno-format-zero-length
13 -CFLAGS += -Wold-style-definition
14 -CFLAGS += -Woverflow
15 -CFLAGS += -Wpointer-arith
16 -CFLAGS += -Wstrict-prototypes
17 -CFLAGS += -Wunused
18 -CFLAGS += -Wvla
7 +DEVELOPER_CFLAGS += -DUSE_PARENS_AROUND_GETTEXT_N=0
8 +endif
9 +DEVELOPER_CFLAGS += -Wall
10 +DEVELOPER_CFLAGS += -Wdeclaration-after-statement
11 +DEVELOPER_CFLAGS += -Wformat-security
12 +DEVELOPER_CFLAGS += -Wno-format-zero-length
13 +DEVELOPER_CFLAGS += -Wold-style-definition
14 +DEVELOPER_CFLAGS += -Woverflow
15 +DEVELOPER_CFLAGS += -Wpointer-arith
16 +DEVELOPER_CFLAGS += -Wstrict-prototypes
17 +DEVELOPER_CFLAGS += -Wunused
18 +DEVELOPER_CFLAGS += -Wvla
19
20 ifndef COMPILER_FEATURES
21 COMPILER_FEATURES := $(shell ./detect-compiler $(CC))
22 endif
23
24 ifneq ($(filter clang4,$(COMPILER_FEATURES)),)
25 -CFLAGS += -Wtautological-constant-out-of-range-compare
25 +DEVELOPER_CFLAGS += -Wtautological-constant-out-of-range-compare
26 endif
27
28 ifneq ($(or $(filter gcc6,$(COMPILER_FEATURES)),$(filter clang4,$(COMPILER_FEATURES))),)
29 -CFLAGS += -Wextra
29 +DEVELOPER_CFLAGS += -Wextra
30 # if a function is public, there should be a prototype and the right
31 # header file should be included. If not, it should be static.
32 -CFLAGS += -Wmissing-prototypes
32 +DEVELOPER_CFLAGS += -Wmissing-prototypes
33 ifeq ($(filter extra-all,$(DEVOPTS)),)
34 # These are disabled because we have these all over the place.
35 -CFLAGS += -Wno-empty-body
36 -CFLAGS += -Wno-missing-field-initializers
37 -CFLAGS += -Wno-sign-compare
38 -CFLAGS += -Wno-unused-parameter
35 +DEVELOPER_CFLAGS += -Wno-empty-body
36 +DEVELOPER_CFLAGS += -Wno-missing-field-initializers
37 +DEVELOPER_CFLAGS += -Wno-sign-compare
38 +DEVELOPER_CFLAGS += -Wno-unused-parameter
39 endif
40 endif
41
@@ -43,6 +43,6 @@ endif
43 # not worth fixing since newer compilers correctly stop complaining
44 ifneq ($(filter gcc4,$(COMPILER_FEATURES)),)
45 ifeq ($(filter gcc5,$(COMPILER_FEATURES)),)
46 -CFLAGS += -Wno-uninitialized
46 +DEVELOPER_CFLAGS += -Wno-uninitialized
47 endif
48 endif