Running commands on all targets linked against another target

I have some binaries that are linked against some libraries most are now set to be interface libs.

I’d like to add some extra postbuild commands to these targets that fix rpath stuff on mac os for instance. I can write a function that I call on my target ofcourse that runs a few add_custom_commands to fix everything. But I’d prefer this to happen automagically when I link against a library that would require this.

So far I haven’t found an option to do this but I’m no CMake expert…

I would initially look at the rpath handling logic that CMake has to solve this issue, instead of rolling your own.

If you actual use case is more extensive, you might be able to use a POST_BUILD step on your executables that does your fix up. To get the set of libraries you have linked against you could look at file(GET_RUNTIME_DEPENDENCIES ( ).

Such as? If your dependencies are using @rpath/, you’ll need to add the appropriate path for them to your INSTALL_RPATH property. I want CMake to eventually help figure this out, but it’s not there yet. If you have libraries which have relative path library IDs, I suggest fixing them in the upstream; those libraries are basically just broken.

If you want to rewrite things to use @executable_path/ and @loader_path/, I suggest setting up the INSTALL_NAME_DIR property to do that correctly. On install, you can use file(GET_RUNTIME_DEPENDENCIES) to find dependent libraries and when you install those that you want to install, you can fix them up for the install tree there (plans for helper functions to do this is in my future roadmap, but there’s no schedule right now).

Yes, macOS shared libraries are complicated. Some things have good solutions, but others (mainly linking to @rpath/-using libraries) aren’t so well-done. I have thoughts for things like INTERFACE_RPATH and such, but haven’t had time to tackle it.

Ok I probably have to dig deeper into fixing this on another level.

But on windows I have similar concerns that I need to have my dll files next to my binaries and I don’t really want to install first before doing this. What is the best way to assure this? Again I would think of a postbuild step that is inherited because I link against such library but I have no idea how to accomplish this with CMake (yet)

Well, one other thing that would likely come along would be INTERFACE_RUNTIME_PATHS which would give you PATH entries for using a library (mainly only of use on Windows; RPATH should be used where available).

You could run file(GET_RUNTIME_DEPENDENCIES) as part of the build to do the copying. You’ll need to give the list of PATH entries to search, but this would allow you to stuff everything into a single place rather than adding a bunch of path entries. I’d probably run it as a separate add_custom_command myself, but that’s me.