Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust support #27

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions base/test-info-alpine.bats
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ load 'assert'
@test "riscv64" {
assert_equal "riscv64-alpine-linux-musl" "$(TARGETPLATFORM=linux/riscv64 xx-info triple)"
assert_equal "riscv64" "$(TARGETPLATFORM=linux/riscv64 xx-info pkg-arch)"

assert_equal "riscv64gc-alpine-linux-musl" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc xx-info triple)"
assert_equal "riscv64" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc xx-info pkg-arch)" # does not change
}

@test "custom-vendor" {
assert_equal "riscv64-unknown-linux-musl" "$(TARGETPLATFORM=linux/riscv64 XX_VENDOR=unknown xx-info triple)"
}

@test "mips" {
Expand Down
4 changes: 4 additions & 0 deletions base/test-info-debian.bats
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ fi
@test "riscv64" {
assert_equal "riscv64-linux-gnu" "$(TARGETPLATFORM=linux/riscv64 xx-info triple)"
assert_equal "riscv64" "$(TARGETPLATFORM=linux/riscv64 xx-info pkg-arch)"

assert_equal "riscv64gc-linux-gnu" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc xx-info triple)"
assert_equal "riscv64" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc xx-info pkg-arch)" # does not change
assert_equal "riscv64gc-unknown-linux-gnu" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc XX_VENDOR=unknown xx-info triple)"
}

@test "mips" {
Expand Down
11 changes: 11 additions & 0 deletions base/xx-apk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ if [ -n "$XX_DEBUG_APK" ]; then
set -x
fi

unset XX_VENDOR # vendor for installing packages is always alpine

for l in $(xx-info env); do
export "${l?}"
done
Expand Down Expand Up @@ -82,6 +84,7 @@ cmd() {
fi
n=$#
iscompilerrt=
isrustlib=
for a in "$@"; do
if [ $# = $n ]; then set --; fi
case "$a" in
Expand All @@ -95,6 +98,10 @@ cmd() {
iscompilerrt=1
set -- "$@" "$a"
;;
"rust-stdlib")
set -- "$@" rust-stdlib
isrustlib=1
;;
*)
set -- "$@" "$a"
;;
Expand All @@ -119,6 +126,10 @@ cmd() {
fi
done
fi
# rust stdlib is accessed from the real root
if [ -n "$isrustlib" ] && [ -d "$root/usr/lib/rustlib/$(xx-info)" ]; then
ln -s "$root/usr/lib/rustlib/$(xx-info)" "/usr/lib/rustlib/$(xx-info)" || true
fi
fi
}

Expand Down
83 changes: 83 additions & 0 deletions base/xx-cargo
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env sh

set -e

if [ -z "$XX_CARGO_NOLOCK" ]; then
lock="/var/lock/xx-cargo"
exec 9>$lock
flock -x 9
export XX_CARGO_NOLOCK=1
fi

if [ -n "$XX_DEBUG_CARGO" ]; then
set -x
fi

setuponly=
printtarget=
n=$#
for a in "$@"; do
if [ $# = $n ]; then set --; fi
case "$a" in
"--setup-target-triple")
setuponly=1
;;
"--print-target")
printtarget=1
;;
*)
set -- "$@" "$a"
;;
esac
done

done_file="/.xx-cargo.$(xx-info arch)"

export RISCV64_TARGET_ARCH=riscv64gc

rustup=
if which rustup >/dev/null 2>&1; then
rustup=1
fi

if [ -n "$rustup" ] || [ ! -f /etc/alpine-release ]; then
export XX_VENDOR="unknown"
fi

if [ ! -f "$done_file" ]; then
xx-clang --setup-target-triple
if [ ! -d "$(rustc --print sysroot)/lib/rustlib/$(xx-info)" ]; then
if [ -n "$rustup" ]; then
rustup target add "$(xx-info)" 2>/dev/null >/dev/null
elif [ -f /etc/alpine-release ]; then
xx-apk add rust-stdlib 2>/dev/null >/dev/null
else
xx-apt install -y libstd-rust-dev 2>/dev/null >/dev/null
fi
fi
touch "$done_file"
fi

export "CARGO_TARGET_$(xx-info | tr '[:lower:]' '[:upper:]' | tr - _)_LINKER=$(xx-info)-clang"
if [ -n "$XX_RUSTFLAGS" ]; then
export "CARGO_TARGET_$(xx-info | tr '[:lower:]' '[:upper:]' | tr - _)_RUSTFLAGS=$XX_RUSTFLAGS"
fi
export "CC_$(xx-info | tr - _)=$(xx-info)-clang"

