How to integrate Cargo with another build system?

When compiling I need to do pre- and post-link tasks. Things like adding linker args, changing what's in the output directory, manipulating the final binary, etc.

There doesn't seem to be an easy way to hook in to Cargo so I was wondering if there's a way to do the opposite and tie Cargo into another build system?

Well you can always call cargo build from another build system?

Sure. But I don't understand how you use that to set linker args and get final binaries and all that.

Linker arguments can be set through the RUSTFLAGS environment variable, e.g.

RUSTFLAGS="-C link-arg=-your-linker-argument" cargo build

Another option is to call rustc directly. You can call cargo build --verbose to see what rustc arguments that cargo uses.

The final binary is written to target/{debug,release}/package_name

I was hoping someone had a ready made solution because it sounds like I'd need to implement a lot myself.

It seems like I'd have to mess was environment variables (being careful of clobbering existing ones) and use something like:

cargo build --message-format json

And parse the json returned to find the final artefacts that are built. Between cargo.toml, .cargo/config and build scripts there are just too many things to take into account manually so I think I'd need to be querying Cargo itself to know what it's actually done.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.