Skip to content

Commit 1da1580

Browse files
pcloudsgitster
authored andcommitted
Makefile: detect compiler and enable more warnings in DEVELOPER=1
The set of extra warnings we enable when DEVELOPER has to be conservative because we can't assume any compiler version the developer may use. Detect the compiler version so we know when it's safe to enable -Wextra and maybe more. These warning settings are mostly from my custom config.mak a long time ago when I tried to enable as many warnings as possible that can still build without showing warnings. Some of those warnings are probably worth fixing instead of just suppressing in future. Helped-by: Jeff King <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d2bff22 commit 1da1580

File tree

3 files changed

+96
-10
lines changed

3 files changed

+96
-10
lines changed

Makefile

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ all::
431431
#
432432
# When cross-compiling, define HOST_CPU as the canonical name of the CPU on
433433
# which the built Git will run (for instance "x86_64").
434+
#
435+
# Define DEVELOPER to enable more compiler warnings. Compiler version
436+
# and family are auto detected, but could be overridden by defining
437+
# COMPILER_FEATURES (see config.mak.dev)
434438

435439
GIT-VERSION-FILE: FORCE
436440
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -439,15 +443,6 @@ GIT-VERSION-FILE: FORCE
439443
# CFLAGS and LDFLAGS are for the users to override from the command line.
440444

441445
CFLAGS = -g -O2 -Wall
442-
DEVELOPER_CFLAGS = -Werror \
443-
-Wdeclaration-after-statement \
444-
-Wno-format-zero-length \
445-
-Wold-style-definition \
446-
-Woverflow \
447-
-Wpointer-arith \
448-
-Wstrict-prototypes \
449-
-Wunused \
450-
-Wvla
451446
LDFLAGS =
452447
ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
453448
ALL_LDFLAGS = $(LDFLAGS)
@@ -1047,7 +1042,7 @@ include config.mak.uname
10471042
-include config.mak
10481043

10491044
ifdef DEVELOPER
1050-
CFLAGS += $(DEVELOPER_CFLAGS)
1045+
include config.mak.dev
10511046
endif
10521047

10531048
comma := ,

config.mak.dev

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CFLAGS += -Werror
2+
CFLAGS += -Wdeclaration-after-statement
3+
CFLAGS += -Wno-format-zero-length
4+
CFLAGS += -Wold-style-definition
5+
CFLAGS += -Woverflow
6+
CFLAGS += -Wpointer-arith
7+
CFLAGS += -Wstrict-prototypes
8+
CFLAGS += -Wunused
9+
CFLAGS += -Wvla
10+
11+
ifndef COMPILER_FEATURES
12+
COMPILER_FEATURES := $(shell ./detect-compiler $(CC))
13+
endif
14+
15+
ifneq ($(filter clang4,$(COMPILER_FEATURES)),)
16+
CFLAGS += -Wtautological-constant-out-of-range-compare
17+
endif
18+
19+
ifneq ($(or $(filter gcc6,$(COMPILER_FEATURES)),$(filter clang4,$(COMPILER_FEATURES))),)
20+
CFLAGS += -Wextra
21+
# if a function is public, there should be a prototype and the right
22+
# header file should be included. If not, it should be static.
23+
CFLAGS += -Wmissing-prototypes
24+
# These are disabled because we have these all over the place.
25+
CFLAGS += -Wno-empty-body
26+
CFLAGS += -Wno-missing-field-initializers
27+
CFLAGS += -Wno-sign-compare
28+
CFLAGS += -Wno-unused-function
29+
CFLAGS += -Wno-unused-parameter
30+
endif
31+
32+
# uninitialized warnings on gcc 4.9.2 in xdiff/xdiffi.c and config.c
33+
# not worth fixing since newer compilers correctly stop complaining
34+
ifneq ($(filter gcc4,$(COMPILER_FEATURES)),)
35+
ifeq ($(filter gcc5,$(COMPILER_FEATURES)),)
36+
CFLAGS += -Wno-uninitialized
37+
endif
38+
endif

detect-compiler

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/sh
2+
#
3+
# Probe the compiler for vintage, version, etc. This is used for setting
4+
# optional make knobs under the DEVELOPER knob.
5+
6+
CC="$*"
7+
8+
# we get something like (this is at least true for gcc and clang)
9+
#
10+
# FreeBSD clang version 3.4.1 (tags/RELEASE...)
11+
get_version_line() {
12+
$CC -v 2>&1 | grep ' version '
13+
}
14+
15+
get_family() {
16+
get_version_line | sed 's/^\(.*\) version [0-9][^ ]* .*/\1/'
17+
}
18+
19+
get_version() {
20+
get_version_line | sed 's/^.* version \([0-9][^ ]*\) .*/\1/'
21+
}
22+
23+
print_flags() {
24+
family=$1
25+
version=$(get_version | cut -f 1 -d .)
26+
27+
# Print a feature flag not only for the current version, but also
28+
# for any prior versions we encompass. This avoids needing to do
29+
# numeric comparisons in make, which are awkward.
30+
while test "$version" -gt 0
31+
do
32+
echo $family$version
33+
version=$((version - 1))
34+
done
35+
}
36+
37+
case "$(get_family)" in
38+
gcc)
39+
print_flags gcc
40+
;;
41+
clang)
42+
print_flags clang
43+
;;
44+
"FreeBSD clang")
45+
print_flags clang
46+
;;
47+
"Apple LLVM")
48+
print_flags clang
49+
;;
50+
*)
51+
: unknown compiler family
52+
;;
53+
esac

0 commit comments

Comments
 (0)