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
248
249
250
251
252
253
254
255
256
257
258
259
260
|
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
type command struct {
Path string `json:"path"`
Args []string `json:"args"`
// Updates and additions have the form:
// `NAME=VALUE`
// Removals have the form:
// `NAME=`.
EnvUpdates []string `json:"env_updates,omitempty"`
}
func newProcessCommand() *command {
// This is a workaround for the fact that ld.so does not support
// passing in the executable name when ld.so is invoked as
// an executable (crbug/1003841).
path := os.Getenv("LD_ARGV0")
if path == "" {
path = os.Args[0]
}
return &command{
Path: path,
Args: os.Args[1:],
}
}
func mergeEnvValues(values []string, updates []string) []string {
envMap := map[string]string{}
for _, entry := range values {
equalPos := strings.IndexRune(entry, '=')
envMap[entry[:equalPos]] = entry[equalPos+1:]
}
for _, update := range updates {
equalPos := strings.IndexRune(update, '=')
key := update[:equalPos]
value := update[equalPos+1:]
if value == "" {
delete(envMap, key)
} else {
envMap[key] = value
}
}
env := []string{}
for key, value := range envMap {
env = append(env, key+"="+value)
}
return env
}
func runCmd(env env, cmd *command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
execCmd := exec.Command(cmd.Path, cmd.Args...)
execCmd.Env = mergeEnvValues(env.environ(), cmd.EnvUpdates)
execCmd.Dir = env.getwd()
execCmd.Stdin = stdin
execCmd.Stdout = stdout
execCmd.Stderr = stderr
return execCmd.Run()
}
func resolveAgainstPathEnv(env env, cmd string) (string, error) {
path, _ := env.getenv("PATH")
for _, path := range strings.Split(path, ":") {
resolvedPath := filepath.Join(path, cmd)
if _, err := os.Lstat(resolvedPath); err == nil {
return resolvedPath, nil
}
}
return "", fmt.Errorf("Couldn't find cmd %q in path", cmd)
}
func getAbsCmdPath(env env, cmd *command) string {
path := cmd.Path
if !filepath.IsAbs(path) {
path = filepath.Join(env.getwd(), path)
}
return path
}
func newCommandBuilder(env env, cfg *config, cmd *command) (*commandBuilder, error) {
basename := filepath.Base(cmd.Path)
var nameParts []string
if basename == "clang-tidy" {
nameParts = []string{basename}
} else {
nameParts = strings.Split(basename, "-")
}
target := builderTarget{}
switch len(nameParts) {
case 1:
// E.g. gcc
target = builderTarget{
compiler: nameParts[0],
}
case 4:
// E.g. armv7m-cros-eabi-gcc
target = builderTarget{
arch: nameParts[0],
vendor: nameParts[1],
abi: nameParts[2],
compiler: nameParts[3],
target: basename[:strings.LastIndex(basename, "-")],
}
case 5:
// E.g. x86_64-cros-linux-gnu-gcc
target = builderTarget{
arch: nameParts[0],
vendor: nameParts[1],
sys: nameParts[2],
abi: nameParts[3],
compiler: nameParts[4],
target: basename[:strings.LastIndex(basename, "-")],
}
default:
return nil, newErrorwithSourceLocf("unexpected compiler name pattern. Actual: %s", basename)
}
var compilerType compilerType
switch {
case strings.HasPrefix(target.compiler, "clang-tidy"):
compilerType = clangTidyType
case strings.HasPrefix(target.compiler, "clang"):
compilerType = clangType
default:
compilerType = gccType
}
target.compilerType = compilerType
absWrapperPath, err := getAbsWrapperPath(env, cmd)
if err != nil {
return nil, err
}
rootPath := filepath.Join(filepath.Dir(absWrapperPath), cfg.rootRelPath)
return &commandBuilder{
path: cmd.Path,
args: createBuilderArgs( /*fromUser=*/ true, cmd.Args),
env: env,
cfg: cfg,
rootPath: rootPath,
absWrapperPath: absWrapperPath,
target: target,
}, nil
}
type commandBuilder struct {
path string
target builderTarget
args []builderArg
envUpdates []string
env env
cfg *config
rootPath string
absWrapperPath string
}
type builderArg struct {
value string
fromUser bool
}
type compilerType int32
const (
gccType compilerType = iota
clangType
clangTidyType
)
type builderTarget struct {
target string
arch string
vendor string
sys string
abi string
compiler string
compilerType compilerType
}
func createBuilderArgs(fromUser bool, args []string) []builderArg {
builderArgs := make([]builderArg, len(args))
for i, arg := range args {
builderArgs[i] = builderArg{value: arg, fromUser: fromUser}
}
return builderArgs
}
func (builder *commandBuilder) clone() *commandBuilder {
return &commandBuilder{
path: builder.path,
args: append([]builderArg{}, builder.args...),
env: builder.env,
cfg: builder.cfg,
rootPath: builder.rootPath,
target: builder.target,
absWrapperPath: builder.absWrapperPath,
}
}
func (builder *commandBuilder) wrapPath(path string) {
builder.args = append([]builderArg{{value: builder.path, fromUser: false}}, builder.args...)
builder.path = path
}
func (builder *commandBuilder) addPreUserArgs(args ...string) {
index := 0
for _, arg := range builder.args {
if arg.fromUser {
break
}
index++
}
builder.args = append(builder.args[:index], append(createBuilderArgs( /*fromUser=*/ false, args), builder.args[index:]...)...)
}
func (builder *commandBuilder) addPostUserArgs(args ...string) {
builder.args = append(builder.args, createBuilderArgs( /*fromUser=*/ false, args)...)
}
// Allows to map and filter arguments. Filters when the callback returns an empty string.
func (builder *commandBuilder) transformArgs(transform func(arg builderArg) string) {
// See https://github.com/golang/go/wiki/SliceTricks
newArgs := builder.args[:0]
for _, arg := range builder.args {
newArg := transform(arg)
if newArg != "" {
newArgs = append(newArgs, builderArg{
value: newArg,
fromUser: arg.fromUser,
})
}
}
builder.args = newArgs
}
func (builder *commandBuilder) updateEnv(updates ...string) {
builder.envUpdates = append(builder.envUpdates, updates...)
}
func (builder *commandBuilder) build() *command {
cmdArgs := make([]string, len(builder.args))
for i, builderArg := range builder.args {
cmdArgs[i] = builderArg.value
}
return &command{
Path: builder.path,
Args: cmdArgs,
EnvUpdates: builder.envUpdates,
}
}
|