Access properties via generator expressions

Hi,
I need a way to conditionally mark some include directories as PRIVATE or PUBLIC depending on global property.

here’s the code i use now that explicitly applies target property if global property is set:

if(GLOBAL_FOO)
    set_target_properties(mytarget PROPERTIES FOO ON)
endif()

target_include_directories(mytarget
    PRIVATE
       $<$<NOT:$<BOOL:$<TARGET_PROPERTY:mytarget,FOO>>>:${CMAKE_CURRENT_SOURCE_DIR}/headers>
    PUBLIC
       $<$<BOOL:$<TARGET_PROPERTY:mytarget,FOO>>:${CMAKE_CURRENT_SOURCE_DIR}/headers>


)

Is there a better way to access global properties via generator expressions?

$<BOOL:${GLOBAL_FOO}> should do what you want.

That will work in the OP’s literal case, where the variable is set to its final value before the target_include_directories() call, and in the same scope. If that mirrors the OP’s real use case, it’s perfectly fine.

It wouldn’t work if in the real code, the variable could be set later/elsewhere, or change value after the target_include_directories() call. In such case, setting a property on a target (as the OP is currently doing) is the only way to make it work of which I know, and I use the same approach in our projects at work.

Thanks, seems like i’ve been taking wrong approach to expose private headers for unit testing.