diff --git a/src/test-go.bats b/src/test-go.bats index 56f599c..9deed0a 100755 --- a/src/test-go.bats +++ b/src/test-go.bats @@ -511,6 +511,96 @@ testHelloCGO() { assert_output --partial "PKG_CONFIG=$(xx-info triple)-pkg-config" } +testHelloCGOZig() { + if ! supportZig; then + skip "Zig not supported" + fi + export CGO_ENABLED=1 + export XX_GO_PREFER_COMPILER=zig + 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 "loong64-hellocgo-zig" { + if ! supportLoong64CGo; then + skip "LOONGARCH64 not supported" + fi + if [ -f /etc/alpine-release ]; then + # FIXME: loong64-hellocgo issue on alpine < 3.21 + # ld.lld: error: unknown emulation: elf64loongarch + # ld.lld: error: /loongarch64-alpine-linux-musl/usr/lib/gcc/loongarch64-alpine-linux-musl/14.2.0/crtbeginS.o:(.text+0x0): unknown relocation (102) against symbol + # error: unknown target triple 'loongarch64-alpine-linux-musl', please use -triple or -arch + alpineRelease=$(cat /etc/alpine-release) + if ! grep PRETTY_NAME /etc/os-release | cut -d '=' -f 2 | tr -d '"' | grep -q "edge$" || [ "$(semver compare "$alpineRelease" "3.21.0")" -lt 0 ]; then + skip + fi + fi + export TARGETARCH=loong64 + 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 XX_GO_PREFER_COMPILER=zig + 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 @@ -535,3 +625,10 @@ testHelloCGO() { assert_success assert_output "$nativeArch" } + +@test "clean-packages" { + if ! supportZig; then + skip "Zig not supported" + fi + del zig +} diff --git a/src/test_helper.bash b/src/test_helper.bash index 3675d78..6f3b256 100644 --- a/src/test_helper.bash +++ b/src/test_helper.bash @@ -142,3 +142,7 @@ supportLoong64CGo() { 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" +} diff --git a/src/xx-go b/src/xx-go index 6e777e7..eb4bc3f 100755 --- a/src/xx-go +++ b/src/xx-go @@ -6,6 +6,9 @@ for l in $(xx-info env); do export "${l?}" done +# XX_GO_PREFER_COMPILER defines the compiler to use if supported +: "${XX_GO_PREFER_COMPILER=clang}" + export GOOS="${TARGETOS}" export GOARCH="${TARGETARCH}" @@ -76,26 +79,33 @@ if command -v "$XX_TRIPLE-g++" >/dev/null 2>/dev/null; then cxx_set=1 fi -if command -v clang >/dev/null 2>/dev/null; then - triple=$(xx-clang --print-target-triple || true) - if [ -n "$triple" ]; then - export CC="$triple-clang" - export CXX="$triple-clang++" +if [ "$XX_GO_PREFER_COMPILER" = "clang" ]; then + if command -v clang >/dev/null 2>/dev/null; then + triple=$(xx-clang --print-target-triple || true) + if [ -n "$triple" ]; then + export CC="$triple-clang" + export CXX="$triple-clang++" + c_set=1 + cxx_set=1 + 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 + fi +elif [ "$XX_GO_PREFER_COMPILER" = "zig" ]; then + if command -v "zig" >/dev/null 2>/dev/null; then + export CC="zig cc -target $XX_ZIG_TRIPLE" c_set=1 + export CXX="zig c++ -target $XX_ZIG_TRIPLE" cxx_set=1 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 -fi - if [ -z "$GOBIN" ] && [ -n "$GOPATH" ] && [ -n "$GOARCH" ] && [ -n "$GOOS" ]; then export PATH="${GOPATH}/bin/${GOOS}_${GOARCH}:${PATH}" fi diff --git a/src/xx-info b/src/xx-info index 0cecfd7..84686c6 100755 --- a/src/xx-info +++ b/src/xx-info @@ -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=}" @@ -233,10 +234,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") @@ -245,11 +253,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") @@ -264,6 +279,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" @@ -273,6 +293,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" @@ -283,10 +308,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") @@ -299,6 +329,11 @@ 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" @@ -306,6 +341,11 @@ case "$TARGETARCH" in 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" @@ -313,6 +353,11 @@ case "$TARGETARCH" in 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 ;; "loong64") XX_MARCH="loong64" @@ -320,6 +365,11 @@ case "$TARGETARCH" in XX_ALPINE_ARCH="loongarch64" XX_RHEL_ARCH="loong64" XX_TRIPLE="loongarch64${vendor}-linux-${XX_LIBC}" + if [ "$XX_LIBC" = "musl" ]; then + XX_ZIG_TRIPLE="loongarch64-linux-musl" + else + XX_ZIG_TRIPLE="loongarch64-linux-gnu" + fi ;; "386") XX_MARCH="i386" @@ -330,8 +380,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") @@ -340,6 +396,11 @@ 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" @@ -347,6 +408,11 @@ case "$TARGETARCH" in 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" @@ -354,6 +420,11 @@ case "$TARGETARCH" in 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" @@ -361,6 +432,11 @@ case "$TARGETARCH" in 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 @@ -416,6 +492,9 @@ case "$1" in "triple") echo "$XX_TRIPLE" ;; + "zig-triple") + echo "$XX_ZIG_TRIPLE" + ;; "vendor") echo "$XX_VENDOR" ;; @@ -432,6 +511,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}"