add_compile_options and generator expressions not working as I expect
I'm having a problem getting add_compile_options
to do what I want with a certain generator expression.
I've got
set(COMPILING_WITH_GFORTRAN -w -std=legacy)
add_compile_options($<$<COMPILE_LANG_AND_ID:Fortran,GNU>:${COMPILING_WITH_GFORTRAN}>)
and this adds the jibberish "\$<1:-w" "-std=legacy>"
to the compilation command line.
Yet if I use the identical generator expression with target_compile_options
target_compile_options(foo PRIVATE $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:${COMPILING_WITH_GFORTRAN}>)
I get exactly what I expect, -w -std=legacy
, added to the compilation command line.
Am I doing something wrong here, or is this a cmake bug? I've tried several recent versions including 3.20.4. In my case I have a dozen executables that I'm creating and I do not want to have to do things individually for each target.
A related question is why in the generator expression I have to use ${COMPILING_WITH_GFORTRAN}
rather than just COMPILING_WITH_GFORTRAN
. The documentation examples suggest the latter should be sufficient, but both functions just add the literal COMPILING_WITH_GFORTRAN
to the compile command line without doing a variable substitution.