mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
add ability to check clang-format
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# Find Clang format
|
||||
#
|
||||
#
|
||||
if(NOT CLANG_FORMAT_BIN_NAME)
|
||||
set(CLANG_FORMAT_BIN_NAME clang-format)
|
||||
endif()
|
||||
|
||||
# if custom path check there first
|
||||
if(CPPCHECK_ROOT_DIR)
|
||||
find_program(CLANG_FORMAT_BIN
|
||||
NAMES
|
||||
${CLANG_FORMAT_BIN_NAME}
|
||||
PATHS
|
||||
"${CLANG_FORMAT_ROOT_DIR}"
|
||||
NO_DEFAULT_PATH)
|
||||
endif()
|
||||
|
||||
find_program(CLANG_FORMAT_BIN NAMES ${CLANG_FORMAT_BIN_NAME})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
|
||||
CLANG_FORMAT
|
||||
DEFAULT_MSG
|
||||
CLANG_FORMAT_BIN)
|
||||
|
||||
mark_as_advanced(
|
||||
CLANG_FORMAT_BIN)
|
||||
|
||||
if(CLANG_FORMAT_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()
|
||||
@@ -0,0 +1,46 @@
|
||||
# A CMake script to find all source files and setup clang-format targets for them
|
||||
|
||||
# Find all source files
|
||||
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h *.cxx *.hxx *.hpp *.cc)
|
||||
|
||||
# Don't include some common build folders
|
||||
set(CLANG_FORMAT_EXCLUDE_PATTERNS ${CLANG_FORMAT_EXCLUDE_PATTERNS} "main2.cpp" "build/" "/CMakeFiles/")
|
||||
|
||||
# get all project files file
|
||||
foreach (SOURCE_FILE ${ALL_SOURCE_FILES})
|
||||
foreach (EXCLUDE_PATTERN ${CLANG_FORMAT_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 ${CLANG_FORMAT_BIN}
|
||||
-style=file
|
||||
-i
|
||||
${ALL_SOURCE_FILES}
|
||||
)
|
||||
|
||||
add_custom_target(format-check
|
||||
COMMENT "Checking clang-format changes"
|
||||
COMMAND ${CLANG_FORMAT_BIN}
|
||||
-style=file
|
||||
-output-replacements-xml
|
||||
${ALL_SOURCE_FILES}
|
||||
| grep "replacement offset" 2>&1 > /dev/null
|
||||
)
|
||||
|
||||
# This is a hack because our CMake root dir isn't the same as our git root dir
|
||||
|
||||
# Get the path to this file
|
||||
get_filename_component(_clangcheckpath ${CMAKE_CURRENT_LIST_FILE} PATH)
|
||||
# call the script to chech 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 ${CLANG_FORMAT_BIN}
|
||||
)
|
||||
|
||||
18
04-static-analysis/clang-format/cmake/scripts/clang-format-check-changed
Executable file
18
04-static-analysis/clang-format/cmake/scripts/clang-format-check-changed
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Required because cmake root isn't git root in this example
|
||||
CLANG_FORMAT_BIN=$1
|
||||
GIT_ROOT=`git rev-parse --show-toplevel`
|
||||
|
||||
pushd ${GIT_ROOT} > /dev/null
|
||||
|
||||
git status --porcelain \
|
||||
| egrep '*\.cpp|*\.h|*\.cxx|*\.hxx|*\.hpp|*\.cc' \
|
||||
| awk -F " " '{print $NF}' \
|
||||
| xargs -r ${CLANG_FORMAT_BIN} -style=file -output-replacements-xml \
|
||||
| grep "replacement offset" 2>&1 > /dev/null
|
||||
|
||||
RET=$?
|
||||
popd > /dev/null
|
||||
|
||||
exit ${RET}
|
||||
Reference in New Issue
Block a user