git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Makefile: detect compiler and enable more warnings in DEVELOPER=1

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 18, 2018, 08:18 UTC
Message-ID
<20180318081834.16081-1-pclouds@gmail.com>
In-Reply-To
<20180317160832.GB15772@sigill.intra.peff.net>

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.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 -dumpversion does not work correctly for clang. So I use "-v" instead
 which seems to produce the same output for both gcc and clang (with a
 minor difference in freebsd where it says "FreeBSD clang" instead of
 just "clang"). Not sure if it helps your "cc on debian" case though
 Tested with clang-5.0.1 and gcc-6.4.0 (too lazy to test on freebsd
 clang 3.4.1 but I don't expect surprises there)
 I will still need to pull more modern gcc/clang on travis, but that
 can wait until this patch settles.
 Makefile        | 11 +----------
 config.mak.dev  | 28 +++++++++++++++++++++++++++
 detect-compiler | 50 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 10 deletions(-)
 create mode 100644 config.mak.dev
 create mode 100755 detect-compiler
diff --git a/Makefile b/Makefile
index a1d8775adb..9dfd152a1e 100644
--- a/Makefile
+++ b/Makefile
@@ -442,15 +442,6 @@ GIT-VERSION-FILE: FORCE
 # CFLAGS and LDFLAGS are for the users to override from the command line.
 
 CFLAGS = -g -O2 -Wall
-DEVELOPER_CFLAGS = -Werror \
-	-Wdeclaration-after-statement \
-	-Wno-format-zero-length \
-	-Wold-style-definition \
-	-Woverflow \
-	-Wpointer-arith \
-	-Wstrict-prototypes \
-	-Wunused \
-	-Wvla
 LDFLAGS =
 ALL_CFLAGS = $(CPPFLAGS) $(CFLAGS)
 ALL_LDFLAGS = $(LDFLAGS)
@@ -1051,7 +1042,7 @@ include config.mak.uname
 -include config.mak
 
 ifdef DEVELOPER
-CFLAGS += $(DEVELOPER_CFLAGS)
+include config.mak.dev
 endif
 
 comma := ,
diff --git a/config.mak.dev b/config.mak.dev
new file mode 100644
index 0000000000..59aef342c4
--- /dev/null
+++ b/config.mak.dev
@@ -0,0 +1,28 @@
+CFLAGS += -Werror
+CFLAGS += -Wdeclaration-after-statement
+CFLAGS += -Wno-format-zero-length
+CFLAGS += -Wold-style-definition
+CFLAGS += -Woverflow
+CFLAGS += -Wpointer-arith
+CFLAGS += -Wstrict-prototypes
+CFLAGS += -Wunused
+CFLAGS += -Wvla
+
+COMPILER_FEATURES := $(shell ./detect-compiler $(CC))
+
+ifneq ($(filter clang4,$(COMPILER_FEATURES)),)
+CFLAGS += -Wtautological-constant-out-of-range-compare
+endif
+
+ifneq ($(or $(filter gcc6,$(COMPILER_FEATURES)),$(filter clang4,$(COMPILER_FEATURES))),)
+CFLAGS += -Wextra
+CFLAGS += -Wmissing-prototypes
+CFLAGS += -Wno-empty-body
+CFLAGS += -Wno-missing-field-initializers
+CFLAGS += -Wno-sign-compare
+CFLAGS += -Wno-unused-function
+CFLAGS += -Wno-unused-parameter
+ifneq ($(filter gcc6,$(COMPILER_FEATURES)),)
+CFLAGS += -Wno-maybe-uninitialized
+endif
+endif
diff --git a/detect-compiler b/detect-compiler
new file mode 100755
index 0000000000..bc2ea39ef5
--- /dev/null
+++ b/detect-compiler
@@ -0,0 +1,50 @@
+#!/bin/sh
+#
+# Probe the compiler for vintage, version, etc. This is used for setting
+# optional make knobs under the DEVELOPER knob.
+
+CC="$*"
+
+# we get something like (this is at least true for gcc and clang)
+#
+# FreeBSD clang version 3.4.1 (tags/RELEASE...)
+get_version_line() {
+	"$CC" -v 2>&1 | grep ' version '
+}
+
+get_family() {
+	get_version_line | sed 's/^\(.*\) version [0-9][^ ]* .*/\1/'
+}
+
+get_version() {
+	get_version_line | sed 's/^.* version \([0-9][^ ]*\) .*/\1/'
+}
+
+print_flags() {
+	family=$1
+	version=$(get_version | cut -f 1 -d .)
+
+	# Print a feature flag not only for the current version, but also
+	# for any prior versions we encompass. This avoids needing to do
+	# numeric comparisons in make, which are awkward.
+	while test "$version" -gt 0
+	do
+		echo $family$version
+		version=$((version - 1))
+	done
+}
+
+case "$(get_family)" in
+gcc)
+	print_flags gcc
+	;;
+*clang)
+	print_flags clang
+	;;
+"FreeBSD clang")
+	print_flags clang
+	;;
+*)
+	: unknown compiler family
+	;;
+esac
-- 
2.17.0.rc0.347.gf9cf61673a
Previous: Junio C HamanoNext: Eric Sunshine
Message 10 of 47 in “travis-ci: enable more warnings on travis linux-gcc job”
  1. travis-ci: enable more warnings on travis linux-gcc jobNguyễn Thái Ngọc Duy, Mar 3, 2018
  2. travis-ci: enable more warnings on travis linux-gcc jobNguyễn Thái Ngọc Duy, Mar 16, 2018
  3. Jeff KingMar 16, 2018
  4. Duy NguyenMar 17, 2018
  5. Lars SchneiderMar 17, 2018
  6. Duy NguyenMar 17, 2018
  7. Jeff KingMar 17, 2018
  8. Jeff KingMar 17, 2018
  9. Junio C HamanoMar 17, 2018
  10. Makefile: detect compiler and enable more warnings in DEVELOPER=1Nguyễn Thái Ngọc Duy, Mar 18, 2018
  11. Eric SunshineMar 18, 2018
  12. Duy NguyenMar 18, 2018
  13. Jeff KingMar 18, 2018
  14. Eric SunshineMar 18, 2018
  15. Jeff KingMar 18, 2018
  16. Eric SunshineMar 18, 2018
  17. Jeff KingMar 18, 2018
  18. Duy NguyenMar 18, 2018
  19. Duy NguyenMar 18, 2018
  20. Ramsay JonesMar 18, 2018
  21. Duy NguyenMar 19, 2018
  22. Jeff KingMar 20, 2018
  23. Makefile: detect compiler and enable more warnings in DEVELOPER=1Nguyễn Thái Ngọc Duy, Mar 24, 2018
  24. Eric SunshineMar 25, 2018
  25. Junio C HamanoMar 26, 2018
  26. Duy NguyenMar 27, 2018
  27. Junio C HamanoMar 27, 2018
  28. 0/3 Enable more compiler warnings for devsNguyễn Thái Ngọc Duy, Mar 29, 2018
  29. 3/3 Makefile: add EAGER_DEVELOPER modeNguyễn Thái Ngọc Duy, Mar 29, 2018
  30. 2/3 Makefile: detect compiler and enable more warnings in DEVELOPER=1Nguyễn Thái Ngọc Duy, Mar 29, 2018
  31. 1/3 connect.c: mark die_initial_contact() NORETURNNguyễn Thái Ngọc Duy, Mar 29, 2018
  32. 4/3 Makefile: untangle DEVELOPER and -WerrorÆvar Arnfjörð Bjarmason, Mar 31, 2018
  33. Duy NguyenMar 31, 2018
  34. Ævar Arnfjörð BjarmasonApr 3, 2018
  35. Duy NguyenApr 3, 2018
  36. Jeff KingApr 6, 2018
  37. Duy NguyenApr 7, 2018
  38. Ævar Arnfjörð BjarmasonApr 7, 2018
  39. Duy NguyenApr 7, 2018
  40. Ævar Arnfjörð BjarmasonApr 7, 2018
  41. 0/4 Make DEVELOPER more more flexible with DEVOPTSÆvar Arnfjörð Bjarmason, Apr 14, 2018
  42. 1/4 connect.c: mark die_initial_contact() NORETURNÆvar Arnfjörð Bjarmason, Apr 14, 2018
  43. 2/4 Makefile: detect compiler and enable more warnings in DEVELOPER=1Ævar Arnfjörð Bjarmason, Apr 14, 2018
  44. 3/4 Makefile: add a DEVOPTS to suppress -Werror under DEVELOPERÆvar Arnfjörð Bjarmason, Apr 14, 2018
  45. 4/4 Makefile: add a DEVOPTS to get all of -WextraÆvar Arnfjörð Bjarmason, Apr 14, 2018
  46. Junio C HamanoApr 16, 2018
  47. Jeff KingMar 17, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.