linuxrustshared-librariescross-compilinglego-mindstorms-ev3

Cross compile shared library for armv5te-unknown-linux-gnueabi Rust [Mindstorm Ev3dev]


Parameters:

(The target is a Lego Mindstorm running a linux image from Ev3dev)

Cargo Configuration:

[package]
name = "ev3"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jni = "0.19"
ev3dev-lang-rust = { version = "0.12.1", features=["screen"]}
jni_proc_macro= {path= "./jni_proc_macro"}

[lib]
crate-type= ["cdylib"]

[workspace]
members= ["jni_proc_macro"]

Build Configuration:

[build]
target = "armv5te-unknown-linux-gnueabi"

[target.armv5te-unknown-linux-gnueabi]
linker = "rust-lld"

Build Error:

error: linking with `rust-lld` failed: exit code: 1
  |
  = note: {...}
= note:   rust-lld: error: unable to find library -lgcc_s
          rust-lld: error: unable to find library -lutil
          rust-lld: error: unable to find library -lrt
          rust-lld: error: unable to find library -lpthread
          rust-lld: error: unable to find library -lm
          rust-lld: error: unable to find library -ldl
          rust-lld: error: unable to find library -lc


error: could not compile `ev3` due to previous error

As the error suggests the linker is missing libraries. I found no clear solution where I can download and or provide these dependencies.

My question is, A is there a diffrent way to build this successfully or B how do I solve these dependencies.

The result needs to be a shared library (.so) for linux and armv5te


Solution

  • Requirements

    (everthing is done in wsl/linux)

    Prep/Build

    Install cross on cargo

    cargo install cross --git https://github.com/cross-rs/cross
    

    Install docker
    Clone the cross repository
    Navigate into the docker folder
    Create a new file with the name "Dockerfile.armv5te-unknown-linux-gnueabi-cross"
    Paste this in the new file:

    FROM ubuntu:16.04
    ARG DEBIAN_FRONTEND=noninteractive
    
    COPY common.sh lib.sh /
    RUN /common.sh
    
    COPY cmake.sh /
    RUN /cmake.sh
    
    COPY xargo.sh /
    RUN /xargo.sh
    
    RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
        g++-arm-linux-gnueabi \
        crossbuild-essential-armel \
        libc6-dev-armel-cross
    
    COPY deny-debian-packages.sh /
    RUN TARGET_ARCH=armel /deny-debian-packages.sh \
        binutils \
        binutils-arm-linux-gnueabi
    
    # Qemu is disabled since we've changed the scripts to require newer Python versions.
    #COPY qemu.sh /
    #RUN /qemu.sh arm
    
    COPY qemu-runner base-runner.sh /
    
    ENV CROSS_TOOLCHAIN_PREFIX=arm-linux-gnueabi-
    ENV CROSS_SYSROOT=/usr/arm-linux-gnueabi
    ENV CARGO_TARGET_ARMV5TE_UNKNOWN_LINUX_GNUEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
        CARGO_TARGET_ARMV5TE_UNKNOWN_LINUX_GNUEABI_RUNNER="/qemu-runner arm" \
        AR_armv5te_unknown_linux_gnueabi="$CROSS_TOOLCHAIN_PREFIX"ar \
        CC_armv5te_unknown_linux_gnueabi="$CROSS_TOOLCHAIN_PREFIX"gcc \
        CXX_armv5te_unknown_linux_gnueabi="$CROSS_TOOLCHAIN_PREFIX"g++ \
        BINDGEN_EXTRA_CLANG_ARGS_armv5te_unknown_linux_gnueabi="--sysroot=$CROSS_SYSROOT" \
        QEMU_LD_PREFIX="$CROSS_SYSROOT" \
        RUST_TEST_THREADS=1 \
        PKG_CONFIG_PATH="/usr/lib/arm-linux-gnueabi/pkgconfig/:${PKG_CONFIG_PATH}"
    

    Make sure the project uses "LF" newlines. if not this fixes it.
    Compile the custom cross/docker build using the following command in the root of the cloned repository:

    cargo build-docker-image armv5te-unknown-linux-gnueabi-cross
    

    This will create a new docker image that will be used to compile the rust code.
    Then navigate to your target project folder and run:

    export CROSS_TARGET_ARMV5TE_UNKNOWN_LINUX_GNUEABI_IMAGE=ghcr.io/cross-rs/armv5te-unknown-linux-gnueabi-cross:local
    

    (Do not close this terminal)
    Now add the following to the Cargo.toml file:

    [package.metadata.cross.build]
    default-target = "armv5te-unknown-linux-gnueabi"
    

    now you can run:

    cross build 
    

    Many cargo options like "--release" can be used (for more info have a look at cross in the credits)

    Credits