I have the following simple CMake code:
cmake_minimum_required(VERSION 3.1)
project(PrintGenerators)
set(TEST_OR_GENERATOR "$<$<OR:0,0,1>:YAY!>")
message(STATUS ${TEST_OR_GENERATOR}) # Print out the result of the build
I expect this code to print out YAY!
, but it does not. I instead get $<$<OR:0,0,1>:YAY!>
as the output. How do I print the result of the evaluated generator expression during configuration?
Introduction. Generator expressions are evaluated during build system generation to produce information specific to each build configuration.
A generator expression is an expression that returns a generator object. Basically, a generator function is a function that contains a yield statement and returns a generator object. The squares generator function returns a generator object that produces square numbers of integers from 0 to length - 1 .
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.
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>")
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 themessage()
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 ofcmake
) would then print the result of$<...>
.Another way is to write debug messages to a file:
file(GENERATE OUTPUT filename CONTENT "$<...>")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With