3 #     Copyright (C) Dean Camera, 2013. 
   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 (see http://cppcheck.sourceforge.net). 
  23 # ----------------------------------------------------------------------------- 
  26 #    cppcheck                  - Scan the project with CPPCheck 
  27 #    cppcheck-config           - Use CPPCheck to look for missing include files 
  29 # MANDATORY PARAMETERS: 
  31 #    SRC                       - List of source files to statically analyze 
  33 # OPTIONAL PARAMETERS: 
  35 #    CPPCHECK_INCLUDES         - Extra include paths to search for missing 
  37 #    CPPCHECK_EXCLUDES         - Source file paths to exclude checking (can be 
  38 #                                a path fragment if desired) 
  39 #    CPPCHECK_MSG_TEMPLATE     - Template for cppcheck error and warning output 
  40 #    CPPCHECK_ENABLE           - General cppcheck category checks to enable 
  41 #    CPPCHECK_SUPPRESS         - Specific cppcheck warnings to disable by ID 
  42 #    CPPCHECK_FAIL_ON_WARNING  - Set to Y to fail the build on cppcheck 
  43 #                                warnings, N to continue even if warnings occur 
  44 #    CPPCHECK_QUIET            - Enable cppcheck verbose or quiet output mode 
  45 #    CPPCHECK_FLAGS            - Additional flags to pass to cppcheck 
  55 # ----------------------------------------------------------------------------- 
  59 ERROR_IF_UNSET   ?
= $(if 
$(filter undefined
, $(origin $(strip $(1)))), $(error Makefile 
$(strip $(1)) value not set
)) 
  60 ERROR_IF_EMPTY   ?
= $(if 
$(strip $($(strip $(1)))), , $(error Makefile 
$(strip $(1)) option cannot be blank
)) 
  61 ERROR_IF_NONBOOL ?
= $(if 
$(filter Y N
, $($(strip $(1)))), , $(error Makefile 
$(strip $(1)) option must be Y or N
)) 
  63 # Default values of optionally user-supplied variables 
  66 CPPCHECK_MSG_TEMPLATE        ?
= {file
}:{line
}: {severity
} ({id
}): {message
} 
  67 CPPCHECK_ENABLE              ?
= all 
  68 CPPCHECK_SUPPRESS            ?
= variableScope missingInclude
 
  69 CPPCHECK_FAIL_ON_WARNING     ?
= Y
 
  73 # Sanity check user supplied values 
  74 $(foreach MANDATORY_VAR
, $(LUFA_BUILD_MANDATORY_VARS
), $(call ERROR_IF_UNSET
, $(MANDATORY_VAR
))) 
  75 $(call ERROR_IF_EMPTY
, SRC
) 
  76 $(call ERROR_IF_EMPTY
, CPPCHECK_MSG_TEMPLATE
) 
  77 $(call ERROR_IF_EMPTY
, CPPCHECK_ENABLE
) 
  78 $(call ERROR_IF_NONBOOL
, CPPCHECK_FAIL_ON_WARNING
) 
  79 $(call ERROR_IF_NONBOOL
, CPPCHECK_QUIET
) 
  81 # Build a default argument list for cppcheck 
  82 BASE_CPPCHECK_FLAGS 
:= --template
="$(CPPCHECK_MSG_TEMPLATE)" $(CPPCHECK_INCLUDES
:%=-I
%) $(CPPCHECK_EXCLUDES
:%=-i
%) --inline-suppr 
--force --std
=c99
 
  84 # Sanity check parameters and construct additional command line arguments to cppcheck 
  85 ifeq ($(CPPCHECK_FAIL_ON_WARNING
), Y
) 
  86    BASE_CPPCHECK_FLAGS 
+= --error-exitcode
=1 
  88 ifeq ($(CPPCHECK_QUIET
), Y
) 
  89    BASE_CPPCHECK_FLAGS 
+= --quiet
 
  93 MSG_CPPCHECK_CMD         
:= ' [CPPCHECK]:' 
  95 # Checks the CPPCheck configuration as used in the user project, to determine if any paths are missing or invalid 
  96 cppcheck-config
: $(MAKEFILE_LIST
) 
  97         @echo 
$(MSG_CPPCHECK_CMD
) Checking cppcheck configuration 
check on source files
 
  98         cppcheck 
$(BASE_CPPCHECK_FLAGS
) --check-config 
$(CPPCHECK_FLAGS
) $(SRC
) 
 100 # Runs a static analysis using CPPCheck to determine if there are any issues 
 101 cppcheck
: $(MAKEFILE_LIST
) 
 102         @echo 
$(MSG_CPPCHECK_CMD
) Performing static analysis on source files
 
 103         cppcheck 
$(BASE_CPPCHECK_FLAGS
) --enable
=$(CPPCHECK_ENABLE
) $(CPPCHECK_SUPPRESS
:%=--suppress
=%) $(CPPCHECK_FLAGS
) $(SRC
) 
 105 # Phony build targets for this module 
 106 .PHONY
: cppcheck-config cppcheck