From 4161793d1907ce7402b42f9c96e6f9745b0e2a3a Mon Sep 17 00:00:00 2001 From: sigiesec Date: Thu, 1 Feb 2018 14:18:02 +0100 Subject: [PATCH] Problem: clang-format not run on CI Solution: add clang-format to cmake, and add another travis-ci build type --- .travis.yml | 2 ++ CMakeLists.txt | 2 ++ builds/cmake/Modules/ClangFormat.cmake | 36 ++++++++++++++++++++++++++ builds/cmake/ci_build.sh | 9 ++++++- builds/cmake/clang-format-check.sh.in | 14 ++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 builds/cmake/Modules/ClangFormat.cmake create mode 100644 builds/cmake/clang-format-check.sh.in diff --git a/.travis.yml b/.travis.yml index 3d2eda29..9dc9ab88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -84,6 +84,8 @@ matrix: - env: BUILD_TYPE=android CURVE=tweetnacl os: linux dist: trusty + - env: BUILD_TYPE=cmake DO_CLANG_FORMAT_CHECK=1 + os: linux sudo: required diff --git a/CMakeLists.txt b/CMakeLists.txt index bbc451e8..15f99dfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1138,3 +1138,5 @@ if (MSVC_VERSION EQUAL 1600) file (APPEND "${ZMQ_SLN_FILENAME}" "\n# This should be regenerated!\n") endif () endif () + +include(ClangFormat) diff --git a/builds/cmake/Modules/ClangFormat.cmake b/builds/cmake/Modules/ClangFormat.cmake new file mode 100644 index 00000000..983c53e2 --- /dev/null +++ b/builds/cmake/Modules/ClangFormat.cmake @@ -0,0 +1,36 @@ +# additional target to perform clang-format run, requires clang-format + +# get all project files +file(GLOB_RECURSE ALL_SOURCE_FILES RELATIVE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/*.cpp ${CMAKE_SOURCE_DIR}/*.h ${CMAKE_SOURCE_DIR}/*.hpp ) + +if("${CLANG_FORMAT}" STREQUAL "") + set(CLANG_FORMAT "clang-format") +endif() + +add_custom_target( + clang-format + COMMAND ${CLANG_FORMAT} -style=file -i ${ALL_SOURCE_FILES} +) + +function(JOIN VALUES GLUE OUTPUT) + string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}") + set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE) +endfunction() + +configure_file(builds/cmake/clang-format-check.sh.in clang-format-check.sh @ONLY) + +add_custom_target( + clang-format-check + COMMAND chmod +x clang-format-check.sh + COMMAND ./clang-format-check.sh + #COMMAND ${CLANG_FORMAT} -style=file -output-replacements-xml ${ALL_SOURCE_FILES} >clang-format-replacements.xml + #COMMAND grep \"/dev/null && exit 1 + COMMENT "Checking correct formatting according to .clang-format file" +) + +add_custom_target( + clang-format-diff + COMMAND ${CLANG_FORMAT} -style=file -i ${ALL_SOURCE_FILES} + COMMAND git diff ${ALL_SOURCE_FILES} + COMMENT "Formatting with clang-format and showing differences with latest commit" +) diff --git a/builds/cmake/ci_build.sh b/builds/cmake/ci_build.sh index 97eed51d..2786317c 100755 --- a/builds/cmake/ci_build.sh +++ b/builds/cmake/ci_build.sh @@ -31,4 +31,11 @@ elif [ $CURVE == "libsodium" ]; then fi # Build, check, and install from local source -( cd ../..; mkdir build_cmake && cd build_cmake && PKG_CONFIG_PATH=${BUILD_PREFIX}/lib/pkgconfig cmake "${CMAKE_OPTS[@]}" .. && make -j5 all VERBOSE=1 && make install && make -j5 test ) || exit 1 +if [ "$DO_CLANG_FORMAT_CHECK" -eq "1" ] ; then + if ! (cd ../..; mkdir build_cmake && cd build_cmake && PKG_CONFIG_PATH=${BUILD_PREFIX}/lib/pkgconfig cmake "${CMAKE_OPTS[@]}" .. && make clang-format-check) ; then + make clang-format-diff + exit 1 + fi +else + ( cd ../..; mkdir build_cmake && cd build_cmake && PKG_CONFIG_PATH=${BUILD_PREFIX}/lib/pkgconfig cmake "${CMAKE_OPTS[@]}" .. && make -j5 all VERBOSE=1 && make install && make -j5 test ) || exit 1 +fi diff --git a/builds/cmake/clang-format-check.sh.in b/builds/cmake/clang-format-check.sh.in new file mode 100644 index 00000000..7ff40196 --- /dev/null +++ b/builds/cmake/clang-format-check.sh.in @@ -0,0 +1,14 @@ +#!/bin/sh +FAILED=0 +IFS=";" +FILES="@ALL_SOURCE_FILES@" +IDS=$(echo -en "\n\b") +for FILE in $FILES +do + @CLANG_FORMAT@ -style=file -output-replacements-xml "$FILE" | grep "/dev/null && + { + echo "$FILE is not correctly formatted" + FAILED=1 + } +done +if [ "$FAILED" -eq "1" ] ; then exit 1 ; fi