MantisBT - CMake
View Issue Details
0015792CMakeCMakepublic2015-10-15 04:492016-03-07 09:12
Pavel Solodovnikov 
 
normalmajoralways
closedwon't fix 
Linux
CMake 3.4 
 
0015792: Bad escaping of parentheses in POST_BUILD events if Ninja generator used
CMake generates wrong post-build events if it contains parentheses: they are not escaped at all (e.g., copy some build artefacts to a directory that contains parens).

I've attached a small example to reproduce the issue.

================= BEGIN EXAMPLE
    cmake_minimum_required(VERSION 2.8.11)

    project(example)
    add_library(ex abc.cpp)
    add_custom_command(TARGET ex POST_BUILD VERBATIM COMMAND cp -f "libex.a" "mydir (abc)")
================= END EXAMPLE

After running cmake, we can easily observe that parentheses are not escaped properly in post build event:

57 build libex.a: CXX_STATIC_LIBRARY_LINKER__ex CMakeFiles/ex.dir/abc.cpp.o
   OBJECT_DIR = CMakeFiles/ex.dir
   POST_BUILD = cd /mnt/sources/work/cmake_parens_test && cp -f libex.a mydir\ (abc)
   PRE_LINK = :
   TARGET_COMPILE_PDB = CMakeFiles/ex.dir/
   TARGET_FILE = libex.a
   TARGET_PDB = libex.pdb

It leads to build errors since it's not valid shell syntax:

FAILED: : && /home/prog/Downloads/cmake-3.4.0-rc1-Linux-i386/bin/cmake -E remove libex.a && /bin/ar qc libex.a CMakeFiles/ex.dir/abc.cpp.o && /bin/ranlib libex.a && cd /mnt/sources/work/cmake_parens_test && cp -f libex.a mydir\ (abc)
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `: && /home/prog/Downloads/cmake-3.4.0-rc1-Linux-i386/bin/cmake -E remove libex.a && /bin/ar qc libex.a CMakeFiles/ex.dir/abc.cpp.o && /bin/ranlib libex.a && cd /mnt/sources/work/cmake_parens_test && cp -f libex.a mydir\ (abc)'
ninja: build stopped: subcommand failed.

It is possible to workaround this by using VERBATIM option in POST_BUILD event, though.

Tested with the latest CMake v.3.4.0 rc1 (I just couldn't select it in "Product Version" dropdown list, so I left CMake 3.3.2 there).
1) Extract attached example.tar
2) Run "cmake -G "Ninja" ." inside extracted dir.
3) Examine produced build.ninja file.
CMake, linux, ninja
tar cmake_parens_test.tar (10,240) 2015-10-15 04:49
https://public.kitware.com/Bug/file/5542/cmake_parens_test.tar
Issue History
2015-10-15 04:49Pavel SolodovnikovNew Issue
2015-10-15 04:49Pavel SolodovnikovFile Added: cmake_parens_test.tar
2015-10-15 04:50Pavel SolodovnikovTag Attached: CMake
2015-10-15 04:50Pavel SolodovnikovTag Attached: linux
2015-10-15 04:50Pavel SolodovnikovTag Attached: ninja
2015-10-15 04:52Pavel SolodovnikovNote Added: 0039607
2015-10-15 08:33Brad KingProduct VersionCMake 3.3.2 => CMake 3.4
2015-10-15 08:36Brad KingNote Added: 0039608
2015-10-15 08:36Brad KingStatusnew => resolved
2015-10-15 08:36Brad KingResolutionopen => won't fix
2016-03-07 09:12Robert MaynardNote Added: 0040618
2016-03-07 09:12Robert MaynardStatusresolved => closed

Notes
(0039607)
Pavel Solodovnikov   
2015-10-15 04:52   
I've made a misprint in posted example, it should be
    add_custom_command(TARGET ex POST_BUILD COMMAND cp -f "libex.a" "mydir (abc)")
without VERBATIM option, to reproduce error.
(0039608)
Brad King   
2015-10-15 08:36   
The VERBATIM option was added specifically to support more advanced escaping while retaining compatibility with the older implementation. When not using VERBATIM it is your responsibility to add shell escapes.

https://cmake.org/cmake/help/v3.4/command/add_custom_command.html [^]
"Use of VERBATIM is recommended as it enables correct behavior. When VERBATIM is not given the behavior is platform specific because there is no protection of tool-specific special characters."
(0040618)
Robert Maynard   
2016-03-07 09:12   
Closing resolved issues that have not been updated in more than 4 months.