How to propagate a property from a lib to a executable

I am in a situation where I build libraries which might set some properties, and the I build a executable which will need to agregate all the lib properties to actually use/process them.
How can I achieve this ?

(one reason is for managing assets. some files are defined at library level because it is where they logically belongs to, however in general we can’t process them at this level. for example, for Windows, we want to copy the files in the executable path $<TARGET_FILE_DIR:${target}> which I can’t know when building the lib; on WebAsssembly I want to add a linker command with -preload-files, …)

I don’t believe there is a way to do this generically. It would need for CMake to support user-specified usage requirements that get inherited.

I think a better process is to:

  • have libraries find themselves via dladdr or related and find assets based on that (e.g., compute the path between the library install and asset install directories and compile that in and make the build tree structure mirror the install tree so that the relative path traversal is preserved)
  • for WASM, do something like:
set(is_exe "$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>")
set(is_wasm "$<PLATFORM_ID:WebAssembly>") # adapt as needed
target_link_options(lib_with_assets
  INTERFACE
    "$<$<AND:${is_exe},${is_wasm}>,LINKER:-preload-files;…>"

You may need separate BUILD_INTERFACE (an absolute path) and INSTALL_INTERFACE logic (traverse from TARGET_FILE_DIR).

If you need to relocate things, keep the relative structure intact and use packaging logic to move things as cohesive units.

1 Like

Thank you for the reply.
I have also other platforms like ios to handle which requires as well a specific treatment like:
set_source_files_properties(... PROPERTIES MACOSX_PACKAGE_LOCATION Resources)

Finally I did it differently. I decided to embed the resource inside the binary and used battery::embed for this. That works really well and is not platform dependent.