Skip to content

Commit

Permalink
Add option to specify extra arguments for cmake-format (#37)
Browse files Browse the repository at this point in the history
The arguments are documented here: https://github.com/cheshirekow/cmake_format#usage
  • Loading branch information
krypty authored Nov 1, 2023
1 parent 734e26f commit db37c4e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ if(GIT_PROGRAM AND CMAKE_FORMAT_PROGRAM)
${name}
COMMAND
${CMAKE_COMMAND} -DGIT_PROGRAM=${GIT_PROGRAM} -DCMAKE_FORMAT_PROGRAM=${CMAKE_FORMAT_PROGRAM}
-DCMAKE_FORMAT_TARGET=${name} -DBINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
-DCMAKE_FORMAT_EXCLUDE=${CMAKE_FORMAT_EXCLUDE} -P
-DCMAKE_FORMAT_EXTRA_ARGS=${CMAKE_FORMAT_EXTRA_ARGS} -DCMAKE_FORMAT_TARGET=${name}
-DBINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} -DCMAKE_FORMAT_EXCLUDE=${CMAKE_FORMAT_EXCLUDE} -P
${CMAKE_CURRENT_LIST_DIR}/cmake-format.cmake
VERBATIM
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ To disable using _cmake_format_ to format CMake files, set the cmake option `FOR

To disable using _clang_format_ to format clang-supported files, set the cmake option `FORMAT_SKIP_CLANG` to a truthy value, e.g. by invoking CMake with `-DFORMAT_SKIP_CLANG=YES`, or enabling the option when [adding the dependency](#how-to-integrate) (recommended).

To specify a extra arguments for cmake-format, use the cmake option `CMAKE_FORMAT_EXTRA_ARGS`, e.g. by invoking CMake with `-DCMAKE_FORMAT_EXTRA_ARGS="-c /path/to/cmake-format-config.{yaml,json,py}"`,
or by enabling the option when [adding the dependency](#how-to-integrate) (recommended).


## Demo

![](https://user-images.githubusercontent.com/4437447/66123312-31ec3500-e5d1-11e9-8404-492b8eff8511.gif)
Expand Down Expand Up @@ -52,6 +56,8 @@ CPMAddPackage(
"FORMAT_SKIP_CLANG NO"
# path to exclude (optional, supports regular expressions)
"CMAKE_FORMAT_EXCLUDE cmake/CPM.cmake"
# extra arguments for cmake_format (optional)
"CMAKE_FORMAT_EXTRA_ARGS -c /path/to/cmake-format.{yaml,json,py}"
)
```

Expand Down
10 changes: 8 additions & 2 deletions cmake-format.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ get_cmake_files(
"\\.cmake$|(^|/)CMakeLists\\.txt$"
)

separate_arguments(CMAKE_FORMAT_EXTRA_ARGS)

if(CMAKE_FORMAT_TARGET STREQUAL fix-cmake-format)
execute_process(COMMAND ${CMAKE_FORMAT_PROGRAM} -i ${CMAKE_FILES})
execute_process(COMMAND ${CMAKE_FORMAT_PROGRAM} ${CMAKE_FORMAT_EXTRA_ARGS} -i ${CMAKE_FILES})
return()
endif()

Expand All @@ -49,7 +51,11 @@ endif()
set(formatted_cmake_file ${BINARY_DIR}/formatted.cmake)
foreach(cmake_file IN LISTS CMAKE_FILES)
set(source_cmake_file ${CMAKE_SOURCE_DIR}/${cmake_file})
execute_process(COMMAND ${CMAKE_FORMAT_PROGRAM} -o ${formatted_cmake_file} ${source_cmake_file})
execute_process(
COMMAND ${CMAKE_FORMAT_PROGRAM} ${CMAKE_FORMAT_EXTRA_ARGS} -o ${formatted_cmake_file}
${source_cmake_file}
)

execute_process(
COMMAND ${GIT_PROGRAM} diff -G. --color --no-index -- ${source_cmake_file}
${formatted_cmake_file} RESULT_VARIABLE result ${OUTPUT_QUIET_OPTION}
Expand Down

0 comments on commit db37c4e

Please sign in to comment.