Imported Upstream version 3.1.9
[platform/upstream/Imath.git] / cmake / clang-format.cmake
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright Contributors to the OpenEXR Project.
3 #
4 # This module was partially adapted from OpenImageIO, also under the
5 # same BSD-3-Clause license.
6
7 ###########################################################################
8 # clang-format options
9 #
10 # clang-format is a source code reformatter that is part of the LLVM tools.
11 # It can be used to check adherence to project code formatting rules and
12 # correct any deviations. If clang-format is found on the system, a
13 # "clang-format" build target will trigger a reformatting.
14 #
15 set (CLANG_FORMAT_ROOT "" CACHE PATH "clang-format executable's directory (will search if not specified")
16 set (CLANG_FORMAT_INCLUDES "*.h" "*.cpp"
17      CACHE STRING "Glob patterns to include for clang-format")
18 set (CLANG_FORMAT_EXCLUDES ""
19      CACHE STRING "Glob patterns to exclude for clang-format")
20
21 # Look for clang-format. If not in the ordinary execution path, you can
22 # hint at it with either CLANG_FORMAT_ROOT or LLVM_ROOT (as a CMake variable
23 # or as an environment variable).
24 find_program (CLANG_FORMAT_EXE
25               NAMES clang-format bin/clang-format
26               HINTS ${CLANG_FORMAT_ROOT}
27                     ENV CLANG_FORMAT_ROOT
28                     LLVM_ROOT
29                     ENV LLVM_ROOT
30               DOC "Path to clang-format executable")
31
32 # If clang-format was found, set up a custom `clang-format` target that will
33 # reformat all source code with filenames patterns matching
34 # CLANG_FORMAT_INCLUDES and excluding CLANG_FORMAT_EXCLUDES.
35 if (CLANG_FORMAT_EXE)
36     message (STATUS "clang-format found: ${CLANG_FORMAT_EXE}")
37     # Start with the list of files to include when formatting...
38     file (GLOB_RECURSE FILES_TO_FORMAT ${CLANG_FORMAT_INCLUDES})
39     # ... then process any list of excludes we are given
40     foreach (_pat ${CLANG_FORMAT_EXCLUDES})
41         file (GLOB_RECURSE _excl ${_pat})
42         list (REMOVE_ITEM FILES_TO_FORMAT ${_excl})
43     endforeach ()
44     #message (STATUS "clang-format file list: ${FILES_TO_FORMAT}")
45     file (COPY ${CMAKE_CURRENT_SOURCE_DIR}/.clang-format
46           DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
47     add_custom_target (clang-format
48         COMMAND "${CLANG_FORMAT_EXE}" -i -style=file ${FILES_TO_FORMAT} )
49 else ()
50     message (STATUS "clang-format not found.")
51 endif ()