git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
	"Ramsay Jones" <ramsay@ramsayjones.plus.com>,
	larsxschneider@gmail.com,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v2] Makefile: detect compiler and enable more warnings in DEVELOPER=1
Date: Sat, 24 Mar 2018 13:53:48 +0100	[thread overview]
Message-ID: <20180324125348.6614-1-pclouds@gmail.com> (raw)
In-Reply-To: <20180318081834.16081-1-pclouds@gmail.com>

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 them those warnings are
probably worth fixing instead of just suppressing in future.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 v2 improves a bit over v1:

 - apple clang support (though I suspect we may want to make it a
   separate compiler family since apple clang has different
   versioning, but I can't and won't work on that front)

 - support CC='ccache gcc' (yes it breaks now if you have spaces in
   your cc path)

 - allow to skip detect-compiler by setting COMPILER_FEATURES in
   config.mak

 I notice Ramsay is working on clean -Wmaybe-uninitialized, if his
 series is merged first I'll stop disabling it here.

 Interdiff

    diff --git a/Makefile b/Makefile
    index 9dfd152a1e..04b2a39bab 100644
    --- a/Makefile
    +++ b/Makefile
    @@ -434,6 +434,10 @@ all::
     #
     # When cross-compiling, define HOST_CPU as the canonical name of the CPU on
     # which the built Git will run (for instance "x86_64").
    +#
    +# Define DEVELOPER to enable more compiler warnings. Compiler version
    +# and faimily are auto detected, but could be overridden by defining
    +# COMPILER_FEATURES (see config.mak.dev)
     
     GIT-VERSION-FILE: FORCE
     	@$(SHELL_PATH) ./GIT-VERSION-GEN
    diff --git a/config.mak.dev b/config.mak.dev
    index 59aef342c4..d8beaf9347 100644
    --- a/config.mak.dev
    +++ b/config.mak.dev
    @@ -8,7 +8,9 @@ CFLAGS += -Wstrict-prototypes
     CFLAGS += -Wunused
     CFLAGS += -Wvla
     
    +ifndef COMPILER_FEATURES
     COMPILER_FEATURES := $(shell ./detect-compiler $(CC))
    +endif
     
     ifneq ($(filter clang4,$(COMPILER_FEATURES)),)
     CFLAGS += -Wtautological-constant-out-of-range-compare
    diff --git a/detect-compiler b/detect-compiler
    index bc2ea39ef5..70b754481c 100755
    --- a/detect-compiler
    +++ b/detect-compiler
    @@ -9,7 +9,7 @@ CC="$*"
     #
     # FreeBSD clang version 3.4.1 (tags/RELEASE...)
     get_version_line() {
    -	"$CC" -v 2>&1 | grep ' version '
    +	$CC -v 2>&1 | grep ' version '
     }
     
     get_family() {
    @@ -38,12 +38,15 @@ case "$(get_family)" in
     gcc)
     	print_flags gcc
     	;;
    -*clang)
    +clang)
     	print_flags clang
     	;;
     "FreeBSD clang")
     	print_flags clang
     	;;
    +"Apple LLVM")
    +	print_flags clang
    +	;;
     *)
     	: unknown compiler family
     	;;

 Makefile        | 15 +++++---------
 config.mak.dev  | 30 ++++++++++++++++++++++++++++
 detect-compiler | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 10 deletions(-)
 create mode 100644 config.mak.dev
 create mode 100755 detect-compiler

diff --git a/Makefile b/Makefile
index a1d8775adb..04b2a39bab 100644
--- a/Makefile
+++ b/Makefile
@@ -434,6 +434,10 @@ all::
 #
 # When cross-compiling, define HOST_CPU as the canonical name of the CPU on
 # which the built Git will run (for instance "x86_64").
+#
+# Define DEVELOPER to enable more compiler warnings. Compiler version
+# and faimily are auto detected, but could be overridden by defining
+# COMPILER_FEATURES (see config.mak.dev)
 
 GIT-VERSION-FILE: FORCE
 	@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -442,15 +446,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 +1046,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..d8beaf9347
--- /dev/null
+++ b/config.mak.dev
@@ -0,0 +1,30 @@
+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
+
+ifndef COMPILER_FEATURES
+COMPILER_FEATURES := $(shell ./detect-compiler $(CC))
+endif
+
+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..70b754481c
--- /dev/null
+++ b/detect-compiler
@@ -0,0 +1,53 @@
+#!/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
+	;;
+"Apple LLVM")
+	print_flags clang
+	;;
+*)
+	: unknown compiler family
+	;;
+esac
-- 
2.17.0.rc0.348.gd5a49e0b6f


  parent reply	other threads:[~2018-03-24 12:54 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-03  1:46 [PATCH] travis-ci: enable more warnings on travis linux-gcc job Nguyễn Thái Ngọc Duy
2018-03-16 19:33 ` [PATCH v2] " Nguyễn Thái Ngọc Duy
2018-03-16 21:22   ` Jeff King
2018-03-17  8:01     ` Duy Nguyen
2018-03-17 14:29       ` Lars Schneider
2018-03-17 14:59         ` Duy Nguyen
2018-03-17 16:08           ` Jeff King
2018-03-17 16:12             ` Jeff King
2018-03-17 23:50             ` Junio C Hamano
2018-03-18  8:18             ` [PATCH] Makefile: detect compiler and enable more warnings in DEVELOPER=1 Nguyễn Thái Ngọc Duy
2018-03-18  9:06               ` Eric Sunshine
2018-03-18  9:17                 ` Duy Nguyen
2018-03-18  9:20                   ` Jeff King
2018-03-18  9:24                   ` Eric Sunshine
2018-03-18  9:28                 ` Jeff King
2018-03-18  9:37                   ` Eric Sunshine
2018-03-18  9:26               ` Jeff King
2018-03-18  9:45                 ` Duy Nguyen
2018-03-18 15:55               ` Duy Nguyen
2018-03-18 18:56                 ` Ramsay Jones
2018-03-19 16:30                   ` Duy Nguyen
2018-03-20  5:32                 ` Jeff King
2018-03-24 12:53               ` Nguyễn Thái Ngọc Duy [this message]
2018-03-25  0:40                 ` [PATCH v2] " Eric Sunshine
2018-03-26 22:02                 ` Junio C Hamano
2018-03-27 15:03                   ` Duy Nguyen
2018-03-27 16:52                     ` Junio C Hamano
2018-03-29 15:03                 ` [PATCH v3 0/3] Enable more compiler warnings for devs Nguyễn Thái Ngọc Duy
2018-03-29 15:03                   ` [PATCH v3 1/3] connect.c: mark die_initial_contact() NORETURN Nguyễn Thái Ngọc Duy
2018-03-29 15:03                   ` [PATCH v3 2/3] Makefile: detect compiler and enable more warnings in DEVELOPER=1 Nguyễn Thái Ngọc Duy
2018-03-29 15:03                   ` [PATCH v3 3/3] Makefile: add EAGER_DEVELOPER mode Nguyễn Thái Ngọc Duy
2018-03-31 16:40                   ` [PATCH 4/3] Makefile: untangle DEVELOPER and -Werror Ævar Arnfjörð Bjarmason
2018-03-31 18:36                     ` Duy Nguyen
2018-04-03  9:19                       ` Ævar Arnfjörð Bjarmason
2018-04-03 15:17                         ` Duy Nguyen
2018-04-06 21:42                           ` Jeff King
2018-04-07  9:52                             ` Duy Nguyen
2018-04-07 12:36                               ` Ævar Arnfjörð Bjarmason
2018-04-07 17:03                                 ` Duy Nguyen
2018-04-07 18:38                                   ` Ævar Arnfjörð Bjarmason
2018-04-14 19:19                   ` [PATCH v4 0/4] Make DEVELOPER more more flexible with DEVOPTS Ævar Arnfjörð Bjarmason
2018-04-16  4:57                     ` Junio C Hamano
2018-04-14 19:19                   ` [PATCH v4 1/4] connect.c: mark die_initial_contact() NORETURN Ævar Arnfjörð Bjarmason
2018-04-14 19:19                   ` [PATCH v4 2/4] Makefile: detect compiler and enable more warnings in DEVELOPER=1 Ævar Arnfjörð Bjarmason
2018-04-14 19:19                   ` [PATCH v4 3/4] Makefile: add a DEVOPTS to suppress -Werror under DEVELOPER Ævar Arnfjörð Bjarmason
2018-04-14 19:19                   ` [PATCH v4 4/4] Makefile: add a DEVOPTS to get all of -Wextra Ævar Arnfjörð Bjarmason
2018-03-17 15:16         ` [PATCH v2] travis-ci: enable more warnings on travis linux-gcc job Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180324125348.6614-1-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=larsxschneider@gmail.com \
    --cc=peff@peff.net \
    --cc=ramsay@ramsayjones.plus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).