diff --git a/CMakeLists.txt b/CMakeLists.txt index 682c046bf..d30e030dc 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/FindClangFormat.cmake b/cmake/FindClangFormat.cmake new file mode 100644 index 000000000..c3a262533 --- /dev/null +++ b/cmake/FindClangFormat.cmake @@ -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() diff --git a/cmake/clang-format.cmake b/cmake/clang-format.cmake new file mode 100644 index 000000000..1156f86c7 --- /dev/null +++ b/cmake/clang-format.cmake @@ -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} +# ) + diff --git a/tests/test.cpp b/tests/test.cpp index 510f9774d..161e2f013 100755 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -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 @@ -43,7 +43,8 @@ int main(int argc, char *argv[]) { test::type = slsDetectorDefs::GENERIC; if (!test::detector_type.empty()) { - test::type = sls::StringTo(test::detector_type); + test::type = + sls::StringTo(test::detector_type); } return session.run();