-
Notifications
You must be signed in to change notification settings - Fork 3
/
runfiles.go
156 lines (135 loc) · 3.54 KB
/
runfiles.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// The go runfiles functions index the manifest by shortpath. we want workspace_name/shortpath
package main
import (
"bytes"
"fmt"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
const runfilesSuffix = ".runfiles"
type Runfiles struct {
strategy RunfilesStrategy
}
func (r *Runfiles) Rlocation(p string) string {
if filepath.IsAbs(p) {
return p
}
for _, prefix := range []string{"../", "external/"} {
if strings.HasPrefix(p, prefix) {
p = p[len(prefix):]
break
}
}
return r.strategy.Rlocation(p)
}
type RunfilesStrategy interface {
Rlocation(path string) string
}
type ManifestStrategy struct {
manifestPath string
data map[string]string
}
func (s *ManifestStrategy) Rlocation(p string) string {
return s.data[p]
}
type DirectoryStrategy struct {
runfileDirectory string
}
func (s *DirectoryStrategy) Rlocation(p string) string {
return path.Join(s.runfileDirectory, p)
}
func EnsureExe(p string) string {
if runtime.GOOS != "windows" {
return p
}
if path.Ext(p) != ".exe" {
return p + ".exe"
}
return p
}
func findRunfilesDir() string {
runfilesDir := os.Getenv(bazel.RUNFILES_DIR)
if runfilesDir != "" {
return runfilesDir
}
runfilesDir = os.Getenv(bazel.TEST_SRCDIR)
if runfilesDir != "" {
return runfilesDir
}
thisBin := EnsureExe(os.Args[0])
runfilesDir = thisBin + runfilesSuffix
dir, err := os.Stat(runfilesDir)
if err != nil {
cwd, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("failed to find runfiles: %v", err))
}
index := strings.Index(cwd, runfilesSuffix)
if index < 0 {
panic(fmt.Errorf("no runfiles directories are a prefix of the current directory: %s", cwd))
}
runfilesDir = cwd[:index+len(runfilesSuffix)]
dir, _ = os.Stat(runfilesDir)
}
if !dir.Mode().IsDir() {
panic(fmt.Errorf("runfiles path is not a directory: %s", runfilesDir))
}
return runfilesDir
}
func getManifestPath(runfilesDir string) string {
manifestPath := os.Getenv(bazel.RUNFILES_MANIFEST_FILE)
if manifestPath != "" {
return manifestPath
}
return filepath.Join(runfilesDir, "MANIFEST")
}
func GetRunfiles() *Runfiles {
runfilesDir := findRunfilesDir()
manifestPath := getManifestPath(runfilesDir)
manifestOnly := os.Getenv("RUNFILES_MANIFEST_ONLY")
runfiles := Runfiles{}
if manifestOnly == "1" || runtime.GOOS == "windows" {
manifestOnly = "1"
s := &ManifestStrategy{manifestPath: manifestPath, data: map[string]string{}}
runfiles.strategy = s
contentBytes, err := ioutil.ReadFile(manifestPath)
if err != nil {
log.Panicf("failed to read runfiles manifest: %v", err)
}
for _, line := range bytes.Split(contentBytes, []byte{'\n'}) {
ind := bytes.IndexRune(line, ' ')
if ind >= 0 {
key := line[0:ind]
value := line[ind+1:]
s.data[string(key)] = string(value)
} else {
// python doesn't have a space in the line for __init__.py
s.data[string(line)] = ""
}
}
} else if runfilesDir != "" {
runfiles.strategy = &DirectoryStrategy{runfileDirectory: runfilesDir}
}
_ = os.Setenv(bazel.RUNFILES_DIR, runfilesDir)
for k, v := range map[string]string{
bazel.RUNFILES_DIR: runfilesDir,
bazel.RUNFILES_MANIFEST_FILE: manifestPath,
"RUNFILES_MANIFEST_ONLY": manifestOnly,
} {
// just assume we need to set the value for windows
if err := os.Setenv(k, v); err != nil {
panic(fmt.Errorf("failed to set the manifest file path: %v", err))
}
}
diag(func() {
abs, _ := filepath.Abs(manifestPath)
fmt.Printf("located manifest file: %s\n", abs)
})
return &runfiles
}