Skip to content

Commit

Permalink
[backport] Add option to toggle the formatting via clang-format in th…
Browse files Browse the repository at this point in the history
…e generator cmake macro (#282) (#308)
  • Loading branch information
tmadlener authored Jun 24, 2022
1 parent 867f25a commit 5331777
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions cmake/podioMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ function(PODIO_GENERATE_DICTIONARY dictionary)
set_target_properties(${dictionary} PROPERTIES CXX_CLANG_TIDY "")
endfunction()


set(PODIO_USE_CLANG_FORMAT AUTO CACHE STRING "Try to use clang-format to format the code generated by podio")
set_property(CACHE PODIO_USE_CLANG_FORMAT PROPERTY STRINGS AUTO ON OFF)
#---------------------------------------------------------------------------------------------------
#---PODIO_GENERATE_DATAMODEL( datamodel YAML_FILE RETURN_HEADERS RETURN_SOURCES
# OUTPUT_FOLDER output_directory
Expand Down Expand Up @@ -139,11 +140,31 @@ function(PODIO_GENERATE_DATAMODEL datamodel YAML_FILE RETURN_HEADERS RETURN_SOUR
ENDIF()

set(CLANG_FORMAT_ARG "")
find_program(CLANG_FORMAT_EXE NAMES "clang-format")
find_file(CLANG_FORMAT_FILE .clang-format PATH ${PROJECT_SOURCE_DIR} NO_DEFAULT_PATH)
if(CLANG_FORMAT_EXE AND CLANG_FORMAT_FILE)
message(STATUS "Found .clang-format file and clang-format executable. Will pass it to podio class generator")
set(CLANG_FORMAT_ARG "--clangformat")
if (PODIO_USE_CLANG_FORMAT STREQUAL AUTO OR PODIO_USE_CLANG_FORMAT)
find_program(CLANG_FORMAT_EXE NAMES "clang-format")
find_file(CLANG_FORMAT_FILE .clang-format PATH ${PROJECT_SOURCE_DIR} NO_DEFAULT_PATH)
# only newer versions of clang-format know about fallback-style.
execute_process(COMMAND ${CLANG_FORMAT_EXE} --fallback-style OUTPUT_VARIABLE CLANG_FALLBACK_STYLE ERROR_VARIABLE CLANG_FALLBACK_STYLE_ERR)
if(CLANG_FORMAT_EXE AND CLANG_FORMAT_FILE
AND NOT ${CLANG_FALLBACK_STYLE_ERR} MATCHES "Unknown")
message(STATUS "Found .clang-format file and clang-format executable. Will pass it to podio class generator")
set(CLANG_FORMAT_ARG "--clangformat")
else()
if(NOT CLANG_FORMAT_EXE)
message(STATUS "Could not find clang-format executable!")
endif()
if(NOT CLANG_FORMAT_FILE)
message(STATUS "Could not find .clang-format file!")
endif()
if(${CLANG_FALLBACK_STYLE_ERR} MATCHES "Unknown")
message(STATUS "Newer version of clang-format required - cannot run with --fallback-style!")
endif()
if(PODIO_USE_CLANG_FORMAT STREQUAL AUTO)
message(STATUS "Skip formatting of generated code files.")
elseif(PODIO_USE_CLANG_FORMAT)
message(FATAL_ERROR "Could not run clang-format on generated code files!")
endif()
endif()
endif()

# Make sure that we re run the generation process everytime either the
Expand Down

0 comments on commit 5331777

Please sign in to comment.