diff --git a/CMakeLists.txt b/CMakeLists.txt index 519ec1e..2faed24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} diff --git a/README.md b/README.md index a5a2171..6792e13 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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}" ) ``` diff --git a/cmake-format.cmake b/cmake-format.cmake index 837f6b9..7a21046 100644 --- a/cmake-format.cmake +++ b/cmake-format.cmake @@ -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() @@ -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}