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
|
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
)
type command struct {
path string
args []string
envUpdates []string
}
func newProcessCommand() *command {
return &command{
path: os.Args[0],
args: os.Args[1:],
}
}
func newExecCmd(env env, cmd *command) *exec.Cmd {
execCmd := exec.Command(cmd.path, cmd.args...)
execCmd.Env = append(env.environ(), cmd.envUpdates...)
ensurePathEnv(execCmd)
execCmd.Dir = env.getwd()
return execCmd
}
func ensurePathEnv(cmd *exec.Cmd) {
for _, env := range cmd.Env {
if strings.HasPrefix(env, "PATH=") {
return
}
}
cmd.Env = append(cmd.Env, "PATH=")
}
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)
nameParts := strings.Split(basename, "-")
if len(nameParts) != 5 {
return nil, newErrorwithSourceLocf("expected 5 parts in the compiler name. Actual: %s", basename)
}
compiler := nameParts[4]
var compilerType compilerType
switch {
case strings.HasPrefix(compiler, "clang"):
compilerType = clangType
default:
compilerType = gccType
}
absWrapperDir, err := getAbsWrapperDir(env, cmd)
if err != nil {
return nil, err
}
rootPath := filepath.Join(absWrapperDir, cfg.rootRelPath)
return &commandBuilder{
path: cmd.path,
args: createBuilderArgs( /*fromUser=*/ true, cmd.args),
env: env,
cfg: cfg,
rootPath: rootPath,
target: builderTarget{
target: strings.Join(nameParts[:4], "-"),
arch: nameParts[0],
vendor: nameParts[1],
sys: nameParts[2],
abi: nameParts[3],
compiler: compiler,
compilerType: compilerType,
},
}, nil
}
type commandBuilder struct {
path string
target builderTarget
args []builderArg
envUpdates []string
env env
cfg *config
rootPath string
}
type builderArg struct {
value string
fromUser bool
}
type compilerType int32
const (
gccType compilerType = iota
clangType
)
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,
}
}
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,
}
}
|