1# Copyright (c) 2021 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) 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 foreach(src ${dir_sources}) 51 get_filename_component(source ${src} ABSOLUTE) 52 file(RELATIVE_PATH src ${PANDA_ROOT} ${source}) 53 STRING(REGEX REPLACE "/" "_" src ${src}) 54 if (CLANG_FORMAT) 55 add_clang_format(${source} ${src}) 56 add_clang_force_format(${source} ${src}) 57 endif() 58 add_check_concurrency_format(${source} ${src}) 59 endforeach() 60 61 # Also add format-target for headers 62 foreach(src ${dir_headers}) 63 get_filename_component(source ${src} ABSOLUTE) 64 file(RELATIVE_PATH src ${PANDA_ROOT} ${source}) 65 STRING(REGEX REPLACE "/" "_" src ${src}) 66 if (CLANG_FORMAT) 67 add_clang_format(${source} ${src}) 68 add_clang_force_format(${source} ${src}) 69 add_dependencies(clang_format clang_format_${src}) 70 add_dependencies(clang_force_format clang_force_format_${src}) 71 endif() 72 add_check_concurrency_format(${source} ${src}) 73 add_dependencies(check_concurrency_format check_concurrency_format_${src}) 74 endforeach() 75endfunction() 76 77# Function to check through clang-format 78function(add_clang_format src tgt) 79 if (TARGET clang_format_${tgt}) 80 return() 81 endif() 82 add_custom_target(clang_format_${tgt} 83 COMMAND ${PANDA_ROOT}/scripts/run-clang-format ${PANDA_CLANG_FORMAT} ${src}) 84 add_dependencies(clang_format clang_format_${tgt}) 85endfunction() 86 87# Function to check correct usage of std primitives. 88function(add_check_concurrency_format src tgt) 89 set(CHECK_CONCURRENCY_FORMAT "${PANDA_ROOT}/scripts/run-check-concurrency-format.sh") 90 91 if (NOT TARGET check_concurrency_format_${tgt}) 92 add_custom_target(check_concurrency_format_${tgt} 93 COMMAND ${CHECK_CONCURRENCY_FORMAT} ${src} 94 ) 95 add_dependencies(check_concurrency_format check_concurrency_format_${tgt}) 96 endif() 97endfunction() 98 99# Function to force style through clang-format 100function(add_clang_force_format src tgt) 101 if (NOT TARGET clang_force_format_${tgt}) 102 add_custom_target(clang_force_format_${tgt} 103 COMMAND ${CLANG_FORMAT} -i -style=file ${src} 104 ) 105 add_dependencies(clang_force_format clang_force_format_${tgt}) 106 endif() 107endfunction() 108 109