# Is there a way to implement a feature similar to CMAKE\_MY\_VAR for setting properties?

**URL:** <https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196>\
**Category:** Code\
**Created:** [May 12, 2020, 1:18pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196 "2020-05-12T13:18:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [May 12, 2020, 1:18pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/1 "2020-05-12T13:18:07Z")

</div>

To disable some compile flags based on a target property, consumers of my library have to do something like the following:

```
find_package(Mylib)
add_executable(app_target)
target_link_libraries(app_target INTERFACE mylib::mylib)
set_target_properties(app_target PROPERTIES DISABLE_SPECIAL_FLAG 1)

```

and my library when created has something like the following

```
add_library(SHARED mylib)
set(genex_condition "$<NOT:$<BOOL:$<TARGET_PROPERTY:DISABLE_SPECIAL_FLAG>>>")
set(flags "$<${genex_condition}:-my-nice-flag>")
target_compile_options("mylib" INTERFACE "${flags}")

```

The above API forces users to explicitly set the _DISABLE\_SPECIAL\_FLAG_ property on each target that is directly linking against mylib (I’m aware that users can work around it by linking publically against their own interface library that sets it only once).

I’m wondering if there’s a way to implement something like _CMAKE\_AUTOMOC_, where if the _CMAKE\_AUTOMOC_ variable is set in a scope, all targets created below that variable get their _AUTOMOC_ property initialized to whatever is set in _CMAKE\_AUTOMOC_.

So I’d like the user to set a GLOBAL\_DISABLE\_SPECIAL\_FLAG variable to 1, and then the DISABLE\_SPECIAL\_FLAG property is automatically populated with 1 for all targets created below that statement.

Is there some way to implement that?

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [May 12, 2020, 1:37pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/2 "2020-05-12T13:37:13Z")

</div>

Variable-initialized properties are handled by CMake at the C++ level, with a hard-coded list. There is no way to update this list without changing CMake’s C++ code. If you would like such a feature, please open a bug report with a proposed solution.

---

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [May 12, 2020, 3:01pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/3 "2020-05-12T15:01:45Z")

</div>

Filed here [https://gitlab.kitware.com/cmake/cmake/-/issues/20698](https://gitlab.kitware.com/cmake/cmake/-/issues/20698)

---

<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:** [May 12, 2020, 8:22pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/4 "2020-05-12T20:22:08Z")

</div>

> [@alcroito](#):
>
> So I’d like the user to set a GLOBAL\_DISABLE\_SPECIAL\_FLAG variable to 1, and then the DISABLE\_SPECIAL\_FLAG property is automatically populated with 1 for all targets created below that statement.

This sounds like using `define_property(INHERITED)` to make `get_property` “chain up to the next higher scope when the requested property is not set in the scope given to the command.” (see [https://cmake.org/cmake/help/latest/command/define\_property.html](https://cmake.org/cmake/help/latest/command/define_property.html) for more details)

---

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [May 12, 2020, 9:28pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/5 "2020-05-12T21:28:41Z")

</div>

From what I’ve read, INHERITED only affects get\_property()-like calls, whereas i would need an inherited set\_property()-like call so that generator expressions can evaluate the appropriate values.

---

<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:** [May 13, 2020, 8:55am UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/6 "2020-05-13T08:55:30Z")

</div>

I guess an example would speak more: [cmake-topic-1196.zip](https://discourse.cmake.org/uploads/short-url/7fprVWPH9PfdyzGUmXAXLGWHHgA.zip) (2.8 KB)

This zip archive contains a small CMake project with a top-level `CMakeLists.txt` that defines `mylib`, and three sub-folders that each define an executable target.

- top-level `CMakeLists.txt`:

- `default/CMakeLists.txt`:

- `disable_special_flag/CMakeLists.txt`:

- `not_disable_special_flag/CMakeLists.txt`:

When you build that project, you’ll see that `-my-nice-flag` is passed to the compiler when building `appDefault` and `appNotDisabled`, but not when building `appDisabled`. On my Windows machine, this results in the following build output:

```auto
$ ~/dev/bin/cmake-3.4.0/bin/cmake --build . -- -v:m
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

cl : Command line warning D9002: ignoring unknown option '-my-nice-flag' [D:\dev\tmp\cmake-topic-1196\build\default\
appDefault.vcxproj]
  main.cpp
  appDefault.vcxproj -> D:\dev\tmp\cmake-topic-1196\build\default\Debug\appDefault.exe
  appDefault.vcxproj -> D:/dev/tmp/cmake-topic-1196/build/default/Debug/appDefault.pdb (Full PDB)
  main.cpp
  appDisabled.vcxproj -> D:\dev\tmp\cmake-topic-1196\build\disable_special_flag\Debug\appDisabled.exe
  appDisabled.vcxproj -> D:/dev/tmp/cmake-topic-1196/build/disable_special_flag/Debug/appDisabled.pdb (Full PDB)
cl : Command line warning D9002: ignoring unknown option '-my-nice-flag' [D:\dev\tmp\cmake-topic-1196\build\not_disa
ble_special_flag\appNotDisabled.vcxproj]
  main.cpp
  appNotDisabled.vcxproj -> D:\dev\tmp\cmake-topic-1196\build\not_disable_special_flag\Debug\appNotDisabled.exe
  appNotDisabled.vcxproj -> D:/dev/tmp/cmake-topic-1196/build/not_disable_special_flag/Debug/appNotDisabled.pdb (Ful
  l PDB)

```

Let me know if I missed something or if I’m totally off wrt. your use case.

---

<div class="post-metadata">

**Author:** ![alcroito](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alcroito/32/186_2.png) [@alcroito](https://discourse.cmake.org/u/alcroito)\
**Post date:** [May 13, 2020, 12:38pm UTC](https://discourse.cmake.org/t/is-there-a-way-to-implement-a-feature-similar-to-cmake-my-var-for-setting-properties/1196/7 "2020-05-13T12:38:11Z")

</div>

Indeed, thank you kindly for the example!

The missing piece of the puzzle for me was the usage of `set_directory_properties`.

Indeed in this case i could create a function called my\_namespaced\_disable\_special\_flag(), which would both define the inherited property, and set the the directory property.

The downsides to this approach that I see compared to how CMAKE\_AUTOMOC works (variable-initialized properties), is that with the inherited property, the considered property value can escape all the way to the global scope, and the ordering of calls is not respected.

Undesired case:

```
define_property(TARGET
  PROPERTY DISABLE_SPECIAL_FLAG
  INHERITED
  BRIEF_DOCS "Disable special flag"
  FULL_DOCS "When true, don't add -my-nice-flag to the compile options of the targets that depend on mylib."
)
set_directory_properties(PROPERTIES DISABLE_SPECIAL_FLAG 1)
add_subdirectory(d1)
set_directory_properties(PROPERTIES DISABLE_SPECIAL_FLAG 0)

```

The final property modification will still affect targets in d1 as if “0” is the value, whereas if i use a hypothetical

```
set(CMAKE_DISABLE_SPECIAL_FLAG 0)
add_subdirectory(d1)
set(CMAKE_DISABLE_SPECIAL_FLAG 1)

```

it would behave as expected.
