How do I output the result of a generator expression in CMake?

From the documentation

Since generator expressions are evaluated during generation of the buildsystem, and not during processing of CMakeLists.txt files, it is not possible to inspect their result with the message() command.

One possible way to generate debug messages is to add a custom target,

add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")

The shell command make genexdebug (invoked after execution of cmake) would then print the result of $<...>.

Another way is to write debug messages to a file:

file(GENERATE OUTPUT filename CONTENT "$<...>")

You can add a custom command that echos the value at build time. This is how I did it when I needed it:

add_custom_command(TARGET mytarget POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E echo 
  "target dir = $<TARGET_FILE_DIR:mytarget>")

How do I print the result of the evaluated generator expression during configuration?

You cannot. Generator expressions are intended for things, which are not exactly known at configuration stage: they depend on build type, which, in case of multiconfiguration generators, becomes known only at the build stage.

You may, however, save a value of the generator expression into the file, but the file will be written only at the end of the configuration stage:

file(GENERATE OUTPUT <filename> CONTENT <string-with-generator-expression>)

More detailed description of file(GENERATOR) see in documentation.

Tags:

Cmake