16
16
package builder_utils
17
17
18
18
import (
19
+ "fmt"
19
20
"os"
20
21
"os/exec"
21
22
"path/filepath"
22
23
"runtime"
23
24
"strings"
24
25
"sync"
25
26
27
+ "github.com/arduino/arduino-cli/arduino/globals"
26
28
"github.com/arduino/arduino-cli/i18n"
27
29
"github.com/arduino/arduino-cli/legacy/builder/constants"
28
30
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -35,93 +37,6 @@ import (
35
37
36
38
var tr = i18n .Tr
37
39
38
- func CompileFilesRecursive (ctx * types.Context , sourcePath * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
39
- objectFiles , err := CompileFiles (ctx , sourcePath , false , buildPath , buildProperties , includes )
40
- if err != nil {
41
- return nil , errors .WithStack (err )
42
- }
43
-
44
- folders , err := utils .ReadDirFiltered (sourcePath .String (), utils .FilterDirs )
45
- if err != nil {
46
- return nil , errors .WithStack (err )
47
- }
48
-
49
- for _ , folder := range folders {
50
- subFolderObjectFiles , err := CompileFilesRecursive (ctx , sourcePath .Join (folder .Name ()), buildPath .Join (folder .Name ()), buildProperties , includes )
51
- if err != nil {
52
- return nil , errors .WithStack (err )
53
- }
54
- objectFiles .AddAll (subFolderObjectFiles )
55
- }
56
-
57
- return objectFiles , nil
58
- }
59
-
60
- func CompileFiles (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
61
- sSources , err := findFilesInFolder (sourcePath , ".S" , recurse )
62
- if err != nil {
63
- return nil , errors .WithStack (err )
64
- }
65
- cSources , err := findFilesInFolder (sourcePath , ".c" , recurse )
66
- if err != nil {
67
- return nil , errors .WithStack (err )
68
- }
69
- cppSources , err := findFilesInFolder (sourcePath , ".cpp" , recurse )
70
- if err != nil {
71
- return nil , errors .WithStack (err )
72
- }
73
-
74
- ctx .Progress .AddSubSteps (len (sSources ) + len (cSources ) + len (cppSources ))
75
- defer ctx .Progress .RemoveSubSteps ()
76
-
77
- sObjectFiles , err := compileFilesWithRecipe (ctx , sourcePath , sSources , buildPath , buildProperties , includes , constants .RECIPE_S_PATTERN )
78
- if err != nil {
79
- return nil , errors .WithStack (err )
80
- }
81
- cObjectFiles , err := compileFilesWithRecipe (ctx , sourcePath , cSources , buildPath , buildProperties , includes , constants .RECIPE_C_PATTERN )
82
- if err != nil {
83
- return nil , errors .WithStack (err )
84
- }
85
- cppObjectFiles , err := compileFilesWithRecipe (ctx , sourcePath , cppSources , buildPath , buildProperties , includes , constants .RECIPE_CPP_PATTERN )
86
- if err != nil {
87
- return nil , errors .WithStack (err )
88
- }
89
-
90
- objectFiles := paths .NewPathList ()
91
- objectFiles .AddAll (sObjectFiles )
92
- objectFiles .AddAll (cObjectFiles )
93
- objectFiles .AddAll (cppObjectFiles )
94
- return objectFiles , nil
95
- }
96
-
97
- func findFilesInFolder (sourcePath * paths.Path , extension string , recurse bool ) (paths.PathList , error ) {
98
- files , err := utils .ReadDirFiltered (sourcePath .String (), utils .FilterFilesWithExtensions (extension ))
99
- if err != nil {
100
- return nil , errors .WithStack (err )
101
- }
102
- var sources paths.PathList
103
- for _ , file := range files {
104
- sources = append (sources , sourcePath .Join (file .Name ()))
105
- }
106
-
107
- if recurse {
108
- folders , err := utils .ReadDirFiltered (sourcePath .String (), utils .FilterDirs )
109
- if err != nil {
110
- return nil , errors .WithStack (err )
111
- }
112
-
113
- for _ , folder := range folders {
114
- otherSources , err := findFilesInFolder (sourcePath .Join (folder .Name ()), extension , recurse )
115
- if err != nil {
116
- return nil , errors .WithStack (err )
117
- }
118
- sources = append (sources , otherSources ... )
119
- }
120
- }
121
-
122
- return sources , nil
123
- }
124
-
125
40
func findAllFilesInFolder (sourcePath string , recurse bool ) ([]string , error ) {
126
41
files , err := utils .ReadDirFiltered (sourcePath , utils .FilterFiles ())
127
42
if err != nil {
@@ -153,17 +68,46 @@ func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
153
68
return sources , nil
154
69
}
155
70
156
- func compileFilesWithRecipe (ctx * types.Context , sourcePath * paths.Path , sources paths.PathList , buildPath * paths.Path , buildProperties * properties.Map , includes []string , recipe string ) (paths.PathList , error ) {
71
+ func CompileFiles (ctx * types.Context , sourcePath * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
72
+ return compileFiles (ctx , sourcePath , false , buildPath , buildProperties , includes )
73
+ }
74
+
75
+ func CompileFilesRecursive (ctx * types.Context , sourcePath * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
76
+ return compileFiles (ctx , sourcePath , true , buildPath , buildProperties , includes )
77
+ }
78
+
79
+ func compileFiles (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
80
+ var sources paths.PathList
81
+ var err error
82
+ if recurse {
83
+ sources , err = sourcePath .ReadDirRecursive ()
84
+ } else {
85
+ sources , err = sourcePath .ReadDir ()
86
+ }
87
+ if err != nil {
88
+ return nil , err
89
+ }
90
+
91
+ validExtensions := []string {}
92
+ for ext := range globals .SourceFilesValidExtensions {
93
+ validExtensions = append (validExtensions , ext )
94
+ }
95
+
96
+ sources .FilterSuffix (validExtensions ... )
97
+ ctx .Progress .AddSubSteps (len (sources ))
98
+ defer ctx .Progress .RemoveSubSteps ()
99
+
157
100
objectFiles := paths .NewPathList ()
101
+ var objectFilesMux sync.Mutex
158
102
if len (sources ) == 0 {
159
103
return objectFiles , nil
160
104
}
161
- var objectFilesMux sync.Mutex
162
105
var errorsList []error
163
106
var errorsMux sync.Mutex
164
107
165
108
queue := make (chan * paths.Path )
166
109
job := func (source * paths.Path ) {
110
+ recipe := fmt .Sprintf ("recipe%s.o.pattern" , source .Ext ())
167
111
objectFile , err := compileFileWithRecipe (ctx , sourcePath , source , buildPath , buildProperties , includes , recipe )
168
112
if err != nil {
169
113
errorsMux .Lock ()
@@ -233,7 +177,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
233
177
return nil , errors .WithStack (err )
234
178
}
235
179
236
- objIsUpToDate , err := ObjFileIsUpToDate (ctx , source , objectFile , depsFile )
180
+ objIsUpToDate , err := ObjFileIsUpToDate (source , objectFile , depsFile )
237
181
if err != nil {
238
182
return nil , errors .WithStack (err )
239
183
}
@@ -260,7 +204,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
260
204
return objectFile , nil
261
205
}
262
206
263
- func ObjFileIsUpToDate (ctx * types. Context , sourceFile , objectFile , dependencyFile * paths.Path ) (bool , error ) {
207
+ func ObjFileIsUpToDate (sourceFile , objectFile , dependencyFile * paths.Path ) (bool , error ) {
264
208
logrus .Debugf ("Checking previous results for %v (result = %v, dep = %v)" , sourceFile , objectFile , dependencyFile )
265
209
if objectFile == nil || dependencyFile == nil {
266
210
logrus .Debugf ("Not found: nil" )
@@ -371,10 +315,7 @@ func unescapeDep(s string) string {
371
315
}
372
316
373
317
func removeEndingBackSlash (s string ) string {
374
- if strings .HasSuffix (s , "\\ " ) {
375
- s = s [:len (s )- 1 ]
376
- }
377
- return s
318
+ return strings .TrimSuffix (s , "\\ " )
378
319
}
379
320
380
321
func nonEmptyString (s string ) bool {
0 commit comments