wasip1: deduplicate target selection

Before this change, `GOOS=wasip1 GOARCH=wasm` and `-target=wasip1` were
entirely separate configurations that just happened to be the same in
practice. This change makes sure we keep only one of them to avoid
inconsistencies.
This commit is contained in:
Ayke van Laethem
2024-08-26 18:56:51 +02:00
parent d144b11611
commit 910cf87ed5
3 changed files with 34 additions and 57 deletions
+34 -28
View File
@@ -176,8 +176,14 @@ func (spec *TargetSpec) resolveInherits() error {
// Load a target specification. // Load a target specification.
func LoadTarget(options *Options) (*TargetSpec, error) { func LoadTarget(options *Options) (*TargetSpec, error) {
if options.Target == "" { switch options.Target {
return defaultTarget(options) case "":
// No target given, use GOOS/GOARCH env variables.
return defaultTarget(options, options.GOOS, options.GOARCH)
case "wasip1", "wasi":
// Special case: support both -target=wasip1 and the GOOS/GOARCH pair.
// They should both have the same effect.
return defaultTarget(options, "wasip1", "wasm")
} }
// See whether there is a target specification for this target (e.g. // See whether there is a target specification for this target (e.g.
@@ -240,11 +246,11 @@ func GetTargetSpecs() (map[string]*TargetSpec, error) {
// Load a target from environment variables (which default to // Load a target from environment variables (which default to
// runtime.GOOS/runtime.GOARCH). // runtime.GOOS/runtime.GOARCH).
func defaultTarget(options *Options) (*TargetSpec, error) { func defaultTarget(options *Options, goos, goarch string) (*TargetSpec, error) {
spec := TargetSpec{ spec := TargetSpec{
GOOS: options.GOOS, GOOS: goos,
GOARCH: options.GOARCH, GOARCH: goarch,
BuildTags: []string{options.GOOS, options.GOARCH}, BuildTags: []string{goos, goarch},
GC: "precise", GC: "precise",
Scheduler: "tasks", Scheduler: "tasks",
Linker: "cc", Linker: "cc",
@@ -255,7 +261,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
// Configure target based on GOARCH. // Configure target based on GOARCH.
var llvmarch string var llvmarch string
switch options.GOARCH { switch goarch {
case "386": case "386":
llvmarch = "i386" llvmarch = "i386"
spec.CPU = "pentium4" spec.CPU = "pentium4"
@@ -325,12 +331,12 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
case "arm64": case "arm64":
spec.CPU = "generic" spec.CPU = "generic"
llvmarch = "aarch64" llvmarch = "aarch64"
if options.GOOS == "darwin" { if goos == "darwin" {
spec.Features = "+fp-armv8,+neon" spec.Features = "+fp-armv8,+neon"
// Looks like Apple prefers to call this architecture ARM64 // Looks like Apple prefers to call this architecture ARM64
// instead of AArch64. // instead of AArch64.
llvmarch = "arm64" llvmarch = "arm64"
} else if options.GOOS == "windows" { } else if goos == "windows" {
spec.Features = "+fp-armv8,+neon,-fmv" spec.Features = "+fp-armv8,+neon,-fmv"
} else { // linux } else { // linux
spec.Features = "+fp-armv8,+neon,-fmv,-outline-atomics" spec.Features = "+fp-armv8,+neon,-fmv,-outline-atomics"
@@ -338,7 +344,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
case "mips", "mipsle": case "mips", "mipsle":
spec.CPU = "mips32r2" spec.CPU = "mips32r2"
spec.CFlags = append(spec.CFlags, "-fno-pic") spec.CFlags = append(spec.CFlags, "-fno-pic")
if options.GOARCH == "mips" { if goarch == "mips" {
llvmarch = "mips" // big endian llvmarch = "mips" // big endian
} else { } else {
llvmarch = "mipsel" // little endian llvmarch = "mipsel" // little endian
@@ -364,16 +370,16 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
"-msign-ext", "-msign-ext",
) )
default: default:
return nil, fmt.Errorf("unknown GOARCH=%s", options.GOARCH) return nil, fmt.Errorf("unknown GOARCH=%s", goarch)
} }
// Configure target based on GOOS. // Configure target based on GOOS.
llvmos := options.GOOS llvmos := goos
llvmvendor := "unknown" llvmvendor := "unknown"
switch options.GOOS { switch goos {
case "darwin": case "darwin":
platformVersion := "10.12.0" platformVersion := "10.12.0"
if options.GOARCH == "arm64" { if goarch == "arm64" {
platformVersion = "11.0.0" // first macosx platform with arm64 support platformVersion = "11.0.0" // first macosx platform with arm64 support
} }
llvmvendor = "apple" llvmvendor = "apple"
@@ -395,7 +401,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
spec.RTLib = "compiler-rt" spec.RTLib = "compiler-rt"
spec.Libc = "musl" spec.Libc = "musl"
spec.LDFlags = append(spec.LDFlags, "--gc-sections") spec.LDFlags = append(spec.LDFlags, "--gc-sections")
if options.GOARCH == "arm64" { if goarch == "arm64" {
// Disable outline atomics. For details, see: // Disable outline atomics. For details, see:
// https://cpufun.substack.com/p/atomics-in-aarch64 // https://cpufun.substack.com/p/atomics-in-aarch64
// A better way would be to fully support outline atomics, which // A better way would be to fully support outline atomics, which
@@ -420,7 +426,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
// normally present in Go (without explicitly opting in). // normally present in Go (without explicitly opting in).
// For more discussion: // For more discussion:
// https://groups.google.com/g/Golang-nuts/c/Jd9tlNc6jUE/m/Zo-7zIP_m3MJ?pli=1 // https://groups.google.com/g/Golang-nuts/c/Jd9tlNc6jUE/m/Zo-7zIP_m3MJ?pli=1
switch options.GOARCH { switch goarch {
case "amd64": case "amd64":
spec.LDFlags = append(spec.LDFlags, spec.LDFlags = append(spec.LDFlags,
"-m", "i386pep", "-m", "i386pep",
@@ -455,16 +461,16 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
) )
llvmos = "wasi" llvmos = "wasi"
default: default:
return nil, fmt.Errorf("unknown GOOS=%s", options.GOOS) return nil, fmt.Errorf("unknown GOOS=%s", goos)
} }
// Target triples (which actually have four components, but are called // Target triples (which actually have four components, but are called
// triples for historical reasons) have the form: // triples for historical reasons) have the form:
// arch-vendor-os-environment // arch-vendor-os-environment
spec.Triple = llvmarch + "-" + llvmvendor + "-" + llvmos spec.Triple = llvmarch + "-" + llvmvendor + "-" + llvmos
if options.GOOS == "windows" { if goos == "windows" {
spec.Triple += "-gnu" spec.Triple += "-gnu"
} else if options.GOOS == "linux" { } else if goos == "linux" {
// We use musl on Linux (not glibc) so we should use -musleabi* instead // We use musl on Linux (not glibc) so we should use -musleabi* instead
// of -gnueabi*. // of -gnueabi*.
// The *hf suffix selects between soft/hard floating point ABI. // The *hf suffix selects between soft/hard floating point ABI.
@@ -476,15 +482,15 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
} }
// Add extra assembly files (needed for the scheduler etc). // Add extra assembly files (needed for the scheduler etc).
if options.GOARCH != "wasm" { if goarch != "wasm" {
suffix := "" suffix := ""
if options.GOOS == "windows" && options.GOARCH == "amd64" { if goos == "windows" && goarch == "amd64" {
// Windows uses a different calling convention on amd64 from other // Windows uses a different calling convention on amd64 from other
// operating systems so we need separate assembly files. // operating systems so we need separate assembly files.
suffix = "_windows" suffix = "_windows"
} }
asmGoarch := options.GOARCH asmGoarch := goarch
if options.GOARCH == "mips" || options.GOARCH == "mipsle" { if goarch == "mips" || goarch == "mipsle" {
asmGoarch = "mipsx" asmGoarch = "mipsx"
} }
spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/asm_"+asmGoarch+suffix+".S") spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/asm_"+asmGoarch+suffix+".S")
@@ -492,11 +498,11 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
} }
// Configure the emulator. // Configure the emulator.
if options.GOARCH != runtime.GOARCH { if goarch != runtime.GOARCH {
// Some educated guesses as to how to invoke helper programs. // Some educated guesses as to how to invoke helper programs.
spec.GDB = []string{"gdb-multiarch"} spec.GDB = []string{"gdb-multiarch"}
if options.GOOS == "linux" { if goos == "linux" {
switch options.GOARCH { switch goarch {
case "386": case "386":
// amd64 can _usually_ run 32-bit programs, so skip the emulator in that case. // amd64 can _usually_ run 32-bit programs, so skip the emulator in that case.
if runtime.GOARCH != "amd64" { if runtime.GOARCH != "amd64" {
@@ -515,8 +521,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
} }
} }
} }
if options.GOOS != runtime.GOOS { if goos != runtime.GOOS {
if options.GOOS == "windows" { if goos == "windows" {
spec.Emulator = "wine {}" spec.Emulator = "wine {}"
} }
} }
-3
View File
@@ -1,3 +0,0 @@
{
"inherits": ["wasip1"]
}
-26
View File
@@ -1,26 +0,0 @@
{
"llvm-target": "wasm32-unknown-wasi",
"cpu": "generic",
"features": "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext",
"build-tags": ["tinygo.wasm"],
"goos": "wasip1",
"goarch": "wasm",
"linker": "wasm-ld",
"libc": "wasi-libc",
"rtlib": "compiler-rt",
"scheduler": "asyncify",
"default-stack-size": 65536,
"cflags": [
"-mbulk-memory",
"-mnontrapping-fptoint",
"-msign-ext"
],
"ldflags": [
"--stack-first",
"--no-demangle"
],
"extra-files": [
"src/runtime/asm_tinygowasm.S"
],
"emulator": "wasmtime --dir={tmpDir}::/tmp {}"
}