forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
637 lines (572 loc) · 17.7 KB
/
utils.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package utils
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"unicode"
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/builder/progress"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/arduino-cli/i18n"
f "github.com/arduino/arduino-cli/internal/algorithms"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var tr = i18n.Tr
// ObjFileIsUpToDate fixdoc
func ObjFileIsUpToDate(sourceFile, objectFile, dependencyFile *paths.Path) (bool, error) {
logrus.Debugf("Checking previous results for %v (result = %v, dep = %v)", sourceFile, objectFile, dependencyFile)
if objectFile == nil || dependencyFile == nil {
logrus.Debugf("Not found: nil")
return false, nil
}
sourceFile = sourceFile.Clean()
sourceFileStat, err := sourceFile.Stat()
if err != nil {
return false, errors.WithStack(err)
}
objectFile = objectFile.Clean()
objectFileStat, err := objectFile.Stat()
if err != nil {
if os.IsNotExist(err) {
logrus.Debugf("Not found: %v", objectFile)
return false, nil
}
return false, errors.WithStack(err)
}
dependencyFile = dependencyFile.Clean()
dependencyFileStat, err := dependencyFile.Stat()
if err != nil {
if os.IsNotExist(err) {
logrus.Debugf("Not found: %v", dependencyFile)
return false, nil
}
return false, errors.WithStack(err)
}
if sourceFileStat.ModTime().After(objectFileStat.ModTime()) {
logrus.Debugf("%v newer than %v", sourceFile, objectFile)
return false, nil
}
if sourceFileStat.ModTime().After(dependencyFileStat.ModTime()) {
logrus.Debugf("%v newer than %v", sourceFile, dependencyFile)
return false, nil
}
rows, err := dependencyFile.ReadFileAsLines()
if err != nil {
return false, errors.WithStack(err)
}
rows = f.Map(rows, removeEndingBackSlash)
rows = f.Map(rows, strings.TrimSpace)
rows = f.Map(rows, unescapeDep)
rows = f.Filter(rows, f.NotEquals(""))
if len(rows) == 0 {
return true, nil
}
firstRow := rows[0]
if !strings.HasSuffix(firstRow, ":") {
logrus.Debugf("No colon in first line of depfile")
return false, nil
}
objFileInDepFile := firstRow[:len(firstRow)-1]
if objFileInDepFile != objectFile.String() {
logrus.Debugf("Depfile is about different file: %v", objFileInDepFile)
return false, nil
}
// The first line of the depfile contains the path to the object file to generate.
// The second line of the depfile contains the path to the source file.
// All subsequent lines contain the header files necessary to compile the object file.
// If we don't do this check it might happen that trying to compile a source file
// that has the same name but a different path wouldn't recreate the object file.
if sourceFile.String() != strings.Trim(rows[1], " ") {
return false, nil
}
rows = rows[1:]
for _, row := range rows {
depStat, err := os.Stat(row)
if err != nil && !os.IsNotExist(err) {
// There is probably a parsing error of the dep file
// Ignore the error and trigger a full rebuild anyway
logrus.WithError(err).Debugf("Failed to read: %v", row)
return false, nil
}
if os.IsNotExist(err) {
logrus.Debugf("Not found: %v", row)
return false, nil
}
if depStat.ModTime().After(objectFileStat.ModTime()) {
logrus.Debugf("%v newer than %v", row, objectFile)
return false, nil
}
}
return true, nil
}
func removeEndingBackSlash(s string) string {
return strings.TrimSuffix(s, "\\")
}
func unescapeDep(s string) string {
s = strings.ReplaceAll(s, "\\ ", " ")
s = strings.ReplaceAll(s, "\\\t", "\t")
s = strings.ReplaceAll(s, "\\#", "#")
s = strings.ReplaceAll(s, "$$", "$")
s = strings.ReplaceAll(s, "\\\\", "\\")
return s
}
// NormalizeUTF8 byte slice
// TODO: use it more often troughout all the project (maybe on logger interface?)
func NormalizeUTF8(buf []byte) []byte {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.Bytes(t, buf)
return result
}
var sourceControlFolders = map[string]bool{"CVS": true, "RCS": true, ".git": true, ".github": true, ".svn": true, ".hg": true, ".bzr": true, ".vscode": true, ".settings": true, ".pioenvs": true, ".piolibdeps": true}
// filterOutSCCS is a ReadDirFilter that excludes known VSC or project files
func filterOutSCCS(file *paths.Path) bool {
return !sourceControlFolders[file.Base()]
}
// filterReadableFiles is a ReadDirFilter that accepts only readable files
func filterReadableFiles(file *paths.Path) bool {
// See if the file is readable by opening it
f, err := file.Open()
if err != nil {
return false
}
f.Close()
return true
}
// filterOutHiddenFiles is a ReadDirFilter that exclude files with a "." prefix in their name
var filterOutHiddenFiles = paths.FilterOutPrefixes(".")
// FindFilesInFolder fixdoc
func FindFilesInFolder(dir *paths.Path, recurse bool, extensions ...string) (paths.PathList, error) {
fileFilter := paths.AndFilter(
filterOutHiddenFiles,
filterOutSCCS,
paths.FilterOutDirectories(),
filterReadableFiles,
)
if len(extensions) > 0 {
fileFilter = paths.AndFilter(
paths.FilterSuffixes(extensions...),
fileFilter,
)
}
if recurse {
dirFilter := paths.AndFilter(
filterOutHiddenFiles,
filterOutSCCS,
)
return dir.ReadDirRecursiveFiltered(dirFilter, fileFilter)
}
return dir.ReadDir(fileFilter)
}
// nolint
const (
Ignore = 0 // Redirect to null
Show = 1 // Show on stdout/stderr as normal
ShowIfVerbose = 2 // Show if verbose is set, Ignore otherwise
Capture = 3 // Capture into buffer
)
func printableArgument(arg string) string {
if strings.ContainsAny(arg, "\"\\ \t") {
arg = strings.ReplaceAll(arg, "\\", "\\\\")
arg = strings.ReplaceAll(arg, "\"", "\\\"")
return "\"" + arg + "\""
}
return arg
}
// Convert a command and argument slice back to a printable string.
// This adds basic escaping which is sufficient for debug output, but
// probably not for shell interpretation. This essentially reverses
// ParseCommandLine.
func printableCommand(parts []string) string {
return strings.Join(f.Map(parts, printableArgument), " ")
}
// ExecCommand fixdoc
func ExecCommand(
verbose bool,
stdoutWriter, stderrWriter io.Writer,
command *executils.Process, stdout int, stderr int,
) ([]byte, []byte, []byte, error) {
verboseInfoBuf := &bytes.Buffer{}
if verbose {
verboseInfoBuf.WriteString(printableCommand(command.GetArgs()))
}
stdoutBuffer := &bytes.Buffer{}
if stdout == Capture {
command.RedirectStdoutTo(stdoutBuffer)
} else if stdout == Show || (stdout == ShowIfVerbose && verbose) {
if stdoutWriter != nil {
command.RedirectStdoutTo(stdoutWriter)
} else {
command.RedirectStdoutTo(os.Stdout)
}
}
stderrBuffer := &bytes.Buffer{}
if stderr == Capture {
command.RedirectStderrTo(stderrBuffer)
} else if stderr == Show || (stderr == ShowIfVerbose && verbose) {
if stderrWriter != nil {
command.RedirectStderrTo(stderrWriter)
} else {
command.RedirectStderrTo(os.Stderr)
}
}
err := command.Start()
if err != nil {
return verboseInfoBuf.Bytes(), nil, nil, errors.WithStack(err)
}
err = command.Wait()
return verboseInfoBuf.Bytes(), stdoutBuffer.Bytes(), stderrBuffer.Bytes(), errors.WithStack(err)
}
// DirContentIsOlderThan DirContentIsOlderThan returns true if the content of the given directory is
// older than target file. If extensions are given, only the files with these
// extensions are tested.
func DirContentIsOlderThan(dir *paths.Path, target *paths.Path, extensions ...string) (bool, error) {
targetStat, err := target.Stat()
if err != nil {
return false, err
}
targetModTime := targetStat.ModTime()
files, err := FindFilesInFolder(dir, true, extensions...)
if err != nil {
return false, err
}
for _, file := range files {
file, err := file.Stat()
if err != nil {
return false, err
}
if file.ModTime().After(targetModTime) {
return false, nil
}
}
return true, nil
}
// PrepareCommandForRecipe fixdoc
func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, removeUnsetProperties bool) (*executils.Process, error) {
pattern := buildProperties.Get(recipe)
if pattern == "" {
return nil, errors.Errorf(tr("%[1]s pattern is missing"), recipe)
}
commandLine := buildProperties.ExpandPropsInString(pattern)
if removeUnsetProperties {
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
}
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
if err != nil {
return nil, errors.WithStack(err)
}
// if the overall commandline is too long for the platform
// try reducing the length by making the filenames relative
// and changing working directory to build.path
var relativePath string
if len(commandLine) > 30000 {
relativePath = buildProperties.Get("build.path")
for i, arg := range parts {
if _, err := os.Stat(arg); os.IsNotExist(err) {
continue
}
rel, err := filepath.Rel(relativePath, arg)
if err == nil && !strings.Contains(rel, "..") && len(rel) < len(arg) {
parts[i] = rel
}
}
}
command, err := executils.NewProcess(nil, parts...)
if err != nil {
return nil, errors.WithStack(err)
}
if relativePath != "" {
command.SetDir(relativePath)
}
return command, nil
}
// CompileFiles fixdoc
func CompileFiles(
sourceDir, buildPath *paths.Path,
buildProperties *properties.Map,
includes []string,
onlyUpdateCompilationDatabase bool,
compilationDatabase *builder.CompilationDatabase,
jobs int,
verbose bool,
warningsLevel string,
stdoutWriter, stderrWriter io.Writer,
verboseInfoFn func(msg string),
verboseStdoutFn, verboseStderrFn func(data []byte),
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
return compileFiles(
onlyUpdateCompilationDatabase,
compilationDatabase,
jobs,
sourceDir,
false,
buildPath, buildProperties, includes,
verbose,
warningsLevel,
stdoutWriter, stderrWriter,
verboseInfoFn, verboseStdoutFn, verboseStderrFn,
progress, progressCB,
)
}
// CompileFilesRecursive fixdoc
func CompileFilesRecursive(
sourceDir, buildPath *paths.Path,
buildProperties *properties.Map,
includes []string,
onlyUpdateCompilationDatabase bool,
compilationDatabase *builder.CompilationDatabase,
jobs int,
verbose bool,
warningsLevel string,
stdoutWriter, stderrWriter io.Writer,
verboseInfoFn func(msg string),
verboseStdoutFn, verboseStderrFn func(data []byte),
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
return compileFiles(
onlyUpdateCompilationDatabase,
compilationDatabase,
jobs,
sourceDir,
true,
buildPath, buildProperties, includes,
verbose,
warningsLevel,
stdoutWriter, stderrWriter,
verboseInfoFn, verboseStdoutFn, verboseStderrFn,
progress, progressCB,
)
}
func compileFiles(
onlyUpdateCompilationDatabase bool,
compilationDatabase *builder.CompilationDatabase,
jobs int,
sourceDir *paths.Path,
recurse bool,
buildPath *paths.Path,
buildProperties *properties.Map,
includes []string,
verbose bool,
warningsLevel string,
stdoutWriter, stderrWriter io.Writer,
verboseInfoFn func(msg string),
verboseStdoutFn, verboseStderrFn func(data []byte),
progress *progress.Struct,
progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
validExtensions := []string{}
for ext := range globals.SourceFilesValidExtensions {
validExtensions = append(validExtensions, ext)
}
sources, err := FindFilesInFolder(sourceDir, recurse, validExtensions...)
if err != nil {
return nil, err
}
progress.AddSubSteps(len(sources))
defer progress.RemoveSubSteps()
objectFiles := paths.NewPathList()
var objectFilesMux sync.Mutex
if len(sources) == 0 {
return objectFiles, nil
}
var errorsList []error
var errorsMux sync.Mutex
queue := make(chan *paths.Path)
job := func(source *paths.Path) {
recipe := fmt.Sprintf("recipe%s.o.pattern", source.Ext())
if !buildProperties.ContainsKey(recipe) {
recipe = fmt.Sprintf("recipe%s.o.pattern", globals.SourceFilesValidExtensions[source.Ext()])
}
objectFile, verboseInfo, verboseStdout, stderr, err := compileFileWithRecipe(
stdoutWriter, stderrWriter,
warningsLevel,
compilationDatabase,
verbose,
onlyUpdateCompilationDatabase,
sourceDir, source, buildPath, buildProperties, includes, recipe,
)
if verbose {
verboseStdoutFn(verboseStdout)
verboseInfoFn(string(verboseInfo))
}
verboseStderrFn(stderr)
if err != nil {
errorsMux.Lock()
errorsList = append(errorsList, err)
errorsMux.Unlock()
} else {
objectFilesMux.Lock()
objectFiles.Add(objectFile)
objectFilesMux.Unlock()
}
}
// Spawn jobs runners
var wg sync.WaitGroup
if jobs == 0 {
jobs = runtime.NumCPU()
}
for i := 0; i < jobs; i++ {
wg.Add(1)
go func() {
for source := range queue {
job(source)
}
wg.Done()
}()
}
// Feed jobs until error or done
for _, source := range sources {
errorsMux.Lock()
gotError := len(errorsList) > 0
errorsMux.Unlock()
if gotError {
break
}
queue <- source
progress.CompleteStep()
// PushProgress
if progressCB != nil {
progressCB(&rpc.TaskProgress{
Percent: progress.Progress,
Completed: progress.Progress >= 100.0,
})
}
}
close(queue)
wg.Wait()
if len(errorsList) > 0 {
// output the first error
return nil, errors.WithStack(errorsList[0])
}
objectFiles.Sort()
return objectFiles, nil
}
func compileFileWithRecipe(
stdoutWriter, stderrWriter io.Writer,
warningsLevel string,
compilationDatabase *builder.CompilationDatabase,
verbose, onlyUpdateCompilationDatabase bool,
sourcePath *paths.Path,
source *paths.Path,
buildPath *paths.Path,
buildProperties *properties.Map,
includes []string,
recipe string,
) (*paths.Path, []byte, []byte, []byte, error) {
verboseStdout, verboseInfo, errOut := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}
properties := buildProperties.Clone()
properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+warningsLevel))
properties.Set("includes", strings.Join(includes, " "))
properties.SetPath("source_file", source)
relativeSource, err := sourcePath.RelTo(source)
if err != nil {
return nil, nil, nil, nil, errors.WithStack(err)
}
depsFile := buildPath.Join(relativeSource.String() + ".d")
objectFile := buildPath.Join(relativeSource.String() + ".o")
properties.SetPath("object_file", objectFile)
err = objectFile.Parent().MkdirAll()
if err != nil {
return nil, nil, nil, nil, errors.WithStack(err)
}
objIsUpToDate, err := ObjFileIsUpToDate(source, objectFile, depsFile)
if err != nil {
return nil, nil, nil, nil, errors.WithStack(err)
}
command, err := PrepareCommandForRecipe(properties, recipe, false)
if err != nil {
return nil, nil, nil, nil, errors.WithStack(err)
}
if compilationDatabase != nil {
compilationDatabase.Add(source, command)
}
if !objIsUpToDate && !onlyUpdateCompilationDatabase {
// Since this compile could be multithreaded, we first capture the command output
info, stdout, stderr, err := ExecCommand(verbose, stdoutWriter, stderrWriter, command, Capture, Capture)
// and transfer all at once at the end...
if verbose {
verboseInfo.Write(info)
verboseStdout.Write(stdout)
}
errOut.Write(stderr)
// ...and then return the error
if err != nil {
return nil, verboseInfo.Bytes(), verboseStdout.Bytes(), errOut.Bytes(), errors.WithStack(err)
}
} else if verbose {
if objIsUpToDate {
verboseInfo.WriteString(tr("Using previously compiled file: %[1]s", objectFile))
} else {
verboseInfo.WriteString(tr("Skipping compile of: %[1]s", objectFile))
}
}
return objectFile, verboseInfo.Bytes(), verboseStdout.Bytes(), errOut.Bytes(), nil
}
// ArchiveCompiledFiles fixdoc
func ArchiveCompiledFiles(
buildPath *paths.Path, archiveFile *paths.Path, objectFilesToArchive paths.PathList, buildProperties *properties.Map,
onlyUpdateCompilationDatabase, verbose bool,
stdoutWriter, stderrWriter io.Writer,
) (*paths.Path, []byte, error) {
verboseInfobuf := &bytes.Buffer{}
archiveFilePath := buildPath.JoinPath(archiveFile)
if onlyUpdateCompilationDatabase {
if verbose {
verboseInfobuf.WriteString(tr("Skipping archive creation of: %[1]s", archiveFilePath))
}
return archiveFilePath, verboseInfobuf.Bytes(), nil
}
if archiveFileStat, err := archiveFilePath.Stat(); err == nil {
rebuildArchive := false
for _, objectFile := range objectFilesToArchive {
objectFileStat, err := objectFile.Stat()
if err != nil || objectFileStat.ModTime().After(archiveFileStat.ModTime()) {
// need to rebuild the archive
rebuildArchive = true
break
}
}
// something changed, rebuild the core archive
if rebuildArchive {
if err := archiveFilePath.Remove(); err != nil {
return nil, nil, errors.WithStack(err)
}
} else {
if verbose {
verboseInfobuf.WriteString(tr("Using previously compiled file: %[1]s", archiveFilePath))
}
return archiveFilePath, verboseInfobuf.Bytes(), nil
}
}
for _, objectFile := range objectFilesToArchive {
properties := buildProperties.Clone()
properties.Set("archive_file", archiveFilePath.Base())
properties.SetPath("archive_file_path", archiveFilePath)
properties.SetPath("object_file", objectFile)
command, err := PrepareCommandForRecipe(properties, "recipe.ar.pattern", false)
if err != nil {
return nil, verboseInfobuf.Bytes(), errors.WithStack(err)
}
verboseInfo, _, _, err := ExecCommand(verbose, stdoutWriter, stderrWriter, command, ShowIfVerbose /* stdout */, Show /* stderr */)
if verbose {
verboseInfobuf.WriteString(string(verboseInfo))
}
if err != nil {
return nil, verboseInfobuf.Bytes(), errors.WithStack(err)
}
}
return archiveFilePath, verboseInfobuf.Bytes(), nil
}