Skip to content

Commit 29c9363

Browse files
committed
wit/bindgen: cgo + linker tricks WIP
1 parent bb6b875 commit 29c9363

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

wit/bindgen/generator.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"go/token"
1111
"io"
12+
"os/exec"
1213
"path"
1314
"path/filepath"
1415
"runtime"
@@ -248,8 +249,8 @@ func (g *generator) defineWorld(w *wit.World) error {
248249
}
249250

250251
// Write WIT file for this world
251-
witFile := g.witFileFor(w)
252-
witFile.WriteString(g.res.WIT(wit.Filter(w, nil), ""))
252+
// witFile := g.witFileFor(w)
253+
// witFile.WriteString(g.res.WIT(wit.Filter(w, nil), ""))
253254

254255
// Write Go package docs
255256
file := g.fileFor(w)
@@ -2319,23 +2320,53 @@ func (g *generator) newPackage(w *wit.World, i *wit.Interface, name string) (*ge
23192320
g.exportScopes[owner] = gen.NewScope(nil)
23202321
pkg.DeclareName("Exports")
23212322

2322-
// Write a Cgo file that adds a library to the linker arguments.
2323+
// Write a Cgo file and a .wasm library to the linker arguments.
23232324
// The library is a WebAssembly file that includes a custom section
23242325
// with a name prefixed with "component-type:". The contents are the
23252326
// Component Model definition for a world that encapsulates the
23262327
// Component Model types and functions imported into and/or exported
23272328
// from this Go package.
23282329
if false {
2329-
cgoFile := g.cgoFileFor(owner)
2330+
// Synthesize a unique-ish library name
23302331
lib := id.String()
23312332
if name != id.Extension {
23322333
lib += "-" + name
23332334
}
23342335
lib = strings.ReplaceAll(lib, "/", "-")
23352336
lib = strings.ReplaceAll(lib, ":", "-")
2337+
libFile := pkg.File("lib" + lib + ".a")
2338+
2339+
// Generate
2340+
witText := g.res.WIT(wit.FilterFor(w, i), "")
2341+
witFile := g.witFileFor(owner)
2342+
witFile.WriteString(witText)
2343+
content, err := g.componentEmbed(witText)
2344+
if err != nil {
2345+
// return nil, err
2346+
}
2347+
libFile.Write(content)
2348+
2349+
// Write Cgo file
2350+
cgoFile := g.cgoFileFor(owner)
23362351
stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n")
23372352
stringio.Write(cgoFile, "import \"C\"\n")
23382353
}
23392354

23402355
return pkg, nil
23412356
}
2357+
2358+
// componentEmbed runs generated WIT through wasm-tools to generate a wasm file with a component-type custom section.
2359+
func (g *generator) componentEmbed(witData string) ([]byte, error) {
2360+
// TODO: --all-features?
2361+
cmd := exec.Command("wasm-tools", "component", "embed", "--only-custom", "/dev/stdin")
2362+
cmd.Stdin = strings.NewReader(witData)
2363+
stdout := &bytes.Buffer{}
2364+
stderr := &bytes.Buffer{}
2365+
cmd.Stdout = stdout
2366+
cmd.Stderr = stderr
2367+
err := cmd.Run()
2368+
if err != nil {
2369+
g.opts.logger.Errorf("wasm-tools: %s", stderr.String())
2370+
}
2371+
return stdout.Bytes(), err
2372+
}

0 commit comments

Comments
 (0)