1#!/bin/sh
2# cargo for Linux from macOS or Linux: `cargo.sh ARCH COMMAND ARGS...`, ARCH x86_64 or aarch64.
3# zig compiles C and links against glibc 2.17, Rust's own floor, which reaches back to RHEL 7,
4# Debian 8 and Ubuntu 14.04.
5set -eu
6here=$(cd "$(dirname "$0")" && pwd)
7case ${1:-} in
8x86_64 | aarch64) ;;
9*)
10 echo "usage: $0 x86_64|aarch64 COMMAND ARGS..." >&2
11 exit 2
12 ;;
13esac
14arch=$1 command=$2
15shift 2
16command -v zig >/dev/null || { echo "zig is required as the cross linker" >&2; exit 1; }
17triple=$arch-unknown-linux-gnu
18rustup target add "$triple" >/dev/null
19variable=$(echo "$triple" | tr - _)
20# The build id ties an executable to its published .debug file.
21env "CARGO_TARGET_$(echo "$triple" | tr 'a-z-' 'A-Z_')_LINKER=$here/cc.sh" \
22 "CC_$variable=$here/cc.sh" "AR_$variable=$here/ar.sh" \
23 ZIG_TARGET="$arch-linux-gnu.2.17" \
24 cargo "$command" --target "$triple" \
25 --config "target.$triple.rustflags=['-C','link-arg=-Wl,--build-id']" "$@"