# Is there DEPENDS property for custom targets?

**URL:** https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207
**Category:** Code
**Created:** [October 5, 2021, 8:20am UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207 "2021-10-05T08:20:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [October 5, 2021, 8:20am UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/1 "2021-10-05T08:20:00Z")

</div>

`add_custom_target()` has syntax like this:

```auto
add_custom_target(Name [ALL] [command1 [args1...]]
                  [COMMAND command2 [args2...] ...]
                  [DEPENDS depend depend depend ...]

```

[The Docs](https://cmake.org/cmake/help/latest/command/add_custom_target.html) say:

> `DEPENDS`  
> &nbsp;&nbsp;&nbsp;&nbsp;Reference files and outputs of custom commands created with `add_custom_command()` command calls in the same directory (`CMakeLists.txt` file). They will be brought up to date when the target is built.

Is there a property corresponding to this parameter? So I could do something like:

```cmake
add_custom_target(foo)
...
set_property(TARGET foo
  APPEND PROPERTY
    DEPENDS
    "${some_files}"
)

```

---

<div class="post-metadata">

### Author: ![krasznaa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/krasznaa/32/893_2.png) [@krasznaa](https://discourse.cmake.org/u/krasznaa)
#### Post date: [October 5, 2021, 11:19am UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/2 "2021-10-05T11:19:24Z")

</div>

Hi,

You could use [add\_dependencies(…)](https://cmake.org/cmake/help/latest/command/add_dependencies.html) to add dependencies to any type of target. Including custom ones. Like:

```cmake
add_custom_target(foo)
add_dependencies(foo bar)

```

However… Usually dependencies like this one are set up **between targets**. If you want to create a custom target that operates on files, and if those files change then the custom target’s command needs to be re-run, then the robust way of setting this up is:

```cmake
add_custom_command(
   OUTPUT "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/foo.bar"
   COMMAND super_processing_script.sh input1.txt input2.h
   DEPENDS input1.txt input2.h )
add_custom_target( foo_bar
   DEPENDS "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/foo.bar" )

```

It may not seem obvious at first why you would want to do this, but generating files using “custom commands” instead of with “custom targets” results in much more flexible code usually.

Cheers,  
Attila

---

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [October 5, 2021, 11:25am UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/3 "2021-10-05T11:25:33Z")

</div>

Thanks for the reply! Yes, I am aware that `add_dependencies()` works with targets, and that the usual approach is to pair `add_custom_command()` with `add_custom_target()`. However, I was asking specifically about the editable property.

The usecase I am after is to create a single target `codegen` at the beginning of CMakeLists that could be built via `cmake --build . --target codegen` , and then later in subfolders declare codegeneration `add_custom_command()`s and mark their output as a dependency for a single `codegen` target. I was hoping to achieve this without creating excessive targets, one per file / group of files.

The need for separate codegeneration target, in turn, is to be able to quickly run the codegeneration, acquire “missing” source files, and then run static code analysis tools without building the whole project.

---

<div class="post-metadata">

### Author: ![krasznaa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/krasznaa/32/893_2.png) [@krasznaa](https://discourse.cmake.org/u/krasznaa)
#### Post date: [October 5, 2021, 11:36am UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/4 "2021-10-05T11:36:43Z")

</div>

Ahh… 😃

I have no idea if this is the most ideal way of doing this or not, but I’ve implemented something like this in my own projects like:

```cmake
add_custom_target( InstallFiles ALL SOURCES
   $<TARGET_PROPERTY:InstallFiles,INSTALLED_FILES> )
add_custom_command(
   OUTPUT "foo/bar"
   COMMAND ... )
set_property( TARGET InstallFiles APPEND PROPERTY
   INSTALLED_FILES "foo/bar" )

```

The actual code is a bit more elaborate than this (creating some stamp files “in the middle”), but the logic is something like this. So I just introduced my own property on top of a custom target, and then used a generator expression to make the custom target depend on the files set up in that custom property.

Cheers,  
Attila

---

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [October 6, 2021, 12:11pm UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/5 "2021-10-06T12:11:59Z")

</div>

I marked this as a solution, since it indeed works on a test project - but unfortunately it is not the case with more complex file structures in older cmake ☹

Specifically, if `add_custom_target()` happens at one directory (say, main `CMakeLists.txt`), and the property is appended in another (included via `add_subdirectory()`), then configuration will fail:

```auto
CMake Error at CMakeLists.txt:5 (add_custom_target):
  Cannot find source file:
    /tmp/cm/sub/foo/bar

```

[This doc](https://cmake.org/cmake/help/latest/prop_sf/GENERATED.html) hints that `Changed in version 3.20: The GENERATED source file property is now visible in all directories`, so this might be the culprit.

In any case, for the solution above to actually work, one also needs `cmake_minimum_required(VERSION 3.20)`, and I am currently stuck with 3.14 😔

---

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [October 6, 2021, 12:14pm UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/6 "2021-10-06T12:14:48Z")

</div>

…on a related note: since it only works in 3.20+ anyway, it is probably a better idea to use [`target_sources()`](https://cmake.org/cmake/help/latest/command/target_sources.html) directly, instead of juggling with properties and generator expressions:

> New in version 3.20: `<target>` can be a custom target.

---

<div class="post-metadata">

### Author: ![krasznaa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/krasznaa/32/893_2.png) [@krasznaa](https://discourse.cmake.org/u/krasznaa)
#### Post date: [October 6, 2021, 12:22pm UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/7 "2021-10-06T12:22:17Z")

</div>

Weird… With absolute path names this setup has worked for me ever since CMake 3.11. 😕 (It probably should work with older versions as well, it’s just that that’s the first version that we used “for production”.)

Thanks for the info about `target_sources(...)`! That could indeed be a good replacement for my implementation in the future.

---

<div class="post-metadata">

### Author: ![Artalus](https://discourse.cmake.org/user_avatar/discourse.cmake.org/artalus/32/1527_2.png) [@Artalus](https://discourse.cmake.org/u/Artalus)
#### Post date: [October 6, 2021, 12:27pm UTC](https://discourse.cmake.org/t/is-there-depends-property-for-custom-targets/4207/8 "2021-10-06T12:27:30Z")

</div>

> [@krasznaa](#):
>
> With absolute path names this setup has worked for me ever since CMake 3.11

Weird indeed! My only wild guess at the moment - is that in those cases `set_property()` is called in the same directory as `add_custom_target()`. But if that is not so, then perhaps I am missing some point and it could actually work for me too.
