-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgopath.go
128 lines (119 loc) · 2.58 KB
/
gopath.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
package pkgconfig
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
var defaultGopath = gopath(os.PathListSeparator)
func existDir(dirs ...string) (err error) {
var fi os.FileInfo
for _, dir := range dirs {
if fi, err = os.Stat(dir); err != nil {
return
}
if !fi.IsDir() {
err = fmt.Errorf("%s is not a directory", dir)
return
}
}
return
}
func existFile(files ...string) (err error) {
var fi os.FileInfo
for _, file := range files {
if fi, err = os.Stat(file); err != nil {
return
}
if fi.IsDir() {
err = fmt.Errorf("%s is not a file", file)
return
}
}
return
}
func gopath(sep rune) []string {
var paths = strings.Split(os.Getenv("GOPATH"), string(sep))
if wd != "" {
if i := strings.Index(wd, src[sep]); i != -1 {
tmp := make([]string, 0, len(paths)+1)
tmp = append(tmp, wd[:i])
paths = append(tmp, paths...)
}
}
for i := 1; i < len(paths); i++ {
if paths[i] == paths[0] {
paths = append(paths[:i], paths[i+1:]...)
break
}
}
return paths
}
// GopathLibrary TODO(rjeczalik): document
func GopathLibrary(path, pkg string) (include, lib string) {
include = filepath.Join(path, "include", pkg)
lib = filepath.Join(path, "lib", runtime.GOOS+"_"+runtime.GOARCH, pkg)
return
}
func walkgopath(pkg string, fn func(string, string, string) bool) bool {
for _, path := range defaultGopath {
include, lib := GopathLibrary(path, pkg)
if existDir(include, lib) != nil {
continue
}
if !fn(path, include, lib) {
return true
}
}
return false
}
// LookupGopath TODO(rjeczalik): document
func LookupGopath(pkg string) (*PC, error) {
var (
vars = map[string]string{"GOOS": runtime.GOOS, "GOARCH": runtime.GOARCH}
err error
pc *PC
f *os.File
)
look := func(path, _, lib string) bool {
file := filepath.Join(lib, pkg+".pc")
vars["GOPATH"] = path
if f, err = os.Open(file); err == nil {
pc, err = NewPCVars(f, vars)
f.Close()
if err == nil {
pc.File = file
return false
}
}
return true
}
if !walkgopath(pkg, look) {
if err != nil {
return nil, err
}
return nil, errors.New("no library found in $GOPATH: " + pkg)
}
return pc, nil
}
// GenerateGopath TODO(rjeczalik): document
func GenerateGopath(pkg string) (*PC, error) {
var pc *PC
gen := func(path, include, lib string) bool {
pc = &PC{
Libs: []string{
"-L" + lib,
"-l" + strings.TrimLeft(pkg, "lib"),
"-Wl,-rpath", "-Wl,$ORIGIN",
},
Cflags: []string{"-I" + include},
}
return false
}
if !walkgopath(pkg, gen) {
return nil, errors.New("no library found in $GOPATH: " + pkg)
}
return pc, nil
}