3 # Copyright (C) Dean Camera, 2012.
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
15 # -----------------------------------------------------------------------------
16 # LUFA CPPCheck Buildsystem Makefile Module.
17 # -----------------------------------------------------------------------------
19 # Provides a set of targets to scan a project with the free "cppcheck" static
20 # analysis tool, to check for code errors at runtime (see http://cppcheck.sourceforge.net).
21 # -----------------------------------------------------------------------------
24 # cppcheck - Scan the project with CPPCheck
25 # cppcheck-config - Use CPPCheck to look for missing include files
27 # MANDATORY PARAMETERS:
29 # SRC - List of source files to statically analyze
31 # OPTIONAL PARAMETERS:
33 # CPPCHECK_INCLUDES - Extra include paths to search for missing
35 # CPPCHECK_EXCLUDES - Source file paths to exclude checking (can be
36 # a path fragment if desired)
37 # CPPCHECK_MSG_TEMPLATE - Template for cppcheck error and warning output
38 # CPPCHECK_ENABLE - General cppcheck category checks to enable
39 # CPPCHECK_SUPPRESS - Specific cppcheck warnings to disable by ID
40 # CPPCHECK_FAIL_ON_WARNING - Set to Y to fail the build on cppcheck
41 # warnings, N to continue even if warnings occur
42 # CPPCHECK_QUIET - Enable cppcheck verbose or quiet output mode
43 # CPPCHECK_FLAGS - Additional flags to pass to cppcheck
45 # -----------------------------------------------------------------------------
47 ERROR_IF_UNSET = $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
48 ERROR_IF_EMPTY = $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
49 ERROR_IF_NONBOOL = $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))
51 # Default values of optionally user-supplied variables
54 CPPCHECK_MSG_TEMPLATE ?= {file}:{line}: {severity} ({id}): {message}
55 CPPCHECK_ENABLE ?= all
56 CPPCHECK_SUPPRESS ?= variableScope missingInclude
57 CPPCHECK_FAIL_ON_WARNING ?= Y
61 # Sanity check user supplied values
62 $(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
63 $(call ERROR_IF_EMPTY, SRC)
64 $(call ERROR_IF_EMPTY, CPPCHECK_MSG_TEMPLATE)
65 $(call ERROR_IF_EMPTY, CPPCHECK_ENABLE)
66 $(call ERROR_IF_NONBOOL, CPPCHECK_FAIL_ON_WARNING)
67 $(call ERROR_IF_NONBOOL, CPPCHECK_QUIET)
69 # Build a default argument list for cppcheck
70 BASE_CPPCHECK_FLAGS := --template="$(CPPCHECK_MSG_TEMPLATE)" $(CPPCHECK_INCLUDES:%=-I%) $(CPPCHECK_EXCLUDES:%=-i%) --inline-suppr --force --std=c99
72 # Sanity check parameters and construct additional command line arguments to cppcheck
73 ifeq ($(CPPCHECK_FAIL_ON_WARNING), Y)
74 BASE_CPPCHECK_FLAGS += --error-exitcode=1
76 ifeq ($(CPPCHECK_QUIET), Y)
77 BASE_CPPCHECK_FLAGS += --quiet
81 MSG_CPPCHECK_CMD := ' [CPPCHECK]:'
84 @echo $(MSG_CPPCHECK_CMD) Checking cppcheck configuration check on source files
85 cppcheck $(BASE_CPPCHECK_FLAGS) --check-config $(CPPCHECK_FLAGS) $(SRC)
88 @echo $(MSG_CPPCHECK_CMD) Performing cppcheck analysis on source files
89 cppcheck $(BASE_CPPCHECK_FLAGS) --enable=$(CPPCHECK_ENABLE) $(CPPCHECK_SUPPRESS:%=--suppress=%) $(CPPCHECK_FLAGS) $(SRC)
91 # Phony build targets for this module
92 .PHONY: cppcheck-config cppcheck