|
| 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() |
0 commit comments