I want to create a CMake function that takes as first argument a list, and a second argument with a single name.
function (my_function PROJECT_SRC DEPENDENCY_LIB)
message (STATUS "Dependency lib is ${DEPENDENCY_LIB}")
endfunction()
When I use it, it's not what I expect. If I prepend a list with some source file, the function prints the second argument of the list.
file (GLOB TEST_SRC *.cpp)
set (MY_DEPENDENCY "Something")
# It prints "Dependency lib is something/inside/TEST_SRC"
my_function(${TEST_SRC} ${MY_DEPENDENCY})
How can I retrieve the second argument that I pass to the function? Is there a way to distinguish between lists and single values when passing variables to a CMake function?
First of all... back to the basics of CMake. What is the list? [1] [2]
It's just string containing ; separated values. So, the following:
set(FOO "test")
list(LENGTH FOO FOO_LEN)
message(STATUS "List length:" ${FOO_LEN})
would produce 1.
set(FOO "aaaaaa;bbbbbbbb")
list(LENGTH FOO FOO_LEN)
message(STATUS "List length:" ${FOO_LEN})
would produce 2.
So, to answer You second question, You can just check the length of the list and if it's equal to 1 --- it's a "list" containing single value which can be interpreted as a basic string.
To answer You first question --- in Your example ${TEST_SRC} ${MY_DEPENDENCY} is being passed to the function as a one big "list". So, to separate those into two separate arguments, You should
a) quote both arguments and by this --- pass them separately
b) do not quote those and in Your function manage with a list passed [3]
[1] https://cmake.org/cmake/help/v3.0/manual/cmake-language.7.html#lists
[2] https://cmake.org/cmake/help/v3.0/command/list.html
I've found a solution by myself.
I pass the list argument as a string:
my_function("${TEST_SRC}" ${MY_DEPENDENCY})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With