Skip to content

Instantly share code, notes, and snippets.

@felixbuenemann
Last active December 18, 2020 00:54
Show Gist options
  • Save felixbuenemann/d08875fc3e1a69fe8d6eaa724829c950 to your computer and use it in GitHub Desktop.
Save felixbuenemann/d08875fc3e1a69fe8d6eaa724829c950 to your computer and use it in GitHub Desktop.
class Go < Formula
desc "Open source programming language to build simple/reliable/efficient software"
homepage "https://golang.org"
license "BSD-3-Clause"
# stable do
# url "https://golang.org/dl/go1.15.5.src.tar.gz"
# mirror "https://fossies.org/linux/misc/go1.15.5.src.tar.gz"
# sha256 "c1076b90cf94b73ebed62a81d802cd84d43d02dea8c07abdc922c57a071c84f1"
#
# go_version = version.major_minor
# resource "gotools" do
# url "https://go.googlesource.com/tools.git",
# branch: "release-branch.go#{go_version}"
# end
# end
livecheck do
url "https://golang.org/dl/"
regex(/href=.*?go[._-]?v?(\d+(?:\.\d+)+)[._-]src\.t/i)
end
# bottle do
# sha256 "61f04e266ba1f69573ae46766f8988029c5a34f4ae4f1ac0951cf3daf4d919d1" => :big_sur
# sha256 "77f172eb6849a0f86eed4fa8eaeb9e8c69223b7f154a86505a7507b4ea78de79" => :catalina
# sha256 "70969a6147bad6960136bdea78063d39b527c993a725ad525a3cf6e9f8ad6fa5" => :mojave
# sha256 "7d39badbe6096ffd120c3e997ee16fd026d12c4037eb055d26290a39f2689582" => :high_sierra
# end
head do
url "https://go.googlesource.com/go.git"
resource "gotools" do
url "https://go.googlesource.com/tools.git"
end
end
# Don't update this unless this version cannot bootstrap the new version.
resource "gobootstrap" do
on_macos do
url "https://storage.googleapis.com/golang/go1.15.5.darwin-amd64.tar.gz"
sha256 "359a4334b8c8f5e3067e5a76f16419791ac3fef4613d8e8e1eac0b9719915f6d"
end
on_linux do
url "https://storage.googleapis.com/golang/go1.15.5.linux-amd64.tar.gz"
sha256 "9a58494e8da722c3aef248c9227b0e9c528c7318309827780f16220998180a0d"
end
end
def install
(buildpath/"gobootstrap").install resource("gobootstrap")
ENV["GOROOT_BOOTSTRAP"] = buildpath/"gobootstrap"
on_macos do
# Work around bug in Rosetta 2, see:
# https://github.com/golang/go/issues/42700
ENV["GODEBUG"] = "asyncpreemptoff=1" if Hardware::CPU.physical_cpu_arm64?
# Skip building a bootstrap compiler unless we build for arm64.
next unless Hardware::CPU.arm?
ENV["GOHOSTARCH"] = "#{Hardware::CPU.arch}"
ENV["CGO_ENABLED"] = "1"
# Write CC wrapper with proper arch otherwise CGO build will fail.
(buildpath/"cc_target").write <<~EOS
#!/bin/sh
exec "#{ENV["CC"]}" -arch "#{Hardware::CPU.arch}" "$@"
EOS
chmod "+x", "cc_target"
ENV["CC_FOR_TARGET"] = buildpath/"cc_target"
end
cd "src" do
ENV["GOROOT_FINAL"] = libexec
system "./make.bash", "--no-clean"
end
(buildpath/"pkg/obj").rmtree
rm_rf "gobootstrap" # Bootstrap not required beyond compile.
libexec.install Dir["*"]
bin.install_symlink Dir[libexec/"bin/go*"]
system bin/"go", "install", "-race", "std"
# Build and install godoc
ENV.prepend_path "PATH", bin
ENV["GOPATH"] = buildpath
(buildpath/"src/golang.org/x/tools").install resource("gotools")
cd "src/golang.org/x/tools/cmd/godoc/" do
system "go", "build"
(libexec/"bin").install "godoc"
end
bin.install_symlink libexec/"bin/godoc"
end
test do
(testpath/"hello.go").write <<~EOS
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
EOS
# Run go fmt check for no errors then run the program.
# This is a a bare minimum of go working as it uses fmt, build, and run.
system bin/"go", "fmt", "hello.go"
assert_equal "Hello World\n", shell_output("#{bin}/go run hello.go")
# godoc was installed
assert_predicate libexec/"bin/godoc", :exist?
assert_predicate libexec/"bin/godoc", :executable?
ENV["GOOS"] = "freebsd"
ENV["GOARCH"] = "amd64"
system bin/"go", "build", "hello.go"
end
end
@felixbuenemann
Copy link
Author

felixbuenemann commented Dec 2, 2020

Installs go HEAD using Homebrew for Apple M1 arm64 architecture.

Usage:

git clone https://gist.github.com/d08875fc3e1a69fe8d6eaa724829c950.git
brew install --HEAD d08875fc3e1a69fe8d6eaa724829c950/go.rb
rm -rf d08875fc3e1a69fe8d6eaa724829c950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment