Skip to content

Commit

Permalink
add loongarch support
Browse files Browse the repository at this point in the history
Signed-off-by: yzewei <[email protected]>
  • Loading branch information
yzewei committed Dec 22, 2023
1 parent c4d7d31 commit 30389ea
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 99 deletions.
4 changes: 3 additions & 1 deletion docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ target "_all-platforms" {
"linux/mips64le",
"linux/ppc64le",
"linux/s390x",
"linux/riscv64"
"linux/riscv64",
"linux/loong64"
]
}

Expand Down Expand Up @@ -161,6 +162,7 @@ target "sdk-extras" {
"linux/mips64le",
"linux/ppc64le",
"linux/riscv64",
"linux/loong64",
"linux/s390x",
"windows/386",
"windows/amd64",
Expand Down
17 changes: 17 additions & 0 deletions src/test-cargo.bats
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ testHelloCargoRustup() {
testHelloCargoRustup
}

@test "loong64-hellocargo-rustup" {
if ! supportLoongArchCGo; then
skip "LOONG64 not supported" # rust stdlib package not available
fi
export TARGETARCH=loong64
testHelloCargoRustup
}

@test "386-hellocargo-rustup" {
export TARGETARCH=386
testHelloCargoRustup
Expand Down Expand Up @@ -143,6 +151,15 @@ testHelloCargoRustup() {
testHelloCargo
}

@test "loong64-hellocargo-rustpkg" {
if ! supportLoongArchCGo; then
skip "LOONG64 not supported" # rust stdlib package not available
fi
export TARGETARCH=loong64
export LOONGARCH64_TARGET_ARCH=loong64
testHelloCargo
}

@test "386-hellocargo-rustpkg" {
export TARGETARCH=386
testHelloCargo
Expand Down
14 changes: 14 additions & 0 deletions src/test-clang.bats
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ testBuildHello() {
expectedSuffix=
}

@test "loong64-c-ld" {
skip "LOONG64 not supported"
export TARGETARCH=loong64
testHelloCLLD # actually runs with ld
expectedLinker=
expectedSuffix=
}

@test "ppc64le-c-lld" {
export TARGETARCH=ppc64le
testHelloCLLD
Expand Down Expand Up @@ -300,6 +308,12 @@ testBuildHello() {
testHelloCPPLLD # actually runs with ld
}

@test "loong64-c++-ld" {
skip "LOONG64 not supported"
export TARGETARCH=loong64
testHelloCPPLLD # actually runs with ld
}

@test "386-c++-lld" {
export TARGETARCH=386
testHelloCPPLLD
Expand Down
103 changes: 12 additions & 91 deletions src/test-go.bats
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ testEnv() {
testEnv
}

@test "loong64-env" {
skip "LOONG64 GO not supported"
export TARGETARCH=loong64
testEnv
}

@test "s390x-env" {
export TARGETARCH=s390x
testEnv
Expand Down Expand Up @@ -164,97 +170,6 @@ testEnv() {
unset TARGETOS
}

testHelloGO() {
run xx-go build -o /tmp/a.out ./fixtures/hello.go
assert_success
xx-verify /tmp/a.out
if ! xx-info is-cross; then
run /tmp/a.out
assert_success
assert_output "hello go"
fi
}

@test "native-hellogo" {
unset TARGETARCH
testHelloGO
}

@test "amd64-hellogo" {
export TARGETARCH=amd64
testHelloGO
}

@test "arm64-hellogo" {
export TARGETARCH=arm64
testHelloGO
}

@test "arm-hellogo" {
export TARGETARCH=arm
testHelloGO
}

@test "armv5-hellogo" {
export TARGETARCH=arm
export TARGETVARIANT=v5
testHelloGO
unset TARGETVARIANT
}

@test "s390x-hellogo" {
export TARGETARCH=s390x
testHelloGO
}

@test "ppc64le-hellogo" {
export TARGETARCH=ppc64le
testHelloGO
}

@test "riscv64-hellogo" {
if ! supportRiscVGo; then
skip "RISC-V GO not supported"
fi
export TARGETARCH=riscv64
testHelloGO
}

@test "386-hellogo" {
export TARGETARCH=386
testHelloGO
}

@test "mipsle-hellogo" {
export TARGETARCH=mipsle
testHelloGO
}

@test "mipsle-softfloat-hellogo" {
export TARGETARCH=mipsle
export TARGETVARIANT=softfloat
testHelloGO
unset TARGETVARIANT
}

@test "mips64le-hellogo" {
export TARGETARCH=mips64le
testHelloGO
}

@test "mips64le-softfloat-hellogo" {
export TARGETARCH=mips64le
export TARGETVARIANT=softfloat
testHelloGO
unset TARGETVARIANT
}

@test "darwin-amd64-hellogo" {
export TARGETARCH=amd64
export TARGETOS=darwin
testHelloGO
}

testHelloCGO() {
export CGO_ENABLED=1
xxadd xx-c-essentials
Expand Down Expand Up @@ -303,6 +218,12 @@ testHelloCGO() {
testHelloCGO
}

@test "loong64-hellocgo" {
skip "LOONG64 CGO not supported"
export TARGETARCH=loong64
testHelloCGO
}

@test "386-hellocgo" {
export TARGETARCH=386
testHelloCGO
Expand Down
5 changes: 5 additions & 0 deletions src/test-info-alpine.bats
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ load 'assert'
assert_equal "riscv64" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc xx-info pkg-arch)" # does not change
}

@test "loong64" {
assert_equal "loong64-alpine-linux-muslabi" "$(TARGETPLATFORM=linux/loong64 xx-info triple)"
assert_equal "loong64" "$(TARGETPLATFORM=linux/loong64 xx-info pkg-arch)"
}

@test "custom-vendor" {
assert_equal "riscv64-unknown-linux-musl" "$(TARGETPLATFORM=linux/riscv64 XX_VENDOR=unknown xx-info triple)"
}
Expand Down
8 changes: 8 additions & 0 deletions src/test-info-common.bats
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ load 'assert'
assert_equal "riscv64" "$(TARGETPLATFORM=linux/riscv64 xx-info march)"
}

@test "loong64" {
assert_equal "loong64" "$(TARGETPLATFORM=linux/loong64 xx-info march)"
}

@test "s390x" {
assert_equal "s390x" "$(TARGETPLATFORM=linux/s390x xx-info march)"
}
Expand Down Expand Up @@ -134,6 +138,10 @@ load 'assert'
assert_success
assert_output "$(TARGETPLATFORM=linux/riscv64 xx-info)"

TARGETPAIR=linux-loong64 run xx-info
assert_success
assert_output "$(TARGETPLATFORM=linux/loong64 xx-info)"

TARGETPAIR=linux-mips run xx-info
assert_success
assert_output "$(TARGETPLATFORM=linux/mips xx-info)"
Expand Down
5 changes: 5 additions & 0 deletions src/test-info-debian.bats
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ fi
assert_equal "riscv64gc-unknown-linux-gnu" "$(TARGETPLATFORM=linux/riscv64 RISCV64_TARGET_ARCH=riscv64gc XX_VENDOR=unknown xx-info triple)"
}

@test "loong64" {
assert_equal "loong64-linux-gnuabi" "$(TARGETPLATFORM=linux/loong64 xx-info triple)"
assert_equal "loong64" "$(TARGETPLATFORM=linux/loong64 xx-info pkg-arch)"
}

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

@test "loong64" {
assert_equal "loong64" "$(TARGETPLATFORM=linux/loong64 xx-info pkg-arch)"
}

@test "mips" {
assert_equal "mips-linux-gnu" "$(TARGETPLATFORM=linux/mips xx-info triple)"
assert_equal "mips" "$(TARGETPLATFORM=linux/mips xx-info pkg-arch)"
Expand Down
5 changes: 5 additions & 0 deletions src/test-verify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ load 'assert'
run xx-verify /idontexist
assert_success

export XX_VERIFY_FILE_CMD_OUTPUT=": ELF 64-bit LSB executable, LoongArch, version 1 (SYSV), statically linked, BuildID[sha1]=4d126b33c220ba2efd23ed68a46ef0db96c31f76, not stripped"
export TARGETPLATFORM=linux/loong64
run xx-verify /idontexist
assert_success

export TARGETPLATFORM=linux/amd64
run xx-verify /idontexist
assert_failure
Expand Down
22 changes: 22 additions & 0 deletions src/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ supportRiscVCGo() {
go version | egrep -E "1.16|1.17|1.18|1.19" >/dev/null 2>&1
}

supportLoongArch() {
if [ -f /etc/debian_version ]; then
if grep "sid" /etc/apt/sources.list 2>/dev/null >/dev/null; then
return 0
else
return 1
fi
fi
return 0
}

supportLoongArchGo() {
go version | grep -E "1.14|1.15|1.16|1.17|1.18|1.19|1.20|1.21" >/dev/null 2>&1
}

supportLoongArchCGo() {
if ! supportLoongArch; then
return 1
fi
go version | egrep -E "1.16|1.17|1.18|1.19|1.20|1.21" >/dev/null 2>&1
}

supportRC() {
command -v llvm-rc >/dev/null 2>&1
}
6 changes: 6 additions & 0 deletions src/xx-apk
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ setup() {
echo "https://dl-cdn.alpinelinux.org/alpine/edge/main" >"$apk_dir/repositories"
echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >>"$apk_dir/repositories"
fi
# add loongarch support
if [ "$TARGETARCH" = "loong64" ]; then
echo "http://alpine.loongnix.cn/v3.11/main" >"$apk_dir/repositories"
echo "http://alpine.loongnix.cn/v3.11/community" >>"$apk_dir/repositories"
echo "http://alpine.loongnix.cn/v3.11/testing" >>"$apk_dir/repositories"
fi
mkdir "$apk_dir/keys"
mkdir "$apk_dir/protected_paths.d"
echo "$XX_PKG_ARCH" >"$apk_dir/arch"
Expand Down
7 changes: 7 additions & 0 deletions src/xx-apt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ if [ "$TARGETARCH" = "riscv64" ] && [ "$(xx-info vendor)" = "debian" ]; then
echo "deb [ arch=riscv64 ] http://ftp.ports.debian.org/debian-ports sid main" >>/etc/apt/sources.list.d/riscv64-sid.list
fi

if [ "$TARGETARCH" = "loong64" ] && [ "$(xx-info vendor)" = "debian" ]; then
apt-get update
apt-get install -y debian-ports-archive-keyring
echo "deb [ arch=loong64 ] http://ftp.ports.debian.org/debian-ports sid main
deb [ arch=loong64 ] http://ftp.ports.debian.org/debian-ports unreleased main" >>/etc/apt/sources.list.d/loong64-sid.list
fi

if ! dpkg --print-foreign-architectures | grep "$XX_PKG_ARCH" >/dev/null; then
if [ "$XX_OS" = "linux" ]; then
if [ "$(xx-info vendor)" = "ubuntu" ] && [ -z "$nocross" ]; then
Expand Down
11 changes: 7 additions & 4 deletions src/xx-cargo
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ fi
if [ -z "$ARM_TARGET_ARCH" ]; then
export ARM_TARGET_ARCH=armv7
fi
if [ -z "$LOONG64_TARGET_ARCH" ]; then
export LOONG64_TARGET_ARCH=loong64
fi

rustup=
if which rustup >/dev/null 2>&1; then
Expand Down Expand Up @@ -79,12 +82,12 @@ if [ -n "$XX_RUSTFLAGS" ]; then
fi
export "CC_$(xx-info | tr - _)=$(xx-info)-clang"

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

Expand Down
9 changes: 9 additions & 0 deletions src/xx-cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ detectTargetOSArch() {
targetarch="s390x"
elif [ "$arch" = "powerpc64le" ]; then
targetarch="ppc64le"
elif [ "$arch" = "loong64" ]; then
targetarch="loong64"
fi

if [ -z "$targetarch" ]; then
Expand Down Expand Up @@ -334,6 +336,10 @@ setup() {
if [ "${target#riscv64}" != "${target}" ]; then
prefer_lld=
fi
# lld has no support for loong64
if [ "${target#loong64}" != "${target}" ]; then
prefer_lld=
fi

if [ -n "${XX_CC_PREFER_STATIC_LINKER}" ] && { [ "${target#386}" != "${target}" ] || [ "${target#powerpc64le}" != "${target}" ]; }; then
prefer_lld=
Expand Down Expand Up @@ -402,6 +408,9 @@ setup() {
if [ "$exp" = "riscv64" ]; then
exp="riscv"
fi
if [ "$exp" = "loong64" ]; then
exp="loong64"
fi
if ld -V 2>/dev/null | grep $exp >/dev/null; then
ln -s "$(command -v ld)" "/usr/bin/${target}-ld"
linker="/usr/bin/${target}-ld"
Expand Down
Loading

0 comments on commit 30389ea

Please sign in to comment.