set_source_file_properties and generator expressions to support partial linking executables

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:

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

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?

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

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

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 property on my_exe and link to it using target_link_libraries?

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.

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

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.