Debian Codegraph @

debian/helpers/goenv.sh

raw
1 #!/bin/sh
2 set -eu
3
4 __goos__deb_arch_os() {
5 case "$1" in
6 kfreebsd) echo freebsd ;;
7 linux) echo "$1" ;;
8 *) echo >&2 "error: unrecongized DEB_*_ARCH_OS: $1"; exit 1 ;;
9 esac
10 }
11
12 __goarch__deb_arch_cpu() {
13 case "$1" in
14 amd64|arm|arm64|loong64|mips|ppc64|riscv64|s390x) echo "$1" ;;
15 i386) echo 386 ;;
16 mips64el) echo mips64le ;;
17 mipsel) echo mipsle ;;
18 ppc64el) echo ppc64le ;;
19 *) echo >&2 "error: unrecongized DEB_*_ARCH_CPU: $1"; exit 1 ;;
20 esac
21 }
22
23 # build machine
24 # The machine the package is built on.
25 #
26 # host machine
27 # The machine the package is built for.
28
29 DEB_HOST_ARCH="$(dpkg-architecture --query DEB_HOST_ARCH 2>/dev/null)"
30 # set _OS and _CPU explicitly via --force so they are never autodetected via CC (this makes it easier to test this script for various target architectures via a simple "DEB_HOST_ARCH=xxx ./debian/helpers/goenv.sh env")
31 DEB_HOST_ARCH_OS="$(dpkg-architecture --force --host-arch "$DEB_HOST_ARCH" --query DEB_HOST_ARCH_OS)"
32 DEB_HOST_ARCH_CPU="$(dpkg-architecture --force --host-arch "$DEB_HOST_ARCH" --query DEB_HOST_ARCH_CPU)"
33 export DEB_HOST_ARCH DEB_HOST_ARCH_OS DEB_HOST_ARCH_CPU
34
35 DEB_BUILD_ARCH_OS="$(dpkg-architecture --query DEB_BUILD_ARCH_OS)"
36 GOHOSTOS="$(__goos__deb_arch_os "$DEB_BUILD_ARCH_OS")"
37 GOOS="$(__goos__deb_arch_os "$DEB_HOST_ARCH_OS")"
38 export GOHOSTOS GOOS
39
40 DEB_BUILD_ARCH_CPU="$(dpkg-architecture --query DEB_BUILD_ARCH_CPU)"
41 GOHOSTARCH="$(__goarch__deb_arch_cpu "$DEB_BUILD_ARCH_CPU")"
42 GOARCH="$(__goarch__deb_arch_cpu "$DEB_HOST_ARCH_CPU")"
43 export GOHOSTARCH GOARCH
44
45 if [ -z "$GOHOSTOS" -o -z "$GOOS" -o -z "$GOHOSTARCH" -o -z "$GOARCH" ]; then
46 exit 1
47 fi
48
49 # Avoid all "go" invocations downloading different toolchains during build.
50 export GOTOOLCHAIN=local
51
52 # Always not use sse2. This is important to ensure that the binaries we build
53 # (both when compiling golang on the buildds and when users cross-compile for
54 # 386) can actually run on older CPUs (where old means e.g. an AMD Athlon XP
55 # 2400+). See http://bugs.debian.org/753160 and
56 # https://code.google.com/p/go/issues/detail?id=8152
57 export GO386=softfloat
58
59 unset GOARM
60 if [ "$GOARCH" = 'arm' ]; then
61 # start with GOARM=5 for maximum compatibility (see note about GO386 above)
62 GOARM=5
63 case "$DEB_HOST_ARCH" in
64 armhf) GOARM=6 ;; # TODO detect Debian vs Raspbian and upgrade this to 7 by default?
65 esac
66 export GOARM
67 fi
68
69 # set CC_FOR_os_arch variables appropriately for supported architectures so that cross-compile even with cgo "just works" in more cases (also consistently across architectures for better reproducibility)
70 linuxArchList="$(dpkg-architecture --list-known --match-wildcard 'gnu-linux-any')"
71 # hotly contested/overlapping architectures: let's arbitrarily choose "armhf" as the cross-compile target for "GOARCH=arm" unless we're explicitly building the "armel" package
72 # this matches upstream's behavior for GOARM: https://github.com/golang/go/blob/go1.25.0/src/cmd/dist/util.go#L397-L405 (set to "7" if unspecified and cross-compiling)
73 armArchForCC='armhf'
74 if [ "$GOARCH" = 'arm' ]; then
75 armArchForCC="$DEB_HOST_ARCH"
76 fi
77 for dpkgArch in $linuxArchList; do
78 archCpu="$(dpkg-architecture --force --host-arch "$dpkgArch" --query DEB_HOST_ARCH_CPU 2>/dev/null)"
79 if goArch="$(__goarch__deb_arch_cpu "$archCpu" 2>/dev/null)"; then
80 if [ "$goArch" = 'arm' ] && [ "$dpkgArch" != "$armArchForCC" ]; then
81 continue
82 fi
83 gnuType="$(dpkg-architecture --force --host-arch "$dpkgArch" --query DEB_HOST_GNU_TYPE 2>/dev/null)"
84 export "CC_FOR_linux_${goArch}=${gnuType}-gcc"
85 export "CXX_FOR_linux_${goArch}=${gnuType}-g++"
86 unset gnuType
87 fi
88 unset archCpu goArch
89 done
90 unset linuxArchList dpkgArch
91
92 unset CGO_ENABLED
93 if [ "$GOOS" = 'linux' ] && [ "$GOARCH" != "$GOHOSTARCH" ] && eval 'test -n "${CC_FOR_'"${GOHOSTOS}_${GOHOSTARCH}"':-}" && "${CC_FOR_'"${GOHOSTOS}_${GOHOSTARCH}"'}" --version > /dev/null'; then
94 # if we're cross-compiling, let's check whether we can safely explicitly enable CGO (and whether we should)
95 # https://github.com/golang/go/blob/go1.24.5/src/cmd/dist/build.go#L1779-L1792 ("cgoEnabled" map)
96 # minus https://github.com/golang/go/blob/go1.24.5/src/cmd/dist/build.go#L1822-L1830 ("broken" map)
97 # (we could use "--dist-tool" to pre-compile the "dist" tool to then use "dist list -json" to get "CgoSupported" instead of hard-coding this list, but then we need something that can parse JSON too, and all that just feels like way too much just to get this list of platforms which should enable CGO by default -Tianon)
98 # $ go tool dist list -json | jq -r 'map(select(.CgoSupported and .GOOS == "linux") | .GOOS + "/" + .GOARCH) | (map(length) | max) as $max | map(. + (" " * ($max - length))) | join(" |\\\n") + " )"'
99 case "$GOOS/$GOARCH" in
100 linux/386 |\
101 linux/amd64 |\
102 linux/arm |\
103 linux/arm64 |\
104 linux/loong64 |\
105 linux/mips |\
106 linux/mips64 |\
107 linux/mips64le |\
108 linux/mipsle |\
109 linux/ppc64le |\
110 linux/riscv64 |\
111 linux/s390x )
112 if eval 'test -n "${CC_FOR_'"${GOOS}_${GOARCH}"':-}" && "${CC_FOR_'"${GOOS}_${GOARCH}"'}" --version > /dev/null'; then
113 export CGO_ENABLED=1
114 fi
115 ;;
116 esac
117 fi
118
119 exec "$@"