What do Rust tools need from the build system?

guys thank you so so much for answering. I didn’t know a lot of things that i’ve read here through your posts. lots of useful information. i want to learn more while taking hgh and not doing anythign at all. do you mind if i am going to have some other questions for you people later? i see that you are really good in these things. thanks a lot for everything again!

1 Like

There are certain workflows that require querying the full rustc command cargo uses to build the current crate; it would be nice to have a way to make this query from within Rust itself!

Gradle integration would be nice… =) Not sure what this requires from Rust.

A year or so ago I added Rust support to Tundra https://github.com/deplinenoise/tundra

Tundra is written in C++ and uses Lua as a description lang for the build setup. I added so you can integrate RustCrates, RustPrograms etc to be used next to C/C++ libs directly.

To setup a C library you do:

StaticLibrary {
	Name = "test_lib",
	Sources = { "native_lib/lib.c" },
}

This will create a lib called test_lib and compiles the file lib.c

Now to build a Rust program that depends on this one does

RustProgram {
	Name = "prog",
	CargoConfig = "prog/Cargo.toml",
	Sources = { "prog/src/main.rs", "prog/build.rs" },
	Depends = { "test_lib" },
}

My problem here is how do I tell Cargo about this dependency? The only way I found out really was to use env variables so it would do something like this

export TUNDRA_OBJECTDIR=t2-output/macosx-gcc-debug-default ; 
export CARGO_TARGET_DIR=t2-output/macosx-gcc-debug-default/__prog ; 
export TUNDRA_STATIC_LIBS="test_lib" ; cargo build --manifest-path=prog/Cargo.toml

Then inside build.rs I have to take the TUNDRA_STATIC_LIBS apart to tell Cargo what libs to link with:

use std::env;

fn main() {
    let tundra_dir = env::var("TUNDRA_OBJECTDIR").unwrap_or("".to_string());
    let libs = env::var("TUNDRA_STATIC_LIBS").unwrap_or("".to_string());

    let native_libs = libs.split(" ");

    println!("cargo:rustc-link-search=native={}", tundra_dir);

    for lib in native_libs {
        println!("cargo:rustc-link-lib=static={}", lib);
        println!("cargo:rerun-if-changed={}", lib);
    }
}

So while this works it’s slightly annoying. It would be great if this could have been passed to Cargo as general arguments instead because now I have to rely on build.rs that takes this in. In practice it hasn’t been a major issue but this could be improved.

2 Likes

It’d be nice if we could get something merged into cmake so that Rust is somewhat built-in instead of having to use add_custom_target() and add_custom_command().

That said, I’m currently rewriting most of my FFI guide (rendered) and found integrating cargo into a wider build system to be incredibly easy. The only real issue I found was making sure the compiled library has some canonical path that the main application can link to regardless of if you’re doing a Debug or Release build. And that’s probably more due to my inexperience with cmake than anything else.

Nice work guys! :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.