| 1 | package interp |
| 2 | |
| 3 | import ( |
| 4 | "go/ast" |
| 5 | "go/build" |
| 6 | "go/parser" |
| 7 | "path" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // buildOk returns true if a file or script matches build constraints |
| 13 | // as specified in https://golang.org/pkg/go/build/#hdr-Build_Constraints. |
| 14 | // An error from parser is returned as well. |
| 15 | func (interp *Interpreter) buildOk(ctx *build.Context, name, src string) (bool, error) { |
| 16 | // Extract comments before the first clause |
| 17 | f, err := parser.ParseFile(interp.fset, name, src, parser.PackageClauseOnly|parser.ParseComments) |
| 18 | if err != nil { |
| 19 | return false, err |
| 20 | } |
| 21 | for _, g := range f.Comments { |
| 22 | // in file, evaluate the AND of multiple line build constraints |
| 23 | for _, line := range strings.Split(strings.TrimSpace(g.Text()), "\n") { |
| 24 | if !buildLineOk(ctx, line) { |
| 25 | return false, nil |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | setYaegiTags(ctx, f.Comments) |
| 30 | return true, nil |
| 31 | } |
| 32 | |
| 33 | // buildLineOk returns true if line is not a build constraint or |
| 34 | // if build constraint is satisfied. |
| 35 | func buildLineOk(ctx *build.Context, line string) (ok bool) { |
| 36 | if len(line) < 7 || line[:7] != "+build " { |
| 37 | return true |
| 38 | } |
| 39 | // In line, evaluate the OR of space-separated options |
| 40 | options := strings.Split(strings.TrimSpace(line[6:]), " ") |
| 41 | for _, o := range options { |
| 42 | if ok = buildOptionOk(ctx, o); ok { |
| 43 | break |
| 44 | } |
| 45 | } |
| 46 | return ok |
| 47 | } |
| 48 | |
| 49 | // buildOptionOk return true if all comma separated tags match, false otherwise. |
| 50 | func buildOptionOk(ctx *build.Context, tag string) bool { |
| 51 | // in option, evaluate the AND of individual tags |
| 52 | for _, t := range strings.Split(tag, ",") { |
| 53 | if !buildTagOk(ctx, t) { |
| 54 | return false |
| 55 | } |
| 56 | } |
| 57 | return true |
| 58 | } |
| 59 | |
| 60 | // buildTagOk returns true if a build tag matches, false otherwise |
| 61 | // if first character is !, result is negated. |
| 62 | func buildTagOk(ctx *build.Context, s string) (r bool) { |
| 63 | not := s[0] == '!' |
| 64 | if not { |
| 65 | s = s[1:] |
| 66 | } |
| 67 | switch { |
| 68 | case contains(ctx.BuildTags, s): |
| 69 | r = true |
| 70 | case s == ctx.GOOS: |
| 71 | r = true |
| 72 | case s == ctx.GOARCH: |
| 73 | r = true |
| 74 | case len(s) > 4 && s[:4] == "go1.": |
| 75 | if n, err := strconv.Atoi(s[4:]); err != nil { |
| 76 | r = false |
| 77 | } else { |
| 78 | r = goMinorVersion(ctx) >= n |
| 79 | } |
| 80 | } |
| 81 | if not { |
| 82 | r = !r |
| 83 | } |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | // setYaegiTags scans a comment group for "yaegi:tags tag1 tag2 ..." lines |
| 88 | // and adds the corresponding tags to the interpreter build tags. |
| 89 | func setYaegiTags(ctx *build.Context, comments []*ast.CommentGroup) { |
| 90 | for _, g := range comments { |
| 91 | for _, line := range strings.Split(strings.TrimSpace(g.Text()), "\n") { |
| 92 | if len(line) < 11 || line[:11] != "yaegi:tags " { |
| 93 | continue |
| 94 | } |
| 95 | |
| 96 | tags := strings.Split(strings.TrimSpace(line[10:]), " ") |
| 97 | for _, tag := range tags { |
| 98 | if !contains(ctx.BuildTags, tag) { |
| 99 | ctx.BuildTags = append(ctx.BuildTags, tag) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func contains(tags []string, tag string) bool { |
| 107 | for _, t := range tags { |
| 108 | if t == tag { |
| 109 | return true |
| 110 | } |
| 111 | } |
| 112 | return false |
| 113 | } |
| 114 | |
| 115 | // goMinorVersion returns the go minor version number. |
| 116 | func goMinorVersion(ctx *build.Context) int { |
| 117 | current := ctx.ReleaseTags[len(ctx.ReleaseTags)-1] |
| 118 | |
| 119 | v := strings.Split(current, ".") |
| 120 | if len(v) < 2 { |
| 121 | panic("unsupported Go version: " + current) |
| 122 | } |
| 123 | |
| 124 | m, err := strconv.Atoi(v[1]) |
| 125 | if err != nil { |
| 126 | panic("unsupported Go version: " + current) |
| 127 | } |
| 128 | return m |
| 129 | } |
| 130 | |
| 131 | // skipFile returns true if file should be skipped. |
| 132 | func skipFile(ctx *build.Context, p string, skipTest bool) bool { |
| 133 | if !strings.HasSuffix(p, ".go") { |
| 134 | return true |
| 135 | } |
| 136 | p = strings.TrimSuffix(path.Base(p), ".go") |
| 137 | if pp := path.Base(p); strings.HasPrefix(pp, "_") || strings.HasPrefix(pp, ".") { |
| 138 | return true |
| 139 | } |
| 140 | if skipTest && strings.HasSuffix(p, "_test") { |
| 141 | return true |
| 142 | } |
| 143 | i := strings.Index(p, "_") |
| 144 | if i < 0 { |
| 145 | return false |
| 146 | } |
| 147 | a := strings.Split(p[i+1:], "_") |
| 148 | last := len(a) - 1 |
| 149 | if last-1 >= 0 { |
| 150 | switch x, y := a[last-1], a[last]; { |
| 151 | case x == ctx.GOOS: |
| 152 | if knownArch[y] { |
| 153 | return y != ctx.GOARCH |
| 154 | } |
| 155 | return false |
| 156 | case knownOs[x] && knownArch[y]: |
| 157 | return true |
| 158 | case knownArch[y] && y != ctx.GOARCH: |
| 159 | return true |
| 160 | default: |
| 161 | return false |
| 162 | } |
| 163 | } |
| 164 | if x := a[last]; knownOs[x] && x != ctx.GOOS || knownArch[x] && x != ctx.GOARCH { |
| 165 | return true |
| 166 | } |
| 167 | return false |
| 168 | } |
| 169 | |
| 170 | var knownOs = map[string]bool{ |
| 171 | "aix": true, |
| 172 | "android": true, |
| 173 | "darwin": true, |
| 174 | "dragonfly": true, |
| 175 | "freebsd": true, |
| 176 | "illumos": true, |
| 177 | "ios": true, |
| 178 | "js": true, |
| 179 | "linux": true, |
| 180 | "netbsd": true, |
| 181 | "openbsd": true, |
| 182 | "plan9": true, |
| 183 | "solaris": true, |
| 184 | "wasip1": true, |
| 185 | "windows": true, |
| 186 | } |
| 187 | |
| 188 | var knownArch = map[string]bool{ |
| 189 | "386": true, |
| 190 | "amd64": true, |
| 191 | "arm": true, |
| 192 | "arm64": true, |
| 193 | "loong64": true, |
| 194 | "mips": true, |
| 195 | "mips64": true, |
| 196 | "mips64le": true, |
| 197 | "mipsle": true, |
| 198 | "ppc64": true, |
| 199 | "ppc64le": true, |
| 200 | "s390x": true, |
| 201 | "wasm": true, |
| 202 | } |