From 718ea71759a9cf468e805764cf9a7fb8c080b885 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Sun, 5 Dec 2021 16:07:20 +0100 Subject: [PATCH] Allow passing lists in the options (#302) * allow list options by passing option parameter as string, not value * add test * remove debug message * run formatter --- cmake/CPM.cmake | 12 ++++++------ test/unit/local_dependency/OptionsCMakeLists.txt.in | 4 +++- test/unit/local_dependency/dependency/CMakeLists.txt | 6 ++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 1c8661d..a9ef272 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -819,8 +819,8 @@ function( set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) foreach(OPTION ${OPTIONS}) - cpm_parse_option(${OPTION}) - set(${OPTION_KEY} ${OPTION_VALUE}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") endforeach() endif() set(CPM_OLD_INDENT "${CPM_INDENT}") @@ -866,15 +866,15 @@ endfunction() # splits a package option function(cpm_parse_option OPTION) - string(REGEX MATCH "^[^ ]+" OPTION_KEY ${OPTION}) - string(LENGTH ${OPTION} OPTION_LENGTH) - string(LENGTH ${OPTION_KEY} OPTION_KEY_LENGTH) + string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") + string(LENGTH "${OPTION}" OPTION_LENGTH) + string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) # no value for key provided, assume user wants to set option to "ON" set(OPTION_VALUE "ON") else() math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") - string(SUBSTRING ${OPTION} "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) + string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) endif() set(OPTION_KEY "${OPTION_KEY}" diff --git a/test/unit/local_dependency/OptionsCMakeLists.txt.in b/test/unit/local_dependency/OptionsCMakeLists.txt.in index 505db4e..96accc9 100644 --- a/test/unit/local_dependency/OptionsCMakeLists.txt.in +++ b/test/unit/local_dependency/OptionsCMakeLists.txt.in @@ -13,7 +13,9 @@ include(@CPM_PATH@/CPM.cmake) CPMAddPackage( NAME Dependency SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/dependency - OPTIONS "DEFINE_ALTERNATIVE_FUNCTION YES" + OPTIONS + "DEFINE_ALTERNATIVE_FUNCTION YES" + "LIST_ARGUMENT a\\\\;b\\\\;c" EXCLUDE_FROM_ALL YES ) diff --git a/test/unit/local_dependency/dependency/CMakeLists.txt b/test/unit/local_dependency/dependency/CMakeLists.txt index 4c7ec33..7d01e6a 100644 --- a/test/unit/local_dependency/dependency/CMakeLists.txt +++ b/test/unit/local_dependency/dependency/CMakeLists.txt @@ -8,6 +8,12 @@ if(NOT DEFINE_ALTERNATIVE_FUNCTION) message("called external method") endfunction() else() + + # check if list was passed correctly + if(NOT "${LIST_ARGUMENT}" STREQUAL "a;b;c") + message(FATAL_ERROR "list argument not properly passed to dependency: '${LIST_ARGUMENT}'") + endif() + function(alternative_dependency_function) message("called alternative external method") endfunction()