Skip to content
Snippets Groups Projects
Unverified Commit 8cd9ec09 authored by René Fritze's avatar René Fritze
Browse files

[cmake] only add format target if clang format 3.7 is found

clang format discovery is improved a lot by using cmake
builtins, allowing us to very easily target a specific
version only.

This is one half of #10
parent 64c7f447
No related branches found
No related tags found
No related merge requests found
......@@ -46,9 +46,9 @@ macro(add_analyze)
endif(EXISTS ${ANALYZER})
endmacro(add_analyze)
find_package(ClangFormat 3.7)
find_package(ClangFormat 3.7 EXACT)
macro(add_format)
if(CLANG_FORMAT_FOUND)
if(ClangFormat_FOUND)
message(STATUS "adding format target")
if (NOT TARGET format)
add_custom_target( format SOURCES ${ARGN} )
......@@ -59,12 +59,13 @@ macro(add_format)
# '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_custom_target("format_${fn}" ${ClangFormat_EXECUTABLE} -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)
message(WARNING "not adding format target because clang-format is missing or"
"wrong version: ${ClangFormat_EXECUTABLE} ${ClangFormat_VERSION}")
endif(ClangFormat_FOUND)
endmacro(add_format)
macro(add_forced_doxygen_target)
......
......@@ -5,25 +5,18 @@
# )
#
# 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
# this module set ClangFormat_EXECUTABLE, ClangFormat_VERSION
# and ClangFormat_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}")
find_program(ClangFormat_EXECUTABLE 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 ${ClangFormat_EXECUTABLE})
execute_process(COMMAND ${ClangFormat_EXECUTABLE} -version OUTPUT_VARIABLE clang_out )
string(REGEX REPLACE ".*clang-format version ([0-9]+\\.[0-9]+).*" "\\1" ClangFormat_VERSION ${clang_out})
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ClangFormat
REQUIRED_VARS ClangFormat_EXECUTABLE
VERSION_VAR ClangFormat_VERSION
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment