File: FindClangFormat.cmake

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (134 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
# Copyright 2009- ECMWF.
#
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#

#
# ClangFormat.cmake allows to easily setup source code formatting
#
# Use this module by including it with:
#
#   find_package(ClangFormat)
#
# And then configure each project target with:
#
#   target_clangformat(<TARGET> [CONDITION <CONDITION>])
#
# This will automatically create the following targets:
#
#   clangformat               - executes of all format targets
#   clangformat_<target-name> - formats the sources of a specific executable/library target
#                               (n.b. only files declared as target sources are formatted;
#                                     #include files are not considered unless explicitly listed as target sources)
#
# If CONDITION is supplied and evaluates to FALSE, these targets will not be created
#

function(target_clangformat TARGET)

  set(options "")
  set(single_value_args "")
  set(multi_value_args CONDITION)
  cmake_parse_arguments( ARGS "${options}" "${single_value_args}" "${multi_value_args}" ${ARGN} )

  # Skip setup if condition supplied and FALSE
  if (DEFINED ARGS_CONDITION)
    if (${${ARGS_CONDITION}})
      # Do nothing, just continue
    else()
      return()
    endif()
  endif()

  # Skip setup if clang-format is not available...
  if (NOT CLANGFORMAT_EXE)
    return()
  endif ()

  # Collect list of target sources
  get_target_property(target_sources ${TARGET} SOURCES)
  if (NOT target_sources MATCHES "-NOTFOUND")
    foreach (clangformat_source ${target_sources})
      get_filename_component(clangformat_source ${clangformat_source} ABSOLUTE)
      list(APPEND clangformat_sources ${clangformat_source})
    endforeach ()
  endif ()

  if(clangformat_sources)
    message(STATUS "clangformat target '${TARGET}': OK")
  else()
    message(STATUS "clangformat target '${TARGET}': Skipping, no sources found")
    return()
  endif()

  # Remove auto-generated sources (e.g. Qt files)
  list(FILTER clangformat_sources EXCLUDE REGEX ".*/moc_.*")
  list(FILTER clangformat_sources EXCLUDE REGEX ".*/ui_.*")
  list(FILTER clangformat_sources EXCLUDE REGEX ".*/qrc_.*")

  # Define name of specific format target
  get_target_property(target_name ${TARGET} NAME)
  set(format_target clangformat_${target_name})

  # Add custom specific format target
  add_custom_target(${format_target}
    COMMAND
    ${CLANGFORMAT_EXE}
    -style=file
    -i
    ${clangformat_sources}
    COMMENT
    "Formatting '${TARGET}' with ${CLANGFORMAT_EXE} ..."
    )

  # Wire up dependencies to main format target
  if (TARGET clangformat)
    add_dependencies(clangformat ${format_target})
  else ()
    add_custom_target(clangformat DEPENDS ${format_target})
  endif ()
endfunction()

function(clangformat_get_version VAR)
  execute_process(COMMAND ${CLANGFORMAT_EXE} -version OUTPUT_VARIABLE VERSION_OUTPUT)
  separate_arguments(VERSION_OUTPUT_LIST UNIX_COMMAND "${VERSION_OUTPUT}")
  list(FIND VERSION_OUTPUT_LIST "version" VERSION_INDEX)
  if (VERSION_INDEX GREATER 0)
    math(EXPR VERSION_INDEX "${VERSION_INDEX} + 1")
    list(GET VERSION_OUTPUT_LIST ${VERSION_INDEX} VERSION)
    set(${VAR} ${VERSION} PARENT_SCOPE)
  else ()
    set(${VAR} "<(n/a)>" PARENT_SCOPE)
  endif ()
endfunction()


# __MAIN__ (the following is evaluated immediately when the module is included)

find_program(CLANGFORMAT_EXE
  NAMES
  clang-format
  clang-format-6
  clang-format-7
  clang-format-8
  clang-format-9
  clang-format-11
  clang-format-12
  clang-format-13
  clang-format-14
  clang-format-15
  clang-format-16
  PATHS
  /usr/bin
  /usr/local/opt/llvm/bin
  )

if (CLANGFORMAT_EXE)
  clangformat_get_version(CLANGFORMAT_VERSION)
  set(CLANGFORMAT_FOUND ON)
endif ()