Generator expressions containing spaces

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Generator expressions containing spaces

Yves Frederix
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
Reply | Threaded
Open this post in threaded view
|

Re: Generator expressions containing spaces

CHEVRIER, Marc

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 <[hidden email]> on behalf of Yves Frederix <yves.frederix+[hidden email]>
Date: Monday 23 April 2018 at 13:08
To: "[hidden email]" <[hidden email]>
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
Reply | Threaded
Open this post in threaded view
|

Re: Generator expressions containing spaces

Yves Frederix
It seems COMMAND_EXPAND_LISTS was indeed the key here. Thanks a lot!

Yves

On Mon, Apr 23, 2018 at 2:11 PM, CHEVRIER, Marc <[hidden email]> wrote:

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 <[hidden email]> on behalf of Yves Frederix <[hidden email]>
Date: Monday 23 April 2018 at 13:08
To: "[hidden email]" <[hidden email]>
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
Reply | Threaded
Open this post in threaded view
|

Re: Generator expressions containing spaces

Elvis Stansvik
In reply to this post by CHEVRIER, Marc
2018-04-23 14:11 GMT+02:00 CHEVRIER, Marc <[hidden email]>:

> 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 <[hidden email]> on behalf of Yves Frederix
> <[hidden email]>
> Date: Monday 23 April 2018 at 13:08
> To: "[hidden email]" <[hidden email]>
> 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
>
--

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
Reply | Threaded
Open this post in threaded view
|

Re: Generator expressions containing spaces

Elvis Stansvik
2018-04-24 18:58 GMT+02:00 Elvis Stansvik <[hidden email]>:

> 2018-04-23 14:11 GMT+02:00 CHEVRIER, Marc <[hidden email]>:
>> 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>"
> )

Sidenote: I know that in this case, since I want the same flags for
all three of these compilers, I could "simplify" it using an
$<OR:?[,?]...>, but that's a separate thing (and I decided against it
since it gives such long unreadable lines).

Elvis

>
> 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 <[hidden email]> on behalf of Yves Frederix
>> <[hidden email]>
>> Date: Monday 23 April 2018 at 13:08
>> To: "[hidden email]" <[hidden email]>
>> 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
>>
--

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
Reply | Threaded
Open this post in threaded view
|

Re: Generator expressions containing spaces

Yves Frederix
In reply to this post by Elvis Stansvik
There is no real need to go through an intermediate variable. The following should work:

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

Yves


On Tue, Apr 24, 2018 at 6:58 PM, Elvis Stansvik <[hidden email]> wrote:
2018-04-23 14:11 GMT+02:00 CHEVRIER, Marc <[hidden email]>:
> 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 <[hidden email]> on behalf of Yves Frederix
> <[hidden email]>
> Date: Monday 23 April 2018 at 13:08
> To: "[hidden email]" <[hidden email]>
> 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
>


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Generator expressions containing spaces

Elvis Stansvik
2018-04-24 21:37 GMT+02:00 Yves Frederix <[hidden email]>:
> There is no real need to go through an intermediate variable. The following
> should work:
>
> add_compile_options(
>    "$<$<CXX_COMPILER_ID:GNU>:-Wall;-Wextra;-Werror>"
> )

Ah, of course. Thanks.

Elvis

>
> Yves
>
>
> On Tue, Apr 24, 2018 at 6:58 PM, Elvis Stansvik
> <[hidden email]> wrote:
>>
>> 2018-04-23 14:11 GMT+02:00 CHEVRIER, Marc <[hidden email]>:
>> > 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 <[hidden email]> on behalf of Yves Frederix
>> > <[hidden email]>
>> > Date: Monday 23 April 2018 at 13:08
>> > To: "[hidden email]" <[hidden email]>
>> > 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
>> >
>
>
--

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