Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/test-go.bats
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,76 @@ testHelloCGO() {
assert_output --partial "PKG_CONFIG=$(xx-info triple)-pkg-config"
}

testHelloCGOZig() {
if ! supportZig; then
skip "Zig not supported"
fi
export CGO_ENABLED=1
add zig
run xx-go build -x -o /tmp/a.out ./fixtures/hello_cgo.go
assert_success
run xx-verify /tmp/a.out
assert_success
if ! xx-info is-cross; then
run /tmp/a.out
assert_success
assert_output "hello cgo"
fi
}

@test "native-hellocgo-zig" {
unset TARGETARCH
testHelloCGOZig
}

@test "amd64-hellocgo-zig" {
export TARGETARCH=amd64
testHelloCGOZig
}

@test "arm64-hellocgo-zig" {
export TARGETARCH=arm64
testHelloCGOZig
}

@test "arm-hellocgo-zig" {
export TARGETARCH=arm
testHelloCGOZig
}

@test "ppc64le-hellocgo-zig" {
export TARGETARCH=ppc64le
testHelloCGOZig
}

@test "riscv64-hellocgo-zig" {
if ! supportRiscVCGo; then
skip "RISC-V CGO not supported"
fi
export TARGETARCH=riscv64
testHelloCGOZig
}

@test "386-hellocgo-zig" {
export TARGETARCH=386
testHelloCGOZig
}

@test "arm64-cgoenv-zig" {
if ! supportZig; then
skip "Zig not supported"
fi
export TARGETARCH=arm64
export CGO_ENABLED=1

add zig
# single/double quotes changed in between go versions
run sh -c "xx-go env | sed 's/[\"'\'']//g'"
assert_success
assert_output --partial "CC=zig cc -target $(xx-info zig-triple)"
assert_output --partial "CXX=zig c++ -target $(xx-info zig-triple)"
}

@test "wrap-unwrap" {
target="arm64"
if [ "$(xx-info arch)" = "arm64" ]; then target="amd64"; fi
Expand All @@ -501,3 +571,10 @@ testHelloCGO() {
assert_success
assert_output "$nativeArch"
}

@test "clean-packages" {
if ! supportZig; then
skip "Zig not supported"
fi
del zig
}
4 changes: 4 additions & 0 deletions src/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ supportWindowsArm64Go() {
supportRC() {
command -v llvm-rc >/dev/null 2>&1
}

supportZig() {
[ -f /etc/alpine-release ] && versionGTE "$(xx-info os-version | cut -d'.' -f1-2)" "3.20"
}
22 changes: 14 additions & 8 deletions src/xx-go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ if command -v clang >/dev/null 2>/dev/null; then
fi
fi

if command -v "$XX_TRIPLE-ar" >/dev/null 2>/dev/null; then
export AR="$XX_TRIPLE-ar"
ar_set=1
fi

if command -v "$XX_TRIPLE-pkg-config" >/dev/null 2>/dev/null; then
export PKG_CONFIG="$XX_TRIPLE-pkg-config"
pkgconfig_set=1
if command -v "zig" >/dev/null 2>/dev/null; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should replace CC with zig just because it is installed. I guess this would be ok if there is no clang and gcc installed. Otherwise I would expect some opt-in env.

export CC="zig cc -target $XX_ZIG_TRIPLE"
c_set=1
export CXX="zig c++ -target $XX_ZIG_TRIPLE"
cxx_set=1
else
if command -v "$XX_TRIPLE-ar" >/dev/null 2>/dev/null; then
export AR="$XX_TRIPLE-ar"
ar_set=1
fi
if command -v "$XX_TRIPLE-pkg-config" >/dev/null 2>/dev/null; then
export PKG_CONFIG="$XX_TRIPLE-pkg-config"
pkgconfig_set=1
fi
fi

if [ -z "$GOBIN" ] && [ -n "$GOPATH" ] && [ -n "$GOARCH" ] && [ -n "$GOOS" ]; then
Expand Down
77 changes: 76 additions & 1 deletion src/xx-info
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
: "${XX_RHEL_ARCH=unknown}"
: "${XX_OS_VERSION=}"
: "${XX_TRIPLE=unknown-unknown-none}"
: "${XX_ZIG_TRIPLE=unknown-unknown-none}"
: "${XX_VENDOR=}"
: "${XX_LIBC=}"

Expand Down Expand Up @@ -228,10 +229,17 @@ case "$TARGETARCH" in
XX_ALPINE_ARCH="x86_64"
XX_RHEL_ARCH="x86_64"
XX_TRIPLE="x86_64${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="x86_64-linux-musl"
else
XX_ZIG_TRIPLE="x86_64-linux-gnu"
fi
if [ "$TARGETOS" = "darwin" ]; then
XX_TRIPLE="x86_64${vendor}-macos${MACOSX_VERSION_MIN}"
XX_ZIG_TRIPLE="x86_64-macos-none"
elif [ "$TARGETOS" = "windows" ]; then
XX_TRIPLE="x86_64-w64-mingw32"
XX_ZIG_TRIPLE="x86_64-windows-gnu"
fi
;;
"arm64")
Expand All @@ -240,11 +248,18 @@ case "$TARGETARCH" in
XX_ALPINE_ARCH="aarch64"
XX_RHEL_ARCH="aarch64"
XX_TRIPLE="aarch64${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="aarch64-linux-musl"
else
XX_ZIG_TRIPLE="aarch64-linux-gnu"
fi
if [ "$TARGETOS" = "darwin" ]; then
XX_MARCH="arm64"
XX_TRIPLE="arm64${vendor}-macos${MACOSX_VERSION_MIN}"
XX_ZIG_TRIPLE="aarch64-macos-none"
elif [ "$TARGETOS" = "windows" ]; then
XX_TRIPLE="aarch64-w64-mingw32"
XX_ZIG_TRIPLE="aarch64-windows-gnu"
fi
;;
"arm")
Expand All @@ -259,6 +274,11 @@ case "$TARGETARCH" in
triplearch="armv7"
fi
XX_TRIPLE="${triplearch}${vendor}-linux-${XX_LIBC}eabihf"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="arm-linux-musleabihf"
else
XX_ZIG_TRIPLE="arm-linux-gnueabihf"
fi
if [ "$TARGETVARIANT" = "v6" ]; then
XX_MARCH="armv6l"
XX_DEBIAN_ARCH="armel"
Expand All @@ -268,6 +288,11 @@ case "$TARGETARCH" in
if [ "$XX_VENDOR" = "alpine" ]; then
XX_TRIPLE="armv6${vendor}-linux-${XX_LIBC}eabihf"
fi
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="arm-linux-musleabi"
else
XX_ZIG_TRIPLE="arm-linux-gnueabi"
fi
fi
if [ "$TARGETVARIANT" = "v5" ]; then
XX_MARCH="armv5l"
Expand All @@ -278,10 +303,15 @@ case "$TARGETARCH" in
if [ "$XX_VENDOR" = "alpine" ]; then
XX_TRIPLE="armv5${vendor}-linux-${XX_LIBC}eabi"
fi
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="arm-linux-musleabi"
else
XX_ZIG_TRIPLE="arm-linux-gnueabi"
fi
fi

