MantisBT - CMake
View Issue Details
0012427CMakeCMakepublic2011-08-28 10:412016-06-10 14:31
Mathnerd314 
Kitware Robot 
normalmajoralways
closedmoved 
Windows764-bit
CMake 2.8.5 
 
0012427: ADD_CUSTOM_COMMAND escapes quotes incorrectly under MSVC
ADD_CUSTOM_COMMAND maps to a batch file, and in batch files the proper way to escape quotes is by doubling them. (i.e., "") However, the generated file doesn't do this, instead escaping them using a backslash (i.e., \"), which cmd.exe does not support.
1. Generate project files for attached CMakeLists.txt
2. Examine test.vcproj, and note that the applicable rule has a CommandLine containing "\""
3. Build the project in Visual Studio, and note how a #define statement is output
4. Examine version.h, and note how the contents is not a valid header file
There appear to be no workarounds, so a speedy fix would be helpful.
No tags attached.
txt CMakeLists.txt (1,025) 2011-08-28 10:41
https://public.kitware.com/Bug/file/4027/CMakeLists.txt
Issue History
2011-08-28 10:41Mathnerd314New Issue
2011-08-28 10:41Mathnerd314File Added: CMakeLists.txt
2011-08-29 10:02Brad KingNote Added: 0027280
2011-08-29 15:19Mathnerd314Note Added: 0027293
2011-08-29 15:44Brad KingNote Added: 0027295
2011-08-29 15:46Brad KingNote Added: 0027296
2011-08-29 15:46Brad KingStatusnew => backlog
2011-08-29 20:32Mathnerd314Note Added: 0027301
2016-06-10 14:28Kitware RobotNote Added: 0041895
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
(0027280)
Brad King   
2011-08-29 10:02   
Windows command line parsing is not done by cmd.exe or in bat files when it comes to executing a native program. The whole command line is passed to the CreateProcess API internally as a single string. It is up to the process startup code to actually do the parsing. The standard MS runtime library parses using a documented approach to define argv for main. CMake constructs escapes correctly according to this standard.

Escaping in custom commands is fairly well tested:

http://cmake.org/gitweb?p=cmake.git;a=blob;f=Tests/CustomCommand/CMakeLists.txt;hb=v2.8.5#l221 [^]

Which VS version/generator are you using?
(0027293)
Mathnerd314   
2011-08-29 15:19   
VS 9 2008 (Express Edition), and I'm generating for that version.
(0027295)
Brad King   
2011-08-29 15:44   
Okay, I can reproduce that. It looks like the shell fails to correctly handle the escapes while pulling out the shell redirection operator. You can work around this by moving the operator before the arguments:

ADD_CUSTOM_TARGET(test ALL
  ${CMAKE_COMMAND} -E echo "#ifndef VERSION_H" > "${CMAKE_BINARY_DIR}/version.h.tmp"
  COMMAND ${CMAKE_COMMAND} -E echo "#define VERSION_H" >> "${CMAKE_BINARY_DIR}/version.h.tmp"
  COMMAND ${CMAKE_COMMAND} >> "${CMAKE_BINARY_DIR}/version.h.tmp" -E echo_append "#define PACKAGE_VERSION \"0.0.0 r" # ***
  COMMAND ${SVNVERSION_EXECUTABLE} -n ${CMAKE_CURRENT_SOURCE_DIR} >> "${CMAKE_BINARY_DIR}/version.h.tmp"
  COMMAND ${CMAKE_COMMAND} >> "${CMAKE_BINARY_DIR}/version.h.tmp" -E echo "\"" # ***
  COMMAND ${CMAKE_COMMAND} -E echo "#endif" >> "${CMAKE_BINARY_DIR}/version.h.tmp"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_BINARY_DIR}/version.h.tmp" "${CMAKE_BINARY_DIR}/version.h"
  COMMAND ${CMAKE_COMMAND} -E remove "${CMAKE_BINARY_DIR}/version.h.tmp"
  WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  COMMENT "Updating ${CMAKE_BINARY_DIR}/version.h..."
  VERBATIM
  )
(0027296)
Brad King   
2011-08-29 15:46   
Moving to backlog as it is unclear whether CMake can do anything about this. We already bend over backwards to accommodate crazy escaping patterns on Windows. I can find no way to get an unmatched double quote in an argument before ">>", though I haven't tried too hard.
(0027301)
Mathnerd314   
2011-08-29 20:32   
A quick workaround is all I needed. Thanks! :-)
(0041895)
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.