tsan: allow to cross-build Go runtime
authorDmitry Vyukov <dvyukov@google.com>
Tue, 16 May 2023 09:10:29 +0000 (11:10 +0200)
committerDmitry Vyukov <dvyukov@google.com>
Wed, 17 May 2023 07:30:51 +0000 (09:30 +0200)
Allow user to provide target GOOS/GOARCH.
If not provided, use the host GOOS/GOARCH as we do now.
This allows to cross-compile the runtime.
Also provide SKIP_TEST knob for cross-compilation
since the test will most likely not run on host.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D150650

compiler-rt/lib/tsan/go/buildgo.sh

index fcb102c..0c5c592 100755 (executable)
@@ -2,6 +2,45 @@
 
 set -e
 
+if [ "`uname -a | grep Linux`" != "" ]; then
+       HOST_GOOS="linux"
+       if [ "`uname -a | grep ppc64le`" != "" ]; then
+               HOST_GOARCH="ppc64le"
+       elif [ "`uname -a | grep x86_64`" != "" ]; then
+               HOST_GOARCH="amd64"
+       elif [ "`uname -a | grep aarch64`" != "" ]; then
+               HOST_GOARCH="arm64"
+       elif [ "`uname -a | grep -i mips64`" != "" ]; then
+               if [ "`lscpu | grep -i Little`" != "" ]; then
+                       HOST_GOARCH="mips64le"
+               else
+                       HOST_GOARCH="mips64"
+               fi
+       elif [ "`uname -a | grep s390x`" != "" ]; then
+               HOST_GOARCH="s390x"
+       fi
+elif [ "`uname -a | grep FreeBSD`" != "" ]; then
+       HOST_GOOS="freebsd"
+       HOST_GOARCH="amd64"
+elif [ "`uname -a | grep NetBSD`" != "" ]; then
+       HOST_GOOS="netbsd"
+       HOST_GOARCH="amd64"
+elif [ "`uname -a | grep Darwin`" != "" ]; then
+       HOST_GOOS="darwin"
+       if [ "`uname -a | grep x86_64`" != "" ]; then
+               HOST_GOARCH="amd64"
+       elif [ "`uname -a | grep arm64`" != "" ]; then
+               HOST_GOARCH="arm64"
+       fi
+elif [ "`uname -a | grep MINGW`" != "" ]; then
+       HOST_GOOS="windows"
+       HOST_GOARCH="amd64"
+fi
+
+GOOS=${GOOS:-$HOST_GOOS}
+GOARCH=${GOARCH:-$HOST_GOARCH}
+SUFFIX="${GOOS}_${GOARCH}"
+
 SRCS="
        tsan_go.cpp
        ../rtl/tsan_external.cpp
@@ -39,7 +78,7 @@ SRCS="
        ../../sanitizer_common/sanitizer_termination.cpp
 "
 
-if [ "`uname -a | grep Linux`" != "" ]; then
+if [ "$GOOS" == "linux" ]; then
        OSCFLAGS="-fPIC -Wno-maybe-uninitialized"
        OSLDFLAGS="-lpthread -fPIC -fpie"
        SRCS="
@@ -54,39 +93,30 @@ if [ "`uname -a | grep Linux`" != "" ]; then
                ../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
                ../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
                "
-       if [ "`uname -a | grep ppc64le`" != "" ]; then
-               SUFFIX="linux_ppc64le"
+       if [ "$GOARCH" == "ppc64le" ]; then
                ARCHCFLAGS="-m64 -mcpu=power8 -fno-function-sections"
-       elif [ "`uname -a | grep x86_64`" != "" ]; then
-               SUFFIX="linux_amd64"
+       elif [ "$GOARCH" == "amd64" ]; then
                if [ "$GOAMD64" = "v3" ]; then
                        ARCHCFLAGS="-m64 -msse4.2"
                else
                        ARCHCFLAGS="-m64 -msse3"
                fi
                OSCFLAGS="$OSCFLAGS -ffreestanding -Wno-unused-const-variable -Wno-unknown-warning-option"
-       elif [ "`uname -a | grep aarch64`" != "" ]; then
-               SUFFIX="linux_arm64"
+       elif [ "$GOARCH" == "arm64" ]; then
                ARCHCFLAGS=""
