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