Skip to content

Commit

Permalink
gop: support -tags
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 12, 2025
1 parent f4daf21 commit c4154bb
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 10 deletions.
10 changes: 10 additions & 0 deletions cmd/internal/base/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base
import (
"flag"
"fmt"
"strings"
)

type stringValue struct {
Expand Down Expand Up @@ -42,6 +43,15 @@ type PassArgs struct {
Flag *flag.FlagSet
}

func (p *PassArgs) Tags() string {
for _, v := range p.Args {
if strings.HasPrefix(v, "-tags=") {
return v[6:]
}
}
return ""
}

func (p *PassArgs) Var(names ...string) {
for _, name := range names {
p.Flag.Var(&stringValue{p: p, name: name}, name, "")
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ var Cmd = &base.Command{
}

var (
flag = &Cmd.Flag
flagDebug = flag.Bool("debug", false, "print debug information")
flagOutput = flag.String("o", "", "gop build output file")
flag = &Cmd.Flag
)

func init() {
Expand Down Expand Up @@ -74,7 +74,7 @@ func runCmd(cmd *base.Command, args []string) {
log.Panicln("too many arguments:", args)
}

conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles)
conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles, pass.Tags())
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/internal/gengo/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
flagSingleMode = flag.Bool("s", false, "run in single file mode for package")
flagIgnoreNotatedErr = flag.Bool(
"ignore-notated-error", false, "ignore notated errors, only available together with -t (check mode)")
flagTags = flag.String("tags", "", "a comma-separated list of additional build tags to consider satisfied")
)

func init() {
Expand Down Expand Up @@ -71,7 +72,7 @@ func runCmd(cmd *base.Command, args []string) {
cl.SetDisableRecover(true)
}

conf, err := gop.NewDefaultConf(".", 0)
conf, err := gop.NewDefaultConf(".", 0, *flagTags)
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func runCmd(cmd *base.Command, args []string) {
cl.SetDisableRecover(true)
}

conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles)
conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles, pass.Tags())
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func runCmd(cmd *base.Command, args []string) {
}

noChdir := *flagNoChdir
conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles)
conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles, pass.Tags())
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func runCmd(cmd *base.Command, args []string) {
cl.SetDisableRecover(true)
}

conf, err := gop.NewDefaultConf(".", 0)
conf, err := gop.NewDefaultConf(".", 0, pass.Tags())
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/fsnotify/fsnotify v1.8.0
github.com/goplus/gogen v1.16.5-0.20250109123606-4aa868a225d7
github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea
github.com/goplus/llgo v0.9.9
github.com/goplus/mod v0.13.13
github.com/qiniu/x v1.13.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/goplus/gogen v1.16.5-0.20250109123606-4aa868a225d7 h1:+w0mFMKgnWpNU9jEIbkVj5GfLXUKRFESSjtC26T12X8=
github.com/goplus/gogen v1.16.5-0.20250109123606-4aa868a225d7/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea h1:xPRFhNEl7DxJTom3UCqK2iSXrqhzuoG4YvkeG/DzwOs=
github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
github.com/goplus/llgo v0.9.9 h1:sCM8ehOtuAEY/NQbJ4xAMRZhxEdDgilouWZpoaLiYrM=
github.com/goplus/llgo v0.9.9/go.mod h1:udcq+s6tGOdhJq7I8fXPTv4qT2j17/KrlvtcJrMZAoM=
github.com/goplus/mod v0.13.13 h1:rvwXCCQciTz4NjB3GLAZ2cskw035B64F7KzRAyMYUCw=
Expand Down
10 changes: 10 additions & 0 deletions imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importe
return ret
}

func (p *Importer) SetTags(tags string) {
p.impFrom.SetTags(tags)
if c, ok := p.impFrom.Cache().(*cache.Impl); ok {
c.SetTags(tags)
}
}

// CacheFile returns file path of the cache.
func (p *Importer) CacheFile() string {
cacheDir, _ := os.UserCacheDir()
Expand All @@ -83,6 +90,9 @@ func (p *Importer) CacheFile() string {
io.WriteString(h, root)
fname = filepath.Base(root)
}
if p.impFrom.Tags() != "" {
io.WriteString(h, p.impFrom.Tags())
}
hash := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
return cacheDir + hash + fname
}
Expand Down
5 changes: 4 additions & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,17 @@ const (
)

// NewDefaultConf creates a dfault configuration for common cases.
func NewDefaultConf(dir string, flags ConfFlags) (conf *Config, err error) {
func NewDefaultConf(dir string, flags ConfFlags, tags ...string) (conf *Config, err error) {
mod, err := LoadMod(dir)
if err != nil {
return
}
gop := gopenv.Get()
fset := token.NewFileSet()
imp := NewImporter(mod, gop, fset)
if len(tags) > 0 {
imp.SetTags(tags[0])
}
conf = &Config{
Gop: gop, Fset: fset, Mod: mod, Importer: imp,
IgnoreNotatedError: flags&ConfFlagIgnoreNotatedError != 0,
Expand Down

0 comments on commit c4154bb

Please sign in to comment.