-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
132 lines (122 loc) · 3.31 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"path/filepath"
"strconv"
"strings"
)
type goMetadata struct {
filename string
matched bool
isCgo bool
pkg string
imports []string
}
// readFiles collects metadata for a list of files.
func readFiles(bctx build.Context, inputs []string) ([]*goMetadata, error) {
outputs := []*goMetadata{}
for _, input := range inputs {
if m, err := readGoMetadata(bctx, abs(input), true); err != nil {
return nil, err
} else if m.matched {
outputs = append(outputs, m)
}
}
return outputs, nil
}
// filterFiles applies build constraints to a list of input files. It returns
// list of input files that should be compiled.
func filterFiles(bctx build.Context, inputs []string) ([]string, error) {
var outputs []string
for _, input := range inputs {
if m, err := readGoMetadata(bctx, input, false); err != nil {
return nil, err
} else if m.matched {
outputs = append(outputs, input)
}
}
return outputs, nil
}
// readGoMetadata applies build constraints to an input file and returns whether
// it should be compiled.
func readGoMetadata(bctx build.Context, input string, needPackage bool) (*goMetadata, error) {
m := &goMetadata{
filename: input,
}
dir, base := filepath.Split(input)
// Check build constraints on non-cgo files.
// Skip cgo files, since they get rejected (due to leading '_') and won't
// have any build constraints anyway.
if strings.HasPrefix(base, "_cgo") {
m.isCgo = true
m.matched = true
} else {
match, err := bctx.MatchFile(dir, base)
if err != nil {
return m, err
}
m.matched = match
}
// if we don't need the package, and we are cgo, no need to parse the file
if !needPackage && bctx.CgoEnabled {
return m, nil
}
// if it's not a go file, there is no package or cgo
if !strings.HasSuffix(input, ".go") {
return m, nil
}
// read the file header
fset := token.NewFileSet()
parsed, err := parser.ParseFile(fset, input, nil, parser.ImportsOnly)
if err != nil {
return m, err
}
m.pkg = parsed.Name.String()
for _, decl := range parsed.Decls {
d, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, dspec := range d.Specs {
spec, ok := dspec.(*ast.ImportSpec)
if !ok {
continue
}
imp, err := strconv.Unquote(spec.Path.Value)
if err != nil {
log.Panicf("%s: invalid string `%s`", input, spec.Path.Value)
}
if imp == "C" {
m.isCgo = true
break
}
}
}
// matched if cgo is enabled or the file is not cgo
m.matched = m.matched && (bctx.CgoEnabled || !m.isCgo)
for _, i := range parsed.Imports {
path, err := strconv.Unquote(i.Path.Value)
if err != nil {
return m, err
}
m.imports = append(m.imports, path)
}
return m, nil
}