This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.
Cross compiling Rust -- Fixed
Set up rust build environment
rustup toolchain install nightly
rustup component add rust-src --toolchain nightly
apt install {binutils,gcc}-mips-linux-gnu
Choose glibc or musl as your libc
You can use glibc, but it’s not worked as well for me as using musl. I got some missing symbols when trying this on larger programs with glibc. So I recommend using musl.
mips-unknown-linux-musl
is with musl libc, and
mips-unknown-linux-gnu
is glibc.
Optional: Set up for glibc
I needed to create a special target for glibc, to turn on
soft-float
. soft-float
seems on by default in
mips-unknown-linux-musl
.
rustc +nightly \
-Z unstable-options \
--print target-spec-json \
--target mips-unknown-linux-gnu \
> mips-nofloat-linux-gnu.json
Then edit mips-nofloat-linux-gnu.json
- Change
is-builtin
tofalse
- add
+soft-float
tofeatures
list.
Create test project
cargo new foo
cd foo
Configure linker
mkdir .cargo
cat > .cargo/config.toml
[target.'cfg(target_arch="mips")']
linker = "mips-linux-gnu-gcc"
^D
Build
Using musl (worked better for me):
cargo +nightly build --release -Zbuild-std --target mips-unknown-linux-musl.json
Using glibc (requires the optional step above):
cargo +nightly build --release -Zbuild-std --target mips-nofloat-linux-gnu.json
Change the “interpreter” to what the Ubiquiti system expects
cd target/mips-unknown-linux-musl/release # or -gnu, if using glibc
patchelf \
--remove-needed ld.so.1 \
--set-interpreter /lib/ld-musl-mips-sf.so.1 \
foo
Does it work?
$ ./foo
Hello, world!
Yay!
Links
- https://doc.rust-lang.org/rustc/targets/custom.html
- https://doc.rust-lang.org/cargo/reference/config.html