For those who were brought here from How do I split a CMake generator expression to multiple lines? I would like to add some notes.
The line continuation method will not work, CMake cannot parse a generator list made with whitespace (indentation) and line continuation.
While the string(CONCAT) solution will provide a generator expression that can be evaluated, the evaluated expression will be surrounded by quotes if the result contains a space.
For each individual option to be added a separate generator list must be constructed, so stacking options like I have done in the following will cause the build to fail:
string(CONCAT WARNING_OPTIONS "$<"
"$<OR:"
"$<CXX_COMPILER_ID:MSVC>,"
"$<STREQUAL:${CMAKE_CXX_SIMULATE_ID},MSVC>"
">:"
"/D_CRT_SECURE_NO_WARNINGS "
">$<"
"$<AND:"
"$<CXX_COMPILER_ID:Clang,GNU>,"
"$<NOT:$<STREQUAL:${CMAKE_CXX_SIMULATE_ID},MSVC>>"
">:"
"-Wall -Werror "
">$<"
"$<CXX_COMPILER_ID:GNU>:"
"-Wno-multichar -Wno-sign-compare "
">")
add_compile_options(${WARNING_OPTIONS})
This is because the resulting options are passed to the compiler in quotes
/usr/lib64/ccache/c++ -DGTEST_CREATE_SHARED_LIBRARY=1 -Dgtest_EXPORTS -I../ThirdParty/googletest/googletest/include -I../ThirdParty/googletest/googletest -std=c++11 -fno-rtti -fno-exceptions -fPIC -std=c++11 -fno-rtti -fno-exceptions -Wall -Wshadow -DGTEST_HAS_PTHREAD=1 -fexceptions -Wextra -Wno-unused-parameter -Wno-missing-field-initializers "-Wall -Werror -Wno-multichar -Wno-sign-compare " -fdiagnostics-color -MD -MT ThirdParty/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o -MF ThirdParty/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o.d -o ThirdParty/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o -c ../ThirdParty/googletest/googletest/src/gtest-all.cc
c++: error: unrecognized command line option ‘-Wall -Werror -Wno-multichar -Wno-sign-compare ’
To evaluate lengthy generator expressions represented using the string(CONCAT) solution, each generator expression must evaluate to a single option with no spaces:
string(CONCAT WALL "$<"
"$<AND:"
"$<CXX_COMPILER_ID:Clang,GNU>,"
"$<NOT:$<STREQUAL:${CMAKE_CXX_SIMULATE_ID},MSVC>>"
">:"
"-Wall"
">")
string(CONCAT WERROR "$<"
"$<AND:"
"$<CXX_COMPILER_ID:Clang,GNU>,"
"$<NOT:$<STREQUAL:${CMAKE_CXX_SIMULATE_ID},MSVC>>"
">:"
"-Werror"
">")
message(STATUS "Warning Options: " ${WALL} ${WERROR})
add_compile_options(${WALL} ${WERROR})
This may be unrelated to the question I am posting an answer to, unfortunately the question I am answering is wrongfully marked as a duplicate of this question.
Generator lists are not handled and parsed the same way as strings are, and because of this, there are additional measures one must take to split a generator list across multiple lines.
message("This is the value of the variable: "
– munsingh Jun 25 '20 at 11:33"${varValue}")