target_compile_definitions: Does not escape semicolons in COMPILE_DEFINITIONS
Attempting to add a compile definition string with a semicolon results in a compilation error
Environment: Windows 11 Pro CMake 3.27.0 CMake Generator : Ninja Multi-Config MSVC 19.29.30145 for x64
There is such CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(CMakeSemicolonBug LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(CMakeSemicolonBug main.cpp)
target_compile_definitions(CMakeSemicolonBug PRIVATE "VAR0=\"VAL0;VAL1;VAL2\"")
# same result:
# add_compile_definitions("VAR0=VAL0;VAL1;VAl2")
Compilation:
cmake.exe --build C:/Users/user/build-CMakeSemicolonBug-Desktop_Qt_6_3_2_MSVC2019_64bit --target all --config Debug
[1/2 27.0/sec] Building CXX object CMakeFiles\CMakeSemicolonBug.dir\Debug\main.cpp.obj
FAILED: CMakeFiles/CMakeSemicolonBug.dir/Debug/main.cpp.obj
C:\PROGRA~2\MICROS~2\2019\PROFES~1\VC\Tools\MSVC\1429~1.301\bin\HostX64\x64\cl.exe /nologo /TP -DVAL1 -DVAL2" -DVAR0=\"VAL0 -DCMAKE_INTDIR=\"Debug\" -DQT_QML_DEBUG /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 -std:c++17 /showIncludes /FoCMakeFiles\CMakeSemicolonBug.dir\Debug\main.cpp.obj /FdCMakeFiles\CMakeSemicolonBug.dir\Debug\ /FS -c C:\Users\user\ViberProjectsShamotsin\CMakeSemicolonBug\main.cpp
cl : Command line error D8003 : missing source filename
ninja: build stopped: subcommand failed.
You may notice that the specified compile definitions are passed as:
-DVAL1 -DVAL2" -DVAR0=\"VAL0
You can also see this in the file generated by CMake C:/Users/user/build-CMakeSemicolonBug-Desktop_Qt_6_3_2_MSVC2019_64bit/CMakeFiles/impl-Debug.ninja :
# ...
DEFINES = -DVAL1 -DVAL2" -DVAR0=\"VAL0
# ...
Whereas I need it like this:
-DVAR0=\"VAL0;VAL1;VAL2\"
Those. it turns out that CMake in this case splits the passed string according to its rules for working with lists, where the separators are the characters ";". But is this correct behavior in this case?
How to fix:
If you change the command adding compile definitions to this form:
target_compile_definitions(CMakeSemicolonBug PRIVATE "VAR0=\"VAL0\;VAL1\;VAL2\"")
then everything works correctly and as I need, here is the corresponding fragment of the generated file C:/Users/user/build-CMakeSemicolonBug-Desktop_Qt_6_3_2_MSVC2019_64bit/CMakeFiles/impl-Debug.ninja :
# ...
DEFINES = -DVAR0=\"VAL0;VAL1;VAL2\"
# ...