How to do autotools VPATH builds with Rust

64 Views Asked by At

I am working on a library project which mixes C, C++, and Rust code. For historical reasons I use autotools to drive building.

Essentially, my Makefile.am looks like this:

libfoo_la_LIBADD += rustfoo/target/release/librustfoo.a

rustfoo/target/release/librustfoo.a:
    cd rustfoo/; \
    cargo rustc --release -- --crate-type staticlib

dist-hook:
    mkdir -p $(distdir)/rustfoo
    cp -a $(srcdir)/rustfoo/* $(distdir)/rustfoo
    rm -rf $(distdir)/rustfoo/target

Notice, that it is not practical to enumerate all Rust source files in some ....librustfoo_a_SOURCES variable or similar. (Mainly because, I bundle some dependencies in rustfoo/dependencies/.)

All works fine for non-VPATH builds, but fails as expected for VPATH build. Autotools does not know about the rust sources and cannot copy/link them into the $(builddir). Sure, I could cd into the source tree as

 rustfoo/target/release/librustfoo.a:
    cd $(srcdir)/rustfoo/; ...

but that would defeat the purpose of VPATH builds.

Is there any best practices how to approach this?

1

There are 1 best solutions below

0
Jose Gracia On

I actually found a solution relying on cargo which works for me at the moment. But I am still interested in a more autotoolish way to accomplish this.

I don't know how to tell autotools what the Rust sources are, but it turns out to be easy to tell Rust where it should put its compilation outputs (--target-dir). When building .../librustfoo.a, cd into the $(srcdir) and redirect outputs into the appropriate target directory in the build tree as

rustfoo/target/release/librustfoo.a:
    cd $(srcdir)/rustfoo/; \
    cargo rustc --release -- --crate-type staticlib --target-dir $(abs_builddir)/rustfoo/target/