Newlines silently cut off next values when passed to compiler flags or definitions
When a CMake variable containing a newline is passed as CXX_FLAGS or CXX_DEFINES, it forces the values that come after it to be placed in a new line. This causes generators like Make to not pass them to the compiler as they're no longer considered part of the variable in Makefile. No error nor warning is emitted, which makes such trivial to make bugs hard to spot.
Ninja generator has this bug too. I haven't checked others.
Consider the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.19.2)
execute_process(COMMAND echo 42 OUTPUT_VARIABLE buggy_value)
add_executable(exec main.cpp)
target_compile_definitions(exec PRIVATE MY_DEFINE=${buggy_value} _GLIBCXX_USE_CXX11_ABI=0)
and it's main.cpp file:
int main() {
return _GLIBCXX_USE_CXX11_ABI;
}
The above code won't compile unless the newline is removed from buggy_value. Exec's flags.make file looks like the following:
CXX_FLAGS =
CXX_DEFINES = -DMY_DEFINE=42
-D_GLIBCXX_USE_CXX11_ABI=0
CXX_INCLUDES =
Other places and variables (e.g. CXX_INCLUDES) can be affected too, but I haven't checked.
One could use OUTPUT_STRIP_TRAILING_WHITESPACE
, as @brad.king suggests in #21660 (closed), but forgetting to do so shouldn't result in a subtle bug like this one.