CMAKE extract generator expression with compile language

145 Views Asked by At

In CMake, how can I print a generator expression that has COMPILE_LANGUAGE values? Also, how can I extract the values for a specific COMPILE_LANGUAGE?

Below is an example of what I need. The first case doesn't have COMPILE_LANGUAGE and works. The second case has a COMPILE_LANGUAGE value and I get Error evaluating generator expression: $<COMPILE_LANGUAGE:C>. I am guessing it is because CMake tries to expand $<COMPILE_LANGUAGE:C to a string which it cannot.

cmake_minimum_required(VERSION 3.20)
project(example)

# This works
add_library(example1 INTERFACE)
set(compile_options "-O2")
target_compile_options(example1 INTERFACE
    ${compile_options}
)
add_custom_target(example1_print ALL
    COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_PROPERTY:example1,INTERFACE_COMPILE_OPTIONS>"
)

# This does NOT work (COMPILE_LANGUAGE)
add_library(example2 INTERFACE)
set(c_compile_options "-O2")
set(asm_compile_options "assembler-with-cpp")
target_compile_options(example2 INTERFACE
    $<$<COMPILE_LANGUAGE:C>:${c_compile_options}>
    $<$<COMPILE_LANGUAGE:ASM>:${asm_compile_options}>
)
add_custom_target(example2_print ALL
    COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_PROPERTY:example2,INTERFACE_COMPILE_OPTIONS>"
)
0

There are 0 best solutions below