From 64c7f447ec9a1504bf02a684e2faa4463c8690c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Milk?= <rene.milk@wwu.de> Date: Tue, 14 Jun 2016 13:37:52 +0200 Subject: [PATCH] [WIP,cmake] rewire clang format discovery --- cmake/modules/DuneXtCommonMacros.cmake | 36 +++++++++++++------------- cmake/modules/FindClangFormat.cmake | 29 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 cmake/modules/FindClangFormat.cmake diff --git a/cmake/modules/DuneXtCommonMacros.cmake b/cmake/modules/DuneXtCommonMacros.cmake index 97e75cfba..e15021c20 100644 --- a/cmake/modules/DuneXtCommonMacros.cmake +++ b/cmake/modules/DuneXtCommonMacros.cmake @@ -46,25 +46,25 @@ macro(add_analyze) endif(EXISTS ${ANALYZER}) endmacro(add_analyze) +find_package(ClangFormat 3.7) macro(add_format) - find_program(FORMAT NAMES clang-format clang-format-3.4 clang-format-3.5 clang-format-3.6 clang-format-3.7 clang-format-3.8 clang-format-3.9) - if(EXISTS ${FORMAT}) - message(STATUS "adding format target") - if (NOT TARGET format) - add_custom_target( format SOURCES ${ARGN} ) - endif (NOT TARGET format) - foreach(_file ${ARGN}) - string(REPLACE "/" "_" fn ${_file}) - if(NOT EXISTS ${_file}) - # 'fix' relative source defs - set(_file ${CMAKE_CURRENT_SOURCE_DIR}/${_file}) - endif(NOT EXISTS ${_file}) - add_custom_target("format_${fn}" ${FORMAT} -i -style=file ${_file} ) - add_dependencies( format "format_${fn}" ) - endforeach(_file ) - else() - message(WARNING "not adding format target because clang-format is missing") - endif(EXISTS ${FORMAT}) + if(CLANG_FORMAT_FOUND) + message(STATUS "adding format target") + if (NOT TARGET format) + add_custom_target( format SOURCES ${ARGN} ) + endif (NOT TARGET format) + foreach(_file ${ARGN}) + string(REPLACE "/" "_" fn ${_file}) + if(NOT EXISTS ${_file}) + # 'fix' relative source defs + set(_file ${CMAKE_CURRENT_SOURCE_DIR}/${_file}) + endif(NOT EXISTS ${_file}) + add_custom_target("format_${fn}" ${CLANG_FORMAT_BINARY} -i -style=file ${_file} ) + add_dependencies( format "format_${fn}" ) + endforeach(_file ) + else() + message(WARNING "not adding format target because clang-format is missing") + endif(CLANG_FORMAT_FOUND) endmacro(add_format) macro(add_forced_doxygen_target) diff --git a/cmake/modules/FindClangFormat.cmake b/cmake/modules/FindClangFormat.cmake new file mode 100644 index 000000000..349043934 --- /dev/null +++ b/cmake/modules/FindClangFormat.cmake @@ -0,0 +1,29 @@ +# Use this module by invoking find_package with the form:: +# +# find_package(ClangFormat +# [version] # Minimum version e.g. 3.7 +# ) +# +# optionally pass the minimal version you require like so find_package(ClangFormat 3.7) +# this module set CLANG_FORMAT_EXECUTABLE, CLANG_FORMAT_VERSION +# and CLANG_FORMAT_FOUND accordingly +# +set(CLANG_FORMAT_FOUND 0) +find_program(format_binary NAMES clang-format clang-format-3.6 clang-format-3.7 clang-format-3.8 clang-format-3.9 clang-format-4.0) +if(EXISTS ${format_binary}) + execute_process(COMMAND ${format_binary} -version OUTPUT_VARIABLE clang_out ) + message("clang verison out" ${clang_out}) + string(REGEX REPLACE ".*clang-format version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_FORMAT_VERSION ${clang_out}) + set(CLANG_FORMAT_FOUND 1) + set(CLANG_FORMAT_EXECUTABLE ${format_binary}) + if(optional argument passed) + check match with CLANG_FORMAT_VERSION + if(not matched) + message(INFO "Requested clang-format >= arg_passed, found ${CLANG_FORMAT_VERSION}" + set(CLANG_FORMAT_FOUND 0) + endif() + endif() + #message("clang-format executable: ${CLANG_FORMAT_EXECUTABLE}") + #message("clang-format version: ${CLANG_FORMAT_VERSION}") +endif() + -- GitLab