0

I have a function which takes a list of arguments that I want to pass to a function in a target. Only when I try to use that multivalueargs, it places the second and further arguments on the following line instead of all on one line separated by spaces.

Here is my minimal version of my function. Save that in a file named CMakeLists.txt.

cmake_minimum_required(VERSION 3.22.1)

function(MyFunc)
    set(options VERBOSE)
    set(oneValueArgs PROJECT_NAME)
    set(multiValueArgs PROJECT_ARGS)
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    project(${ARG_PROJECT_NAME})

    add_custom_command(
        OUTPUT sample
        COMMAND echo "starting..."
        COMMAND echo "ARG_PROJECT_ARGS = [${ARG_PROJECT_ARGS}]"
        COMMAND echo "done..."
    )

    add_custom_target(${PROJECT_NAME} DEPENDS sample)
endfunction()

MyFunc(
    PROJECT_NAME testing
    PROJECT_ARGS
        --some additional
        --parameters here
        --to-test)

To generate, use:

cmake .

To run the result, use:

make testing

That outputs the correct first argument, but the following ones are on the next line and interpreted as commands instead of being just parameters in the echo ... string.

[100%] Generating sample
starting...
ARG_PROJECT_ARGS = [--some
/bin/sh: 1: additional: not found
/bin/sh: 1: --parameters: not found
/bin/sh: 1: here: not found
/bin/sh: 1: --to-test]: not found
make[3]: *** [CMakeFiles/testing.dir/build.make:74: sample] Error 127
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/testing.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/testing.dir/rule] Error 2
make: *** [Makefile:124: testing] Error 2

What do I have to do to use the list as a string which does not end up on multiple lines like in the above?

1 Answer 1

0

Found out that since cmake 3.12 we can use the list JOIN command like so:

list(JOIN <list-var-name> <separator> <output-var-name)

So in the above, I can use that just before the add_custom_target() and replace the variable name with the output:

list(JOIN ARG_PROJECT_ARGS " " ARGUMENTS)
add_custom_command(
    OUTPUT sample
    COMMAND echo "starting..."
    COMMAND echo "ARG_PROJECT_ARGS -> ARGUMENTS = [${ARGUMENTS}]"
    COMMAND echo "done..."
)

And now I get one line and the "done..." prints in the output as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.