Skip to content

Commit 2fa2d70

Browse files
authored
Merge pull request zerebubuth#382 from Woazboat/cmake-clang-tidy
Integrate clang-tidy in cmake build
2 parents f3a94a3 + 949389a commit 2fa2d70

File tree

5 files changed

+128
-9
lines changed

5 files changed

+128
-9
lines changed

.clang-tidy

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
Checks: '-*,cppcoreguidelines-*,bugprone-*,performance*,modernize-*,-modernize-use-trailing-return-type'
3+
...

.iwyu_mappings.imp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{ include: ["@<libmemcached-.*/.*>", "public", "<libmemcached/memcached.h>", "public"] },
3+
{ include: ["@<boost/algorithm/string/.*>", "public", "<boost/algorithm/string.hpp>", "public"] },
4+
{ include: ["@<boost/program_options/.*>", "public", "<boost/program_options.hpp>", "public"] },
5+
{ include: ["@<boost/lexical_cast/.*>", "public", "<boost/lexical_cast.hpp>", "public"] },
6+
{ include: ["@<pqxx/.*\\.h.*>", "private", "<pqxx/pqxx>", "public"] },
7+
{ include: ["<cryptopp/config_int.h>", "private", "<cryptopp/config.h>", "public"] },
8+
{ include: ["<bits/chrono.h>", "private", "<chrono>", "public"] },
9+
{ include: ["<bits/local_lim.h>", "private", "<limits.h>", "public"] },
10+
]

CMakeLists.txt

+3-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ endif()
5353

5454
include(CTest)
5555
include(AutotoolsCompatibilityDefinitions)
56+
include(BuildLint)
5657

5758
#########################
5859
# common compiler options
@@ -145,17 +146,12 @@ target_link_libraries(openstreetmap_cgimap
145146
PQXX::PQXX)
146147

147148

148-
149149
#############################################################
150150
# Optional "clang-tidy" target
151151
#############################################################
152152

