COMPILE_LANGUAGE genex is evaluated differently in different occurrences.
Generator expression $<COMPILE_LANGUAGE>
(and possibly other similar generator expressions) seems to be evaluated to different values when being using in different places, e.g. in calculating a target's compile definitions and in being used in add_custom_target
. This seems to be stably reproducible on CMake 3.24.
Suppose there's a repository with two files:
./test1.cpp
#if defined(FOO)
#warning "1"
#endif
int main(){}
./CMakeLists.txt
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(test1)
add_executable(test1 test1.cpp)
target_compile_definitions(test1 PRIVATE BAR $<$<COMPILE_LANGUAGE:CXX>:FOO>
add_custom_target(genexdebug ALL COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_PROPERTY:test1,COMPILE_DEFINITIONS>")
I then build the project with the following command, under a Linux environment with GCC as the compiler.
mkdir ./build && cd ./build
cmake ..
cmake --build .
Ideally, the output of genexdebug
would be the same as the compile definitions of target test1
, a.k.a. BAR;FOO
. However, the output seems to be different, and it was like:
[ 50%] Building CXX object CMakeFiles/test1.dir/test1.cpp.o
/home/gnaggnoyil/workspace/test_proj4/test1.cpp:2:2: warning: #warning "1" [-Wcpp]
2 | #warning "1"
| ^~~~~~~
[100%] Linking CXX executable test1
[100%] Built target test1
BAR
[100%] Built target genexdebug
The output of genexdebug
was BAR
instead of BAR;FOO
, while target test1
indeed has had proper compile definitions applied.
Is this intended behavior? I cannot find any places in the documentation that says the above real behavior would occur.