Skip to content

Instantly share code, notes, and snippets.

@noahp
Last active December 16, 2019 17:57
Show Gist options
  • Save noahp/4203a733015dea97459db4e45ebdfe9c to your computer and use it in GitHub Desktop.
Save noahp/4203a733015dea97459db4e45ebdfe9c to your computer and use it in GitHub Desktop.
Check if a C compiler warning is supported by clang/gcc
# Detect if a warning flag is supported by the $(CC) compiler
#
# HEADS UP- This should be used after including CppUTest MakefileWorker.mk,
# which populates the $(COMPILER_NAME) variable, but will work if you don't
# (it's just slower by a few ms because it will attempt to use the wrong
# option when testing).
#
# Example usage:
# ifeq ($(call WARNING_SUPPORTED,address-of-packed-member),supported)
# CPPUTEST_ADDITIONAL_CPPFLAGS += \
# -Werror=address-of-packed-member
# endif
# For gcc, grep through the warning list output by the compiler. This is about
# 2x as slow as using -dumpversion (~12ms vs ~6ms) but more likely to give a
# correct result
define GNUC_WARNING_SUPPORTED =
$(strip
$(if $(filter $(COMPILER_NAME),$(CLANG_STR)),
,
$(shell $(CC) -Q --help=warning | grep --quiet $(1) > /dev/null 2>&1 && echo supported)
)
)
endef
# For clang, the '__has_warning' compiler extension is used
define CLANG_WARNING_SUPPORTED =
$(strip
$(if $(filter $(COMPILER_NAME),$(CLANG_STR)),
$(shell echo '_Static_assert(__has_warning("-W$(1)")==1, "error");' | $(CC) -x c -c - > /dev/null 2>&1 && echo supported),
)
)
endef
# Combine together the clang + gcc check
define WARNING_SUPPORTED =
$(or $(call CLANG_WARNING_SUPPORTED,$(1)),$(call GNUC_WARNING_SUPPORTED,$(1)))
endef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment