From 533177747494049492e0a01d27b36d663fed7748 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 24 Jun 2022 12:00:16 +0200 Subject: [PATCH] [backport] Add option to toggle the formatting via clang-format in the generator cmake macro (#282) (#308) --- cmake/podioMacros.cmake | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cmake/podioMacros.cmake b/cmake/podioMacros.cmake index eb4db3c8f..178dcee60 100644 --- a/cmake/podioMacros.cmake +++ b/cmake/podioMacros.cmake @@ -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 @@ -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