if [ "$TARGETOS" = "windows" ]; then
XX_TRIPLE="armv7-w64-mingw32"
XX_ZIG_TRIPLE="arm-windows-gnu"
fi
;;
"riscv64")
Expand All @@ -294,20 +324,35 @@ case "$TARGETARCH" in
triplearch="${RISCV64_TARGET_ARCH}"
fi
XX_TRIPLE="${triplearch}${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="riscv64-linux-musl"
else
XX_ZIG_TRIPLE="riscv64-linux-gnu"
fi
;;
"ppc64le")
XX_MARCH="ppc64le"
XX_DEBIAN_ARCH="ppc64el"
XX_ALPINE_ARCH="ppc64le"
XX_RHEL_ARCH="ppc64le"
XX_TRIPLE="powerpc64le${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="powerpc64le-linux-musl"
else
XX_ZIG_TRIPLE="powerpc64le-linux-gnu"
fi
;;
"s390x")
XX_MARCH="s390x"
XX_DEBIAN_ARCH="s390x"
XX_ALPINE_ARCH="s390x"
XX_RHEL_ARCH="s390x"
XX_TRIPLE="s390x${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="s390x-linux-musl"
else
XX_ZIG_TRIPLE="s390x-linux-gnu"
fi
;;
"386")
XX_MARCH="i386"
Expand All @@ -318,8 +363,14 @@ case "$TARGETARCH" in
if [ "$XX_VENDOR" = "alpine" ]; then
XX_TRIPLE="i586${vendor}-linux-${XX_LIBC}"
fi
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="x86-linux-musl"
else
XX_ZIG_TRIPLE="x86-linux-gnu"
fi
if [ "$TARGETOS" = "windows" ]; then
XX_TRIPLE="i686-w64-mingw32"
XX_ZIG_TRIPLE="x86-windows-gnu"
fi
;;
"mips")
Expand All @@ -328,27 +379,47 @@ case "$TARGETARCH" in
XX_ALPINE_ARCH="mips"
XX_RHEL_ARCH="mips"
XX_TRIPLE="mips${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="mips-linux-musl"
else
XX_ZIG_TRIPLE="mips-linux-gnueabi"
fi
;;
"mipsle")
XX_MARCH="mipsle"
XX_DEBIAN_ARCH="mipsel"
XX_ALPINE_ARCH="mipsle"
XX_RHEL_ARCH="mipsel"
XX_TRIPLE="mipsel${vendor}-linux-${XX_LIBC}"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="mipsel-linux-musl"
else
XX_ZIG_TRIPLE="mipsel-linux-gnueabi"
fi
;;
"mips64")
XX_MARCH="mips64"
XX_DEBIAN_ARCH="mips64"
XX_ALPINE_ARCH="mips64"
XX_RHEL_ARCH="mips64"
XX_TRIPLE="mips64${vendor}-linux-${XX_LIBC}abi64"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="mips64-linux-musl"
else
XX_ZIG_TRIPLE="mips64-linux-gnuabi64"
fi
;;
"mips64le")
XX_MARCH="mips64le"
XX_DEBIAN_ARCH="mips64el"
XX_ALPINE_ARCH="mips64le"
XX_RHEL_ARCH="mips64el"
XX_TRIPLE="mips64el${vendor}-linux-${XX_LIBC}abi64"
if [ "$XX_LIBC" = "musl" ]; then
XX_ZIG_TRIPLE="mips64el-linux-musl"
else
XX_ZIG_TRIPLE="mips64el-linux-gnuabi64"
fi
;;
esac

Expand Down Expand Up @@ -404,6 +475,9 @@ case "$1" in
"triple")
echo "$XX_TRIPLE"
;;
"zig-triple")
echo "$XX_ZIG_TRIPLE"
;;
"vendor")
echo "$XX_VENDOR"
;;
Expand All @@ -420,6 +494,7 @@ case "$1" in
echo "XX_PKG_ARCH=${XX_PKG_ARCH}"
fi
echo "XX_TRIPLE=${XX_TRIPLE}"
echo "XX_ZIG_TRIPLE=${XX_ZIG_TRIPLE}"
echo "XX_LIBC=${XX_LIBC}"
echo "TARGETOS=${TARGETOS}"
echo "TARGETARCH=${TARGETARCH}"
Expand Down
Loading