2

I just don't seem to be able to wrap my head around CMake's escape rules. Given:

set(X A B C)
add_custom_target( works COMMAND DUMMY=0 X="${X}" env | grep ^X= COMMENT "This works")
add_custom_target( fails COMMAND X="${X}" env | grep ^X= COMMENT "This fails")

The intention is to execute command X="A B C" env. The custom target works correctly constructs the command, where as fails incorrectly executes:

X=\"A B C\" env ...

But why?

Ben
  • 282
  • 2
  • 10
  • 1
    Possible duplicate of [cmake: when to quote variables?](http://stackoverflow.com/questions/35847655/cmake-when-to-quote-variables) Do try `set(X "A B C")` and don't put quotes around the `${X}`. – Florian Nov 16 '16 at 08:30
  • 2
    @Florian, `set(X "A B C")` results in: `"X=\"A B C\"" env`, which is still wrong. – Ben Nov 16 '16 at 22:15
  • 1
    I'm having exactly the same problem, `ExternalProject_Add_Step(COMMAND build --args="my args")` will execute `build --args=\"my args\"` which is incorrect. I'll start a bounty if I can't find a solution tonight. – Axel Isouard Nov 20 '16 at 08:23

1 Answers1

1

Actually you ran into two problems:

  1. Don't quote CMake variables in custom commands. CMake will do the necessary escape sequences for you.
  2. The first literal after COMMAND is assumed to be a command name or file. So CMake tries to handle it as a single "string".

So I changed the quoting and the env call and the following did work for me:

cmake_minimum_required(VERSION 2.8)

project(QuotedStrings)

set(X "A B C")
add_custom_target( works COMMAND env DUMMY=0 X=${X} | grep ^X= COMMENT "This works")
add_custom_target( fails_no_more COMMAND env X=${X} | grep ^X= COMMENT "This works too")

For more details and possibilities see:

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149