Skip to content

Commit 31604e7

Browse files
Jason Mobarakjungleraptor
andauthored
clang-tidy: allow target to be disabled (#109)
* write clang-tidy to build dir * port clang-tidy enable/disable from legacy module * Revert "write clang-tidy to build dir" This reverts commit 8dfb955. * update docs * Update ClangTidy.cmake Co-authored-by: Isaac Torres <[email protected]> * define early_exit macro Co-authored-by: Isaac Torres <[email protected]>
1 parent f22a0f0 commit 31604e7

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

ClangTidy.cmake

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,60 @@
1313
#
1414
# A module to create custom targets to lint source files using clang-tidy
1515
#
16-
# Call swift_create_clang_tidy_targets at the end of the top level CMakeLists.txt to create targets for running clang-tidy
16+
# Call swift_create_clang_tidy_targets at the end of the top level
17+
# CMakeLists.txt to create targets for running clang-tidy
1718
#
18-
# clang-tidy version 6 must be available on the system for this module to work properly. If an appropriate clang-tidy can't be found no targets will be created and a warning will
19-
# be logged
19+
# clang-tidy version 6 must be available on the system for this module to work
20+
# properly. If an appropriate clang-tidy can't be found no targets will be
21+
# created and a warning will be logged
2022
#
21-
# swift_create_clang_tidy_targets will only have an effect for top level projects. If called within a subproject it will return without taking any action
23+
# swift_create_clang_tidy_targets will only have an effect for top level
24+
# projects. If called within a subproject it will return without taking any
25+
# action
2226
#
23-
# For every compilable target defined within the calling repository two targets will be created.
27+
# For every compilable target defined within the calling repository two targets
28+
# will be created.
2429
#
25-
# The first target 'clang-tidy-${target}' will invoke clang-tidy for all source files which make up the given target. It will export fixes to a file called 'fixes-${target}.yaml'
26-
# in the top level project source directory.
30+
# The first target 'clang-tidy-${target}' will invoke clang-tidy for all source
31+
# files which make up the given target. It will export fixes to a file called
32+
# 'fixes-${target}.yaml' in the top level project source directory.
2733
#
28-
# The second target 'clang-tidy-${target}-check' will run clang-tidy as the target described above and then return an error code if any warning/errors were generated
34+
# The second target 'clang-tidy-${target}-check' will run clang-tidy as the
35+
# target described above and then return an error code if any warning/errors
36+
# were generated
2937
#
30-
# In addition there are two other targets created which lint multiple targets at the same time
38+
# In addition there are two other targets created which lint multiple targets
39+
# at the same time
3140
#
32-
# clang-tidy-all runs clang-tidy on all "core" targets in the repository (targets which were added with swift_add_executable or swift_add_library)
41+
# clang-tidy-all runs clang-tidy on all "core" targets in the repository
42+
# (targets which were added with swift_add_executable or swift_add_library)
3343
#
34-
# clang-tidy-world runs clang-tidy on all compilable targets in the repository including all test suites
44+
# clang-tidy-world runs clang-tidy on all compilable targets in the repository
45+
# including all test suites
3546
#
36-
# clang-tidy-all and clang-tidy-world each have a "check" variant which returns an error code should any warning/errors be generated
47+
# clang-tidy-all and clang-tidy-world each have a "check" variant which returns
48+
# an error code should any warning/errors be generated
3749
#
38-
# swift_create_clang_tidy_targets will generate a .clang-tidy file in the project source directory which contains the Swift master config for clang-tidy. There is no need for
39-
# repositories to maintain their own version of .clang-tidy, it should be added to .gitignore in each repository to prevent being checked in. Pass the option
40-
# DONT_GENERATE_CLANG_TIDY_CONFIG to disable the autogenerated config
50+
# swift_create_clang_tidy_targets will generate a .clang-tidy file in the
51+
# project source directory which contains the Swift master config for
52+
# clang-tidy. There is no need for repositories to maintain their own version
53+
# of .clang-tidy, it should be added to .gitignore in each repository to
54+
# prevent being checked in. Pass the option DONT_GENERATE_CLANG_TIDY_CONFIG to
55+
# disable the autogenerated config
56+
#
57+
# In addition this function sets up a cmake option which can be used to control
58+
# whether the targets are created either on the command line or by a super project.
59+
# The option has the name
60+
#
61+
# ${PROJECT_NAME}_ENABLE_CLANG_TIDY
62+
#
63+
# The default value is ON for top level projects, and OFF for any others.
64+
#
65+
# Running
66+
#
67+
# cmake -D<project>_ENABLE_CLANG_TIDY=OFF ..
68+
#
69+
# will explicitly disable these targets from the command line at configure time
4170
#
4271

4372
# Helper function to actually create the targets, not to be used outside this file
@@ -54,6 +83,11 @@ function(create_clang_tidy_targets key fixes)
5483
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
5584
endfunction()
5685

86+
macro(early_exit level msg)
87+
message(${level} "${msg}")
88+
return()
89+
endmacro()
90+
5791
function(swift_create_clang_tidy_targets)
5892
if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
5993
return()
@@ -68,6 +102,19 @@ function(swift_create_clang_tidy_targets)
68102
message(FATAL_ERROR "Unparsed arguments to swift_create_clang_tidy_targets ${ARGN}")
69103
endif()
70104

105+
# Global clang-tidy enable option, influences the default project specific enable option
106+
option(ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy globally" ON)
107+
if(NOT ENABLE_CLANG_TIDY)
108+
early_exit(STATUS "clang-tidy is disabled globally")
109+
endif()
110+
111+
# Create a cmake option to enable linting of this specific project
112+
option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project" ON)
113+
114+
if(NOT ${PROJECT_NAME}_ENABLE_CLANG_TIDY)
115+
early_exit(STATUS "${PROJECT_NAME} clang-tidy support is DISABLED")
116+
endif()
117+
71118
# This is required so that clang-tidy can work out what compiler options to use for each file
72119
set(CMAKE_EXPORT_COMPILE_COMMANDS
73120
ON

0 commit comments

Comments
 (0)