[CMake] Problem with foreach() iteration over a list that uses generator expressions
Tadeusz A. Kadłubowski
tadeusz.kadlubowski at pix4d.com
Mon May 6 10:44:22 EDT 2019
Hi,
I apologize for initially sending this message to
cmake-developers at cmake.org, rather than here. I was confused and
mistook the proper mailing list.
I have an `INTERFACE IMPORTED` library target that depends on a bunch
of optional dependencies. Please see the snippet below:
cmake_minimum_required(VERSION 3.14)
add_library(example INTERFACE IMPORTED)
set_property(TARGET example PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Debug>:${undefined_dependency_one}
${undefined_dependency_two}>
)
It so happens that both of the optional dependencies
`${undefined_dependency_one}` and `${undefined_dependency_two}` are
set to empty/undefined on my system, which is perfectly fine.
I want to do some postprocessing to the dependencies of my target:
get_target_property(all_dependencies example INTERFACE_LINK_LIBRARIES)
foreach(lib IN LISTS all_dependencies)
message(STATUS "dependency on " ${lib})
endforeach()
This `foreach()` loop gets confused about the generator expression.
The output of the `message(STATUS ...)` calls in the snippet above is:
-- dependency on $<$<CONFIG:Debug>:
-- dependency on >
Please note that the generator expression got mangled into two pieces
by the `foreach()` loop. I was expecting to see a single line of
`$<$<CONFIG:Debug>:>`
I experimented with different versions of this test case and found
variants that do not get mangled in the `foreach()` loop. In a
modification when there is only one empty/undefined dependency the
`foreach()` loop works as expected.
set_property(TARGET example PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Debug>:${undefined_dependency_one}>
)
A modification in which each empty/undefined dependency is surrounded
with its own generator expression works correctly too:
set_property(TARGET example PROPERTY INTERFACE_LINK_LIBRARIES
$<$<CONFIG:Debug>:${undefined_dependency_one}>
$<$<CONFIG:Debug>:${undefined_dependency_two}>
)
Is that a bug in how CMake handles generator expressions? Am I
misunderstanding some CMake language concepts here?
(all code tested with CMake 3.14.3 on Ubuntu 18.04)
I would be grateful on any feedback about what's going on in that
`foreach` loop.
--
Tadeusz Andrzej Kadłubowski | Senior Software Engineer
Berlin, Germany
tadeusz.kadlubowski at pix4d.com
www.pix4d.com
--
Tadeusz Andrzej Kadłubowski | Senior Software Engineer
Berlin, Germany
tadeusz.kadlubowski at pix4d.com
www.pix4d.com
More information about the CMake
mailing list