153-
find_program(CLANG_TIDY
154-
NAMES clang-tidy clang-tidy-16 clang-tidy-15 clang-tidy-14 clang-tidy-13 clang-tidy-12 clang-tidy-11)
155-
156-
if (CLANG_TIDY)
157-
message(STATUS "Looking for clang-tidy - found ${CLANG_TIDY}")
158-
153+
# BuildLint module already checks for clang-tidy
154+
if(CLANG_TIDY)
159155
file(GLOB CT_CHECK_FILES src/*.cpp src/api06/*.cpp src/api06/changeset_upload/*.cpp src/api07/*.cpp
160156
src/backend/apidb/*.cpp src/backend/apidb/changeset_upload/*.cpp
161157
test/*.cpp test/*.hpp)
@@ -166,8 +162,6 @@ if (CLANG_TIDY)
166162
-p ${CMAKE_BINARY_DIR}
167163
${CT_CHECK_FILES}
168164
)
169-
else()
170-
message(STATUS "Looking for clang-tidy - not found")
171165
endif()
172166

173167

cmake/BuildLint.cmake

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# CMAKE_CXX_<LINTER> variables need to be set before targets are created
2+
3+
############
4+
# clang-tidy
5+
############
6+
option(ENABLE_BUILD_LINT_CLANG_TIDY "Automatically check code with clang-tidy during compilation" OFF)
7+
8+
find_program(CLANG_TIDY
9+
NAMES clang-tidy clang-tidy-16 clang-tidy-15 clang-tidy-14 clang-tidy-13 clang-tidy-12 clang-tidy-11)
10+
11+
if(CLANG_TIDY)
12+
message(STATUS "Looking for clang-tidy - found ${CLANG_TIDY}")
13+
14+
if(ENABLE_BUILD_LINT_CLANG_TIDY)
15+
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY}" -extra-arg=-std=c++17)
16+
endif()
17+
else()
18+
message(STATUS "Looking for clang-tidy - not found")
19+
endif()
20+
21+
##########
22+
# cppcheck
23+
##########
24+
option(ENABLE_BUILD_LINT_CPPCHECK "Automatically check code with cppcheck during compilation" OFF)
25+
26+
if(ENABLE_BUILD_LINT_CPPCHECK)
27+
find_program(CPPCHECK NAMES cppcheck)
28+
29+
if(CPPCHECK)
30+
message(STATUS "Looking for cppcheck - found ${CPPCHECK}")
31+
32+
set(CMAKE_CXX_CPPCHECK "${CPPCHECK}")
33+
else()
34+
message(STATUS "Looking for cppcheck - not found")
35+
endif()
36+
endif()
37+
38+
#########
39+
# cpplint
40+
#########
41+
option(ENABLE_BUILD_LINT_CPPLINT "Automatically check code with cpplint during compilation" OFF)
42+
43+
if(ENABLE_BUILD_LINT_CPPLINT)
44+
find_program(CPPLINT NAMES cpplint)
45+
46+
if(CPPLINT)
47+
message(STATUS "Looking for cpplint - found ${CPPLINT}")
48+
49+
set(CMAKE_CXX_CPPLINT "${CPPLINT}" --filter=-whitespace,-whitespace/braces,-whitespace/indent,-whitespace/line_length,-whitespace/comments,-readability/todo,-runtime/references,-build/c++11,-build/include_order)
50+
else()
51+
message(STATUS "Looking for cpplint - not found")
52+
endif()
53+
endif()
54+
55+
######################
56+
# include-what-you-use
57+
######################
58+
option(ENABLE_BUILD_LINT_IWYU "Automatically check code with include-what-you-use during compilation" OFF)
59+
60+
if(ENABLE_BUILD_LINT_IWYU)
61+
find_program(IWYU NAMES iwyu)
62+
63+
if(IWYU)
64+
message(STATUS "Looking for include-what-you-use - found ${IWYU}")
65+
66+
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU}" -std=c++17 -Xiwyu --cxx17ns -Xiwyu --no_fwd_decls -Xiwyu "--mapping_file=${CMAKE_SOURCE_DIR}/.iwyu_mappings.imp")
67+
else()
68+
message(STATUS "Looking for include-what-you-use - not found")
69+
endif()
70+
endif()
71+
72+
73+
########################################################
74+
# Disable / save / restore lint config utility functions
75+
########################################################
76+
77+
set(BUILD_LINT_CONFIGS CMAKE_CXX_CLANG_TIDY CMAKE_CXX_CPPCHECK CMAKE_CXX_CPPLINT CMAKE_CXX_INCLUDE_WHAT_YOU_USE)
78+
79+
function(save_build_lint_config LINT_CONFIG_VAR)
80+
foreach(LINT_CONFIG IN LISTS BUILD_LINT_CONFIGS)
81+
string(REPLACE ";" "@SEMICOLON@" ${LINT_CONFIG} "${${LINT_CONFIG}}")
82+
list(APPEND LINT_CONFIGS_LIST "${${LINT_CONFIG}}")
83+
endforeach()
84+
85+
set(${LINT_CONFIG_VAR} ${LINT_CONFIGS_LIST} PARENT_SCOPE)
86+
endfunction()
87+
88+
89+
function(restore_build_lint_config LINT_CONFIG_VAR)
90+
foreach(LINT_CONFIG IN LISTS BUILD_LINT_CONFIGS)
91+
list(POP_FRONT ${LINT_CONFIG_VAR} ${LINT_CONFIG})
92+
string(REPLACE "@SEMICOLON@" ";" ${LINT_CONFIG} "${${LINT_CONFIG}}")
93+
set(${LINT_CONFIG} ${${LINT_CONFIG}} PARENT_SCOPE)
94+
endforeach()
95+
endfunction()
96+
97+
98+
macro(disable_build_lint)
99+
foreach(LINT_CONFIG IN LISTS BUILD_LINT_CONFIGS)
100+
unset(${LINT_CONFIG})
101+
endforeach()
102+
endmacro()
103+
104+
105+
function(print_build_lint_config)
106+
foreach(LINT_CONFIG IN LISTS BUILD_LINT_CONFIGS)
107+
message("lint config ${LINT_CONFIG}: ${${LINT_CONFIG}}")
108+
endforeach()
109+
endfunction()

contrib/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Disable linters during build of contrib libraries
2+
disable_build_lint()
3+
14
add_subdirectory(catch2)
25
add_subdirectory(libxml++)
36

0 commit comments

Comments
 (0)