-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpkgconfig.go
141 lines (129 loc) · 2.69 KB
/
pkgconfig.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
133
134
135
136
137
138
139
140
141
package pkgconfig
import (
"bytes"
"errors"
"io"
"os"
"strings"
)
var errSkipGithub = errors.New("PKG_CONDIF_GITHUB not exported, skipping github.com lookup")
func lookupGithubIfEnv(pkg string) (*PC, error) {
if os.Getenv("PKG_CONFIG_GITHUB") == "1" {
return LookupGithub(pkg)
}
return nil, errSkipGithub
}
type pathLookupPair struct {
name string
fn func(string) (*PC, error)
}
var lookups = []pathLookupPair{
{"$GOPATH", LookupGopath},
{"github.com", lookupGithubIfEnv},
{"$PKG_CONFIG_PATH", LookupPC},
{"<autogenerated>", GenerateGopath},
}
type namedErrors map[string]error
func (ne namedErrors) Error() string {
s := make([]string, 0, len(ne))
for name, err := range ne {
s = append(s, "error "+name+": "+err.Error())
}
return strings.Join(s, "\n")
}
// DefaultLookup TODO(rjeczalik): document
func DefaultLookup(pkg string) (pc *PC, err error) {
ne := make(map[string]error, len(lookups))
for _, lookup := range lookups {
if pc, err = lookup.fn(pkg); err == nil {
return
}
ne[lookup.name] = err
}
return nil, namedErrors(ne)
}
// Pkg TODO(rjeczalik): document
type Pkg struct {
Packages []string
Libs bool
Cflags bool
Lookup func(string) (*PC, error)
pc []*PC
}
// NewPkgArgs TODO(rjeczalik): document
func NewPkgArgs(args []string) *Pkg {
pkg := &Pkg{}
for _, arg := range args {
switch {
case arg == "--libs":
pkg.Libs = true
case arg == "--cflags":
pkg.Cflags = true
case strings.HasPrefix(arg, "-"):
default:
pkg.Packages = append(pkg.Packages, arg)
}
}
return pkg
}
// Resolve TODO(rjeczalik): document
func (pkg *Pkg) Resolve() error {
if len(pkg.Packages) == 0 {
return ErrEmptyPC
}
lu := pkg.Lookup
if lu == nil {
lu = DefaultLookup
}
var (
pc = make([]*PC, 0, len(pkg.Packages))
dups = make(map[string]struct{})
)
for _, p := range pkg.Packages {
if _, ok := dups[p]; !ok {
pkg, err := lu(p)
if err != nil {
return err
}
pc = append(pc, pkg)
dups[p] = struct{}{}
}
}
pkg.pc = pc
return nil
}
// WriteTo TODO(rjeczalik): document
func (pkg Pkg) WriteTo(w io.Writer) (int64, error) {
var (
dups = make(map[string]struct{})
buf bytes.Buffer
)
if pkg.Cflags {
for _, pc := range pkg.pc {
for _, cflag := range pc.Cflags {
if _, ok := dups[cflag]; !ok {
buf.WriteString(cflag)
buf.WriteByte(' ')
dups[cflag] = struct{}{}
}
}
}
}
if pkg.Libs {
for _, pc := range pkg.pc {
for _, lib := range pc.Libs {
if _, ok := dups[lib]; !ok {
buf.WriteString(lib)
buf.WriteByte(' ')
dups[lib] = struct{}{}
}
}
}
}
if buf.Len() == 0 {
return 0, ErrEmptyPC
}
p := buf.Bytes()
p[len(p)-1] = '\n'
return io.Copy(w, bytes.NewBuffer(p))
}