1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14# Currently we fix a certain version of clang-format to avoid unstable linting, 15# which may occur if different versions of the tools are used by developers. 16set(PANDA_CLANG_FORMAT "clang-format-9") 17 18# Require clang-format 19find_program( 20 CLANG_FORMAT 21 NAMES "${PANDA_CLANG_FORMAT}" 22 DOC "Path to clang-format executable" 23 ) 24if(NOT CLANG_FORMAT) 25 message(WARNING "Clang-format not found.") 26else() 27 message(STATUS "clang-format found: ${CLANG_FORMAT}") 28endif() 29 30# Function to add targets for clang_format, clang_force_format 31function(add_check_style dir) 32 file(GLOB_RECURSE dir_sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${dir}/*.cpp ${dir}/*.cc) 33 file(GLOB_RECURSE dir_headers RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${dir}/*.h ${dir}/*.inl) 34 35 if (CLANG_FORMAT) 36 if(TARGET clang_format) 37 else() 38 add_custom_target(clang_format) 39 endif() 40 if(TARGET clang_force_format) 41 else() 42 add_custom_target(clang_force_format) 43 endif() 44 endif() 45 46 if(NOT TARGET check_concurrency_format) 47 add_custom_target(check_concurrency_format) 48 endif() 49 50 if(NOT TARGET check_atomic_format) 51 add_custom_target(check_atomic_format) 52 endif() 53 54 foreach(src ${dir_sources}) 55 get_filename_component(source ${src} ABSOLUTE) 56 file(RELATIVE_PATH src ${PANDA_ROOT} ${source}) 57 STRING(REGEX REPLACE "/" "_" src ${src}) 58 if (CLANG_FORMAT) 59 add_clang_format(${source} ${src}) 60 add_clang_force_format(${source} ${src}) 61 endif() 62 add_check_concurrency_format(${source} ${src}) 63 add_check_atomic_format(${source} ${src}) 64 endforeach() 65 66 # Also add format-target for headers 67 foreach(src ${dir_headers}) 68 get_filename_component(source ${src} ABSOLUTE) 69 file(RELATIVE_PATH src ${PANDA_ROOT} ${source}) 70 STRING(REGEX REPLACE "/" "_" src ${src}) 71 if (CLANG_FORMAT) 72 add_clang_format(${source} ${src}) 73 add_clang_force_format(${source} ${src}) 74 add_dependencies(clang_format clang_format_${src}) 75 add_dependencies(clang_force_format clang_force_format_${src}) 76 endif() 77 add_check_concurrency_format(${source} ${src}) 78 add_dependencies(check_concurrency_format check_concurrency_format_${src}) 79 add_check_atomic_format(${source} ${src}) 80 add_dependencies(check_atomic_format check_atomic_format_${src}) 81 endforeach() 82endfunction() 83 84# Function to check through clang-format 85function(add_clang_format src tgt) 86 if (TARGET clang_format_${tgt}) 87 return() 88 endif() 89 add_custom_target(clang_format_${tgt} 90 COMMAND ${PANDA_ROOT}/scripts/run-clang-format ${PANDA_CLANG_FORMAT} ${src}) 91 add_dependencies(clang_format clang_format_${tgt}) 92endfunction() 93 94# Function to check correct usage of std primitives. 95function(add_check_concurrency_format src tgt) 96 set(CHECK_CONCURRENCY_FORMAT "${PANDA_ROOT}/scripts/run-check-concurrency-format.sh") 97 98 if (NOT TARGET check_concurrency_format_${tgt}) 99 add_custom_target(check_concurrency_format_${tgt} 100 COMMAND ${CHECK_CONCURRENCY_FORMAT} ${src} 101 ) 102 add_dependencies(check_concurrency_format check_concurrency_format_${tgt}) 103 endif() 104endfunction() 105 106# Function to check correct usage of memory order in atomics. 107function(add_check_atomic_format src tgt) 108 set(CHECK_CONCURRENCY_FORMAT "${PANDA_ROOT}/scripts/run_check_atomic_format.py") 109 110 if (NOT TARGET check_atomic_format_${tgt}) 111 add_custom_target(check_atomic_format_${tgt} 112 COMMAND ${CHECK_CONCURRENCY_FORMAT} ${src} 113 ) 114 add_dependencies(check_atomic_format check_atomic_format_${tgt}) 115 endif() 116endfunction() 117 118# Function to force style through clang-format 119function(add_clang_force_format src tgt) 120 if (NOT TARGET clang_force_format_${tgt}) 121 add_custom_target(clang_force_format_${tgt} 122 COMMAND ${CLANG_FORMAT} -i -style=file ${src} 123 ) 124 add_dependencies(clang_force_format clang_force_format_${tgt}) 125 endif() 126endfunction() 127 128