Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow specifying extra arguments to CMake format #37

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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