35

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?

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88

4 Answers4

49

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
29

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>")
Bob Prystanek
  • 431
  • 3
  • 5
11

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 "$<...>")
user7610
  • 25,267
  • 15
  • 124
  • 150
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
1

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 write debug messages to a file with file(GENERATE):

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

reference

mbagher
  • 387
  • 4
  • 14