Escaping dollar sign with ninja generator.
When using the Ninja generator with '$' in various places on Linux (cmake version 3.7.20161031-g098a1) the following will setup will (silently) create an invalid ninja configuration which will not build, and once that is fixed will create one which silently does the unintended things.
cmake_minimum_required(VERSION 3.7)
add_executable(foo "more$.cpp")
set_property(TARGET foo APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/binof$")
set_target_properties(foo PROPERTIES COMPILE_DEFINITIONS "more$=good$")
set_target_properties(foo PROPERTIES COMPILE_FLAGS "-fmy$is=not$his")
set_target_properties(foo PROPERTIES LINK_FLAGS "rupies=$")
touch more\$.cpp
mkdir build
cd build
cmake -GNinja ..
ninja
- In some paths (INCLUDES) '
' seems to be escaped to '\$
' (ninja and shell), but sometimes not at all (DEP_FILE). - In COMPILE_DEFINITIONS '
' is escaped to '\$
' in values, but is not escaped at all in names (ninja or shell). - In COMPILE_FLAGS '$' is not escaped at all (ninja or shell)
- In LINK_FLAGS '
' is partially escaped to '
$' (ninja, but not shell)
The COMPILE_DEFINITIONS is interesting because the value goes through cmOutputConverter::EscapeForShell with sets Shell_Flag_Make and Shell_Flag_IsUnix, but the value does not.
Interestingly, this behavior appears to be consistent with the escaping in the Makefile generator. I can maybe understand that in certain places it might be undesirable to escape the shell (though I'm hard pressed to think of a good reason, I think CMake really doesn't want environment variables or backtick commands changing the build out from under it). However, the cases where values are not being escaped for the generator itself seem quite surprising. It means that a non-manually escaped '$' in COMPILE_FLAGS will silently cause a ninja rule to be written incorrectly.
The current behavior seems quite... inconsistent and not very well documented. Is there any hope for some consistency (an equivalent of VERBATIM for set?) or updated documentation pinning down what the rules are?