Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

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?

like image 498
Arnav Borborah Avatar asked Jul 15 '18 23:07

Arnav Borborah


People also ask

What is generator expression in CMake?

Introduction. Generator expressions are evaluated during build system generation to produce information specific to each build configuration.

What are generator expressions in Python?

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 .


Video Answer


3 Answers

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.

like image 167
Tsyvarev Avatar answered Oct 08 '22 01:10

Tsyvarev


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>")
like image 24
Bob Prystanek Avatar answered Oct 08 '22 00:10

Bob Prystanek


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 "$<...>")
like image 10
Mark Ingram Avatar answered Oct 07 '22 23:10

Mark Ingram