36

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?

4 Answers 4

57

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.

30

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>")
3
  • 1
    I was aware of this; the point of this question was to be able to print the value at configuration time. Commented Sep 7, 2018 at 16:18
  • What are the advantages of this answer over the accepted one?
    – Zingam
    Commented Dec 25, 2020 at 7:32
  • Is it can print $<CXX_COMPILER_ID>?
    – yode
    Commented Nov 13, 2023 at 6:17
12

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 "$<...>")
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.