MantisBT - CMake
View Issue Details
0013946CMakeCMakepublic2013-02-23 01:202016-06-10 14:31
Dave Abrahams 
Kitware Robot 
normalminoralways
closedmoved 
MacOS X10.8
CMake 2.8.10 
 
0013946: custom command escaping of shell parentheses
A left parenthesis in the COMMAND of add_custom_target is always passed to the shell in double quotation marks. That prevents COMMANDs like

"(" A "||" B ")" "&&" C

from working.
just try it :-)
No tags attached.
Issue History
2013-02-23 01:20Dave AbrahamsNew Issue
2013-02-23 01:42Dave AbrahamsNote Added: 0032365
2013-02-25 08:34Brad KingNote Added: 0032370
2013-02-25 09:42Dave AbrahamsNote Added: 0032375
2013-02-25 10:02Brad KingNote Added: 0032376
2013-02-26 10:20Dave AbrahamsNote Added: 0032383
2013-02-26 10:42Brad KingNote Added: 0032384
2013-02-26 10:42Brad KingSeveritymajor => minor
2013-02-26 10:42Brad KingStatusnew => backlog
2013-02-26 10:42Brad KingSummaryadd_custom_target COMMANDs have problems with left parens => custom command escaping of shell parentheses
2016-06-10 14:28Kitware RobotNote Added: 0042234
2016-06-10 14:28Kitware RobotStatusbacklog => resolved
2016-06-10 14:28Kitware RobotResolutionopen => moved
2016-06-10 14:28Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0032365)
Dave Abrahams   
2013-02-23 01:42   
Here's an example that didn't work for me:
https://github.com/ryppl/Boost2Git/blob/fc0fe838a1ae806e2ff9d7492d50d29bb02047c1/CMakeLists.txt#L73 [^]
The left paren was quoted to the shell, but the right paren was not!
(0032370)
Brad King   
2013-02-25 08:34   
The open paren is being treated as the argv[0] of a single command line.

Without the VERBATIM option quoting of special characters is not done consistently (here 'verbatim' means that we quote the value to get it through to the actual command's argv array as CMake sees it). Adding the option I get

 "(" A || B ")" && C

which at least is consistent with left and right parens. The shell operators are not quoted as they are treated as special cases already by VERBATIM due to previous such requests.

These kinds of shell expressions are not portable AFAIK. I suggest writing

 VERBATIM COMMAND sh -c "( A || B ) && C"

in the custom command instead, at least to work around this.

The list of special exceptions for shell operators is here:

 http://cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmLocalGenerator.cxx;hb=v2.8.10.2#l2948 [^]

Try adding "(" and ")" to that list.
(0032375)
Dave Abrahams   
2013-02-25 09:42   
1. Is that kind of shell expression any less portable than the availability of sh?

2. Not sure to whom the "Try adding…" advice is directed, but I don't plan to rebuild cmake for this one

3. Aside: I noticed that multiple COMMANDs get joined up by cmake with &&, at least on *nix. This can result in line lengths that would surely blow up CMD.EXE. The lack of parens around each clause means the user has to be aware of how cmake will construct the full command.
(0032376)
Brad King   
2013-02-25 10:02   
Re 0013946:0032375:

(1) No, but sh should be available anywhere you can use shell expressions and you don't need a new CMake to use it. I've seen projects go as far as using find_program to get sh, or just hard-code /bin/sh.

(2) The advice was directed to you to see if the fix is sufficient. Perhaps this fix will uncover another problem your project exposes which could then be reported too.

(3) Multiple commands are handled differently by different generators. If you don't put in your own shell expressions then you don't need to know how CMake combines them. Using "sh -c" will hide the operators inside a string argument and CMake will escape the command after -c so sh sees it cleanly (with VERBATIM).
(0032383)
Dave Abrahams   
2013-02-26 10:20   
I appreciate your efforts, Brad, but we found a different approach.
(0032384)
Brad King   
2013-02-26 10:42   
Okay, sending to backlog for now and updating summary text.
(0042234)
Kitware Robot   
2016-06-10 14:28   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.