diff options
Diffstat (limited to 'include/make')
-rw-r--r-- | include/make/compiler.mk | 42 | ||||
-rw-r--r-- | include/make/options.mk | 52 | ||||
-rw-r--r-- | include/make/verbose.mk | 30 |
3 files changed, 124 insertions, 0 deletions
diff --git a/include/make/compiler.mk b/include/make/compiler.mk new file mode 100644 index 0000000..f251693 --- /dev/null +++ b/include/make/compiler.mk @@ -0,0 +1,42 @@ +# WARNING: Do not change cc-opt, cc-opt-alt or cc-warning without checking if +# clang bug #49364 is fixed. stderr is redirected to /dev/null on +# purpose, to work around a clang 11 bug that crashes if stderr is +# redirected to stdin. +# +# Function used to detect support of a given option by the compiler. +# Usage: CFLAGS += $(call cc-opt,option). Eg: $(call cc-opt,-fwrapv) +# Note: ensure the referencing variable is assigned using ":=" and not "=" to +# call it only once. +cc-opt = $(shell set -e; if $(CC) -Werror $(1) -E -xc - -o /dev/null </dev/null >&0 2>/dev/null; then echo "$(1)"; fi;) + +# same but tries with $2 if $1 is not supported +cc-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))) + +# validate a list of options one at a time +cc-all-opts = $(foreach a,$(1),$(call cc-opt,$(a))) + +# try to pass plenty of options at once, take them on success or try them +# one at a time on failure and keep successful ones. This is handy to quickly +# validate most common options. +cc-all-fast = $(if $(call cc-opt,$(1)),$(1),$(call cc-all-opts,$(1))) + +# Below we verify that the compiler supports any -Wno-something option to +# disable any warning, or if a special option is needed to achieve that. This +# will allow to get rid of testing when the compiler doesn't care. The result +# is made of two variables: +# - cc-anywno that's non-empty if the compiler supports disabling anything +# - cc-wnouwo that may contain an option needed to enable this behavior +# Gcc 4.x and above do not need any option but will still complain about unknown +# options if another warning or error happens, and as such they're not testable. +# Clang needs a special option -Wno-unknown-warning-option. Compilers not +# supporting this option will check all warnings individually. +cc-anywno := $(call cc-opt,-Wno-haproxy-warning) +cc-wnouwo := $(if $(cc-anywno),,$(call cc-opt,-Wno-unknown-warning-option)) +cc-anywno := $(if $(cc-anywno)$(cc-wnouwo),1) + +# Disable a warning when supported by the compiler. Don't put spaces around the +# warning! And don't use cc-opt which doesn't always report an error until +# another one is also returned. If "cc-anywno" is set, the compiler supports +# -Wno- followed by anything so we don't even need to start the compiler. +# Usage: CFLAGS += $(call cc-nowarn,warning). Eg: $(call cc-opt,format-truncation) +cc-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;)) diff --git a/include/make/options.mk b/include/make/options.mk new file mode 100644 index 0000000..022981c --- /dev/null +++ b/include/make/options.mk @@ -0,0 +1,52 @@ +# this contains various functions and macros used to manipulate USE_* options +# and their flags + +# Depending on the target platform, some options are set, as well as some +# CFLAGS and LDFLAGS. All variables pre-set here will not appear in the build +# options string. They may be set to any value, but are historically set to +# "implicit" which eases debugging. You should not have to change anything +# there unless you're adding support for a new platform. +default_opts = $(foreach name,$(1),$(eval $(name)=implicit)) + +# Return USE_xxx=$(USE_xxx) if the variable was set from the environment or the +# command line. +ignore_implicit = $(if $(subst environment,,$(origin $(1))), \ + $(if $(subst command line,,$(origin $(1))),, \ + $(1)=$($(1))), \ + $(1)=$($(1))) \ + +# This macro collects all USE_* values except those set to "implicit". This +# is used to report a list of all flags which were used to build this version. +# Do not assign anything to it. +build_options = $(foreach opt,$(use_opts),$(call ignore_implicit,$(opt))) + +# Make a list of all known features with +/- prepended depending on their +# activation status. Must be a macro so that dynamically enabled ones are +# evaluated with their current status. +build_features = $(foreach opt,$(patsubst USE_%,%,$(sort $(use_opts))),$(if $(USE_$(opt)),+$(opt),-$(opt))) + +# This returns a list of -DUSE_* for all known USE_* that are set +opts_as_defines = $(foreach opt,$(use_opts),$(if $($(opt)),-D$(opt),)) + +# Lists all enabled or disabled options without the "USE_" prefix +enabled_opts = $(foreach opt,$(patsubst USE_%,%,$(use_opts)),$(if $(USE_$(opt)),$(opt),)) +disabled_opts = $(foreach opt,$(patsubst USE_%,%,$(use_opts)),$(if $(USE_$(opt)),,$(opt))) + +# preset all XXX_{INC,LIB,CFLAGS,LDFLAGS,SRC} variables to empty for $1=XXX +reset_opt_vars = $(foreach name,INC LIB CFLAGS LDFLAGS SRC,$(eval $(1)_$(name)=)) + +# preset all variables for all supported build options among use_opts +reset_opts_vars = $(foreach opt,$(patsubst USE_%,%,$(use_opts)),$(call reset_opt_vars,$(opt))) + +# append $(1)_{C,LD}FLAGS into OPTIONS_{C,LD}FLAGS if not empty +define collect_opt_flags + ifneq ($$($(1)_CFLAGS),) + OPTIONS_CFLAGS += $$($(1)_CFLAGS) + endif + ifneq ($$($(1)_LDFLAGS),) + OPTIONS_LDFLAGS += $$($(1)_LDFLAGS) + endif +endef + +# collect all enabled USE_foo's foo_{C,LD}FLAGS into OPTIONS_{C,LD}FLAGS +collect_opts_flags = $(foreach opt,$(enabled_opts),$(eval $(call collect_opt_flags,$(opt)))) diff --git a/include/make/verbose.mk b/include/make/verbose.mk new file mode 100644 index 0000000..c37d513 --- /dev/null +++ b/include/make/verbose.mk @@ -0,0 +1,30 @@ +# verbosity: pass V=1 for verbose shell invocation +V = 0 +Q = @ +ifeq ($V,1) +Q= +endif + +# Some common commands such as CC/LD/AR are redefined with a cmd_ equivalent +# and are either mapped to a silent rule just indicating what is being done, +# or to themselves depending on the verbosity level. +ifeq ($V,1) +cmd_CC = $(CC) +cmd_LD = $(LD) +cmd_AR = $(AR) +cmd_MAKE = +$(MAKE) +else +ifeq (3.81,$(firstword $(sort $(MAKE_VERSION) 3.81))) +# 3.81 or above +cmd_CC = $(info $ CC $@) $(Q)$(CC) +cmd_LD = $(info $ LD $@) $(Q)$(LD) +cmd_AR = $(info $ AR $@) $(Q)$(AR) +cmd_MAKE = $(info $ MAKE $@) $(Q)+$(MAKE) +else +# 3.80 or older +cmd_CC = $(Q)echo " CC $@";$(CC) +cmd_LD = $(Q)echo " LD $@";$(LD) +cmd_AR = $(Q)echo " AR $@";$(AR) +cmd_MAKE = $(Q)echo " MAKE $@";$(MAKE) +endif +endif |