Extract linker args for dependencies

I have a setup where an external tool (cargo) produces a shared library, but it needs to be able to link dependencies generated by CMake. My current setup looks like this:

add_custom_target(${target_name} ALL
  # This needs to contain `-l`/`-L` arguments
  COMMENT "start cargo for ${target_name} with '${cargo_cmd}' dynamic"

# `foo` and `bar` are created via `add_library` in some other file,
# also has some `target_link_libraryes` set
add_dependencies(${target_name} foo bar)

My problem is that the add_custom_target command needs to be passed -l and -L linker args to correctly link libfoo and libbar, including their transitive dependencies. This seems like a case for generator expressions, but I don’t see any on this page that helps locate information about dependencies.

I also cannot query anything with get_target_property before the dependency is added.

Ideally I would have something simple that expands to -Lnative=/build/foo-path -Lnative=/build/bar-path -lstatic=foo -lstatic=bar, or just the full library paths to pass to the linker.

What is the best way to go about this? My current solution is to hand-write library output paths, but this isn’t very maintainable.

It’s a bit annoying but you can:

  • write a target that links like the Rust target should
  • set the <LANG>_COMPILER_LAUNCHER property on that target and capture the relevant bits and write it to a file
  • read that file and pass it to the cargo command as you see fit