3 # Copyright (C) Dean Camera, 2014.
5 # dean [at] fourwalledcubicle [dot] com
9 LUFA_BUILD_MODULES += CPPCHECK
10 LUFA_BUILD_TARGETS += cppcheck cppcheck-config
11 LUFA_BUILD_MANDATORY_VARS += SRC
12 LUFA_BUILD_OPTIONAL_VARS += CPPCHECK_INCLUDES CPPCHECK_EXCLUDES CPPCHECK_MSG_TEMPLATE CPPCHECK_ENABLE \
13 CPPCHECK_SUPPRESS CPPCHECK_FAIL_ON_WARNING CPPCHECK_QUIET CPPCHECK_FLAGS
14 LUFA_BUILD_PROVIDED_VARS +=
15 LUFA_BUILD_PROVIDED_MACROS +=
17 # -----------------------------------------------------------------------------
18 # LUFA CPPCheck Buildsystem Makefile Module.
19 # -----------------------------------------------------------------------------
21 # Provides a set of targets to scan a project with the free "cppcheck" static
22 # analysis tool, to check for code errors at runtime
23 # (see http://cppcheck.sourceforge.net).
24 # -----------------------------------------------------------------------------
27 # cppcheck - Scan the project with CPPCheck
28 # cppcheck-config - Use CPPCheck to look for missing include files
30 # MANDATORY PARAMETERS:
32 # SRC - List of source files to statically analyze
34 # OPTIONAL PARAMETERS:
36 # CPPCHECK_INCLUDES - Extra include paths to search for missing
38 # CPPCHECK_EXCLUDES - Source file paths to exclude checking (can be
39 # a path fragment if desired)
40 # CPPCHECK_MSG_TEMPLATE - Template for cppcheck error and warning output
41 # CPPCHECK_ENABLE - General cppcheck category checks to enable
42 # CPPCHECK_SUPPRESS - Specific cppcheck warnings to disable by ID
43 # CPPCHECK_FAIL_ON_WARNING - Set to Y to fail the build on cppcheck
44 # warnings, N to continue even if warnings occur
45 # CPPCHECK_QUIET - Enable cppcheck verbose or quiet output mode
46 # CPPCHECK_FLAGS - Additional flags to pass to cppcheck
56 # -----------------------------------------------------------------------------
60 ERROR_IF_UNSET ?= $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
61 ERROR_IF_EMPTY ?= $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
62 ERROR_IF_NONBOOL ?= $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
64 # Default values of optionally user-supplied variables
67 CPPCHECK_MSG_TEMPLATE ?= {file}:{line}: {severity} ({id}): {message}
68 CPPCHECK_ENABLE ?= all
69 CPPCHECK_SUPPRESS ?= variableScope missingInclude
70 CPPCHECK_FAIL_ON_WARNING ?= Y
74 # Sanity check user supplied values
75 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
76 $(call ERROR_IF_EMPTY, SRC)
77 $(call ERROR_IF_EMPTY, CPPCHECK_MSG_TEMPLATE)
78 $(call ERROR_IF_EMPTY, CPPCHECK_ENABLE)
79 $(call ERROR_IF_NONBOOL, CPPCHECK_FAIL_ON_WARNING)
80 $(call ERROR_IF_NONBOOL, CPPCHECK_QUIET)
82 # Build a default argument list for cppcheck
83 BASE_CPPCHECK_FLAGS := --template="$(CPPCHECK_MSG_TEMPLATE)" $(CPPCHECK_INCLUDES:%=-I%) $(CPPCHECK_EXCLUDES:%=-i%) --inline-suppr --force --std=c99
85 # Sanity check parameters and construct additional command line arguments to cppcheck
86 ifeq ($(CPPCHECK_FAIL_ON_WARNING), Y)
87 BASE_CPPCHECK_FLAGS += --error-exitcode=1
89 ifeq ($(CPPCHECK_QUIET), Y)
90 BASE_CPPCHECK_FLAGS += --quiet
94 MSG_CPPCHECK_CMD := ' [CPPCHECK]:'
96 # Checks the CPPCheck configuration as used in the user project, to determine if any paths are missing or invalid
97 cppcheck-config: $(MAKEFILE_LIST)
98 @echo $(MSG_CPPCHECK_CMD) Checking cppcheck configuration check on source files
99 cppcheck $(BASE_CPPCHECK_FLAGS) --check-config $(CPPCHECK_FLAGS) $(SRC)
101 # Runs a static analysis using CPPCheck to determine if there are any issues
102 cppcheck: $(MAKEFILE_LIST)
103 @echo $(MSG_CPPCHECK_CMD) Performing static analysis on source files
104 cppcheck $(BASE_CPPCHECK_FLAGS) --enable=$(CPPCHECK_ENABLE) $(CPPCHECK_SUPPRESS:%=--suppress=%) $(CPPCHECK_FLAGS) $(SRC)
106 # Phony build targets for this module
107 .PHONY: cppcheck-config cppcheck