Skip to content

Commit ba83421

Browse files
committed
Rework of tools.cmake items
Previously, the turning on/off clang-tidy, include-what-you-use and cppcheck was handled by a combination of an option and the use of macros for specific details. This has meant only straightforward usage was accomodated. Rather, there is a desire to be more flexible, to allow for difference code to be compiled with different options, or to turn on/off tools altogether for specific scopes. The options of `CLANG_TIDY`, `IWYU` and `CPPCHECK` have been removed. Instead, it is up to the including project on when/how to enable these tools. The ability to 'reset' the tools, to disable them for certain scopes has been accomodated via the use of `reset_*` macros.
1 parent 50b5175 commit ba83421

File tree

2 files changed

+162
-129
lines changed

2 files changed

+162
-129
lines changed

README.md

+54-21
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@ This is a collection of quite useful scripts that expand the possibilities for b
2020
- [Compiler Options `compiler-options.cmake`](#compiler-options-compiler-optionscmake)
2121
- [Dependency Graph `dependency-graph.cmake`](#dependency-graph-dependency-graphcmake)
2222
- [Required Arguments](#required-arguments)
23-
- [OUTPUT_TYPE *STR*](#output_type-str)
23+
- [OUTPUT\_TYPE *STR*](#output_type-str)
2424
- [Optional Arguments](#optional-arguments)
25-
- [ADD_TO_DEP_GRAPH](#add_to_dep_graph)
26-
- [TARGET_NAME *STR*](#target_name-str)
27-
- [OUTPUT_DIR *STR*](#output_dir-str)
25+
- [ADD\_TO\_DEP\_GRAPH](#add_to_dep_graph)
26+
- [TARGET\_NAME *STR*](#target_name-str)
27+
- [OUTPUT\_DIR *STR*](#output_dir-str)
2828
- [GLSL Shader File Targeted Compilation`glsl-shaders.cmake`](#glsl-shader-file-targeted-compilationglsl-shaderscmake)
2929
- [Example](#example)
3030
- [Required Arguments](#required-arguments-1)
31-
- [TARGET_NAME](#target_name)
31+
- [TARGET\_NAME](#target_name)
3232
- [Optional Arguments](#optional-arguments-1)
3333
- [INTERFACE *FILES*](#interface-files)
3434
- [PUBLIC *FILES*](#public-files)
3535
- [PRIVATE *FILES*](#private-files)
36-
- [COMPILE_OPTIONS *OPTIONS*](#compile_options-options)
36+
- [COMPILE\_OPTIONS *OPTIONS*](#compile_options-options)
3737
- [Doxygen `doxygen.cmake`](#doxygen-doxygencmake)
3838
- [Optional Arguments](#optional-arguments-2)
39-
- [ADD_TO_DOC](#add_to_doc)
39+
- [ADD\_TO\_DOC](#add_to_doc)
4040
- [INSTALLABLE](#installable)
41-
- [PROCESS_DOXYFILE](#process_doxyfile)
42-
- [TARGET_NAME *STR*](#target_name-str-1)
43-
- [OUTPUT_DIR *STR*](#output_dir-str-1)
44-
- [INSTALL_PATH *STR*](#install_path-str)
45-
- [DOXYFILE_PATH *STR*](#doxyfile_path-str)
41+
- [PROCESS\_DOXYFILE](#process_doxyfile)
42+
- [TARGET\_NAME *STR*](#target_name-str-1)
43+
- [OUTPUT\_DIR *STR*](#output_dir-str-1)
44+
- [INSTALL\_PATH *STR*](#install_path-str)
45+
- [DOXYFILE\_PATH *STR*](#doxyfile_path-str)
4646
- [Prepare the Catch Test Framework `prepare-catch.cmake`](#prepare-the-catch-test-framework-prepare-catchcmake)
4747
- [Optional Arguments](#optional-arguments-3)
48-
- [COMPILED_CATCH](#compiled_catch)
48+
- [COMPILED\_CATCH](#compiled_catch)
4949
- [CATCH1](#catch1)
5050
- [CLONE](#clone)
5151
- [Tools `tools.cmake`](#tools-toolscmake)
@@ -338,27 +338,60 @@ Force cloning of Catch, rather than attempting to use a locally-found variant.
338338

339339
## Tools [`tools.cmake`](tools.cmake)
340340

341+
The three tools in this are used via two provided functions each, for example for clang-tidy:
342+
```
343+
add_executable(big_test)
344+
345+
clang_tidy()
346+
347+
# Sources provided here are run with clang-tidy with no options
348+
add_executable(test2 main2.cpp)
349+
target_sources(big_test test2.c test2.cpp)
350+
351+
clang_tidy(-header-filter='${CMAKE_SOURCE_DIR}/*')
352+
353+
# Sources provided here are run with clang-tidy with the header-filter options provided to it from above
354+
add_execuable(test1 main1.cpp)
355+
target_sources(big_test test1.c test1.cpp)
356+
357+
reset_clang_tidy()
358+
359+
# Sources provided here are not run with clang-tidy at all
360+
add_executable(test3 main3.cpp)
361+
target_sources(big_test test3.c test3.cpp)
362+
363+
clang_tidy()
364+
365+
# Sources provided here are run with clang-tidy with no options
366+
add_executable(test4 main4.cpp)
367+
target_sources(big_test test4.c test4.cpp)
368+
```
369+
341370
### clang-tidy
342371

343372
> clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis. clang-tidy is modular and provides a convenient interface for writing new checks.
344373
>
345-
> [clang-tidy page](https://clang.llvm.org/extra/clang-tidy/)
374+
> [clang-tidy](https://clang.llvm.org/extra/clang-tidy/)
346375
347-
When detected, [clang-tidy](https://clang.llvm.org/extra/clang-tidy/) can be enabled by using the option of `-DCLANG_TIDY=ON`, as it is disabled by default.
348-
349-
To use, add the `clang_tidy()` function, with the arguments being the options to pass to the clang tidy program, such as '-checks=*'.
376+
To use, add the `clang_tidy()` macro, with the arguments being the options passed to the clang-tidy call in the form of `clang-tidy ${ARGS}`. The settings used with clang-tidy can be changed by calling `clang_tidy()` macro again. It can be turned off by calling the `reset_clang_tidy()` macro.
350377

351378
### include-what-you-use
352379

353-
This tool helps to organize headers for all files encompass all items being used in that file, without accidentally relying upon headers deep down a chain of other headers. This is disabled by default, and can be enabled via have the program installed and adding `-DIWYU=ON`.
380+
> "Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo.h should #include a .h file that exports the declaration of that symbol. The include-what-you-use tool is a program that can be built with the clang libraries in order to analyze #includes of source files to find include-what-you-use violations, and suggest fixes for them.
381+
>
382+
> The main goal of include-what-you-use is to remove superfluous #includes. It does this both by figuring out what #includes are not actually needed for this file (for both .cc and .h files), and replacing #includes with forward-declares when possible.
383+
>
384+
> [include-what-you-use](https://include-what-you-use.org/)
354385
355-
To use, add the `include_what_you_use()` function, with the arguments being the options to pass to the program.
386+
To use, add the `include_what_you_use()` macro, with the arguments being the options passed to the include_what_you_use call in the form of `include-what-you-use ${ARGS}`. The settings used with include-what-you-use can be changed by calling `include_what_you_use()` macro again. It can be turned off by calling the `reset_include_what_you_use()` macro.
356387

357388
### cppcheck
358389

359-
This tool is another static analyzer in the vein of clang-tidy, which focuses on having no false positives. This is by default disabled, and can be enabled via have the program installed and adding `-DCPPCHECK=ON`.
390+
> Cppcheck is a static analysis tool for C/C++ code. It provides unique code analysis to detect bugs and focuses on detecting undefined behaviour and dangerous coding constructs. The goal is to have very few false positives. Cppcheck is designed to be able to analyze your C/C++ code even if it has non-standard syntax (common in embedded projects).
391+
>
392+
> [cppcheck](http://cppcheck.net/)
360393
361-
To use, add the `cppcheck()` function, with the arguments being the options to pass to the program.
394+
To use, add the `cppcheck()` macro, with the arguments being the options passed to the cppcheck call in the form of `cppcheck ${ARGS}`. The settings used with iwyu can be changed by calling `cppcheck()` macro again. It can be turned off by calling the `reset_cppcheck()` macro.
362395

363396
## Formatting [`formatting.cmake`](formatting.cmake)
364397

tools.cmake

+108-108
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,34 @@
1313
# License for the specific language governing permissions and limitations under
1414
# the License.
1515

16-
option(CLANG_TIDY "Turns on clang-tidy processing if it is found." OFF)
17-
option(IWYU "Turns on include-what-you-use processing if it is found." OFF)
18-
option(CPPCHECK "Turns on cppcheck processing if it is found." OFF)
16+
# CLANG-TIDY
17+
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
18+
set(CLANG_TIDY_MESSAGE_OUTPUT # Control output messages to occur only once
19+
FALSE
20+
CACHE INTERNAL FALSE)
21+
mark_as_advanced(FORCE CLANG_TIDY_EXE CMAKE_C_CLANG_TIDY CMAKE_CXX_CLANG_TIDY)
1922

20-
# Adds clang-tidy checks to the compilation, with the given arguments being used
21-
# as the options set.
23+
# Adds clang-tidy to code compiled after this macro. All arguments are added to
24+
# the clang-tidy application call in the form of `clang-tidy ${ARGN}`.
25+
#
26+
# If the clang-tidy application is not found, the macro will cause CMake to
27+
# produce an error and not generate.
28+
#
29+
# Options provided can be changed by calling the macro again with the new
30+
# arguments.
2231
macro(clang_tidy)
23-
if(CLANG_TIDY AND CLANG_TIDY_EXE)
32+
# Only want to output whether clang-tidy was found once
33+
if(NOT CLANG_TIDY_MESSAGE_OUTPUT)
34+
set(CLANG_TIDY_MESSAGE_OUTPUT TRUE)
35+
if(CLANG_TIDY_EXE)
36+
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
37+
else()
38+
message(SEND_ERROR "clang-tidy not found!")
39+
endif()
40+
endif()
41+
42+
# Only pass the options if the tool was found
43+
if(CLANG_TIDY_EXE)
2444
set(CMAKE_C_CLANG_TIDY
2545
${CLANG_TIDY_EXE} ${ARGN}
2646
CACHE STRING "" FORCE)
@@ -30,131 +50,111 @@ macro(clang_tidy)
3050
endif()
3151
endmacro()
3252

33-
# Adds include_what_you_use to the compilation, with the given arguments being
34-
# used as the options set.
35-
macro(include_what_you_use)
36-
if(IWYU AND IWYU_EXE)
37-
set(CMAKE_C_INCLUDE_WHAT_YOU_USE
38-
${IWYU_EXE} ${ARGN}
39-
CACHE STRING "" FORCE)
40-
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
41-
${IWYU_EXE} ${ARGN}
42-
CACHE STRING "" FORCE)
43-
endif()
44-
endmacro()
45-
46-
# Adds cppcheck to the compilation, with the given arguments being used as the
47-
# options set.
48-
macro(cppcheck)
49-
if(CPPCHECK AND CPPCHECK_EXE)
50-
set(CMAKE_C_CPPCHECK
51-
${CPPCHECK_EXE} ${ARGN}
52-
CACHE STRING "" FORCE)
53-
set(CMAKE_CXX_CPPCHECK
54-
${CPPCHECK_EXE} ${ARGN}
55-
CACHE STRING "" FORCE)
56-
endif()
57-
endmacro()
58-
59-
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
60-
mark_as_advanced(FORCE CLANG_TIDY_EXE CMAKE_C_CLANG_TIDY CMAKE_CXX_CLANG_TIDY)
61-
if(CLANG_TIDY_EXE)
62-
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
63-
if(NOT CLANG_TIDY)
64-
message(STATUS "clang-tidy NOT ENABLED via 'CLANG_TIDY' variable!")
65-
set(CMAKE_C_CLANG_TIDY
66-
""
67-
CACHE STRING "" FORCE) # delete it
68-
set(CMAKE_CXX_CLANG_TIDY
69-
""
70-
CACHE STRING "" FORCE) # delete it
71-
endif()
72-
elseif(CLANG_TIDY)
73-
message(SEND_ERROR "Cannot enable clang-tidy, as executable not found!")
53+
# Clears clang-tidy so it is not called on any following defined code
54+
# compilation. clang-tidy can be re-enabled by another call to `clang_tidy()`.
55+
macro(reset_clang_tidy)
7456
set(CMAKE_C_CLANG_TIDY
7557
""
76-
CACHE STRING "" FORCE) # delete it
58+
CACHE STRING "" FORCE)
7759
set(CMAKE_CXX_CLANG_TIDY
7860
""
79-
CACHE STRING "" FORCE) # delete it
80-
else()
81-
message(STATUS "clang-tidy not found!")
82-
set(CMAKE_C_CLANG_TIDY
83-
""
84-
CACHE STRING "" FORCE) # delete it
85-
set(CMAKE_CXX_CLANG_TIDY
86-
""
87-
CACHE STRING "" FORCE) # delete it
88-
endif()
61+
CACHE STRING "" FORCE)
62+
endmacro()
8963

64+
# INCLUDE-WHAT-YOU-USE
9065
find_program(IWYU_EXE NAMES "include-what-you-use")
66+
set(IWYU_MESSAGE_OUTPUT # Control output messages to occur only once
67+
FALSE
68+
CACHE INTERNAL FALSE)
9169
mark_as_advanced(FORCE IWYU_EXE CMAKE_C_INCLUDE_WHAT_YOU_USE
9270
CMAKE_CXX_INCLUDE_WHAT_YOU_USE)
93-
if(IWYU_EXE)
94-
message(STATUS "include-what-you-use found: ${IWYU_EXE}")
95-
if(NOT IWYU)
96-
message(STATUS "include-what-you-use NOT ENABLED via 'IWYU' variable!")
71+
72+
# Adds include-what-you-use to code compiled after this macro. All arguments are
73+
# added to the include-what-you-use application call in the form of
74+
# `include-what-you-use ${ARGN}`.
75+
#
76+
# If the include-what-you-use application is not found, the macro will cause
77+
# CMake to produce an error and not generate.
78+
#
79+
# Options provided can be changed by calling the macro again with the new
80+
# arguments.
81+
macro(include_what_you_use)
82+
# Only want to output whether clang-tidy was found once
83+
if(NOT IWYU_MESSAGE_OUTPUT)
84+
set(IWYU_MESSAGE_OUTPUT TRUE)
85+
if(IWYU_EXE)
86+
message(STATUS "include-what-you-use found: ${IWYU_EXE}")
87+
else()
88+
message(SEND_ERROR "include-what-you-use not found!")
89+
endif()
90+
endif()
91+
92+
# Only pass the options if the tool was found
93+
if(IWYU_EXE)
9794
set(CMAKE_C_INCLUDE_WHAT_YOU_USE
98-
""
99-
CACHE STRING "" FORCE) # delete it
95+
${IWYU_EXE} ${ARGN}
96+
CACHE STRING "" FORCE)
10097
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
101-
""
102-
CACHE STRING "" FORCE) # delete it
98+
${IWYU_EXE} ${ARGN}
99+
CACHE STRING "" FORCE)
103100
endif()
104-
elseif(IWYU)
105-
message(
106-
SEND_ERROR "Cannot enable include-what-you-use, as executable not found!")
107-
set(CMAKE_C_INCLUDE_WHAT_YOU_USE
108-
""
109-
CACHE STRING "" FORCE) # delete it
110-
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
111-
""
112-
CACHE STRING "" FORCE) # delete it
113-
else()
114-
message(STATUS "include-what-you-use not found!")
101+
endmacro()
102+
103+
# Clears include-what-you-use so it is not called on any following defined code
104+
# compilation. It can be re-enabled by another call to `include_what_you_use()`.
105+
macro(reset_include_what_you_use)
115106
set(CMAKE_C_INCLUDE_WHAT_YOU_USE
116107
""
117-
CACHE STRING "" FORCE) # delete it
108+
CACHE STRING "" FORCE)
118109
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
119110
""
120-
CACHE STRING "" FORCE) # delete it
121-
endif()
111+
CACHE STRING "" FORCE)
112+
endmacro()
122113

114+
# CPPCHECK
123115
find_program(CPPCHECK_EXE NAMES "cppcheck")
116+
set(CPPCHECK_MESSAGE_OUTPUT # Control output messages to occur only once
117+
FALSE
118+
CACHE INTERNAL FALSE)
124119
mark_as_advanced(FORCE CPPCHECK_EXE CMAKE_C_CPPCHECK CMAKE_CXX_CPPCHECK)
125-
if(CPPCHECK_EXE)
126-
message(STATUS "cppcheck found: ${CPPCHECK_EXE}")
127-
if(CPPECHECK)
128-
set(CMAKE_C_CPPCHECK
129-
"${CPPCHECK_EXE};--enable=warning,performance,portability,missingInclude;--template=\"[{severity}][{id}] {message} {callstack} \(On {file}:{line}\)\";--suppress=missingIncludeSystem;--quiet;--verbose;--force"
130-
)
131-
set(CMAKE_CXX_CPPCHECK
132-
"${CPPCHECK_EXE};--enable=warning,performance,portability,missingInclude;--template=\"[{severity}][{id}] {message} {callstack} \(On {file}:{line}\)\";--suppress=missingIncludeSystem;--quiet;--verbose;--force"
133-
)
120+
121+
# Adds cppcheck to code compiled after this macro. All arguments are added to
122+
# the cppcheck application call in the form of `cppcheck ${ARGN}`.
123+
#
124+
# If the include-what-you-use application is not found, the macro will cause
125+
# CMake to produce an error and not generate.
126+
#
127+
# Options provided can be changed by calling the macro again with the new
128+
# arguments.
129+
macro(cppcheck)
130+
# Only want to output whether clang-tidy was found once
131+
if(NOT CPPCHECK_MESSAGE_OUTPUT)
132+
set(CPPCHECK_MESSAGE_OUTPUT TRUE)
133+
if(CPPCHECK_EXE)
134+
message(STATUS "cppcheck found: ${CPPCHECK_EXE}")
135+
else()
136+
message(SEND_ERROR "cppcheck not found!")
137+
endif()
134138
endif()
135-
if(NOT CPPCHECK)
136-
message(STATUS "cppcheck NOT ENABLED via 'CPPCHECK' variable!")
139+
140+
# Only pass the options if the tool was found
141+
if(CPPCHECK_EXE)
137142
set(CMAKE_C_CPPCHECK
138-
""
139-
CACHE STRING "" FORCE) # delete it
143+
${CPPCHECK_EXE} ${ARGN}
144+
CACHE STRING "" FORCE)
140145
set(CMAKE_CXX_CPPCHECK
141-
""
142-
CACHE STRING "" FORCE) # delete it
146+
${CPPCHECK_EXE} ${ARGN}
147+
CACHE STRING "" FORCE)
143148
endif()
144-
elseif(CPPCHECK)
145-
message(SEND_ERROR "Cannot enable cppcheck, as executable not found!")
146-
set(CMAKE_C_CPPCHECK
147-
""
148-
CACHE STRING "" FORCE) # delete it
149-
set(CMAKE_CXX_CPPCHECK
150-
""
151-
CACHE STRING "" FORCE) # delete it
152-
else()
153-
message(STATUS "cppcheck not found!")
149+
endmacro()
150+
151+
# Clears include-what-you-use so it is not called on any following defined code
152+
# compilation. It can be re-enabled by another call to `cppcheck()`.
153+
macro(reset_cppcheck)
154154
set(CMAKE_C_CPPCHECK
155155
""
156-
CACHE STRING "" FORCE) # delete it
156+
CACHE STRING "" FORCE)
157157
set(CMAKE_CXX_CPPCHECK
158158
""
159-
CACHE STRING "" FORCE) # delete it
160-
endif()
159+
CACHE STRING "" FORCE)
160+
endmacro()

0 commit comments

Comments
 (0)