Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake generator expression is not evaluated

Tags:

cmake

I cannot understand what I'm doing wrong. I'm always getting the string "$<TARGET_FILE:tgt1>" instead of the path to the library.

I've created the dummy project.

Here is my root CMakeLists.txt

cmake_minimum_required (VERSION 3.0) # also tried 2.8 with the same result set(PROJECT_NAME CMP0026)  add_subdirectory(src)  set(TGT_PATH $<TARGET_FILE:tgt1>) message(STATUS "${TGT_PATH}") 

Here is my src/CMakeLists.txt

add_library(tgt1 a.c) 

File a.c is created and is empty

I've tried the following generators: Visual Studio 2013 Win64, Ninja and MingW Makefile. I've used Android toolchain for the last two, downloaded from here

I expect that the last message(STATUS command would print full path to the library. However, all variants print the string $<TARGET_FILE:tgt1>.

like image 783
wl2776 Avatar asked Feb 24 '15 10:02

wl2776


1 Answers

Generator expressions are not evaluated at configure time (when CMake is parsing CMakeLists, executing commands like add_target() or message() etc.). At this time, a generator expression is just a literal string - the character $ followed by <, then T, then ...

Evaluation of generator expressions happens at generate time (that's why they are called "generator expressions"). Generate time occurs after all CMake code is parsed and processed, and CMake is starting to act on the data therein to produce buildsystem files. Only then does it have all the information necessary to evaluate generator expressions.

So you can only really use generator expressions for things which occur at generate time or later (such as build time). A contrived example would be this:

add_custom_target(   GenexDemo   COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_FILE:tgt1>"   VERBATIM ) 

At configure time, CMake will record the literal string $<TARGET_FILE:tgt1> as the argument of COMMAND. Then at generate time (when the location of tgt1 is known for each configuration and guaranteed not to change any more), it will substitute it for the generator expression.

like image 129
Angew is no longer proud of SO Avatar answered Oct 04 '22 04:10

Angew is no longer proud of SO