Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/wit-bindgen-go/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ var Command = &cli.Command{
Name: "dry-run",
Usage: "do not write files; print to stdout",
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Value: "wasip2",
OnlyOnce: true,
Config: cli.StringConfig{TrimSpace: true},
Usage: "sets the Go build tag for the generated WIT bindings",
},
},
Action: action,
}
Expand Down Expand Up @@ -98,6 +106,7 @@ func action(ctx context.Context, cmd *cli.Command) error {
bindgen.PackageRoot(pkgRoot),
bindgen.Versioned(cmd.Bool("versioned")),
bindgen.CMPackage(cmd.String("cm")),
bindgen.Target(cmd.String("target")),
)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ require (
github.com/sergi/go-diff v1.3.1
github.com/urfave/cli/v3 v3.0.0-alpha9
golang.org/x/mod v0.20.0
golang.org/x/tools v0.23.0
golang.org/x/tools v0.24.0
)

require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sync v0.8.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
11 changes: 8 additions & 3 deletions wit/bindgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func newGenerator(res *wit.Resolve, opts ...Option) (*generator, error) {
if g.opts.cmPackage == "" {
g.opts.cmPackage = cmPackage
}

if g.opts.target == "" {
g.opts.target = BuildDefault
}

g.res = res
return g, nil
}
Expand Down Expand Up @@ -2110,23 +2115,23 @@ func (g *generator) ensureEmptyAsm(pkg *gen.Package) error {
func (g *generator) abiFile(pkg *gen.Package) *gen.File {
file := pkg.File("abi.go")
file.GeneratedBy = g.opts.generatedBy
file.Build = BuildDefault
file.Build = g.opts.target
return file
}

func (g *generator) fileFor(id wit.Ident) *gen.File {
pkg := g.packageFor(id)
file := pkg.File(id.Extension + ".wit.go")
file.GeneratedBy = g.opts.generatedBy
file.Build = BuildDefault
file.Build = g.opts.target
return file
}

func (g *generator) exportsFileFor(id wit.Ident) *gen.File {
pkg := g.packageFor(id)
file := pkg.File(id.Extension + ".exports.go")
file.GeneratedBy = g.opts.generatedBy
file.Build = BuildDefault
file.Build = g.opts.target
if len(file.Header) == 0 {
exports := file.GetName("Exports")
var b strings.Builder
Expand Down
25 changes: 25 additions & 0 deletions wit/bindgen/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package bindgen

import (
"fmt"
"strings"
)

// Option represents a single configuration option for this package.
type Option interface {
applyOption(*options) error
Expand Down Expand Up @@ -28,6 +33,9 @@ type options struct {

// versioned determines if Go packages are generated with version numbers.
versioned bool

// target determines the golang build tags that should be added to the generated wit bindings
target string
}

func (opts *options) apply(o ...Option) error {
Expand Down Expand Up @@ -82,3 +90,20 @@ func Versioned(versioned bool) Option {
return nil
})
}

// Target returns an [Option] that specifices that all generated bindings include a build tag allowing
// the target suppilied.
func Target(target string) Option {
return optionFunc(func(opts *options) error {
target = strings.ToLower(target)
switch target {
case "wasip1":
opts.target = target
case "wasip2":
opts.target = BuildDefault
default:
return fmt.Errorf("target: %s not supported", target)
}
return nil
})
}