-
Notifications
You must be signed in to change notification settings - Fork 71
/
CommonCPPLint.cmake
125 lines (106 loc) · 4.22 KB
/
CommonCPPLint.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# - Run cpplint on c++ source files as a custom target and a test
#
# include(CpplintTargets)
# common_cpplint(<target-name> [FILES files] [CATEGORY_FILTER_OUT category ...]
# [EXTENSIONS extension ...] [VERBOSE level]
# [COUNTING level_of_detail] [ROOT subdir] [LINELENGTH digits]
# [EXCLUDE_PATTERN pattern ...])
# Create a target to check a target's sources with cpplint and the indicated
# options
#
# Input variables:
# * CPPLINT_ADD_TESTS: When set to ON, add cpplint targets to tests
if(TARGET ${PROJECT_NAME}-cpplint)
return()
endif()
include(CMakeParseArguments)
include(GetSourceFilesFromTarget)
if(NOT CPPLINT_FOUND)
find_package(cpplint QUIET)
endif()
if(NOT CPPLINT_FOUND)
add_custom_target(${PROJECT_NAME}-cpplint COMMENT "${CPPLINT_NOT_FOUND_MSG}")
set_target_properties(${PROJECT_NAME}-cpplint PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests/cpplint)
endif(NOT CPPLINT_FOUND)
if(NOT TARGET cpplint)
add_custom_target(cpplint)
set_target_properties(cpplint PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON)
endif()
function(common_cpplint _name)
if(NOT CPPLINT_FOUND)
return()
endif()
set(oneValueArgs VERBOSE COUNTING ROOT LINELENGTH EXCLUDE_PATTERN)
set(multiValueArgs FILES CATEGORY_FILTER_OUT EXTENSIONS)
cmake_parse_arguments(common_cpplint "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})
if(NOT TARGET ${_name})
message(FATAL_ERROR
"common_cpplint is given a target name that does not exist: '${_name}' !")
endif()
set(_cpplint_args)
# handles category filters
set(_category_filter)
set(_category_filter_in "+build,+legal,+readability,+runtime,+whitespace")
if (common_cpplint_CATEGORY_FILTER_OUT)
string(REPLACE ";" ",-" common_cpplint_CATEGORY_FILTER_OUT "${common_cpplint_CATEGORY_FILTER_OUT}")
set(_category_filter "--filter=${_category_filter_in},-${common_cpplint_CATEGORY_FILTER_OUT}")
endif()
list(APPEND _cpplint_args ${_category_filter})
# handles allowed extensions
if (common_cpplint_EXTENSIONS)
string(REPLACE ";" "," common_cpplint_EXTENSIONS "${common_cpplint_EXTENSIONS}")
set(common_cpplint_EXTENSIONS "--extensions=${common_cpplint_EXTENSIONS}")
list(APPEND _cpplint_args ${common_cpplint_EXTENSIONS})
endif()
# handles verbosity level ([0-5])
if (common_cpplint_VERBOSE)
list(APPEND _cpplint_args "--verbose=${common_cpplint_VERBOSE}")
endif()
# handles counting level of detail (total|toplevel|detailed)
if (common_cpplint_COUNTING)
list(APPEND _cpplint_args "--counting=${common_cpplint_COUNTING}")
endif()
# handles root directory used for deriving header guard CPP variable
if(common_cpplint_ROOT)
list(APPEND _cpplint_args "--root=${common_cpplint_ROOT}")
endif()
# handles line length
if (common_cpplint_LINELENGTH)
list(APPEND _cpplint_args "--linelength=${common_cpplint_LINELENGTH}")
endif()
set(_files ${common_cpplint_FILES})
if(NOT _files)
# handles exclude pattern
if(NOT common_cpplint_EXCLUDE_PATTERN)
set(common_cpplint_EXCLUDE_PATTERN "^$") # Empty string regex
endif()
get_source_files(${_name} ${common_cpplint_EXCLUDE_PATTERN})
if(NOT ${_name}_FILES) # nothing to check
return()
endif()
set(_files ${${_name}_FILES})
endif()
if(CPPLINT_ADD_TESTS)
if(NOT TARGET ${PROJECT_NAME}-tests)
add_custom_target(${PROJECT_NAME}-tests)
set_target_properties(${PROJECT_NAME}-tests PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests)
endif()
add_dependencies(${PROJECT_NAME}-tests cpplint_run_${_name})
endif()
add_custom_target(cpplint_run_${_name}
COMMAND ${CPPLINT_SCRIPT} ${_cpplint_args} ${_files}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
set_target_properties(cpplint_run_${_name} PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests/cpplint)
if(NOT TARGET ${PROJECT_NAME}-cpplint)
add_custom_target(${PROJECT_NAME}-cpplint)
set_target_properties(${PROJECT_NAME}-cpplint PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests/cpplint)
endif()
add_dependencies(${PROJECT_NAME}-cpplint cpplint_run_${_name})
add_dependencies(cpplint ${PROJECT_NAME}-cpplint)
endfunction()