I'm trying to set a cmake variable to a list from the commandline (bash, but probably the same problem with other shells). This MRE script sort of describes my work flow (which is more complicated: I drive cmake from a makefile):
#!/bin/bash
# dummy programs
rm -rf a b a.cxx b.xx
touch a.cxx b.cxx
# cmake file
cat >CMakeLists.txt <<'EOF'
cmake_minimum_required( VERSION 3.20 )
project( args VERSION 1.0 )
option( PROGRAMS "list of programs" OFF )
if( PROGRAMS )
message( NOTICE "for programs: ${PROGRAMS}" )
else()
message( NOTICE "no programs: ${PROGRAMS}" )
endif()
foreach( program IN ITEMS ${PROGRAMS} )
message( NOTICE "program: ${program}" )
add_executable( ${program} ${program}.cxx )
target_compile_features( ${program} PRIVATE cxx_std_17 )
install( TARGETS ${program} DESTINATION . )
endforeach()
EOF
# drive cmake
programs="a b"
rm -rf build
mkdir -p build
cd build
set -x
cmake -D CMAKE_VERBOSE_MAKEFILE=ON \
-D PROGRAMS="${programs}" \
..
leads to:
for programs: a b
program: a b
CMake Error at CMakeLists.txt:13 (add_executable):
The target name "a b" is reserved or not valid for certain CMake features,
such as generator expressions, and may result in undefined behavior.
I'm pretty sure I need double quotes in the invocation of cmake, so how do I make cmake lose them in that variable?