From d0dd8ddee071bba0d1434d158ae66dcba27d2e34 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Sat, 30 Nov 2024 12:02:58 -0800 Subject: [PATCH] wit/bindgen: generate .wasm.syso files in each Go package Currently stubbed out (if false). Depends on https://github.com/tinygo-org/tinygo/pull/4593 --- wit/bindgen/generator.go | 46 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/wit/bindgen/generator.go b/wit/bindgen/generator.go index f0fc2edb..c71a32cb 100644 --- a/wit/bindgen/generator.go +++ b/wit/bindgen/generator.go @@ -21,6 +21,7 @@ import ( "go.bytecodealliance.org/internal/codec" "go.bytecodealliance.org/internal/go/gen" "go.bytecodealliance.org/internal/stringio" + "go.bytecodealliance.org/internal/wasm" "go.bytecodealliance.org/wit" "go.bytecodealliance.org/wit/logging" ) @@ -2327,34 +2328,45 @@ func (g *generator) newPackage(w *wit.World, i *wit.Interface, name string) (*ge // Component Model types and functions imported into and/or exported // from this Go package. if false { - // Synthesize a unique-ish library name - lib := id.String() + // Synthesize a unique-ish name + worldName := id.String() if name != id.Extension { - lib += "-" + name - } - lib = strings.ReplaceAll(lib, "/", "-") - lib = strings.ReplaceAll(lib, ":", "-") - libFile := pkg.File("lib" + lib + ".a") - - // Generate - witText := g.res.WIT(wit.Filter(w, i), "") - witFile := g.witFileFor(owner) - witFile.WriteString(witText) + worldName += "-" + name + } + worldName += "-go" + worldName = replacer.Replace(worldName) + // libFile := pkg.File("lib" + lib + ".a") + sysoFile := pkg.File(worldName + ".wasm.syso") + + // Generate wasm file + world := w.Clone() + world.Name = worldName + w.Package.Worlds.Set(worldName, world) + witText := g.res.WIT(wit.Filter(world, i), "") + w.Package.Worlds.Delete(worldName) + // witFile := g.witFileFor(owner) + // witFile.WriteString(witText) content, err := g.componentEmbed(witText) if err != nil { - // return nil, err + return nil, err } - libFile.Write(content) + componentType := &wasm.CustomSection{ + Name: "component-type:" + worldName, + Contents: content, + } + wasm.Write(sysoFile, []wasm.Section{&wasm.LinkingSection{}, componentType}) // Write Cgo file - cgoFile := g.cgoFileFor(owner) - stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n") - stringio.Write(cgoFile, "import \"C\"\n") + // cgoFile := g.cgoFileFor(owner) + // stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n") + // stringio.Write(cgoFile, "import \"C\"\n") } return pkg, nil } +var replacer = strings.NewReplacer("/", "-", ":", "-", "@", "-v", ".", "") + // componentEmbed runs generated WIT through wasm-tools to generate a wasm file with a component-type custom section. func (g *generator) componentEmbed(witData string) ([]byte, error) { // TODO: --all-features?