1616package builder_utils
1717
1818import (
19+ "fmt"
1920 "os"
2021 "os/exec"
2122 "path/filepath"
2223 "runtime"
2324 "strings"
2425 "sync"
2526
27+ "github.com/arduino/arduino-cli/arduino/globals"
2628 "github.com/arduino/arduino-cli/i18n"
2729 "github.com/arduino/arduino-cli/legacy/builder/constants"
2830 "github.com/arduino/arduino-cli/legacy/builder/types"
@@ -35,93 +37,6 @@ import (
3537
3638var tr = i18n .Tr
3739
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-
12540func findAllFilesInFolder (sourcePath string , recurse bool ) ([]string , error ) {
12641 files , err := utils .ReadDirFiltered (sourcePath , utils .FilterFiles ())
12742 if err != nil {
@@ -153,17 +68,46 @@ func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
15368 return sources , nil
15469}
15570
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+
157100 objectFiles := paths .NewPathList ()
101+ var objectFilesMux sync.Mutex
158102 if len (sources ) == 0 {
159103 return objectFiles , nil
160104 }
161- var objectFilesMux sync.Mutex
162105 var errorsList []error
163106 var errorsMux sync.Mutex
164107
165108 queue := make (chan * paths.Path )
166109 job := func (source * paths.Path ) {
110+ recipe := fmt .Sprintf ("recipe%s.o.pattern" , source .Ext ())
167111 objectFile , err := compileFileWithRecipe (ctx , sourcePath , source , buildPath , buildProperties , includes , recipe )
168112 if err != nil {
169113 errorsMux .Lock ()
@@ -233,7 +177,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
233177 return nil , errors .WithStack (err )
234178 }
235179
236- objIsUpToDate , err := ObjFileIsUpToDate (ctx , source , objectFile , depsFile )
180+ objIsUpToDate , err := ObjFileIsUpToDate (source , objectFile , depsFile )
237181 if err != nil {
238182 return nil , errors .WithStack (err )
239183 }
@@ -260,7 +204,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
260204 return objectFile , nil
261205}
262206
263- func ObjFileIsUpToDate (ctx * types. Context , sourceFile , objectFile , dependencyFile * paths.Path ) (bool , error ) {
207+ func ObjFileIsUpToDate (sourceFile , objectFile , dependencyFile * paths.Path ) (bool , error ) {
264208 logrus .Debugf ("Checking previous results for %v (result = %v, dep = %v)" , sourceFile , objectFile , dependencyFile )
265209 if objectFile == nil || dependencyFile == nil {
266210 logrus .Debugf ("Not found: nil" )
@@ -371,10 +315,7 @@ func unescapeDep(s string) string {
371315}
372316
373317func removeEndingBackSlash (s string ) string {
374- if strings .HasSuffix (s , "\\ " ) {
375- s = s [:len (s )- 1 ]
376- }
377- return s
318+ return strings .TrimSuffix (s , "\\ " )
378319}
379320
380321func nonEmptyString (s string ) bool {
0 commit comments