|
| 1 | +################################################################################ |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2017 Advanced Micro Devices, Inc. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +################################################################################ |
| 26 | +include(CMakeParseArguments) |
| 27 | +include(Analyzers) |
| 28 | + |
| 29 | +get_filename_component(CLANG_TIDY_EXE_HINT "${CMAKE_CXX_COMPILER}" PATH) |
| 30 | + |
| 31 | +find_program(CLANG_TIDY_EXE |
| 32 | + NAMES |
| 33 | + clang-tidy |
| 34 | + clang-tidy-5.0 |
| 35 | + clang-tidy-4.0 |
| 36 | + clang-tidy-3.9 |
| 37 | + clang-tidy-3.8 |
| 38 | + clang-tidy-3.7 |
| 39 | + clang-tidy-3.6 |
| 40 | + clang-tidy-3.5 |
| 41 | + HINTS |
| 42 | + ${CLANG_TIDY_EXE_HINT} |
| 43 | + PATH_SUFFIXES |
| 44 | + compiler/bin |
| 45 | + PATHS |
| 46 | + /opt/rocm/llvm/bin |
| 47 | + /opt/rocm/hcc |
| 48 | + /usr/local/opt/llvm/bin |
| 49 | +) |
| 50 | + |
| 51 | +function(find_clang_tidy_version VAR) |
| 52 | + execute_process(COMMAND ${CLANG_TIDY_EXE} -version OUTPUT_VARIABLE VERSION_OUTPUT) |
| 53 | + separate_arguments(VERSION_OUTPUT_LIST UNIX_COMMAND "${VERSION_OUTPUT}") |
| 54 | + list(FIND VERSION_OUTPUT_LIST "version" VERSION_INDEX) |
| 55 | + if(VERSION_INDEX GREATER 0) |
| 56 | + math(EXPR VERSION_INDEX "${VERSION_INDEX} + 1") |
| 57 | + list(GET VERSION_OUTPUT_LIST ${VERSION_INDEX} VERSION) |
| 58 | + set(${VAR} ${VERSION} PARENT_SCOPE) |
| 59 | + else() |
| 60 | + set(${VAR} "0.0" PARENT_SCOPE) |
| 61 | + endif() |
| 62 | + |
| 63 | +endfunction() |
| 64 | + |
| 65 | +if( NOT CLANG_TIDY_EXE ) |
| 66 | + message( STATUS "Clang tidy not found" ) |
| 67 | + set(CLANG_TIDY_VERSION "0.0") |
| 68 | +else() |
| 69 | + find_clang_tidy_version(CLANG_TIDY_VERSION) |
| 70 | + message( STATUS "Clang tidy found: ${CLANG_TIDY_VERSION}") |
| 71 | +endif() |
| 72 | + |
| 73 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 74 | + |
| 75 | +set(CLANG_TIDY_FIXIT_DIR ${CMAKE_BINARY_DIR}/fixits) |
| 76 | +file(MAKE_DIRECTORY ${CLANG_TIDY_FIXIT_DIR}) |
| 77 | +set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${CLANG_TIDY_FIXIT_DIR}) |
| 78 | + |
| 79 | +macro(enable_clang_tidy) |
| 80 | + set(options ANALYZE_TEMPORARY_DTORS ALL) |
| 81 | + set(oneValueArgs HEADER_FILTER) |
| 82 | + set(multiValueArgs CHECKS ERRORS EXTRA_ARGS) |
| 83 | + |
| 84 | + cmake_parse_arguments(PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 85 | + string(REPLACE ";" "," CLANG_TIDY_CHECKS "${PARSE_CHECKS}") |
| 86 | + string(REPLACE ";" "," CLANG_TIDY_ERRORS "${PARSE_ERRORS}") |
| 87 | + set(CLANG_TIDY_EXTRA_ARGS) |
| 88 | + foreach(ARG ${PARSE_EXTRA_ARGS}) |
| 89 | + list(APPEND CLANG_TIDY_EXTRA_ARGS "-extra-arg=${ARG}") |
| 90 | + endforeach() |
| 91 | + |
| 92 | + set(CLANG_TIDY_ALL) |
| 93 | + if(PARSE_ALL) |
| 94 | + set(CLANG_TIDY_ALL ALL) |
| 95 | + endif() |
| 96 | + |
| 97 | + message(STATUS "Clang tidy checks: ${CLANG_TIDY_CHECKS}") |
| 98 | + |
| 99 | + if (${PARSE_ANALYZE_TEMPORARY_DTORS}) |
| 100 | + set(CLANG_TIDY_ANALYZE_TEMPORARY_DTORS "-analyze-temporary-dtors") |
| 101 | + endif() |
| 102 | + |
| 103 | + if (${CLANG_TIDY_VERSION} VERSION_LESS "3.9.0") |
| 104 | + set(CLANG_TIDY_ERRORS_ARG "") |
| 105 | + else() |
| 106 | + set(CLANG_TIDY_ERRORS_ARG "-warnings-as-errors='${CLANG_TIDY_ERRORS}'") |
| 107 | + endif() |
| 108 | + |
| 109 | + if (${CLANG_TIDY_VERSION} VERSION_LESS "3.9.0") |
| 110 | + set(CLANG_TIDY_QUIET_ARG "") |
| 111 | + else() |
| 112 | + set(CLANG_TIDY_QUIET_ARG "-quiet") |
| 113 | + endif() |
| 114 | + |
| 115 | + if(PARSE_HEADER_FILTER) |
| 116 | + string(REPLACE "$" "$$" CLANG_TIDY_HEADER_FILTER "${PARSE_HEADER_FILTER}") |
| 117 | + else() |
| 118 | + set(CLANG_TIDY_HEADER_FILTER ".*") |
| 119 | + endif() |
| 120 | + |
| 121 | + set(CLANG_TIDY_COMMAND |
| 122 | + ${CLANG_TIDY_EXE} |
| 123 | + ${CLANG_TIDY_QUIET_ARG} |
| 124 | + -p ${CMAKE_BINARY_DIR} |
| 125 | + -checks='${CLANG_TIDY_CHECKS}' |
| 126 | + ${CLANG_TIDY_ERRORS_ARG} |
| 127 | + ${CLANG_TIDY_EXTRA_ARGS} |
| 128 | + ${CLANG_TIDY_ANALYZE_TEMPORARY_DTORS} |
| 129 | + -header-filter='${CLANG_TIDY_HEADER_FILTER}' |
| 130 | + ) |
| 131 | + add_custom_target(tidy ${CLANG_TIDY_ALL}) |
| 132 | + mark_as_analyzer(tidy) |
| 133 | + add_custom_target(tidy-base) |
| 134 | + add_custom_target(tidy-make-fixit-dir COMMAND ${CMAKE_COMMAND} -E make_directory ${CLANG_TIDY_FIXIT_DIR}) |
| 135 | + add_custom_target(tidy-rm-fixit-dir COMMAND ${CMAKE_COMMAND} -E remove_directory ${CLANG_TIDY_FIXIT_DIR}) |
| 136 | + add_dependencies(tidy-make-fixit-dir tidy-rm-fixit-dir) |
| 137 | + add_dependencies(tidy-base tidy-make-fixit-dir) |
| 138 | +endmacro() |
| 139 | + |
| 140 | +function(clang_tidy_check TARGET) |
| 141 | + get_target_property(SOURCES ${TARGET} SOURCES) |
| 142 | + # TODO: Use generator expressions instead |
| 143 | + # COMMAND ${CLANG_TIDY_COMMAND} $<TARGET_PROPERTY:${TARGET},SOURCES> |
| 144 | + # COMMAND ${CLANG_TIDY_COMMAND} $<JOIN:$<TARGET_PROPERTY:${TARGET},SOURCES>, > |
| 145 | + foreach(SOURCE ${SOURCES}) |
| 146 | + if((NOT "${SOURCE}" MATCHES "(h|hpp|hxx)$") AND (NOT "${SOURCE}" MATCHES "TARGET_OBJECTS")) |
| 147 | + string(MAKE_C_IDENTIFIER "${SOURCE}" tidy_file) |
| 148 | + set(tidy_target tidy-target-${TARGET}-${tidy_file}) |
| 149 | + add_custom_target(${tidy_target} |
| 150 | + # for some targets clang-tidy not able to get information from .clang-tidy |
| 151 | + DEPENDS ${SOURCE} |
| 152 | + COMMAND ${CLANG_TIDY_COMMAND} "-config=\{CheckOptions: \[\{key: bugprone-reserved-identifier.AllowedIdentifiers,value: __HIP_PLATFORM_HCC__\; __HIP_ROCclr__\}\]\}" ${SOURCE} "-export-fixes=${CLANG_TIDY_FIXIT_DIR}/${TARGET}-${tidy_file}.yaml" |
| 153 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 154 | + COMMENT "clang-tidy: Running clang-tidy on target ${SOURCE}..." |
| 155 | + ) |
| 156 | + add_dependencies(${tidy_target} ${TARGET}) |
| 157 | + add_dependencies(${tidy_target} tidy-base) |
| 158 | + add_dependencies(tidy ${tidy_target}) |
| 159 | + endif() |
| 160 | + endforeach() |
| 161 | +endfunction() |
| 162 | + |
0 commit comments