Warning, /frameworks/extra-cmake-modules/kde-modules/KDEClangFormat.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2019 Christoph Cullmann <cullmann@kde.org>
0002 # SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0003 #
0004 # SPDX-License-Identifier: BSD-3-Clause
0005 
0006 #[=======================================================================[.rst:
0007 KDEClangFormat
0008 --------------------
0009 
0010 This module provides a functionality to format the source
0011 code of your repository according to a predefined KDE
0012 clang-format file.
0013 
0014 This module provides the following function:
0015 
0016 ::
0017 
0018   kde_clang_format(<files>)
0019 
0020 Using this function will create a clang-format target that will format all
0021 ``<files>`` passed to the function with the predefined KDE clang-format style.
0022 To format the files you have to invoke the target with ``make clang-format`` or ``ninja clang-format``.
0023 Once the project is formatted it is recommended to enforce the formatting using a pre-commit hook,
0024 this can be done using :kde-module:`KDEGitCommitHooks`.
0025 
0026 The ``.clang-format`` file from ECM will be copied to the source directory. This file should not be
0027 added to version control. It is recommended to add it to the ``.gitignore`` file: ``/.clang-format``.
0028 
0029 Since 5.79: If the source folder already contains a .clang-format file it is not overwritten.
0030 Since version 5.80 this function is called by default in :kde-module:`KDEFrameworkCompilerSettings`. If directories should be excluded from
0031 the formatting a .clang-format file with ``DisableFormat: true`` and ``SortIncludes: false`` should be created.
0032 
0033 Example usage:
0034 
0035 .. code-block:: cmake
0036 
0037   include(KDEClangFormat)
0038   file(GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES *.cpp *.h *.hpp *.c)
0039   kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
0040 
0041 To exclude directories from the formatting add a ``.clang-format``
0042 file in the directory with the following contents:
0043 
0044 .. code-block:: yaml
0045 
0046    DisableFormat: true
0047    SortIncludes: false
0048 
0049 Since 5.64
0050 #]=======================================================================]
0051 
0052 # try to find clang-format in path
0053 find_program(KDE_CLANG_FORMAT_EXECUTABLE clang-format)
0054 
0055 # instantiate our clang-format file, must be in source directory for tooling if we have the tool
0056 if(KDE_CLANG_FORMAT_EXECUTABLE)
0057     set(CLANG_FORMAT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/.clang-format)
0058     if (EXISTS ${CLANG_FORMAT_FILE})
0059         file(READ ${CLANG_FORMAT_FILE} CLANG_FORMAT_CONTENTS LIMIT 1000)
0060         string(FIND "${CLANG_FORMAT_CONTENTS}" "This file got automatically created by ECM, do not edit" matchres)
0061         if(${matchres} EQUAL -1)
0062             message(WARNING "The .clang-format file already exists. Please remove it in order to use the file provided by ECM")
0063         else()
0064             configure_file(${CMAKE_CURRENT_LIST_DIR}/clang-format.in ${CLANG_FORMAT_FILE} @ONLY)
0065         endif()
0066     else()
0067         configure_file(${CMAKE_CURRENT_LIST_DIR}/clang-format.in ${CLANG_FORMAT_FILE} @ONLY)
0068     endif()
0069 endif()
0070 
0071 # formatting target
0072 function(KDE_CLANG_FORMAT)
0073     if (TARGET clang-format)
0074         message(WARNING "the kde_clang_format function was already called")
0075         return()
0076     endif()
0077 
0078     # add target without specific commands first, we add the real calls file-per-file to avoid command line length issues
0079     add_custom_target(clang-format COMMENT "Formatting sources in ${CMAKE_CURRENT_SOURCE_DIR} with ${KDE_CLANG_FORMAT_EXECUTABLE}...")
0080 
0081     # run clang-format only if available, else signal the user what is missing
0082     if(KDE_CLANG_FORMAT_EXECUTABLE)
0083         get_filename_component(_binary_dir ${CMAKE_BINARY_DIR} REALPATH)
0084         foreach(_file ${ARGV})
0085             # check if the file is inside the build directory => ignore such files
0086             get_filename_component(_full_file_path ${_file} REALPATH)
0087             string(FIND ${_full_file_path} ${_binary_dir} _index)
0088             if(NOT _index EQUAL 0)
0089                 add_custom_command(TARGET clang-format
0090                     COMMAND
0091                         ${KDE_CLANG_FORMAT_EXECUTABLE}
0092                         -style=file
0093                         -i
0094                         ${_full_file_path}
0095                     WORKING_DIRECTORY
0096                         ${CMAKE_CURRENT_SOURCE_DIR}
0097                     COMMENT
0098                         "Formatting ${_full_file_path}..."
0099                     )
0100             endif()
0101         endforeach()
0102     else()
0103         add_custom_command(TARGET clang-format
0104             COMMAND
0105                 ${CMAKE_COMMAND} -E echo "Could not set up the clang-format target as the clang-format executable is missing."
0106             )
0107     endif()
0108 endfunction()