if which "qemu-$(RISCV64_TARGET_ARCH='' xx-info march)" >/dev/null 2>&1; then
export "CARGO_TARGET_$(xx-info | tr '[:lower:]' '[:upper:]' | tr - _)_RUNNER=qemu-$(RISCV64_TARGET_ARCH='' xx-info march)"
if [ -f /etc/alpine-release ]; then
export "QEMU_LD_PREFIX=/$(RISCV64_TARGET_ARCH='' xx-info)/"
else
export "QEMU_LD_PREFIX=/lib/$(RISCV64_TARGET_ARCH='' XX_VENDOR='' xx-info)/"
fi
fi

if [ -n "$printtarget" ]; then
xx-info
exit 0
fi

if [ -z "$setuponly" ]; then
cargo "$@" --target="$(xx-info)"
fi
14 changes: 13 additions & 1 deletion base/xx-cc
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ EOT
fi
fi

config="--target=${target} -fuse-ld=${linker}"
config="--target=$(echo "${target}" | sed s/^riscv64gc-/riscv64-/) -fuse-ld=${linker}"
if [ "${nativeTarget}" != "${target}" ]; then
if [ "$targetos" = "darwin" ]; then
detectMacOSSDK
Expand All @@ -588,6 +588,7 @@ export PKG_CONFIG_LIBDIR=/${target}/usr/lib/pkgconfig/
exec pkg-config "\$@"
EOT
chmod +x "/usr/bin/${target}-pkg-config"

fi
elif [ ! -f "/usr/bin/${target}-pkg-config" ] && [ ! -h "/usr/bin/${target}-pkg-config" ]; then
ln -s pkg-config "/usr/bin/${target}-pkg-config"
Expand All @@ -598,6 +599,17 @@ EOT
ln -s "${f}" "/usr/bin/${target}.cfg"
fi

if [ -f /etc/alpine-release ]; then
# if vendor is not alpine then sysroot needs to be linked to the custom vendor
alpinetriple=$(echo "$target" | sed s/-[[:alpha:]][[:alpha:]]*-/-alpine-/ | sed s/^riscv64gc-/riscv64-/)
if [ "$target" != "$alpinetriple" ]; then
# shellcheck disable=SC2044
for f in $(find / -type d -name "$alpinetriple"); do
ln -s "$alpinetriple" "$(dirname "$f")/$target"
done
fi
fi

if [ "${targetos}" = "darwin" ]; then
if ! command -v xcrun 2>/dev/null >/dev/null; then
writexcrun
Expand Down
39 changes: 24 additions & 15 deletions base/xx-info
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
: "${XX_RHEL_ARCH=unknown}"
: "${XX_OS_VERSION=unknown}"
: "${XX_TRIPLE=unknown-unknown-none}"
: "${XX_VENDOR=unknown}"
: "${XX_VENDOR=}"
: "${XX_LIBC=}"

usage() {
Expand Down Expand Up @@ -105,27 +105,32 @@ if [ -n "$TARGETPLATFORM" ]; then
fi
fi

distro=""
# detect distro vendor
if [ "$TARGETOS" = "darwin" ]; then
XX_VENDOR="apple"
elif [ -f /etc/os-release ]; then
# shellcheck disable=SC1091
if . /etc/os-release 2>/dev/null; then
XX_VENDOR=$ID
XX_OS_VERSION=$VERSION_ID
fi
# shellcheck disable=SC1091
if . /etc/os-release 2>/dev/null; then
distro=$ID
fi

vendor=""
if [ -z "$XX_VENDOR" ]; then
if [ -n "$distro" ]; then
XX_VENDOR="$distro"
fi
if [ "$TARGETOS" = "darwin" ]; then
XX_VENDOR="apple"
fi
if [ -z "$XX_VENDOR" ]; then
XX_VENDOR="unknown"
fi
fi
case "$XX_VENDOR" in
unknown | debian | ubuntu | rhel | fedora | centos | rocky | ol) ;;
*)
vendor="-${XX_VENDOR}"
;;
debian | ubuntu | rhel | fedora | centos | rocky | ol) ;;
*) vendor="-${XX_VENDOR}" ;;
esac

if [ -z "$XX_LIBC" ]; then
if [ "$XX_VENDOR" = "alpine" ]; then
if [ "$distro" = "alpine" ]; then
XX_LIBC="musl"
else
XX_LIBC="gnu"
Expand Down Expand Up @@ -267,7 +272,11 @@ case "$TARGETARCH" in
XX_DEBIAN_ARCH="riscv64"
XX_ALPINE_ARCH="riscv64"
XX_RHEL_ARCH="riscv64"
XX_TRIPLE="riscv64${vendor}-linux-${XX_LIBC}"
triplearch="riscv64"
if [ -n "$RISCV64_TARGET_ARCH" ]; then
triplearch="${RISCV64_TARGET_ARCH}"
fi
XX_TRIPLE="${triplearch}${vendor}-linux-${XX_LIBC}"
;;
"ppc64le")
XX_MARCH="ppc64le"
Expand Down