| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package buildutil |
| 6 | |
| 7 | // This duplicated logic must be kept in sync with that from go build: |
| 8 | // $GOROOT/src/cmd/go/internal/work/build.go (tagsFlag.Set) |
| 9 | // $GOROOT/src/cmd/go/internal/base/flag.go (StringsFlag.Set) |
| 10 | // $GOROOT/src/cmd/internal/quoted/quoted.go (isSpaceByte, Split) |
| 11 | |
| 12 | import ( |
| 13 | "fmt" |
| 14 | "strings" |
| 15 | ) |
| 16 | |
| 17 | const TagsFlagDoc = "a list of `build tags` to consider satisfied during the build. " + |
| 18 | "For more information about build tags, see the description of " + |
| 19 | "build constraints in the documentation for the go/build package" |
| 20 | |
| 21 | // TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses |
| 22 | // a flag value the same as go build's -tags flag and populates a []string slice. |
| 23 | // |
| 24 | // See $GOROOT/src/go/build/doc.go for description of build tags. |
| 25 | // See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag. |
| 26 | // |
| 27 | // Example: |
| 28 | // |
| 29 | // flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc) |
| 30 | type TagsFlag []string |
| 31 | |
| 32 | func (v *TagsFlag) Set(s string) error { |
| 33 | // See $GOROOT/src/cmd/go/internal/work/build.go (tagsFlag.Set) |
| 34 | // For compatibility with Go 1.12 and earlier, allow "-tags='a b c'" or even just "-tags='a'". |
| 35 | if strings.Contains(s, " ") || strings.Contains(s, "'") { |
| 36 | var err error |
| 37 | *v, err = splitQuotedFields(s) |
| 38 | if *v == nil { |
| 39 | *v = []string{} |
| 40 | } |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | // Starting in Go 1.13, the -tags flag is a comma-separated list of build tags. |
| 45 | *v = []string{} |
| 46 | for s := range strings.SplitSeq(s, ",") { |
| 47 | if s != "" { |
| 48 | *v = append(*v, s) |
| 49 | } |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | func (v *TagsFlag) Get() any { return *v } |
| 55 | |
| 56 | func splitQuotedFields(s string) ([]string, error) { |
| 57 | // See $GOROOT/src/cmd/internal/quoted/quoted.go (Split) |
| 58 | // This must remain in sync with that logic. |
| 59 | var f []string |
| 60 | for len(s) > 0 { |
| 61 | for len(s) > 0 && isSpaceByte(s[0]) { |
| 62 | s = s[1:] |
| 63 | } |
| 64 | if len(s) == 0 { |
| 65 | break |
| 66 | } |
| 67 | // Accepted quoted string. No unescaping inside. |
| 68 | if s[0] == '"' || s[0] == '\'' { |
| 69 | quote := s[0] |
| 70 | s = s[1:] |
| 71 | i := 0 |
| 72 | for i < len(s) && s[i] != quote { |
| 73 | i++ |
| 74 | } |
| 75 | if i >= len(s) { |
| 76 | return nil, fmt.Errorf("unterminated %c string", quote) |
| 77 | } |
| 78 | f = append(f, s[:i]) |
| 79 | s = s[i+1:] |
| 80 | continue |
| 81 | } |
| 82 | i := 0 |
| 83 | for i < len(s) && !isSpaceByte(s[i]) { |
| 84 | i++ |
| 85 | } |
| 86 | f = append(f, s[:i]) |
| 87 | s = s[i:] |
| 88 | } |
| 89 | return f, nil |
| 90 | } |
| 91 | |
| 92 | func (v *TagsFlag) String() string { |
| 93 | return "<tagsFlag>" |
| 94 | } |
| 95 | |
| 96 | func isSpaceByte(c byte) bool { |
| 97 | // See $GOROOT/src/cmd/internal/quoted/quoted.go (isSpaceByte, Split) |
| 98 | // This list must remain in sync with that. |
| 99 | return c == ' ' || c == '\t' || c == '\n' || c == '\r' |
| 100 | } |