# set\_source\_file\_properties and generator expressions to support partial linking executables

**URL:** <https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932>\
**Category:** Usage\
**Created:** [January 25, 2024, 1:53pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932 "2024-01-25T13:53:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [January 25, 2024, 1:53pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932/1 "2024-01-25T13:53:46Z")

</div>

In my CMake project I’m trying to build an executable that is partially linked and then goes through a secondary link step later in the build process.

This is fairly typical strategy when dealing with ARINC-653 embedded OS. First you build an executable in the typical fashion but is partially linked, then after processing the OS settings a `.s` and `.o` is created, the executable is relinked with the generated files. This allows the executable to be reused when building multiple final images with different OS settings.

The pseudo code looks something like:

```auto
add_executable(my_exe a.c b.c)
#link option for partial linking
target_link_options(my_exe PRIVATE -relobj)

add_executable(final_exe)
target_sources(final_exe PRIVATE "$<TARGET_FILE:my_exe>" os_gen.o os_gen.s)
add_dependencies(final_exe my_exe)

```

The first roadblock was an “Cannot file source file: /full/path/to/my\_exe” error.

What I tried was to add `set_source_file_properties( "$<TARGET_FILE:my_exe>" PROPERTIES GENERATED ON)`. But this didn’t work because it seems that `set_source_file_properties` doesn’t allow generator expressions. Everything works if I use `set_source_file_properties( "/full/path/to/my_exe" PROPERTIES GENERATED ON)`.

Any ideas on how to set the GENERATED property set on the partially linked executable file without hardcoding the path name?

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [January 25, 2024, 3:43pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932/2 "2024-01-25T15:43:34Z")

</div>

So far the best I could come up with is:  
`set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/my_exe" PROPERTIES GENERATED ON)`.

---

<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:** [January 26, 2024, 1:56pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932/3 "2024-01-26T13:56:29Z")

</div>

I feel like this needs CMake to be aware of what is going on to be truly reliable.

> [@fdk17](#):
>
> But this didn’t work because it seems that `set_source_file_properties` doesn’t allow generator expressions.

In that position, nothing does. Generator expression support typically corresponds to the values of the properties being set, not the entities or property names used in the call.

Does it work if you enable the [`ENABLE_EXPORTS`](https://cmake.org/cmake/help/latest/prop_tgt/ENABLE_EXPORTS.html) property on `my_exe` and link to it using `target_link_libraries`?

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [January 27, 2024, 3:01pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932/4 "2024-01-27T15:01:02Z")

</div>

`ENABLE_EXPORTS` with using `target_link_libraries()` almost worked, but I think there is an issue with the Green Hills generator.

The `final_exe` generated project file picks up the `my_exe` settings from `target_link_options(my_exe PUBLIC)`. But it does not have any reference to the actual my\_exe file.

Since `target_link_libraries()` did not include the object file, I restored the `target_sources(final_exe PRIVATE "$<TARGET_FILE:my_exe>" os_gen.o os_gen.s)` to inject the file into the generated project file. Unforetunately, with `ENABLE_EXPORTS` set to `ON`, `target_sources` does not know that the file `"$<TARGET_FILE:my_exe>"` is generated.

With `ENABLE_EXPORTS` enabled I think CMake should have known that `"$<TARGET_FILE:my_exe>"` is a generated file. I think this is a CMake deficiency and should have worked per the `GENERATED` documentation.

---

<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 4, 2024, 1:34pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932/5 "2024-02-04T13:34:00Z")

</div>

What happens if you use `target_link_libraries` to use `my_exe` rather than trying to treat it as a source file?

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [February 5, 2024, 3:23pm UTC](https://discourse.cmake.org/t/set-source-file-properties-and-generator-expressions-to-support-partial-linking-executables/9932/6 "2024-02-05T15:23:41Z")

</div>

I edited my post to clarify I did try using `target_link_libraries()`, with the Green Hills MULTI generator it did not quite work. Using `target_link_libraries()` and `ENABLE_EXPORTS` does simplify what I am trying to do with more complicated executables that need to propagate linker options.
