Skip to content

Commit

Permalink
feat: add custom clang-format file support (#668)
Browse files Browse the repository at this point in the history
Add custom clang-format file support

Allow injecting a custom clang-format file instead of using the one provided by the infra library.
  • Loading branch information
fabiangottstein authored Jul 5, 2024
1 parent b33ed06 commit 3653875
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cmake/emil_clang_tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ function(emil_exclude_directory_from_clang_format directory)
endfunction()

function(emil_clangformat_directories postfix directories)
set(singleArgs CLANG_FORMAT_FILE)
set(multiValueArgs DIRECTORIES)
cmake_parse_arguments(PARSE_ARGV 1 MY "" "" "${multiValueArgs}")
cmake_parse_arguments(PARSE_ARGV 1 MY "" "${singleArgs}" "${multiValueArgs}")

emil_get_subdirectories(subdirectories DIRECTORIES ${MY_DIRECTORIES})
emil_get_targets_from_directories(targets DIRECTORIES ${subdirectories})
Expand Down Expand Up @@ -53,6 +54,10 @@ function(emil_clangformat_directories postfix directories)
if (CLANGFORMAT)
add_custom_target(clangformat_${postfix})

if (NOT DEFINED MY_CLANG_FORMAT_FILE)
set(MY_CLANG_FORMAT_FILE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../.clang-format)
endif()

set(run_cmd)
if (CMAKE_HOST_WIN32)
# Executing clang-format under windows runs a batch script that terminates the shell after running the first command
Expand All @@ -67,7 +72,7 @@ function(emil_clangformat_directories postfix directories)
if (${size} GREATER 7000)
add_custom_command(
TARGET clangformat_${postfix} POST_BUILD
COMMAND ${run_cmd} ${CLANGFORMAT} -style=file:${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../.clang-format -i ${clangformat_sources}
COMMAND ${run_cmd} ${CLANGFORMAT} -style=file:${MY_CLANG_FORMAT_FILE} -i ${clangformat_sources}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Formatting ${postfix} with ${CLANGFORMAT} ..."
)
Expand All @@ -78,7 +83,7 @@ function(emil_clangformat_directories postfix directories)
if (clangformat_sources)
add_custom_command(
TARGET clangformat_${postfix} POST_BUILD
COMMAND ${run_cmd} ${CLANGFORMAT} -style=file:${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../.clang-format -i ${clangformat_sources}
COMMAND ${run_cmd} ${CLANGFORMAT} -style=file:${MY_CLANG_FORMAT_FILE} -i ${clangformat_sources}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Formatting ${postfix} with ${CLANGFORMAT} ..."
)
Expand Down

0 comments on commit 3653875

Please sign in to comment.