Adding support for clang-format (#94)

This commit is contained in:
Erik Fröjdh 2020-05-04 16:16:23 +02:00 committed by GitHub
parent efcb6cf480
commit 4e4f06560c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 2 deletions

View File

@ -46,6 +46,20 @@ option(SLS_BUILD_DOCS "docs" OFF)
option(SLS_BUILD_EXAMPLES "examples" OFF)
option(SLS_TUNE_LOCAL "tune to local machine" OFF)
# set(ClangFormat_BIN_NAME clang-format)
set(ClangFormat_EXCLUDE_PATTERNS "build/"
"libs/"
"slsDetectorCalibration/"
"slsDetectorServers/"
"ctbGui/"
"slsSupportLib/"
"slsReceiverSoftware/"
"manual/"
"slsDetectorGui/"
"python/"
"sample/"
${CMAKE_BINARY_DIR})
find_package(ClangFormat)
#Enable LTO if available
check_ipo_supported(RESULT SLS_LTO_AVAILABLE)

View File

@ -0,0 +1,34 @@
# Find Clang format
#
#
if(NOT ClangFormat_BIN_NAME)
set(ClangFormat_BIN_NAME clang-format)
endif()
# if custom path check there first
if(ClangFormat_ROOT_DIR)
find_program(ClangFormat_BIN
NAMES
${ClangFormat_BIN_NAME}
PATHS
"${ClangFormat_ROOT_DIR}"
NO_DEFAULT_PATH)
endif()
find_program(ClangFormat_BIN NAMES ${ClangFormat_BIN_NAME})
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
ClangFormat
DEFAULT_MSG
ClangFormat_BIN)
mark_as_advanced(
ClangFormat_BIN)
if(ClangFormat_FOUND)
# A CMake script to find all source files and setup clang-format targets for them
include(clang-format)
else()
message("clang-format not found. Not setting up format targets")
endif()

78
cmake/clang-format.cmake Normal file
View File

@ -0,0 +1,78 @@
# A CMake script to find all source files and setup clang-format targets for them
# Find all source files
set(ClangFormat_CXX_FILE_EXTENSIONS ${ClangFormat_CXX_FILE_EXTENSIONS} *.cpp *.h *.cxx *.hxx *.hpp *.cc *.ipp)
file(GLOB_RECURSE ALL_SOURCE_FILES ${ClangFormat_CXX_FILE_EXTENSIONS})
# Don't include some common build folders
set(ClangFormat_EXCLUDE_PATTERNS ${ClangFormat_EXCLUDE_PATTERNS} "/CMakeFiles/" "cmake")
# get all project files file
foreach (SOURCE_FILE ${ALL_SOURCE_FILES})
foreach (EXCLUDE_PATTERN ${ClangFormat_EXCLUDE_PATTERNS})
string(FIND ${SOURCE_FILE} ${EXCLUDE_PATTERN} EXCLUDE_FOUND)
if (NOT ${EXCLUDE_FOUND} EQUAL -1)
list(REMOVE_ITEM ALL_SOURCE_FILES ${SOURCE_FILE})
endif ()
endforeach ()
endforeach ()
add_custom_target(format
COMMENT "Running clang-format to change files"
COMMAND ${ClangFormat_BIN}
-style=file
-i
${ALL_SOURCE_FILES}
)
#put back i
add_custom_target(format-check
COMMENT "Checking clang-format changes"
# Use ! to negate the result for correct output
COMMAND !
${ClangFormat_BIN}
-style=file
-output-replacements-xml
${ALL_SOURCE_FILES}
| grep -q "replacement offset"
)
add_custom_target(
sf
COMMAND
echo ${ALL_SOURCE_FILES}
)
add_custom_target( list
COMMAND
foreach (SOURCE_FILE ${ALL_SOURCE_FILES})
message(${SOURCE_FILE})
endforeach ()
)
# # Get the path to this file
# get_filename_component(_clangcheckpath ${CMAKE_CURRENT_LIST_FILE} PATH)
# # have at least one here by default
# set(CHANGED_FILE_EXTENSIONS ".cpp")
# foreach(EXTENSION ${ClangFormat_CXX_FILE_EXTENSIONS})
# set(CHANGED_FILE_EXTENSIONS "${CHANGED_FILE_EXTENSIONS},${EXTENSION}" )
# endforeach()
# set(EXCLUDE_PATTERN_ARGS)
# foreach(EXCLUDE_PATTERN ${ClangFormat_EXCLUDE_PATTERNS})
# list(APPEND EXCLUDE_PATTERN_ARGS "--exclude=${EXCLUDE_PATTERN}")
# endforeach()
# # call the script to check changed files in git
# add_custom_target(format-check-changed
# COMMENT "Checking changed files in git"
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
# COMMAND ${_clangcheckpath}/../scripts/clang-format-check-changed.py
# --file-extensions \"${CHANGED_FILE_EXTENSIONS}\"
# ${EXCLUDE_PATTERN_ARGS}
# --clang-format-bin ${ClangFormat_BIN}
# )

View File

@ -3,9 +3,9 @@
// #include "catch.hpp"
#define CATCH_CONFIG_RUNNER
#include "ToString.h"
#include "catch.hpp"
#include "sls_detector_defs.h"
#include "ToString.h"
#include "tests/config.h"
#include <string>
@ -43,7 +43,8 @@ int main(int argc, char *argv[]) {
test::type = slsDetectorDefs::GENERIC;
if (!test::detector_type.empty()) {
test::type = sls::StringTo<slsDetectorDefs::detectorType>(test::detector_type);
test::type =
sls::StringTo<slsDetectorDefs::detectorType>(test::detector_type);
}
return session.run();