From 9486c76d705ecac82c7a11f343cf3671281c4283 Mon Sep 17 00:00:00 2001
From: Ross Miller <rgmiller@ornl.gov>
Date: Wed, 4 May 2022 10:16:53 -0400
Subject: Use gccgo to bootstrap go on aarch64 (#30350)

The go-bootstrap package doesn't work on aarch64 platforms, so the only way
to build Go is to use gccgo.

Also, some versions of gccgo have a bug that prevents them from compiling
go (see golang/go#47771), so this patch limits gcc to versions newer than
10.4.0 or 11.3.0.

Co-authored-by: Massimiliano Culpo <massimiliano.culpo@gmail.com>
---
 var/spack/repos/builtin/packages/go-bootstrap/package.py |  4 ++--
 var/spack/repos/builtin/packages/go/package.py           | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 3 deletions(-)

(limited to 'var')

diff --git a/var/spack/repos/builtin/packages/go-bootstrap/package.py b/var/spack/repos/builtin/packages/go-bootstrap/package.py
index 2549d526a0..ec10c89ee5 100644
--- a/var/spack/repos/builtin/packages/go-bootstrap/package.py
+++ b/var/spack/repos/builtin/packages/go-bootstrap/package.py
@@ -36,8 +36,8 @@ class GoBootstrap(Package):
     depends_on('git', type=('build', 'link', 'run'))
 
     conflicts('os=monterey', msg="go-bootstrap won't build on new macOS")
-    conflicts('target=aarch64:', when='platform=darwin',
-              msg='Go bootstrap is too old for Apple Silicon')
+    conflicts('target=aarch64:',
+              msg="Go bootstrap doesn't support aarch64 architectures")
 
     def patch(self):
         if self.spec.satisfies('@:1.4.3'):
diff --git a/var/spack/repos/builtin/packages/go/package.py b/var/spack/repos/builtin/packages/go/package.py
index 125355e81b..f7c0fc1f41 100644
--- a/var/spack/repos/builtin/packages/go/package.py
+++ b/var/spack/repos/builtin/packages/go/package.py
@@ -4,6 +4,7 @@
 # SPDX-License-Identifier: (Apache-2.0 OR MIT)
 
 import os
+import platform
 import re
 
 import llnl.util.tty as tty
@@ -130,7 +131,20 @@ class Go(Package):
     provides('golang')
 
     depends_on('git', type=('build', 'link', 'run'))
-    depends_on('go-bootstrap', type='build')
+
+    # aarch64 machines (including Macs with Apple silicon) can't use
+    # go-bootstrap because it pre-dates aarch64 support in Go.  These machines
+    # have to rely on Go support in gcc (which may require compiling a version
+    # of gcc with Go support just to satisfy this requirement).  However,
+    # there's also a bug in some versions of GCC's Go front-end that prevents
+    # these versions from properly bootstrapping Go.  (See issue #47771
+    # https://github.com/golang/go/issues/47771 )  On the 10.x branch, we need
+    # at least 10.4.  On the 11.x branch, we need at least 11.3.
+
+    if platform.machine() == 'aarch64':
+        depends_on('gcc@10.4.0:10,11.3.0: languages=go', type='build')
+    else:
+        depends_on('go-bootstrap', type='build')
 
     # https://github.com/golang/go/issues/17545
     patch('time_test.patch', when='@1.6.4:1.7.4')
-- 
cgit v1.2.3-70-g09d2