Skip to content

Commit

Permalink
cmake: add targets to format the code
Browse files Browse the repository at this point in the history
For each module/test a target named clang-format-<name> is added.
There is also a global target, called clang-format, that depends
on all other clang-format targets.
  • Loading branch information
andrea-iob committed Sep 6, 2022
1 parent 875fefc commit afd5e61
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ include(GetGitRevisionDescription)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/clang-format/cmake")
include(ClangFormat)

#------------------------------------------------------------------------------------#
# Functions
#------------------------------------------------------------------------------------#
Expand Down Expand Up @@ -1133,6 +1136,12 @@ if(HAVE___BUILTIN_UNREACHABLE)
addPrivateDefinitions("HAVE___BUILTIN_UNREACHABLE")
endif()

#------------------------------------------------------------------------------------#
# Source code formatting
#------------------------------------------------------------------------------------#
SET(CLANG_FORMAT_TARGET "clang-format")
ClangFormatAddTarget(${CLANG_FORMAT_TARGET})

#------------------------------------------------------------------------------------#
# Code coverage analysis
#------------------------------------------------------------------------------------#
Expand Down
110 changes: 110 additions & 0 deletions external/clang-format/cmake/ClangFormat.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#---------------------------------------------------------------------------
#
# bitpit
#
# Copyright (C) 2015-2022 OPTIMAD engineering Srl
#
# -------------------------------------------------------------------------
# License
# This file is part of bitpit.
#
# bitpit is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License v3 (LGPL)
# as published by the Free Software Foundation.
#
# bitpit is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with bitpit. If not, see <http://www.gnu.org/licenses/>.
#
#---------------------------------------------------------------------------*/

#[[

This file allows to format source files using clang-format.

This module provides the following function:

::

ClangFormatAddTarget(<target>)

This function will create an empty clang-format target. Once the target has been
created, it is possible to add sources files to be formatted by the target using
the function ClangFormatTargetSources. Files can be formatted invoke the target
with ``make <target>``.

ClangFormatAddTarget(<target> <files>)

This function will add the specified source files to clanf-format target
previously created.

Example usage:

.. code-block:: cmake

include(ClangFormat)
file(GLOB_RECURSE SOURCE_FILES *.cpp *.h *.hpp *.c)
ClangFormatAddTarget("clang-format")
kde_clang_format("clang-format" ${SOURCE_FILES})

The ``.clang-format`` file should be provided by the repository.

To exclude directories from the formatting add a ``.clang-format``
file in the directory with the following contents:

.. code-block:: yaml

DisableFormat: true
SortIncludes: false

]]

# Find clang-format executabel
find_program(CLANG_FORMAT_EXECUTABLE clang-format)

# Add clang-format target
function(ClangFormatAddTarget TARGET)
# Early return if clang-formt is not available
if (NOT CLANG_FORMAT_EXECUTABLE)
return()
endif()

# Create format target
add_custom_target(${TARGET} COMMENT "Formatting sources using ${CLANG_FORMAT_EXECUTABLE}...")
endfunction()

# Specifies sources to be formatted by the target
function(ClangFormatTargetSources TARGET FILE_PATHS)
# Early return if clang-formt is not available
if (NOT TARGET ${TARGET})
message(FATAL_ERROR "Target ${TARGET} is not valid.")
endif()

# Early return if clang-formt is not available
if (NOT CLANG_FORMAT_EXECUTABLE)
return()
endif()

# Add files to the target
get_filename_component(FULL_BINARY_DIR ${CMAKE_BINARY_DIR} REALPATH)
foreach(FILE_PATH ${FILE_PATHS})
# Skip files inside the buil directory
get_filename_component(FULL_FILE_PATH ${FILE_PATH} REALPATH)
string(FIND ${FULL_FILE_PATH} ${FULL_BINARY_DIR} BINARY_DIR_INDEX)
if(BINARY_DIR_INDEX EQUAL 0)
continue()
endif()

# Add file
add_custom_command(
TARGET ${TARGET}
COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -i ${FULL_FILE_PATH}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Formatting ${FULL_FILE_PATH}..."
)
endforeach()
endfunction()
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ function(configureModule MODULE_NAME)
if (NOT "${${UPPER_MODULE_NAME}_HEADERS}" STREQUAL "")
install(FILES ${${UPPER_MODULE_NAME}_HEADERS} DESTINATION include/${PROJECT_NAME})
endif ()

# Configure source formatting
set(MODULE_CLANG_FORMAT_TARGET "clang-format-${MODULE_NAME}")
ClangFormatAddTarget("${MODULE_CLANG_FORMAT_TARGET}")
ClangFormatTargetSources("${MODULE_CLANG_FORMAT_TARGET}" "${${UPPER_MODULE_NAME}_SOURCES}")
ClangFormatTargetSources("${MODULE_CLANG_FORMAT_TARGET}" "${${UPPER_MODULE_NAME}_HEADERS}")
add_dependencies(${CLANG_FORMAT_TARGET} "${MODULE_CLANG_FORMAT_TARGET}")

endfunction()

#------------------------------------------------------------------------------------#
Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ function(addTest TEST_NAME TEST_TYPE TEST_LIBRARIES WORKING_DIRECTORY N_PROCS PA
target_link_libraries(${TEST_TARGET_NAME} "gcov")
endif()

# Configure source formatting
set(TEST_CLANG_FORMAT_TARGET "clang-format-${TEST_TARGET_NAME}")
ClangFormatAddTarget("${TEST_CLANG_FORMAT_TARGET}")
ClangFormatTargetSources("${TEST_CLANG_FORMAT_TARGET}" "${TEST_NAME}.cpp")
add_dependencies(${CLANG_FORMAT_TARGET} "${TEST_CLANG_FORMAT_TARGET}")

# Add test
add_test(NAME "${TEST_TARGET_NAME}" COMMAND ${TEST_EXEC} ${TEST_ARGS} WORKING_DIRECTORY "${WORKING_DIRECTORY}")

Expand Down

0 comments on commit afd5e61

Please sign in to comment.