From b0aef96340bed9dcaefcb2cf8ae2d2ce5718bd72 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 21 Feb 2025 10:33:54 +0100 Subject: [PATCH] fix: only infer target for wasm when GOOS and GOARCH are set correctly, not just based on file extension Signed-off-by: deadprogram --- builder/builder_test.go | 1 - compileopts/target.go | 31 +++---------------------------- main.go | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/builder/builder_test.go b/builder/builder_test.go index 1d4584c34..ccccef30b 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -69,7 +69,6 @@ func TestClangAttributes(t *testing.T) { {GOOS: "darwin", GOARCH: "arm64"}, {GOOS: "windows", GOARCH: "amd64"}, {GOOS: "windows", GOARCH: "arm64"}, - {GOOS: "wasip1", GOARCH: "wasm"}, } { name := "GOOS=" + options.GOOS + ",GOARCH=" + options.GOARCH if options.GOARCH == "arm" { diff --git a/compileopts/target.go b/compileopts/target.go index 7893e5829..64cbc2daa 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -356,17 +356,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) { return nil, fmt.Errorf("invalid GOMIPS=%s: must be hardfloat or softfloat", options.GOMIPS) } case "wasm": - llvmarch = "wasm32" - spec.CPU = "generic" - spec.Features = "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext,-multivalue,-reference-types" - spec.BuildTags = append(spec.BuildTags, "tinygo.wasm") - spec.CFlags = append(spec.CFlags, - "-mbulk-memory", - "-mnontrapping-fptoint", - "-mno-multivalue", - "-mno-reference-types", - "-msign-ext", - ) + return nil, fmt.Errorf("GOARCH=wasm but GOOS is unset. Please set GOOS to wasm, wasip1, or wasip2.") default: return nil, fmt.Errorf("unknown GOARCH=%s", options.GOARCH) } @@ -446,23 +436,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) { "--no-insert-timestamp", "--no-dynamicbase", ) - case "wasip1": - spec.GC = "" // use default GC - spec.Scheduler = "asyncify" - spec.Linker = "wasm-ld" - spec.RTLib = "compiler-rt" - spec.Libc = "wasi-libc" - spec.DefaultStackSize = 1024 * 64 // 64kB - spec.LDFlags = append(spec.LDFlags, - "--stack-first", - "--no-demangle", - ) - spec.Emulator = "wasmtime run --dir={tmpDir}::/tmp {}" - spec.ExtraFiles = append(spec.ExtraFiles, - "src/runtime/asm_tinygowasm.S", - "src/internal/task/task_asyncify_wasm.S", - ) - llvmos = "wasi" + case "wasm", "wasip1", "wasip2": + return nil, fmt.Errorf("GOOS=%s but GOARCH is unset. Please set GOARCH to wasm", options.GOOS) default: return nil, fmt.Errorf("unknown GOOS=%s", options.GOOS) } diff --git a/main.go b/main.go index 1f2fc95b1..d4562fc4f 100644 --- a/main.go +++ b/main.go @@ -1684,8 +1684,24 @@ func main() { usage(command) os.Exit(1) } - if options.Target == "" && filepath.Ext(outpath) == ".wasm" { - options.Target = "wasm" + if options.Target == "" { + switch { + case options.GOARCH == "wasm": + switch options.GOOS { + case "js": + options.Target = "wasm" + case "wasip1": + options.Target = "wasip1" + case "wasip2": + options.Target = "wasip2" + default: + fmt.Fprintln(os.Stderr, "GOARCH=wasm but GOOS is not set correctly. Please set GOOS to wasm, wasip1, or wasip2.") + os.Exit(1) + } + case filepath.Ext(outpath) == ".wasm": + fmt.Fprintln(os.Stderr, "you appear to want to build a wasm file, but have not specified either a target flag, or the GOARCH/GOOS to use.") + os.Exit(1) + } } err := Build(pkgName, outpath, options)