# How to create file-dependency for all sources of target?

**URL:** https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706
**Category:** Code
**Created:** [February 26, 2020, 1:24pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706 "2020-02-26T13:24:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pboettch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pboettch/32/216_2.png) [@pboettch](https://discourse.cmake.org/u/pboettch)
#### Post date: [February 26, 2020, 1:24pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706/1 "2020-02-26T13:24:29Z")

</div>

I have different use-cases where I would like to declare a file-dependency at target-level, be it generated or in the sources, which influences the compile-process for all sources of this target.

Imagine a configuration-file for the compiler (added with `target_compile_options()`), or generated meta-information used for optimisations.

I’m now using `set_source_files_properties(.. PROPERTIES OBJECT_DEPENDS ...)` but this applies it globally to the source-file and thus is used in other target (where the same source file is compiled) and the dependency does not yet exist (and cannot, it is created in a later stage).

Is there something like `set_target_properties(.. ALL_SOURCES_DEPENDS ..)`?

---

<div class="post-metadata">

### Author: ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)
#### Post date: [February 26, 2020, 8:19pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706/2 "2020-02-26T20:19:36Z")

</div>

You should probably do this in two stages. Something like:

```auto
add_custom_command(
    OUTPUT "MyProj.conf"
    COMMAND generate_conf # first arg is a target or an executable in the PATH
    DEPENDS generate_conf # ... other targets, files
    )

add_custom_target(GenerateMyProjConf DEPENDS "MyProj.conf")

add_executable(MyProjExe Src1.cpp)
target_compile_options(MyProjExe PRIVATE -some_flag "${CMAKE_CURRENT_BINARY_DIR}/MyProj.conf")
add_dependencies(MyProjExe GenerateMyProjConf)

```

---

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [February 27, 2020, 12:13pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706/3 "2020-02-27T12:13:32Z")

</div>

Keep in mind the limitations of [add\_dependencies](https://cmake.org/cmake/help/latest/search.html?q=add_dependencies):

`Makes a top-level ``depend on other top-level targets to ensure that they build before``does.`  
…  
`See the DEPENDS option of add_custom_target() and add_custom_command() commands for adding file-level dependencies in custom rules. See the OBJECT_DEPENDS source file property to add file-level dependencies to object files.`

This means, `MyProjExe` is NOT rebuilt when `MyProj.conf` changed!  
Therfore you should probably use a combination of both your (Alex and Patrick) suggestions.

---

<div class="post-metadata">

### Author: ![pboettch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pboettch/32/216_2.png) [@pboettch](https://discourse.cmake.org/u/pboettch)
#### Post date: [February 27, 2020, 3:53pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706/4 "2020-02-27T15:53:20Z")

</div>

If the a dependency to MyProj.conf is changed, MyProj.conf is regenerated, but Src1.cpp is not recompiled. Unfortunately this is not the solution.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 28, 2020, 8:38pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706/5 "2020-02-28T20:38:51Z")

</div>

`OBJECT_DEPENDS` does sound like the right thing, but as you noticed, source file properties apply to all usages of the file. I suggest somehow giving the file another name so that CMake doesn’t see it as the same file (copy it during the build then compile that file per target, use a symlink, something even more creative perhaps). I’d also like to see source file/target combo properties, but I don’t know how to specify it. Maybe `$<$<STREQUAL:$<TARGETI_NAME>,WithConf>:-flag ${CONF}>` works?

---

<div class="post-metadata">

### Author: ![pboettch](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pboettch/32/216_2.png) [@pboettch](https://discourse.cmake.org/u/pboettch)
#### Post date: [March 5, 2020, 3:59pm UTC](https://discourse.cmake.org/t/how-to-create-file-dependency-for-all-sources-of-target/706/6 "2020-03-05T15:59:15Z")

</div>

Copying the file during build came to my mind as well.

And I think I know why there is no way to have this feature: How would you write this in a GNU Makefile?
