[CMake] Generator expressions containing spaces

Elvis Stansvik elvis.stansvik at orexplore.com
Tue Apr 24 12:58:21 EDT 2018


2018-04-23 14:11 GMT+02:00 CHEVRIER, Marc <marc.chevrier at sap.com>:
> 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
>
>   )
>

Interesting thread. I was sort of in the same boat, having a

add_compile_options(
    "$<$<CXX_COMPILER_ID:GNU>:-Wall>"
    "$<$<CXX_COMPILER_ID:GNU>:-Wextra>"
    "$<$<CXX_COMPILER_ID:GNU>:-Werror>"

    "$<$<CXX_COMPILER_ID:Clang>:-Wall>"
    "$<$<CXX_COMPILER_ID:Clang>:-Wextra>"
    "$<$<CXX_COMPILER_ID:Clang>:-Werror>"

    "$<$<CXX_COMPILER_ID:AppleClang>:-Wall>"
    "$<$<CXX_COMPILER_ID:AppleClang>:-Wextra>"
    "$<$<CXX_COMPILER_ID:AppleClang>:-Werror>"
)

and of course wishing I could just write something like

add_compile_options(
    $<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra -Werror>
    $<$<CXX_COMPILER_ID:Clang>:-Wall -Wextra -Werror>
    $<$<CXX_COMPILER_ID:AppleClang>:-Wall -Wextra -Werror>
)

instead.

I can't depend on CMake 3.8+ just yet, but I'll remember your solution
for when I can.

I don't quite like having to introduce variables though. Is there no
way to solve this without having to first stuff the elements in a list
and use that?

Cheers,
Elvis

>
>
>
>
> From: CMake <cmake-bounces at cmake.org> on behalf of Yves Frederix
> <yves.frederix+cmake at gmail.com>
> Date: Monday 23 April 2018 at 13:08
> To: "cmake at cmake.org" <cmake at cmake.org>
> 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).
>
>
>
> 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
>


More information about the CMake mailing list