# Access properties via generator expressions

**URL:** https://discourse.cmake.org/t/access-properties-via-generator-expressions/2152
**Category:** Code
**Created:** [November 11, 2020, 7:57am UTC](https://discourse.cmake.org/t/access-properties-via-generator-expressions/2152 "2020-11-11T07:57:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DreamCaptain](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dreamcaptain/32/918_2.png) [@DreamCaptain](https://discourse.cmake.org/u/DreamCaptain)
#### Post date: [November 11, 2020, 7:57am UTC](https://discourse.cmake.org/t/access-properties-via-generator-expressions/2152/1 "2020-11-11T07:57:23Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [November 11, 2020, 4:14pm UTC](https://discourse.cmake.org/t/access-properties-via-generator-expressions/2152/2 "2020-11-11T16:14:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [November 12, 2020, 7:03am UTC](https://discourse.cmake.org/t/access-properties-via-generator-expressions/2152/3 "2020-11-12T07:03:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![DreamCaptain](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dreamcaptain/32/918_2.png) [@DreamCaptain](https://discourse.cmake.org/u/DreamCaptain)
#### Post date: [November 12, 2020, 7:39am UTC](https://discourse.cmake.org/t/access-properties-via-generator-expressions/2152/4 "2020-11-12T07:39:57Z")

</div>

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