From fa8dcbff328d019521cc986ca3f570a8bae7060d Mon Sep 17 00:00:00 2001 From: Christopher Tam Date: Tue, 31 Dec 2024 12:39:07 -0500 Subject: [PATCH] uplink-sys(build): Fix cross-compilation This fixes cross-compilation by setting the Go `GOOS` and `GOARCH` flags for `uplink-c`. --- .../uplink-sys-cross-aarch64-linux.yml | 40 +++++++++++++++++++ .github/workflows/uplink-sys.yml | 2 +- uplink-sys/build.rs | 37 +++++++++++++++++ uplink-sys/uplink-c | 2 +- 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/uplink-sys-cross-aarch64-linux.yml diff --git a/.github/workflows/uplink-sys-cross-aarch64-linux.yml b/.github/workflows/uplink-sys-cross-aarch64-linux.yml new file mode 100644 index 0000000..2f5e106 --- /dev/null +++ b/.github/workflows/uplink-sys-cross-aarch64-linux.yml @@ -0,0 +1,40 @@ +name: Cross-compile uplink-sys to aarch64 Linux + +on: + push: + branches: ["main"] + paths: + - 'uplink-sys/**' + - 'Cargo.lock' + - 'Cargo.toml' + - 'docker-compose.yaml' + - 'Makefile' + pull_request: + branches: ["main"] + paths: + - 'uplink-sys/**' + - 'Cargo.lock' + - 'Cargo.toml' + - 'docker-compose.yaml' + - 'Makefile' +env: + CARGO_TERM_COLOR: always + +jobs: + build: + runs-on: ubuntu-latest + env: + CARGO_BUILD_TARGET: aarch64-unknown-linux-gnu + BINDGEN_EXTRA_CLANG_ARGS: "--sysroot=/usr/aarch64-linux-gnu" + + steps: + - uses: actions/checkout@v4 + + - name: Set up `aarch64-unknown-linux-gnu` Rust target + run: rustup target add aarch64-unknown-linux-gnu + + - name: Install dependencies + run: sudo apt-get update && sudo apt-get install -y --no-install-recommends clang libclang-dev libc6-dev-arm64-cross && sudo snap install zig --classic --beta + + - name: Build + run: make -C uplink-sys build diff --git a/.github/workflows/uplink-sys.yml b/.github/workflows/uplink-sys.yml index dd3216b..1d51251 100644 --- a/.github/workflows/uplink-sys.yml +++ b/.github/workflows/uplink-sys.yml @@ -2,7 +2,7 @@ name: uplink-sys on: push: - branches: main + branches: ["main"] paths: - 'uplink-sys/**' - 'Cargo.lock' diff --git a/uplink-sys/build.rs b/uplink-sys/build.rs index 2c9bb97..231bb7a 100644 --- a/uplink-sys/build.rs +++ b/uplink-sys/build.rs @@ -4,7 +4,42 @@ use std::env; use std::path::PathBuf; use std::process::Command; +/// Converts Rust's architecture name to Go's architecture name. +fn rust_arch_to_go_arch(rust_arch: &str) -> &str { + match rust_arch { + "x86" => "386", + "x86_64" => "amd64", + "arm" => "arm", + "aarch64" => "arm64", + "riscv32" => "riscv", + "riscv64" => "riscv64", + "loongarch64" => "loong64", + "mips" => "mips", + "mipsel" => "mipsle", + "mips64" => "mips64", + "mips64el" => "mips64le", + "powerpc" => "ppc", + "powerpc64" => "ppc64", + "wasm64" => "wasm", + _ => panic!("Unsupported architecture: {rust_arch}"), + } +} + +fn rust_os_to_go_os<'a>(rust_arch: &str, rust_os: &'a str) -> &'a str { + if rust_arch.starts_with("wasm") { + "js" + } else { + rust_os + } +} + fn main() { + // Set the environment variables for `go` compilation, so that cross-compilation works. + let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not defined"); + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not defined"); + let go_arch = rust_arch_to_go_arch(&target_arch); + let go_os = rust_os_to_go_os(&target_arch, &target_os); + let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not defined")); // Directory containing uplink-c project source @@ -22,6 +57,8 @@ fn main() { // parent tree directory and with the same depth. Command::new("make") .arg("build") + .env("GOOS", go_os) + .env("GOARCH", go_arch) .current_dir(&uplink_c_src) .status() .expect("Failed to run make command from build.rs."); diff --git a/uplink-sys/uplink-c b/uplink-sys/uplink-c index 4ecaa42..9044471 160000 --- a/uplink-sys/uplink-c +++ b/uplink-sys/uplink-c @@ -1 +1 @@ -Subproject commit 4ecaa42f90a18b243ebacbc0a035ac62e17ecdca +Subproject commit 9044471c245d7d6f072f0b7304cf6af17c3d400f