diff --git a/ClangTidy.cmake b/ClangTidy.cmake index 154169e..858db49 100644 --- a/ClangTidy.cmake +++ b/ClangTidy.cmake @@ -13,31 +13,60 @@ # # A module to create custom targets to lint source files using clang-tidy # -# Call swift_create_clang_tidy_targets at the end of the top level CMakeLists.txt to create targets for running clang-tidy +# Call swift_create_clang_tidy_targets at the end of the top level +# CMakeLists.txt to create targets for running clang-tidy # -# 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 -# be logged +# 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 be logged # -# 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 +# 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 # -# For every compilable target defined within the calling repository two targets will be created. +# For every compilable target defined within the calling repository two targets +# will be created. # -# 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' -# in the top level project source directory. +# 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' in the top level project source directory. # -# 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 +# 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 # -# In addition there are two other targets created which lint multiple targets at the same time +# In addition there are two other targets created which lint multiple targets +# at the same time # -# 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) +# 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) # -# clang-tidy-world runs clang-tidy on all compilable targets in the repository including all test suites +# clang-tidy-world runs clang-tidy on all compilable targets in the repository +# including all test suites # -# clang-tidy-all and clang-tidy-world each have a "check" variant which returns an error code should any warning/errors be generated +# clang-tidy-all and clang-tidy-world each have a "check" variant which returns +# an error code should any warning/errors be generated # -# 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 -# 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 -# DONT_GENERATE_CLANG_TIDY_CONFIG to disable the autogenerated config +# 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 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 DONT_GENERATE_CLANG_TIDY_CONFIG to +# disable the autogenerated config +# +# In addition this function sets up a cmake option which can be used to control +# whether the targets are created either on the command line or by a super project. +# The option has the name +# +# ${PROJECT_NAME}_ENABLE_CLANG_TIDY +# +# The default value is ON for top level projects, and OFF for any others. +# +# Running +# +# cmake -D_ENABLE_CLANG_TIDY=OFF .. +# +# will explicitly disable these targets from the command line at configure time # # 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) WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) endfunction() +macro(early_exit level msg) + message(${level} "${msg}") + return() +endmacro() + function(swift_create_clang_tidy_targets) if(NOT ${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) return() @@ -68,6 +102,19 @@ function(swift_create_clang_tidy_targets) message(FATAL_ERROR "Unparsed arguments to swift_create_clang_tidy_targets ${ARGN}") endif() + # Global clang-tidy enable option, influences the default project specific enable option + option(ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy globally" ON) + if(NOT ENABLE_CLANG_TIDY) + early_exit(STATUS "clang-tidy is disabled globally") + endif() + + # Create a cmake option to enable linting of this specific project + option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable auto-linting of code using clang-tidy for project" ON) + + if(NOT ${PROJECT_NAME}_ENABLE_CLANG_TIDY) + early_exit(STATUS "${PROJECT_NAME} clang-tidy support is DISABLED") + endif() + # This is required so that clang-tidy can work out what compiler options to use for each file set(CMAKE_EXPORT_COMPILE_COMMANDS ON