diff --git a/Formula/g/go@1.21.rb b/Formula/g/go@1.21.rb new file mode 100644 index 0000000000000..e782c69d95df3 --- /dev/null +++ b/Formula/g/go@1.21.rb @@ -0,0 +1,88 @@ +class GoAT121 < Formula + desc "Open source programming language to build simple/reliable/efficient software" + homepage "https://go.dev/" + url "https://go.dev/dl/go1.21.7.src.tar.gz" + mirror "https://fossies.org/linux/misc/go1.21.7.src.tar.gz" + sha256 "00197ab20f33813832bff62fd93cca1c42a08cc689a32a6672ca49591959bff6" + license "BSD-3-Clause" + + livecheck do + url "https://go.dev/dl/?mode=json" + regex(/^go[._-]?v?(1\.21(?:\.\d+)*)[._-]src\.t.+$/i) + strategy :json do |json, regex| + json.map do |release| + next if release["stable"] != true + next if release["files"].none? { |file| file["filename"].match?(regex) } + + release["version"][/(\d+(?:\.\d+)+)/, 1] + end + end + end + + keg_only :versioned_formula + + depends_on "go" => :build + + def install + ENV["GOROOT_BOOTSTRAP"] = buildpath/"gobootstrap" + + cd "src" do + ENV["GOROOT_FINAL"] = libexec + # Set portable defaults for CC/CXX to be used by cgo + with_env(CC: "cc", CXX: "c++") { system "./make.bash" } + end + + libexec.install Dir["*"] + bin.install_symlink Dir[libexec/"bin/go*"] + + system bin/"go", "install", "std", "cmd" + + # Remove useless files. + # Breaks patchelf because folder contains weird debug/test files + (libexec/"src/debug/elf/testdata").rmtree + # Binaries built for an incompatible architecture + (libexec/"src/runtime/pprof/testdata").rmtree + 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") + + with_env(GOOS: "freebsd", GOARCH: "amd64") do + system bin/"go", "build", "hello.go" + end + + (testpath/"hello_cgo.go").write <<~EOS + package main + + /* + #include + #include + void hello() { printf("%s\\n", "Hello from cgo!"); fflush(stdout); } + */ + import "C" + + func main() { + C.hello() + } + EOS + + # Try running a sample using cgo without CC or CXX set to ensure that the + # toolchain's default choice of compilers work + with_env(CC: nil, CXX: nil) do + assert_equal "Hello from cgo!\n", shell_output("#{bin}/go run hello_cgo.go") + end + end +end