blob: f25169304470b64c6c966360ac7ec837964a83e8 [file] [log] [blame]
Willy Tarreau2fd6dbf2022-11-17 09:02:56 +01001# WARNING: Do not change cc-opt, cc-opt-alt or cc-warning without checking if
2# clang bug #49364 is fixed. stderr is redirected to /dev/null on
3# purpose, to work around a clang 11 bug that crashes if stderr is
4# redirected to stdin.
5#
6# Function used to detect support of a given option by the compiler.
7# Usage: CFLAGS += $(call cc-opt,option). Eg: $(call cc-opt,-fwrapv)
8# Note: ensure the referencing variable is assigned using ":=" and not "=" to
9# call it only once.
10cc-opt = $(shell set -e; if $(CC) -Werror $(1) -E -xc - -o /dev/null </dev/null >&0 2>/dev/null; then echo "$(1)"; fi;)
11
12# same but tries with $2 if $1 is not supported
13cc-opt-alt = $(if $(shell set -e; if $(CC) -Werror $(1) -E -xc - -o /dev/null </dev/null >&0 2>/dev/null; then echo 1;fi),$(1),$(call cc-opt,$(2)))
14
15# validate a list of options one at a time
16cc-all-opts = $(foreach a,$(1),$(call cc-opt,$(a)))
17
18# try to pass plenty of options at once, take them on success or try them
19# one at a time on failure and keep successful ones. This is handy to quickly
20# validate most common options.
21cc-all-fast = $(if $(call cc-opt,$(1)),$(1),$(call cc-all-opts,$(1)))
22
23# Below we verify that the compiler supports any -Wno-something option to
24# disable any warning, or if a special option is needed to achieve that. This
25# will allow to get rid of testing when the compiler doesn't care. The result
26# is made of two variables:
27# - cc-anywno that's non-empty if the compiler supports disabling anything
28# - cc-wnouwo that may contain an option needed to enable this behavior
29# Gcc 4.x and above do not need any option but will still complain about unknown
30# options if another warning or error happens, and as such they're not testable.
31# Clang needs a special option -Wno-unknown-warning-option. Compilers not
32# supporting this option will check all warnings individually.
33cc-anywno := $(call cc-opt,-Wno-haproxy-warning)
34cc-wnouwo := $(if $(cc-anywno),,$(call cc-opt,-Wno-unknown-warning-option))
35cc-anywno := $(if $(cc-anywno)$(cc-wnouwo),1)
36
37# Disable a warning when supported by the compiler. Don't put spaces around the
38# warning! And don't use cc-opt which doesn't always report an error until
39# another one is also returned. If "cc-anywno" is set, the compiler supports
40# -Wno- followed by anything so we don't even need to start the compiler.
41# Usage: CFLAGS += $(call cc-nowarn,warning). Eg: $(call cc-opt,format-truncation)
42cc-nowarn = $(if $(cc-anywno),-Wno-$(1),$(shell set -e; if $(CC) -Werror -W$(1) -E -xc - -o /dev/null </dev/null >&0 2>/dev/null; then echo "-Wno-$(1)"; fi;))