-       elif [ "`uname -a | grep -i mips64`" != "" ]; then
-               if [ "`lscpu | grep -i Little`" != "" ]; then
-                       SUFFIX="linux_mips64le"
-                       ARCHCFLAGS="-mips64 -EL"
-               else
-                       SUFFIX="linux_mips64"
-                       ARCHCFLAGS="-mips64 -EB"
-               fi
-       elif [ "`uname -a | grep s390x`" != "" ]; then
+       elif [ "$GOARCH" == "mips64le" ]; then
+               ARCHCFLAGS="-mips64 -EL"
+       elif [ "$GOARCH" == "mips64" ]; then
+               ARCHCFLAGS="-mips64 -EB"
+       elif [ "$GOARCH" == "s390x" ]; then
                SRCS="$SRCS ../../sanitizer_common/sanitizer_linux_s390.cpp"
-               SUFFIX="linux_s390x"
                ARCHCFLAGS=""
        fi
-elif [ "`uname -a | grep FreeBSD`" != "" ]; then
+elif [ "$GOOS" == "freebsd" ]; then
        # The resulting object still depends on libc.
        # We removed this dependency for Go runtime for other OSes,
        # and we should remove it for FreeBSD as well, but there is no pressing need.
        DEPENDS_ON_LIBC=1
-       SUFFIX="freebsd_amd64"
        OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
        ARCHCFLAGS="-m64"
        OSLDFLAGS="-lpthread -fPIC -fpie"
@@ -102,12 +132,11 @@ elif [ "`uname -a | grep FreeBSD`" != "" ]; then
                ../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
                ../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
        "
-elif [ "`uname -a | grep NetBSD`" != "" ]; then
+elif [ "$GOOS" == "netbsd" ]; then
        # The resulting object still depends on libc.
        # We removed this dependency for Go runtime for other OSes,
        # and we should remove it for NetBSD as well, but there is no pressing need.
        DEPENDS_ON_LIBC=1
-       SUFFIX="netbsd_amd64"
        OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
        ARCHCFLAGS="-m64"
        OSLDFLAGS="-lpthread -fPIC -fpie"
@@ -124,7 +153,7 @@ elif [ "`uname -a | grep NetBSD`" != "" ]; then
                ../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
                ../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
        "
-elif [ "`uname -a | grep Darwin`" != "" ]; then
+elif [ "$GOOS" == "darwin" ]; then
        OSCFLAGS="-fPIC -Wno-unused-const-variable -Wno-unknown-warning-option -mmacosx-version-min=10.7"
        OSLDFLAGS="-lpthread -fPIC -fpie -mmacosx-version-min=10.7"
        SRCS="
@@ -136,15 +165,12 @@ elif [ "`uname -a | grep Darwin`" != "" ]; then
                ../../sanitizer_common/sanitizer_posix_libcdep.cpp
                ../../sanitizer_common/sanitizer_procmaps_mac.cpp
        "
-       if [ "`uname -a | grep x86_64`" != "" ]; then
-               SUFFIX="darwin_amd64"
+       if [ "$GOARCH" == "amd64" ]; then
                ARCHCFLAGS="-m64"
-       elif [ "`uname -a | grep arm64`" != "" ]; then
-               SUFFIX="darwin_arm64"
+       elif [ "$GOARCH" == "arm64" ]; then
                ARCHCFLAGS=""
        fi
-elif [ "`uname -a | grep MINGW`" != "" ]; then
-       SUFFIX="windows_amd64"
+elif [ "$GOOS" == "windows" ]; then
        OSCFLAGS="-Wno-error=attributes -Wno-attributes -Wno-unused-const-variable -Wno-unknown-warning-option"
        ARCHCFLAGS="-m64"
        OSLDFLAGS=""
@@ -204,6 +230,10 @@ if [ "$DEPENDS_ON_LIBC" != "1" ]; then
        fi
 fi
 
+if [ "$SKIP_TEST" == "1" ]; then
+       exit 0
+fi
+
 if [ "`uname -a | grep NetBSD`" != "" ]; then
   # Turn off ASLR in the test binary.
   /usr/sbin/paxctl +a $DIR/test