Passing list variables to external project commands

Hello, everyone.

For some reason in my project I need to set a compiler launcher this way:

set(CMAKE_C_COMPILER_LAUNCHER env DIR=/path/to/dir)

But when I try to configure an external cmake project with the same launcher:

ExternalProject_Add(name
	CONFIGURE_COMMAND cmake -DCMAKE_C_COMPILER_LAUNCHER="${CMAKE_C_COMPILER_LAUNCHER}" ...
    ...
)

I end up with the following configure command:

cmake -DCMAKE_C_COMPILER_LAUNCHER=\"env DIR=/path/to/dir\" ...

Which is incorrect and I expect something like this:

cmake -DCMAKE_C_COMPILER_LAUNCHER="env;DIR=/path/to/dir" ...

How could I achieve the result I expect without using any string transformations?

1 Like

Use CMAKE_CACHE_ARGS as those are transferred with a file and not via command line

1 Like

CMake doesn’t have the same quoting rules as a bash shell. In particular, quotes anywhere other than at the start and end of an argument are not treated specially and become part of the string. Instead of putting the opening quote in the middle of the argument, put it at the start like so:

ExternalProject_Add(name
	CONFIGURE_COMMAND cmake "-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}" ...
    ...
)

Unfortunately, your variant does not work as well:

set(CMAKE_C_COMPILER_LAUNCHER env DIR=/tmp)

ExternalProject_Add(name
	CONFIGURE_COMMAND cmake "-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}" ...
)

Which produces the following output:

cd ... && cmake -DCMAKE_C_COMPILER_LAUNCHER=env DIR=/tmp ...

Now I have to convert it to a string to make it work:

string(REPLACE ";" "$<SEMICOLON>" CMAKE_C_COMPILER_LAUNCHER_STR "${CMAKE_C_COMPILER_LAUNCHER}")

But this is pretty inconvenient.

PS. cmake version 3.28.3