0

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>"
)
Topa
  • 90
  • 1
  • 8
  • [CMAKE_VERBOSE_MAKEFILE](https://cmake.org/cmake/help/latest/variable/CMAKE_VERBOSE_MAKEFILE.html) or [CMAKE_EXPORT_COMPILE_COMMANDS](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html) might help. – krisz Aug 12 '22 at 03:20
  • I wonder if it could have anything to do with enabling/specifying the languages in the project? See [the `project()` docs](https://cmake.org/cmake/help/latest/command/project.html). Make sure to specify `ASM` last. – starball Oct 27 '22 at 17:05

0 Answers0