Skip to content

Commit 1d5357a

Browse files
committed
wit/bindgen: prototype Cgo file generation
1 parent 5c6afbe commit 1d5357a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

wit/bindgen/generator.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,16 @@ func (g *generator) wasmFileFor(owner wit.TypeOwner) *gen.File {
22172217
return file
22182218
}
22192219

2220+
func (g *generator) cgoFileFor(owner wit.TypeOwner) *gen.File {
2221+
pkg := g.packageFor(owner)
2222+
file := pkg.File(pkg.Name + ".cgo.go")
2223+
file.GeneratedBy = g.opts.generatedBy
2224+
if file.GoBuild == "" {
2225+
file.GoBuild = "tinygo.wasm"
2226+
}
2227+
return file
2228+
}
2229+
22202230
func (g *generator) packageFor(owner wit.TypeOwner) *gen.Package {
22212231
return g.witPackages[owner]
22222232
}
@@ -2287,5 +2297,21 @@ func (g *generator) newPackage(w *wit.World, i *wit.Interface, name string) (*ge
22872297
g.exportScopes[owner] = gen.NewScope(nil)
22882298
pkg.DeclareName("Exports")
22892299

2300+
// Write a Cgo file that adds a library to the linker arguments.
2301+
// The library is a WebAssembly file that includes a custom section
2302+
// with a name prefixed with "component-type:". The contents are the
2303+
// Component Model definition for a world that encapsulates the
2304+
// Component Model types and functions imported into and/or exported
2305+
// from this Go package.
2306+
cgoFile := g.cgoFileFor(owner)
2307+
lib := id.String()
2308+
if name != id.Extension {
2309+
lib += "-" + name
2310+
}
2311+
lib = strings.ReplaceAll(lib, "/", "-")
2312+
lib = strings.ReplaceAll(lib, ":", "-")
2313+
stringio.Write(cgoFile, "// #cgo LDFLAGS: -L. -l", lib, "\n")
2314+
stringio.Write(cgoFile, "import \"C\"\n")
2315+
22902316
return pkg, nil
22912317
}

0 commit comments

Comments
 (0)