From 8ac7ada7f92f1b557106c1c4ac84c503da05ae89 Mon Sep 17 00:00:00 2001 From: xgopilot Date: Thu, 16 Oct 2025 06:05:54 +0000 Subject: [PATCH] refactor: use go:linkname to link internal/platform.CgoSupported MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of creating a custom platform package, use the go:linkname directive to directly link to Go's standard library internal/platform.CgoSupported function. This approach: - Eliminates the need to maintain a copy of platform support data - Uses Go's canonical platform information directly - Reduces code duplication and maintenance burden - Follows Go's linkname pattern for accessing internal packages Changes: - Added import _ "unsafe" to enable linkname - Added //go:linkname directive for cgoSupported function - Removed custom runtime/internal/lib/internal/platform package - Updated function call from platform.CgoSupported to cgoSupported 🤖 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang --- runtime/internal/lib/go/build/build.go | 8 ++- .../lib/internal/platform/platform.go | 65 ------------------- 2 files changed, 5 insertions(+), 68 deletions(-) delete mode 100644 runtime/internal/lib/internal/platform/platform.go diff --git a/runtime/internal/lib/go/build/build.go b/runtime/internal/lib/go/build/build.go index 65afc07f..b79c5a69 100644 --- a/runtime/internal/lib/go/build/build.go +++ b/runtime/internal/lib/go/build/build.go @@ -14,8 +14,7 @@ import ( "runtime" "strconv" "strings" - - "github.com/goplus/llgo/runtime/internal/lib/internal/platform" + _ "unsafe" ) // Type aliases to reference standard library types @@ -79,7 +78,7 @@ func defaultContext() Context { c.CgoEnabled = false default: if runtime.GOARCH == c.GOARCH && runtime.GOOS == c.GOOS { - c.CgoEnabled = platform.CgoSupported(c.GOOS, c.GOARCH) + c.CgoEnabled = cgoSupported(c.GOOS, c.GOARCH) break } c.CgoEnabled = false @@ -122,3 +121,6 @@ func buildToolTags() []string { "goexperiment.boringcrypto", // Default boring crypto experiment } } + +//go:linkname cgoSupported internal/platform.CgoSupported +func cgoSupported(goos, goarch string) bool diff --git a/runtime/internal/lib/internal/platform/platform.go b/runtime/internal/lib/internal/platform/platform.go deleted file mode 100644 index f5b1f6c7..00000000 --- a/runtime/internal/lib/internal/platform/platform.go +++ /dev/null @@ -1,65 +0,0 @@ -// 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 platform provides platform-specific utilities. -package platform - -// llgo:skipall -type _platform struct{} - -// An OSArch is a pair of GOOS and GOARCH values indicating a platform. -type OSArch struct { - GOOS, GOARCH string -} - -func (p OSArch) String() string { - return p.GOOS + "/" + p.GOARCH -} - -// osArchInfo describes information about an OSArch. -type osArchInfo struct { - CgoSupported bool - FirstClass bool - Broken bool -} - -// distInfo maps OSArch to information about cgo support. -// This is a simplified version covering the most common platforms. -var distInfo = map[OSArch]osArchInfo{ - {"darwin", "amd64"}: {CgoSupported: true, FirstClass: true}, - {"darwin", "arm64"}: {CgoSupported: true, FirstClass: true}, - {"linux", "386"}: {CgoSupported: true, FirstClass: true}, - {"linux", "amd64"}: {CgoSupported: true, FirstClass: true}, - {"linux", "arm"}: {CgoSupported: true, FirstClass: true}, - {"linux", "arm64"}: {CgoSupported: true, FirstClass: true}, - {"linux", "ppc64le"}: {CgoSupported: true}, - {"linux", "riscv64"}: {CgoSupported: true}, - {"linux", "s390x"}: {CgoSupported: true}, - {"windows", "386"}: {CgoSupported: true, FirstClass: true}, - {"windows", "amd64"}: {CgoSupported: true, FirstClass: true}, - {"windows", "arm64"}: {CgoSupported: true}, - {"freebsd", "386"}: {CgoSupported: true}, - {"freebsd", "amd64"}: {CgoSupported: true}, - {"freebsd", "arm"}: {CgoSupported: true}, - {"freebsd", "arm64"}: {CgoSupported: true}, - {"openbsd", "386"}: {CgoSupported: true}, - {"openbsd", "amd64"}: {CgoSupported: true}, - {"openbsd", "arm"}: {CgoSupported: true}, - {"openbsd", "arm64"}: {CgoSupported: true}, - {"netbsd", "386"}: {CgoSupported: true}, - {"netbsd", "amd64"}: {CgoSupported: true}, - {"netbsd", "arm"}: {CgoSupported: true}, - {"netbsd", "arm64"}: {CgoSupported: true}, - {"android", "386"}: {CgoSupported: true}, - {"android", "amd64"}: {CgoSupported: true}, - {"android", "arm"}: {CgoSupported: true}, - {"android", "arm64"}: {CgoSupported: true}, - {"illumos", "amd64"}: {CgoSupported: true}, - {"solaris", "amd64"}: {CgoSupported: true}, -} - -// CgoSupported reports whether goos/goarch supports cgo. -func CgoSupported(goos, goarch string) bool { - return distInfo[OSArch{goos, goarch}].CgoSupported -}