forked from bazel-contrib/rules_go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
link.go
247 lines (229 loc) · 6.9 KB
/
link.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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.
// link combines the results of a compile step using "go tool link". It is invoked by the
// Go rules as an action.
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
type archive struct {
label, pkgPath, file string
}
func run(args []string) error {
// Parse arguments.
args, err := readParamsFiles(args)
if err != nil {
return err
}
builderArgs, toolArgs := splitArgs(args)
xstamps := multiFlag{}
stamps := multiFlag{}
xdefs := multiFlag{}
archives := archiveMultiFlag{}
flags := flag.NewFlagSet("link", flag.ExitOnError)
goenv := envFlags(flags)
main := flags.String("main", "", "Path to the main archive.")
packagePath := flags.String("p", "", "Package path of the main archive.")
outFile := flags.String("o", "", "Path to output file.")
flags.Var(&archives, "arc", "Label, package path, and file name of a dependency, separated by '='")
packageList := flags.String("package_list", "", "The file containing the list of standard library packages")
buildmode := flags.String("buildmode", "", "Build mode used.")
flags.Var(&xdefs, "X", "A string variable to replace in the linked binary (repeated).")
flags.Var(&xstamps, "Xstamp", "Like -X but the values are looked up in the -stamp file.")
flags.Var(&stamps, "stamp", "The name of a file with stamping values.")
if err := flags.Parse(builderArgs); err != nil {
return err
}
if err := goenv.checkFlags(); err != nil {
return err
}
// On Windows, take the absolute path of the output file and main file.
// This is needed on Windows because the relative path is frequently too long.
// os.Open on Windows converts absolute paths to some other path format with
// longer length limits. Absolute paths do not work on macOS for .dylib
// outputs because they get baked in as the "install path".
if runtime.GOOS != "darwin" {
*outFile = abs(*outFile)
}
*main = abs(*main)
// If we were given any stamp value files, read and parse them
stampMap := map[string]string{}
for _, stampfile := range stamps {
stampbuf, err := ioutil.ReadFile(stampfile)
if err != nil {
return fmt.Errorf("Failed reading stamp file %s: %v", stampfile, err)
}
scanner := bufio.NewScanner(bytes.NewReader(stampbuf))
for scanner.Scan() {
line := strings.SplitN(scanner.Text(), " ", 2)
switch len(line) {
case 0:
// Nothing to do here
case 1:
// Map to the empty string
stampMap[line[0]] = ""
case 2:
// Key and value
stampMap[line[0]] = line[1]
}
}
}
// Build an importcfg file.
importcfgName, err := buildImportcfgFile(archives, *packageList, goenv.installSuffix, filepath.Dir(*outFile))
if err != nil {
return err
}
defer os.Remove(importcfgName)
// generate any additional link options we need
goargs := goenv.goTool("link")
goargs = append(goargs, "-importcfg", importcfgName)
parseXdef := func(xdef string) (pkg, name, value string, err error) {
eq := strings.IndexByte(xdef, '=')
if eq < 0 {
return "", "", "", fmt.Errorf("-X or -Xstamp flag does not contain '=': %s", xdef)
}
dot := strings.LastIndexByte(xdef[:eq], '.')
if dot < 0 {
return "", "", "", fmt.Errorf("-X or -Xstamp flag does not contain '.': %s", xdef)
}
pkg, name, value = xdef[:dot], xdef[dot+1:eq], xdef[eq+1:]
if pkg == *packagePath {
pkg = "main"
}
return pkg, name, value, nil
}
for _, xdef := range xstamps {
pkg, name, key, err := parseXdef(xdef)
if err != nil {
return err
}
if value, ok := stampMap[key]; ok {
goargs = append(goargs, "-X", fmt.Sprintf("%s.%s=%s", pkg, name, value))
}
}
for _, xdef := range xdefs {
pkg, name, value, err := parseXdef(xdef)
if err != nil {
return err
}
goargs = append(goargs, "-X", fmt.Sprintf("%s.%s=%s", pkg, name, value))
}
if *buildmode != "" {
goargs = append(goargs, "-buildmode", *buildmode)
}
goargs = append(goargs, "-o", *outFile)
// add in the unprocess pass through options
goargs = append(goargs, toolArgs...)
goargs = append(goargs, *main)
if err := goenv.runCommand(goargs); err != nil {
return err
}
if *buildmode == "c-archive" {
if err := stripArMetadata(*outFile); err != nil {
return fmt.Errorf("error stripping archive metadata: %v", err)
}
}
return nil
}
func buildImportcfgFile(archives []archive, packageList, installSuffix, dir string) (string, error) {
buf := &bytes.Buffer{}
goroot, ok := os.LookupEnv("GOROOT")
if !ok {
return "", errors.New("GOROOT not set")
}
prefix := abs(filepath.Join(goroot, "pkg", installSuffix))
packageListFile, err := os.Open(packageList)
if err != nil {
return "", err
}
defer packageListFile.Close()
scanner := bufio.NewScanner(packageListFile)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
fmt.Fprintf(buf, "packagefile %s=%s.a\n", line, filepath.Join(prefix, filepath.FromSlash(line)))
}
if err := scanner.Err(); err != nil {
return "", err
}
depsSeen := map[string]string{}
for _, arc := range archives {
if conflictLabel, ok := depsSeen[arc.pkgPath]; ok {
// TODO(#1327): link.bzl should report this as a failure after 0.11.0.
// At this point, we'll prepare an importcfg file and remove logic here.
log.Printf(`warning: package %q is provided by more than one rule:
%s
%s
Set "importmap" to different paths in each library.
This will be an error in the future.`, arc.pkgPath, arc.label, conflictLabel)
continue
}
depsSeen[arc.pkgPath] = arc.label
fmt.Fprintf(buf, "packagefile %s=%s\n", arc.pkgPath, arc.file)
}
f, err := ioutil.TempFile(dir, "importcfg")
if err != nil {
return "", err
}
filename := f.Name()
if _, err := io.Copy(f, buf); err != nil {
f.Close()
os.Remove(filename)
return "", err
}
if err := f.Close(); err != nil {
os.Remove(filename)
return "", err
}
return filename, nil
}
type archiveMultiFlag []archive
func (m *archiveMultiFlag) String() string {
if m == nil || len(*m) == 0 {
return ""
}
return fmt.Sprint(m)
}
func (m *archiveMultiFlag) Set(v string) error {
parts := strings.Split(v, "=")
if len(parts) != 3 {
return fmt.Errorf("badly formed -arc flag: %s", v)
}
*m = append(*m, archive{
label: parts[0],
pkgPath: parts[1],
file: abs(parts[2]),
})
return nil
}
func main() {
log.SetFlags(0)
log.SetPrefix("GoLink: ")
if err := run(os.Args[1:]); err != nil {
log.Fatal(err)
}
}