The space is used to separate arguments passed to COMMAND. So your generator
expression is splitted before evaluation and elements are no longer valid
generator expression.
So, to solve your problem, encapsulate the generator expression inside quotes.
And apply the following advices for correct result:
* Separate command from args
* Use variable to list your arguments and add option COMMAND_EXPAND_LISTS
(available with version 3.8) to avoid “spurious” semi-colons in the result
Your example reworked:
Set (args foo bar)
add_custom_target(foo)
add_custom_command(TARGET foo POST_BUILD
COMMAND $<1:echo> "$<1 :${args}>"
COMMAND_EXPAND_LISTS
)
From: CMake <[email protected]> on behalf of Yves Frederix
<[email protected]>
Date: Monday 23 April 2018 at 13:08
To: "[email protected]" <[email protected]>
Subject: [CMake] Generator expressions containing spaces
Hi,
I recently learned that the COMMAND line in a custom command supports generator
expressions. In particular, what makes this powerful is that if the expression
evaluates to the empty string, no corresponding code will be added to the
target (as documented in the
docs<https://cmake.org/cmake/help/v3.11/command/add_custom_command.html?highlight=add_custom_command>).
While this works very nicely for single-string command, I fail to make it work
for commands consisting of multiple space-separated strings:
```
~/tmp/genex_with_spaces$ cat CMakeLists.txt
cmake_minimum_required(VERSION
3.6)
add_custom_target(foo)
add_custom_command(TARGET foo POST_BUILD
COMMAND $<1:echo bar>
)
~/tmp/genex_with_spaces$ grep bar build/CMakeFiles/foo.dir/build.make
"\$$<1:echo" bar>
```
As can be seen, the generator expression is not expanded.
My question is now whether I am doing something wrong (is there a correct way
of dealing with spaces in the context of generator expressions?) or might this
be an inherent limitation of generator expression in general?
As a workaround, the only thing that seems to work is to put each of the
space-separated components of the command in (typically identical) genexes:
```
~/tmp/genex_with_spaces$ cat CMakeLists.txt
cmake_minimum_required(VERSION
3.6)
add_custom_target(foo)
add_custom_command(TARGET foo POST_BUILD
COMMAND $<1:echo> $<1:bar>
)
~/tmp/genex_with_spaces$ grep bar build/CMakeFiles/foo.dir/build.make
echo bar
```
Of course, while this works, things becomes very unreadable if the genex is
more complex.
Other things that I have tried but failed:
* escape the space with a backslash -> this quotes the entire expression
inside the genex.
* define the command directly as a list inside the genex -> this removes
spaces between the different components of the command and quotes that result.
* treat the command as a list, perform string operations to wrap each
element in the desired regex and convert semicolon to spaces -> again results
in the command being quoted as a whole.
Any advice is highly appreciated.
Thanks,
Yves
--
Powered by www.kitware.com
Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ
Kitware offers various services to support the CMake community. For more
information on each offering, please visit:
CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake