|
| 1 | +# Copyright (C) 2025 Andrew D Smith |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify it |
| 4 | +# under the terms of the GNU General Public License as published by the Free |
| 5 | +# Software Foundation, either version 3 of the License, or (at your option) |
| 6 | +# any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | +# more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License along with |
| 14 | +# this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +# StaticAnalysis |
| 17 | +message(STATUS "Enabling static analysis") |
| 18 | +# If no specific static analysis is requested, do them all |
| 19 | +if(NOT RUN_CPPCHECK AND NOT RUN_IWYU AND |
| 20 | + NOT RUN_CPPLINT AND NOT RUN_CLANG_TIDY) |
| 21 | + set(RUN_CPPCHECK on) |
| 22 | + set(RUN_IWYU on) |
| 23 | + set(RUN_CPPLINT on) |
| 24 | + set(RUN_CLANG_TIDY on) |
| 25 | +endif() |
| 26 | + |
| 27 | +set(STATIC_ANALYSIS_CHECKS "") |
| 28 | +if(RUN_CPPCHECK) |
| 29 | + list(APPEND STATIC_ANALYSIS_CHECKS "cppcheck") |
| 30 | +endif() |
| 31 | +if(RUN_CPPLINT) |
| 32 | + list(APPEND STATIC_ANALYSIS_CHECKS "cpplint") |
| 33 | +endif() |
| 34 | +if(RUN_IWYU) |
| 35 | + list(APPEND STATIC_ANALYSIS_CHECKS "iwyu") |
| 36 | +endif() |
| 37 | +if(RUN_CLANG_TIDY) |
| 38 | + list(APPEND STATIC_ANALYSIS_CHECKS "clang-tidy") |
| 39 | +endif() |
| 40 | + |
| 41 | +message(STATUS "Requested static analysis: ${STATIC_ANALYSIS_CHECKS}") |
| 42 | + |
| 43 | +# cpplint: all options are in the config file |
| 44 | +if ("cpplint" IN_LIST STATIC_ANALYSIS_CHECKS) |
| 45 | + find_program(FOUND_CPPLINT cpplint) |
| 46 | + if(FOUND_CPPLINT) |
| 47 | + message(STATUS "Enabling cpplint analysis") |
| 48 | + set(CMAKE_CXX_CPPLINT cpplint --quiet) |
| 49 | + else() |
| 50 | + message(STATUS "Could not find cpplint; disabling cpplint") |
| 51 | + endif() |
| 52 | +endif() |
| 53 | + |
| 54 | +# include-what-you-use: config is a mappings file |
| 55 | +if ("iwyu" IN_LIST STATIC_ANALYSIS_CHECKS) |
| 56 | + find_program(FOUND_IWYU include-what-you-use) |
| 57 | + if(FOUND_IWYU) |
| 58 | + message(STATUS "Enabling include-what-you-use analysis") |
| 59 | + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE |
| 60 | + include-what-you-use |
| 61 | + -Xiwyu |
| 62 | + --comment_style=none |
| 63 | + -Xiwyu |
| 64 | + --quoted_includes_first |
| 65 | + -Xiwyu |
| 66 | + --mapping_file=${PROJECT_SOURCE_DIR}/iwyu.json |
| 67 | + ) |
| 68 | + else() |
| 69 | + message(STATUS "Could not find iwyu; disabling iwyu") |
| 70 | + endif() |
| 71 | +endif() |
| 72 | + |
| 73 | +# cppcheck: options on the command line as there is no config file |
| 74 | +if ("cppcheck" IN_LIST STATIC_ANALYSIS_CHECKS) |
| 75 | + find_program(FOUND_CPPCHECK cppcheck) |
| 76 | + if(FOUND_CPPCHECK) |
| 77 | + message(STATUS "Enabling cppcheck analysis") |
| 78 | + set(CMAKE_CXX_CPPCHECK |
| 79 | + cppcheck |
| 80 | + --quiet |
| 81 | + --enable=all |
| 82 | + --inline-suppr |
| 83 | + --max-configs=1 |
| 84 | + --suppressions-list=${PROJECT_SOURCE_DIR}/.cppcheck_suppress |
| 85 | + ) |
| 86 | + else() |
| 87 | + message(STATUS "Could not find cppcheck; disabling cppcheck") |
| 88 | + endif() |
| 89 | +endif() |
| 90 | + |
| 91 | +# clang-tidy: need to make sure version is at least 20 |
| 92 | +if ("clang-tidy" IN_LIST STATIC_ANALYSIS_CHECKS) |
| 93 | + find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy) |
| 94 | + # Minimum required version |
| 95 | + set(MIN_CLANG_TIDY_VERSION "20.0.0") |
| 96 | + if(CLANG_TIDY_EXECUTABLE) |
| 97 | + execute_process( |
| 98 | + COMMAND |
| 99 | + bash -c |
| 100 | + "${CLANG_TIDY_EXECUTABLE} --version | grep version | tr -cd '0-9.\n'" |
| 101 | + OUTPUT_VARIABLE CLANG_TIDY_VERSION |
| 102 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 103 | + ) |
| 104 | + # Compare the version numbers |
| 105 | + if(CLANG_TIDY_VERSION VERSION_GREATER_EQUAL MIN_CLANG_TIDY_VERSION) |
| 106 | + message(STATUS "Enabling clang-tidy (version: ${CLANG_TIDY_VERSION})") |
| 107 | + set(CMAKE_CXX_CLANG_TIDY |
| 108 | + clang-tidy |
| 109 | + --quiet |
| 110 | + --allow-no-checks |
| 111 | + -p ${PROJECT_BINARY_DIR} |
| 112 | + ) |
| 113 | + else() |
| 114 | + message(STATUS "Not enabling clang-tidy (min version not found") |
| 115 | + endif() |
| 116 | + else() |
| 117 | + message(STATUS "Could not find clang-tidy; disabling clang-tidy") |
| 118 | + endif() |
| 119 | +endif() |
0 commit comments