-
Notifications
You must be signed in to change notification settings - Fork 64
/
make.go
196 lines (163 loc) · 4.31 KB
/
make.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package make
import (
"context"
"fmt"
"reflect"
"runtime"
"strings"
"github.com/cli/safeexec"
"kraftkit.sh/exec"
)
const (
DefaultBinaryName = "make"
DefaultDarwinBinaryName = "gmake"
DefaultWindowsBinaryName = "nmake"
)
type export struct {
export string
omitempty bool
def string
}
func parseExport(tag reflect.StructTag) (*export, error) {
parts := strings.Split(tag.Get("export"), ",")
if len(parts) == 0 {
return nil, fmt.Errorf("could not identify export tag")
}
e := &export{
export: parts[0],
}
def := tag.Get("default")
if len(def) > 0 {
e.def = def
}
for _, part := range parts[1:] {
switch true {
case part == "omitempty":
e.omitempty = true
}
}
return e, nil
}
type Make struct {
opts *MakeOptions
seq *exec.SequentialProcesses
cpw *calculateProgressWriter
}
// NewFromInterface prepares a GNU Make command call by parsing the input
// interface searching for `export` annotations within each attribute's tag.
func NewFromInterface(args interface{}, mopts ...MakeOption) (*Make, error) {
t := reflect.TypeOf(args)
v := reflect.ValueOf(args)
if v.Kind() == reflect.Ptr {
return nil, fmt.Errorf("cannot derive interface arguments from pointer: passed by reference")
}
for i := 0; i < t.NumField(); i++ {
e, err := parseExport(t.Field(i).Tag)
if err != nil {
return nil, fmt.Errorf("could not parse export tag: %s", err)
}
if len(e.export) > 0 {
val := v.Field(i).String()
if len(val) == 0 && len(e.def) > 0 {
val = e.def
}
if e.omitempty && len(val) == 0 {
continue
}
mopts = append(mopts,
WithVar(e.export, val),
)
}
}
var err error
make := &Make{}
make.opts, err = NewMakeOptions(mopts...)
if err != nil {
return nil, err
}
if len(make.opts.bin) == 0 {
switch runtime.GOOS {
case "darwin":
// Check if gmake is installed
// If not, fall back to make
_, err := safeexec.LookPath(DefaultDarwinBinaryName)
if err != nil {
make.opts.bin = DefaultBinaryName
} else {
make.opts.bin = DefaultDarwinBinaryName
}
case "windows":
make.opts.bin = DefaultWindowsBinaryName
default:
make.opts.bin = DefaultBinaryName
}
}
var processes []*exec.Process
var calcProgressExec *exec.Executable
// The trick to determining the progress of the execution of the make
// invocation is to first call `make -n` and read the number of lines. Set up
// a sequential call to first invoke this execution. The exec library's
// SequentialProcesses will handle correctly invoking the command under the
// same conditions.
if make.opts.onProgress != nil && !make.opts.justPrint {
popts, err := NewMakeOptions(
WithJustPrint(true),
WithDirectory(make.opts.directory),
WithBinPath(make.opts.bin),
WithVars(make.opts.vars),
WithTarget(make.opts.targets...),
)
if err != nil {
return nil, err
}
calcProgressExec, err = exec.NewExecutable(make.opts.bin, *popts, popts.Vars()...)
if err != nil {
return nil, err
}
onProgressCallback := &onProgressWriter{
onProgress: make.opts.onProgress,
}
make.cpw = &calculateProgressWriter{}
calcProgressProcess, err := exec.NewProcessFromExecutable(
calcProgressExec,
append(make.opts.eopts,
exec.WithStdout(make.cpw),
exec.WithOnExitCallback(func(exitCode int) {
if exitCode != 0 {
return
}
onProgressCallback.total = make.cpw.totalLines
}),
)...,
)
if err != nil {
return nil, err
}
make.opts.eopts = append(make.opts.eopts,
exec.WithStdoutCallback(onProgressCallback),
)
processes = append(processes, calcProgressProcess)
}
mainExec, err := exec.NewExecutable(make.opts.bin, *make.opts, make.opts.Vars()...)
if err != nil {
return nil, err
}
mainProcess, err := exec.NewProcessFromExecutable(mainExec, make.opts.eopts...)
if err != nil {
return nil, err
}
seq, err := exec.NewSequential(append(processes, mainProcess)...)
if err != nil {
return nil, err
}
make.seq = seq
return make, nil
}
// Execute starts and waits on the prepared make invocation
func (m *Make) Execute(ctx context.Context) error {
return m.seq.StartAndWait(ctx)
}