@@ -2217,6 +2217,16 @@ func (g *generator) wasmFileFor(owner wit.TypeOwner) *gen.File {
2217
2217
return file
2218
2218
}
2219
2219
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
+
2220
2230
func (g * generator ) packageFor (owner wit.TypeOwner ) * gen.Package {
2221
2231
return g .witPackages [owner ]
2222
2232
}
@@ -2287,5 +2297,21 @@ func (g *generator) newPackage(w *wit.World, i *wit.Interface, name string) (*ge
2287
2297
g .exportScopes [owner ] = gen .NewScope (nil )
2288
2298
pkg .DeclareName ("Exports" )
2289
2299
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
+
2290
2316
return pkg , nil
2291
2317
}
0 commit comments