From 0069a6bf9abd9705b0a0f64b8d819745affba7ea Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 14 Apr 2025 16:17:13 +0200 Subject: [PATCH] build system: enable support for static analysis Modules can now add the following snipped to their `Makefile`: MODULE_SUPPORTS_STATIC_ANALYSIS := 1 When the application is then build with `make STATIC_ANALYSIS=1`, all modules that opted in to static analysis get build with static analysis. --- Makefile.base | 8 ++++++++ core/Makefile | 3 +++ makefiles/toolchain/gnu.inc.mk | 3 +++ makefiles/vars.inc.mk | 1 + 4 files changed, 15 insertions(+) diff --git a/Makefile.base b/Makefile.base index 95ad434773..b9feb37f40 100644 --- a/Makefile.base +++ b/Makefile.base @@ -78,6 +78,14 @@ ifneq (,$(SRCXX)) endif endif +# If static analysis is enabled *and* the module claims to compile fine with it, +# add the corresponding flags +ifeq (1,$(STATIC_ANALYSIS)) + ifeq (1,$(MODULE_SUPPORTS_STATIC_ANALYSIS)) + CFLAGS := $(CFLAGS_STATIC_ANALYSIS) $(CFLAGS) + endif +endif + compile-commands: | $(DIRS:%=COMPILE-COMMANDS--%) $(file >$(BINDIR)/$(MODULE)/compile_cmds.txt,SRC: $(sort $(SRC) $(SRC_NO_LTO))) $(file >>$(BINDIR)/$(MODULE)/compile_cmds.txt,SRC_NO_LTO: $(sort $(SRC_NO_LTO))) diff --git a/core/Makefile b/core/Makefile index e03d095c13..2ce4fb9f02 100644 --- a/core/Makefile +++ b/core/Makefile @@ -4,4 +4,7 @@ SRC := $(filter-out mbox.c msg.c msg_bus.c thread.c thread_flags.c,$(wildcard *. # enable submodules SUBMODULES := 1 +# this module is expected to pass static analysis +MODULE_SUPPORTS_STATIC_ANALYSIS := 1 + include $(RIOTBASE)/Makefile.base diff --git a/makefiles/toolchain/gnu.inc.mk b/makefiles/toolchain/gnu.inc.mk index 20245c2756..baaed88d0d 100644 --- a/makefiles/toolchain/gnu.inc.mk +++ b/makefiles/toolchain/gnu.inc.mk @@ -36,6 +36,9 @@ OPTIONAL_CFLAGS_BLACKLIST += -Wdocumentation -Wno-error=documentation -Wno-docum # We use GDB for debugging include $(RIOTMAKE)/tools/gdb.inc.mk +# GCC's static analysis tools can be enabled using -fanalyzer +CFLAGS_STATIC_ANALYSIS := -fanalyzer + # Data address spaces starts at zero for all supported architectures. This fixes # compilation at least on MSP430 and AVR. # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523 diff --git a/makefiles/vars.inc.mk b/makefiles/vars.inc.mk index d595f15a72..1e01c83e06 100644 --- a/makefiles/vars.inc.mk +++ b/makefiles/vars.inc.mk @@ -70,6 +70,7 @@ export CXX # The CXX compiler to use. export CCAS # The C compiler to use for assembler files, typically the same as CC. export CFLAGS # The compiler flags. Must only ever be used with `+=`. export CFLAGS_CPU # CPU architecture specific compiler flags +export CFLAGS_STATIC_ANALYSIS# Additional CFLAGS to use when static analysis should be enabled export CXXUWFLAGS # (Patterns of) flags in CFLAGS that should not be passed to CXX. export CXXEXFLAGS # Additional flags that should be passed to CXX. export CCASUWFLAGS # (Patterns of) flags in CFLAGS that should not be passed to CCAS.