From 2b59261c212f451435e61bee3601e10cd040d9e5 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 11 Nov 2024 12:47:19 -0700 Subject: [PATCH 1/2] builder, loader: link .syso files in Go package directories Among other things, this enables WebAssembly programs to link in custom sections, which can be stored in a relocatable wasm module with a .syso suffix in a package directory. This will simplify the build process for WASI 0.2, removing the need to run 'wasm-tools component embed' with the original source WIT files. --- builder/build.go | 6 ++++++ loader/loader.go | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/builder/build.go b/builder/build.go index 57b67ed455..4a92fa4409 100644 --- a/builder/build.go +++ b/builder/build.go @@ -711,6 +711,12 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe } linkerDependencies = append(linkerDependencies, job) } + + // Add .syso files + // TODO: is this the right way to do this? + for _, filename := range pkg.SysoFiles { + ldflags = append(ldflags, filepath.Join(pkg.Dir, filename)) + } } // Linker flags from CGo lines: diff --git a/loader/loader.go b/loader/loader.go index e935a9de3a..f14e43e019 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -60,9 +60,10 @@ type PackageJSON struct { } // Source files - GoFiles []string - CgoFiles []string - CFiles []string + GoFiles []string + CgoFiles []string + CFiles []string + SysoFiles []string // Embedded files EmbedFiles []string From 1f3fd8f91df09df03dc47ebf10dd05f47b009033 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 11 Nov 2024 12:48:10 -0700 Subject: [PATCH 2/2] compiler: ignore go:wasmexport directives on non-wasm architectures --- compiler/symbol.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/symbol.go b/compiler/symbol.go index 93c27803e2..cf806be317 100644 --- a/compiler/symbol.go +++ b/compiler/symbol.go @@ -350,6 +350,10 @@ func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) { info.wasmModule = parts[1] info.wasmName = parts[2] case "//go:wasmexport": + if c.archFamily() != "wasm32" { + // go:wasmimport is ignored on non-wasm architectures + continue + } if f.Blocks == nil { c.addError(f.Pos(), "can only use //go:wasmexport on definitions") continue @@ -367,9 +371,6 @@ func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) { c.addError(f.Pos(), fmt.Sprintf("//go:wasmexport does not allow main.main to be exported with -buildmode=%s", c.BuildMode)) continue } - if c.archFamily() != "wasm32" { - c.addError(f.Pos(), "//go:wasmexport is only supported on wasm") - } c.checkWasmImportExport(f, comment.Text) info.wasmExport = name info.wasmExportPos = comment.Slash