/
object_makefile.go
396 lines (326 loc) · 8.51 KB
/
object_makefile.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
pathutil "path"
"strings"
)
// Represents a Makefile
type makefile_t struct {
entry_t
parent *dir_t
contents *makefile_contents_t // Initially nil
sources []go_source_code_t
built bool
nowBuilding bool
}
const (
MAKEFILE_CMD = iota
MAKEFILE_PKG
MAKEFILE_UNKNOWN_TYPE
)
type makefile_contents_t struct {
targ string
kind int // One of: MAKEFILE_CMD, MAKEFILE_PKG, MAKEFILE_UNKNOWN_TYPE
goFiles []string
cgo bool
}
// ==========
// makefile_t
// ==========
func new_makefile(entry entry_t, parent *dir_t) (*makefile_t, error) {
makefile := &makefile_t{
entry_t: entry,
parent: parent,
sources: make([]go_source_code_t, 0, 0),
}
if parent.makefile_orNil == nil {
parent.makefile_orNil = makefile
} else {
return nil, errors.New("directory \"" + parent.path + "\" contains multiple makefiles")
}
newObjects[makefile] = 0
return makefile, nil
}
func (m *makefile_t) UpdateFileSystemModel() {
m.UpdateFileInfo()
}
func (m *makefile_t) Contents() (*makefile_contents_t, error) {
if m.contents == nil {
var err error
m.contents, err = read_makefile_contents(m.path, m.parent.path)
if err != nil {
return nil, err
}
}
return m.contents, nil
}
func (m *makefile_t) InferObjects(updateTests bool) error {
if !m.exists {
return nil
}
contents, err := m.Contents()
if err != nil {
return err
}
if len(contents.targ) == 0 {
return nil
}
if len(contents.goFiles) > 0 {
sources := make([]go_source_code_t, len(contents.goFiles))
for i, goFile := range contents.goFiles {
goFile = pathutil.Clean(goFile)
if !strings.HasSuffix(goFile, ".go") {
return errors.New("makefile \"" + m.Path() + "\": Go file \"" + goFile + "\"' should have .go extension")
}
var _src object_t = m.parent.getObject_orNil(strings.Split(goFile, "/"))
var src go_source_code_t
if _src == nil {
return errors.New("failed to find file \"" + goFile + "\"")
} else {
var isSrc bool
src, isSrc = _src.(go_source_code_t)
if !isSrc {
return errors.New("file \"" + _src.Path() + "\" was expected to be a Go file")
}
if !src.Exists() {
return errors.New("file \"" + _src.Path() + "\" does not exist")
}
}
sources[i] = src
}
m.sources = sources
}
if contents.kind == MAKEFILE_CMD {
var exe *executable_t
exe_dir := m.parent
exe_name := pathutil.Base(contents.targ)
exe, err := exe_dir.getOrCreate_executable(exe_name)
if err != nil {
return err
}
// Register 'm' with the 'exe'
err = exe.addMakefile(m)
if err != nil {
return err
}
} else if contents.kind == MAKEFILE_PKG {
objDir := m.parent.getOrCreateSubDir("_obj")
dirPath, baseName := pathutil.Split(contents.targ)
lib_dir := objDir.getOrCreateSubDirs(strings.Split(dirPath, "/"))
lib_name := goArchiver_libNamePrefix + baseName + ".a"
var lib *library_t
lib, err = lib_dir.getOrCreate_library(lib_name)
if err != nil {
return err
}
// Register 'm' with the 'lib'
err = lib.addMakefile(m)
if err != nil {
return err
}
// Add 'lib' to the package resolution table
err = mapImportPath(contents.targ, lib, objDir, /*test*/ false)
if err != nil {
return err
}
}
return nil
}
func (m *makefile_t) PrintDependencies(w io.Writer) {
return
}
func (m *makefile_t) Info(info *info_t) {
return
}
var make_exe = &Executable{
name: "make",
}
func (m *makefile_t) Make() error {
if m.built {
return nil
}
if m.nowBuilding {
return errors.New("circular dependency involving \"" + m.path + "\"")
}
m.nowBuilding = true
defer func() { m.nowBuilding = false }()
// Build all prerequisites
{
for _, src := range m.sources {
contents, err := src.Contents()
if err != nil {
return err
}
_, err = contents.makePrerequisites( /*testPackage_orEmpty*/ "")
if err != nil {
return err
}
}
}
args := []string{make_exe.name, "-f", m.name}
err := make_exe.runSimply(args, /*dir*/ m.parent.path, /*dontPrint*/ false)
if err != nil {
return err
}
// Update our internal model of filesystem contents
m.parent.UpdateFileSystemModel()
m.built = true
return nil
}
func (m *makefile_t) MakeInstall() error {
// Build all prerequisites and build the target
err := m.Make()
if err != nil {
return err
}
args := []string{make_exe.name, "-f", m.name, "install"}
err = make_exe.runSimply(args, /*dir*/ m.parent.path, /*dontPrint*/ false)
if err != nil {
return err
}
// Update our internal model of filesystem contents
m.parent.UpdateFileSystemModel()
return nil
}
func (m *makefile_t) MakeTests() error {
args := []string{make_exe.name, "-f", m.name, "test"}
err := make_exe.runSimply(args, /*dir*/ m.parent.path, /*dontPrint*/ false)
if err != nil {
return err
}
// Update our internal model of filesystem contents
m.parent.UpdateFileSystemModel()
return nil
}
func (m *makefile_t) RunTests(testPattern, benchPattern string, errors *[]error) {
err := m.MakeTests()
if err != nil {
*errors = append(*errors, err)
}
}
func (m *makefile_t) Clean() error {
args := []string{make_exe.name, "-f", m.name, "clean"}
err := make_exe.runSimply(args, /*dir*/ m.parent.path, /*dontPrint*/ false)
if err != nil {
return err
}
// Update our internal model of filesystem contents
m.parent.UpdateFileSystemModel()
return nil
}
func (m *makefile_t) GoFmt(files *[]string) error {
return nil
}
// ===================
// makefile_contents_t
// ===================
const make_print_ruleName = "__printMakefileVars"
const make_print_rule = "\n\n" + make_print_ruleName + ": \n" +
"\t@echo $(TARG)\n" +
"\t@echo $(GOFILES)\n" +
"\t@echo $(CGOFILES)\n"
func read_makefile_contents(path, dir string) (*makefile_contents_t, error) {
var makefileText string
{
_makefileText, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
makefileText = string(_makefileText)
}
var makeOutput_lines []string
{
// Execute "make -f -"
args := []string{make_exe.name, "-f", "-", make_print_ruleName}
makeOutput, _, err := make_exe.run(args, dir, (makefileText + make_print_rule), /*mergeStdoutAndStderr*/ false)
if err != nil {
return nil, errors.New("failed to extract needed variables from \"" + path + "\": " + err.Error())
}
makeOutput_lines = strings.Split(makeOutput, "\n")
if len(makeOutput_lines) < 3 {
return nil, errors.New("failed to extract needed variables from \"" + path + "\"")
}
}
var targ string = strings.TrimSpace(makeOutput_lines[0])
var goFiles []string = strings.Fields(makeOutput_lines[1])
var cgoFiles string = strings.TrimSpace(makeOutput_lines[2])
if len(targ) == 0 {
goFiles = make([]string, 0)
cgoFiles = ""
}
if len(cgoFiles) > 0 {
// The variable GOFILES was modified by
// the (pressumably) included "$(GOROOT)/src/Make.{cmd,pkg,...}".
// Reconstruct the original value of GOFILES:
i, j := 0, 0
for i < len(goFiles) {
goFiles_i := goFiles[i]
if strings.HasPrefix(goFiles_i, "_cgo_") {
// Ignore 'goFiles[i]'
i++
} else if strings.HasSuffix(goFiles_i, ".cgo1.go") {
f := goFiles_i
goFiles[j] = f[0:len(f)-len(".cgo1.go")] + ".go"
i++
j++
} else {
goFiles[j] = goFiles_i
i++
j++
}
}
goFiles = goFiles[0:j]
if !(*flag_gcc) {
// Check the minimum compiler version
compilerVersion, err := getGoCompilerVersion()
if err != nil {
return nil, err
}
if compilerVersion < min_compiler_version_for_cgo {
msg := fmt.Sprintf("The makefile \"%s\" is using CGO."+
" Found Go compiler has version %d."+
" Minimum version supported by GOAM is %d.",
path, compilerVersion, min_compiler_version_for_cgo)
return nil, errors.New(msg)
}
}
}
if *flag_debug {
fmt.Printf("read makefile: %s\n", path)
fmt.Printf(" TARG: %s\n", targ)
fmt.Printf(" GOFILES: %v\n", goFiles)
fmt.Printf(" CGO: %v\n", (len(cgoFiles) > 0))
}
cmd := (strings.Index(makefileText, "Make.cmd") != -1)
pkg := (strings.Index(makefileText, "Make.pkg") != -1)
var kind int
switch {
case cmd && !pkg:
if *flag_debug {
fmt.Printf(" kind: cmd\n")
}
kind = MAKEFILE_CMD
case !cmd && pkg:
if *flag_debug {
fmt.Printf(" kind: pkg\n")
}
kind = MAKEFILE_PKG
case cmd && pkg:
return nil, errors.New("the makefile \"" + path + "\" contains both 'Make.cmd' and 'Make.pkg'")
default:
if *flag_debug {
fmt.Printf(" kind: unknown\n")
}
kind = MAKEFILE_UNKNOWN_TYPE
}
contents := &makefile_contents_t{
targ: targ,
kind: kind,
goFiles: goFiles,
cgo: (len(cgoFiles) > 0),
}
return contents, nil
}