Skip to content

Commit eac875b

Browse files
bernhardkaindlBernhard Kaindl
authored andcommitted
go,gcc: Support external go compilers for Go bootstrap
For ARM64, fallback to gccgo. (go-bootstrap@1.4 can't support ARM64)
1 parent 25e4d48 commit eac875b

4 files changed

Lines changed: 56 additions & 11 deletions

File tree

etc/spack/defaults/packages.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ packages:
2727
fuse: [libfuse]
2828
gl: [glx, osmesa]
2929
glu: [mesa-glu, openglu]
30-
golang: [gcc]
30+
golang: [go, gcc]
31+
go-external-or-gccgo-bootstrap: [go-bootstrap, gcc]
3132
iconv: [libiconv]
3233
ipp: [intel-ipp]
3334
java: [openjdk, jdk, ibm-java]

var/spack/repos/builtin/packages/gcc/package.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,24 @@ class Gcc(AutotoolsPackage, GNUMirrorPackage):
206206
provides("golang@:1.4", when="@5:")
207207
provides("golang@:1.6.1", when="@6:")
208208
provides("golang@:1.8", when="@7:")
209+
provides("golang@:1.10", when="@8:")
210+
provides("golang@:1.12", when="@9:")
211+
provides("golang@:1.14", when="@10:")
212+
provides("golang@:1.16", when="@11:")
213+
provides("golang@:1.18", when="@11:")
209214
# GCC 4.6 added support for the Go programming language.
210215
# See https://gcc.gnu.org/gcc-4.6/changes.html
211216
conflicts("@:4.5", msg="support for Go has been added in GCC 4.6")
217+
# aarch64 machines (including Macs with Apple silicon) can't use
218+
# go-bootstrap because it pre-dates aarch64 support in Go. When not
219+
# using an external go bootstrap go, These machines have to rely on
220+
# Go support in gcc (which may require compiling a version of gcc
221+
# with Go support just to satisfy this requirement). However,
222+
# there's also a bug in some versions of GCC's Go front-end that prevents
223+
# these versions from properly bootstrapping Go. (See issue #47771
224+
# https://github.com/golang/go/issues/47771 ) On the 10.x branch, we need
225+
# at least 10.4. On the 11.x branch, we need at least 11.3:
226+
provides("go-external-or-gccgo-bootstrap", when="gcc@10.4.0:10,11.3.0:target=aarch64:")
212227
# Go is not supported on macOS
213228
conflicts("platform=darwin", msg="Go not supported on MacOS")
214229

@@ -442,7 +457,7 @@ class Gcc(AutotoolsPackage, GNUMirrorPackage):
442457

443458
@classproperty
444459
def executables(cls):
445-
names = [r"gcc", r"[^\w]?g\+\+", r"gfortran", r"gdc"]
460+
names = [r"gcc", r"[^\w]?g\+\+", r"gfortran", r"gdc", r"gccgo"]
446461
suffixes = [r"", r"-mp-\d+\.\d", r"-\d+\.\d", r"-\d+", r"\d\d"]
447462
return [r"".join(x) for x in itertools.product(names, suffixes)]
448463

@@ -520,6 +535,9 @@ def determine_variants(cls, exes, version_str):
520535
elif "gcc" in basename:
521536
languages.add("c")
522537
compilers["c"] = exe
538+
elif "gccgo" in basename:
539+
languages.add("go")
540+
compilers["go"] = exe
523541
elif "gdc" in basename:
524542
languages.add("d")
525543
compilers["d"] = exe

var/spack/repos/builtin/packages/go-bootstrap/package.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
55

6+
import re
7+
68
from spack.package import *
79

810
# THIS PACKAGE SHOULD NOT EXIST
@@ -47,6 +49,25 @@ class GoBootstrap(Package):
4749
conflicts("os=monterey", msg="go-bootstrap won't build on new macOS")
4850
conflicts("target=aarch64:", msg="Go bootstrap doesn't support aarch64 architectures")
4951

52+
# This virtual package allows a fallback to gccgo for aarch64,
53+
# where go-bootstrap cannot be built(aarch64 was added with Go 1.5)
54+
provides("go-external-or-gccgo-bootstrap")
55+
56+
# Support for aarch64 was added in Go 1.5, use an external package or gccgo instead:
57+
conflicts("@:1.4", when="target=aarch64:")
58+
59+
executables = ["^go$"]
60+
61+
# When the user adds a go compiler using ``spack external find go-bootstrap``,
62+
# this lets us get the version for packages.yaml. Then, the solver can avoid
63+
# to build the bootstrap go compiler(for aarch64, it's only gccgo) from source:
64+
@classmethod
65+
def determine_version(cls, exe):
66+
"""Return the version of an externally provided go executable or ``None``"""
67+
output = Executable(exe)("version", output=str, error=str)
68+
match = re.search(r"go version go(\S+)", output)
69+
return match.group(1) if match else None
70+
5071
def patch(self):
5172
if self.spec.satisfies("@:1.4.3"):
5273
# NOTE: Older versions of Go attempt to download external files that have
@@ -72,7 +93,13 @@ def install(self, spec, prefix):
7293
install_tree(".", prefix)
7394

7495
def setup_dependent_build_environment(self, env, dependent_spec):
75-
env.set("GOROOT_BOOTSTRAP", self.spec.prefix)
96+
"""Set GOROOT_BOOTSTRAP: When using an external compiler, get its GOROOT env"""
97+
if self.spec.external:
98+
# Use the go compiler added by ``spack external find go-bootstrap``:
99+
goroot = Executable(self.spec.prefix.bin.go)("env", "GOROOT", output=str)
100+
else:
101+
goroot = self.spec.prefix
102+
env.set("GOROOT_BOOTSTRAP", goroot)
76103

77104
def setup_build_environment(self, env):
78105
env.set("GOROOT_FINAL", self.spec.prefix)

var/spack/repos/builtin/packages/go/package.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,13 @@ class Go(Package):
152152
# aarch64 machines (including Macs with Apple silicon) can't use
153153
# go-bootstrap because it pre-dates aarch64 support in Go. These machines
154154
# have to rely on Go support in gcc (which may require compiling a version
155-
# of gcc with Go support just to satisfy this requirement). However,
156-
# there's also a bug in some versions of GCC's Go front-end that prevents
157-
# these versions from properly bootstrapping Go. (See issue #47771
158-
# https://github.com/golang/go/issues/47771 ) On the 10.x branch, we need
159-
# at least 10.4. On the 11.x branch, we need at least 11.3.
160-
161-
if platform.machine() == "aarch64":
162-
depends_on("gcc@10.4.0:10,11.3.0: languages=go", type="build")
155+
# of gcc with Go support just to satisfy this requirement) or external go:
156+
157+
# #27769: On M1/MacOS, platform.machine() may return arm64:
158+
if platform.machine() in ["arm64", "aarch64"]:
159+
# Use an external go compiler from packages.yaml/`spack external find go-bootstrap`,
160+
# but fallback to build go-bootstrap@1.4 or to gcc with languages=go (for aarch64):
161+
depends_on("go-external-or-gccgo-bootstrap", type="build")
163162
else:
164163
depends_on("go-bootstrap", type="build")
165164

0 commit comments

Comments
 (0)