Skip to content

Commit b0aef96

Browse files
committed
fix: only infer target for wasm when GOOS and GOARCH are set correctly, not just based on file extension
Signed-off-by: deadprogram <[email protected]>
1 parent 056394e commit b0aef96

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

builder/builder_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func TestClangAttributes(t *testing.T) {
6969
{GOOS: "darwin", GOARCH: "arm64"},
7070
{GOOS: "windows", GOARCH: "amd64"},
7171
{GOOS: "windows", GOARCH: "arm64"},
72-
{GOOS: "wasip1", GOARCH: "wasm"},
7372
} {
7473
name := "GOOS=" + options.GOOS + ",GOARCH=" + options.GOARCH
7574
if options.GOARCH == "arm" {

compileopts/target.go

+3-28
Original file line numberDiff line numberDiff line change
@@ -356,17 +356,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
356356
return nil, fmt.Errorf("invalid GOMIPS=%s: must be hardfloat or softfloat", options.GOMIPS)
357357
}
358358
case "wasm":
359-
llvmarch = "wasm32"
360-
spec.CPU = "generic"
361-
spec.Features = "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext,-multivalue,-reference-types"
362-
spec.BuildTags = append(spec.BuildTags, "tinygo.wasm")
363-
spec.CFlags = append(spec.CFlags,
364-
"-mbulk-memory",
365-
"-mnontrapping-fptoint",
366-
"-mno-multivalue",
367-
"-mno-reference-types",
368-
"-msign-ext",
369-
)
359+
return nil, fmt.Errorf("GOARCH=wasm but GOOS is unset. Please set GOOS to wasm, wasip1, or wasip2.")
370360
default:
371361
return nil, fmt.Errorf("unknown GOARCH=%s", options.GOARCH)
372362
}
@@ -446,23 +436,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
446436
"--no-insert-timestamp",
447437
"--no-dynamicbase",
448438
)
449-
case "wasip1":
450-
spec.GC = "" // use default GC
451-
spec.Scheduler = "asyncify"
452-
spec.Linker = "wasm-ld"
453-
spec.RTLib = "compiler-rt"
454-
spec.Libc = "wasi-libc"
455-
spec.DefaultStackSize = 1024 * 64 // 64kB
456-
spec.LDFlags = append(spec.LDFlags,
457-
"--stack-first",
458-
"--no-demangle",
459-
)
460-
spec.Emulator = "wasmtime run --dir={tmpDir}::/tmp {}"
461-
spec.ExtraFiles = append(spec.ExtraFiles,
462-
"src/runtime/asm_tinygowasm.S",
463-
"src/internal/task/task_asyncify_wasm.S",
464-
)
465-
llvmos = "wasi"
439+
case "wasm", "wasip1", "wasip2":
440+
return nil, fmt.Errorf("GOOS=%s but GOARCH is unset. Please set GOARCH to wasm", options.GOOS)
466441
default:
467442
return nil, fmt.Errorf("unknown GOOS=%s", options.GOOS)
468443
}

main.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -1684,8 +1684,24 @@ func main() {
16841684
usage(command)
16851685
os.Exit(1)
16861686
}
1687-
if options.Target == "" && filepath.Ext(outpath) == ".wasm" {
1688-
options.Target = "wasm"
1687+
if options.Target == "" {
1688+
switch {
1689+
case options.GOARCH == "wasm":
1690+
switch options.GOOS {
1691+
case "js":
1692+
options.Target = "wasm"
1693+
case "wasip1":
1694+
options.Target = "wasip1"
1695+
case "wasip2":
1696+
options.Target = "wasip2"
1697+
default:
1698+
fmt.Fprintln(os.Stderr, "GOARCH=wasm but GOOS is not set correctly. Please set GOOS to wasm, wasip1, or wasip2.")
1699+
os.Exit(1)
1700+
}
1701+
case filepath.Ext(outpath) == ".wasm":
1702+
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.")
1703+
os.Exit(1)
1704+
}
16891705
}
16901706

16911707
err := Build(pkgName, outpath, options)

0 commit comments

Comments
 (0)