Cc-rs cross compiling automake needs --host


I’m trying to cross-compile my rust application for the freebsd target from linux. I have everything (cross compilers, cargo configurations) configured so that simple rust applications compile and run fine on the target. Unfortunately, things get complicated when my dependencies have a build dependency on cc-rs.

When I need to cross-compile, I was doing:

$ cargo build --target=x86_64-unknown-freebsd

…but with the need to build C libraries, I needed to provide the CC binary path as well, as spelled out in the cc-rs README, so I added that to the command.

$ CC=/path/to/cc cargo build --target=86_64-unknown-freebsd

This runs for a bit, and bails out when it tries to build this dependency

running: "sh" "/home/builder/.cargo/registry/src/" "--with-pic" "--disable-multilib" "--disable-shared" "--disable-host-shared" "--host=x86_64-unknown-linux-gnu" "--build=x86_64-unknown-linux-gnu"
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for x86_64-unknown-linux-gnu-gcc... /home/builder/cross/bin/cc
checking for C compiler default output file name... a.out
checking whether the C compiler works...
--- stderr
configure: error: in '/home/builder/rust-program/target/debug/build/backtrace-sys-6e3910c5866618b7/out':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use '--host'.

(Here’s the kicker). If I run cargo build --target=x86_64-unknown-freebsd again without the CC variable, it succeeds, builds a binary for me, and that binary runs successfully on the target system.

Looking at config.log, it’s clear that we’re not passing target information down to the automake/autoconf that cc-rs seems to be using, as it still seems to think that the target is x86_64-unknown-linux-gnu. automake is compiling an a.out program, using the cross compiler, and because it doesn’t realize the target is different, it then tries to execute a.out and thinks that the compiler can’t make executables. I’ve tried passing TARGET as an env variable, trying to set it in CFLAGS, etc, to no avail.

I’ve got a workaround, but running the command twice, once with the CC variable and then again without it, isn’t very ergonomic or helpful. I suspect there’s a flag missing that I can’t find documentation for, and I’d really love some help tracking this down.