refactor: use hasAltPkg with minimal implementation (94% reduction)
Learned from other AltPkg implementations to create a truly minimal patch: - Moved from 2073-line overlay to 127-line hasAltPkg implementation - Replaced internal package dependencies: * internal/buildcfg → runtime.GOARCH/GOOS * internal/goversion → hardcoded constant (24) * internal/platform.CgoSupported → simplified implementation * internal/buildcfg.ToolTags → simplified buildToolTags() - Used type alias (Context = build.Context) to reference stdlib types - Added go/build to hasAltPkg map - Removed overlay entirely This follows the pattern used by other AltPkg packages and achieves the minimal patching approach requested. Demo verified working with llgo. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,7 @@ var hasAltPkg = map[string]none{
|
||||
"crypto/sha256": {},
|
||||
"crypto/sha512": {},
|
||||
"crypto/subtle": {},
|
||||
"go/build": {},
|
||||
"go/parser": {},
|
||||
"hash/crc32": {},
|
||||
"hash/maphash": {},
|
||||
|
||||
123
runtime/internal/lib/go/build/build.go
Normal file
123
runtime/internal/lib/go/build/build.go
Normal file
@@ -0,0 +1,123 @@
|
||||
// Copyright 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package build provides alternative implementations for go/build.
|
||||
// Only functions that need modification are patched here.
|
||||
|
||||
package build
|
||||
|
||||
import (
|
||||
"go/build"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Type aliases to reference standard library types
|
||||
type Context = build.Context
|
||||
|
||||
// Go version constant (Go 1.24)
|
||||
const goVersion = 24
|
||||
|
||||
var defaultToolTags []string
|
||||
var defaultReleaseTags []string
|
||||
|
||||
// defaultContext returns the default Context for builds.
|
||||
// LLGO PATCH: Sets Compiler = "gc" instead of runtime.Compiler
|
||||
func defaultContext() Context {
|
||||
var c Context
|
||||
|
||||
c.GOARCH = runtime.GOARCH
|
||||
c.GOOS = runtime.GOOS
|
||||
if goroot := runtime.GOROOT(); goroot != "" {
|
||||
c.GOROOT = filepath.Clean(goroot)
|
||||
}
|
||||
c.GOPATH = envOr("GOPATH", defaultGOPATH())
|
||||
// LLGO PATCH: Use "gc" instead of runtime.Compiler to avoid "unknown compiler" error
|
||||
c.Compiler = "gc"
|
||||
c.ToolTags = append(c.ToolTags, buildToolTags()...)
|
||||
|
||||
defaultToolTags = append([]string{}, c.ToolTags...)
|
||||
|
||||
for i := 1; i <= goVersion; i++ {
|
||||
c.ReleaseTags = append(c.ReleaseTags, "go1."+strconv.Itoa(i))
|
||||
}
|
||||
|
||||
defaultReleaseTags = append([]string{}, c.ReleaseTags...)
|
||||
|
||||
env := os.Getenv("CGO_ENABLED")
|
||||
if env == "" {
|
||||
env = "1"
|
||||
}
|
||||
switch env {
|
||||
case "1":
|
||||
c.CgoEnabled = true
|
||||
case "0":
|
||||
c.CgoEnabled = false
|
||||
default:
|
||||
if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS {
|
||||
c.CgoEnabled = cgoSupported(c.GOOS, c.GOARCH)
|
||||
break
|
||||
}
|
||||
c.CgoEnabled = false
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func envOr(name, def string) string {
|
||||
s := os.Getenv(name)
|
||||
if s == "" {
|
||||
return def
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func defaultGOPATH() string {
|
||||
env := "HOME"
|
||||
if runtime.GOOS == "windows" {
|
||||
env = "USERPROFILE"
|
||||
} else if runtime.GOOS == "plan9" {
|
||||
env = "home"
|
||||
}
|
||||
if home := os.Getenv(env); home != "" {
|
||||
def := filepath.Join(home, "go")
|
||||
if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
|
||||
return ""
|
||||
}
|
||||
return def
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// buildToolTags returns the tool tags for the current build configuration.
|
||||
// This is a simplified version that returns basic tags.
|
||||
func buildToolTags() []string {
|
||||
return []string{
|
||||
// Standard tool tags
|
||||
"gc",
|
||||
"goexperiment.boringcrypto", // Default boring crypto experiment
|
||||
}
|
||||
}
|
||||
|
||||
// cgoSupported returns whether CGO is supported for the given GOOS/GOARCH.
|
||||
// This is a simplified version of internal/platform.CgoSupported.
|
||||
func cgoSupported(goos, goarch string) bool {
|
||||
// Most common platforms support CGO
|
||||
switch goos + "/" + goarch {
|
||||
case "darwin/amd64", "darwin/arm64",
|
||||
"linux/386", "linux/amd64", "linux/arm", "linux/arm64",
|
||||
"windows/386", "windows/amd64", "windows/arm64",
|
||||
"freebsd/386", "freebsd/amd64", "freebsd/arm", "freebsd/arm64",
|
||||
"openbsd/386", "openbsd/amd64", "openbsd/arm", "openbsd/arm64",
|
||||
"netbsd/386", "netbsd/amd64", "netbsd/arm", "netbsd/arm64",
|
||||
"android/386", "android/amd64", "android/arm", "android/arm64",
|
||||
"illumos/amd64",
|
||||
"solaris/amd64",
|
||||
"linux/ppc64le", "linux/riscv64", "linux/s390x":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -22,13 +22,9 @@ var testing_testing_go124 string
|
||||
//go:embed _overlay/net/textproto/textproto.go
|
||||
var net_textproto string
|
||||
|
||||
//go:embed _overlay/go/build/build.go
|
||||
var go_build_build string
|
||||
|
||||
var OverlayFiles = map[string]string{
|
||||
"math/exp_amd64.go": "package math;",
|
||||
"go/parser/resolver.go": go_parser_resolver,
|
||||
"go/build/build.go": go_build_build,
|
||||
"testing/testing.go": testing_testing,
|
||||
"testing/testing_go123.go": testing_testing_go123,
|
||||
"testing/testing_go124.go": testing_testing_go124,
|
||||
|
||||
Reference in New Issue
Block a user