/
package.go
116 lines (98 loc) · 3.01 KB
/
package.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
/* 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 packages
import "strings"
// Package contains metadata about a Go package extracted from a directory.
// It fills a similar role to go/build.Package, but it separates files by
// target instead of by type, and it supports multiple platforms.
type Package struct {
Dir string
Name string
Library, CgoLibrary, Binary, Test, XTest Target
}
// Target contains metadata about a buildable Go target in a package.
type Target struct {
Sources, Imports PlatformStrings
COpts, CLinkOpts PlatformStrings
}
// PlatformStrings contains a set of strings associated with a buildable
// Go target in a package. This is used to store source file names,
// import paths, and flags.
type PlatformStrings struct {
// Generic is a list of strings not specific to any platform.
Generic []string
// Platform is a map of lists of platform-specific strings. The map is keyed
// by the name of the platform.
Platform map[string][]string
}
// IsCommand returns true if the package name is "main".
func (p *Package) IsCommand() bool {
return p.Name == "main"
}
// HasGo returns true if at least one target in the package contains a
// .go source file. If a package does not contain Go code, Gazelle will
// not generate rules for it.
func (p *Package) HasGo() bool {
return p.Library.HasGo() || p.CgoLibrary.HasGo() || p.Binary.HasGo() || p.Test.HasGo() || p.XTest.HasGo()
}
// firstGoFile returns the name of a .go file if the package contains at least
// one .go file, or "" otherwise. Used by HasGo and for error reporting.
func (p *Package) firstGoFile() string {
if f := p.Library.firstGoFile(); f != "" {
return f
}
if f := p.CgoLibrary.firstGoFile(); f != "" {
return f
}
if f := p.Binary.firstGoFile(); f != "" {
return f
}
if f := p.Test.firstGoFile(); f != "" {
return f
}
return p.XTest.firstGoFile()
}
func (t *Target) HasGo() bool {
return t.Sources.HasGo()
}
func (t *Target) firstGoFile() string {
return t.Sources.firstGoFile()
}
func (ts *PlatformStrings) HasGo() bool {
return ts.firstGoFile() != ""
}
func (ts *PlatformStrings) IsEmpty() bool {
if len(ts.Generic) > 0 {
return false
}
for _, s := range ts.Platform {
if len(s) > 0 {
return false
}
}
return true
}
func (ts *PlatformStrings) firstGoFile() string {
for _, f := range ts.Generic {
if strings.HasSuffix(f, ".go") {
return f
}
}
for _, fs := range ts.Platform {
for _, f := range fs {
if strings.HasSuffix(f, ".go") {
return f
}
}
}
return ""
}