Ninja: Newline in custom command argument produces Ninja syntax error
The following CMakeLists.txt produces an invalid ninja.build file:
cmake_minimum_required(VERSION 3.20)
project(NMC-bug)
add_executable(generator main.cpp)
set(bad_string "123\n456")
add_custom_command(
OUTPUT gen.txt
COMMAND generator "${bad_string}" > gen.txt
)
add_custom_target(
gen.txt.update
ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/gen.txt"
)
main.cpp
can just be an empty main() function. Doesn't matter what's in it. Here's what I see when I try to run this:
D:\nmc>cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
-- The C compiler identification is MSVC 19.28.29913.0
-- The CXX compiler identification is MSVC 19.28.29913.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx86/x86/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/nmc/build
D:\nmc>cmake --build build
ninja: error: build.ninja:115: expected '=', got lexing error
456 > gen.txt"
^ near here
Inside build.ninja, I see:
build gen.txt: CUSTOM_COMMAND || generator.exe
COMMAND = cmd.exe /C "cd /D D:\nmc\build && D:\nmc\build\generator.exe 123
456 > gen.txt"
DESC = Generating gen.txt
restat = 1
This issue also affects Ninja Multi-Config.
Edited by Brad King