16
16
package phases
17
17
18
18
import (
19
+ "io"
19
20
"strings"
20
21
22
+ "github.com/arduino/arduino-cli/arduino/builder"
21
23
"github.com/arduino/arduino-cli/arduino/builder/cpp"
24
+ "github.com/arduino/arduino-cli/arduino/builder/progress"
22
25
"github.com/arduino/arduino-cli/arduino/builder/utils"
23
26
"github.com/arduino/arduino-cli/arduino/libraries"
24
27
f "github.com/arduino/arduino-cli/internal/algorithms"
25
28
"github.com/arduino/arduino-cli/legacy/builder/constants"
26
- "github.com/arduino/arduino-cli/legacy/builder/types "
29
+ rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1 "
27
30
"github.com/arduino/go-paths-helper"
28
31
"github.com/arduino/go-properties-orderedmap"
29
32
"github.com/pkg/errors"
@@ -32,26 +35,43 @@ import (
32
35
var FLOAT_ABI_CFLAG = "float-abi"
33
36
var FPU_CFLAG = "fpu"
34
37
35
- type LibrariesBuilder struct {}
36
-
37
- func (s * LibrariesBuilder ) Run (ctx * types.Context ) error {
38
- librariesBuildPath := ctx .LibrariesBuildPath
39
- buildProperties := ctx .BuildProperties
40
- includesFolders := ctx .SketchLibrariesDetector .IncludeFolders ()
38
+ func LibrariesBuilder (
39
+ librariesBuildPath * paths.Path ,
40
+ buildProperties * properties.Map ,
41
+ includesFolders paths.PathList ,
42
+ importedLibraries libraries.List ,
43
+ verbose , onlyUpdateCompilationDatabase bool ,
44
+ compilationDatabase * builder.CompilationDatabase ,
45
+ jobs int ,
46
+ warningsLevel string ,
47
+ stdoutWriter , stderrWriter io.Writer ,
48
+ verboseInfoFn func (msg string ),
49
+ verboseStdoutFn , verboseStderrFn func (data []byte ),
50
+ progress * progress.Struct , progressCB rpc.TaskProgressCB ,
51
+ ) (paths.PathList , error ) {
41
52
includes := f .Map (includesFolders .AsStrings (), cpp .WrapWithHyphenI )
42
- libs := ctx . SketchLibrariesDetector . ImportedLibraries ()
53
+ libs := importedLibraries
43
54
44
55
if err := librariesBuildPath .MkdirAll (); err != nil {
45
- return errors .WithStack (err )
56
+ return nil , errors .WithStack (err )
46
57
}
47
58
48
- objectFiles , err := compileLibraries (ctx , libs , librariesBuildPath , buildProperties , includes )
59
+ librariesObjectFiles , err := compileLibraries (
60
+ libs , librariesBuildPath , buildProperties , includes ,
61
+ verbose , onlyUpdateCompilationDatabase ,
62
+ compilationDatabase ,
63
+ jobs ,
64
+ warningsLevel ,
65
+ stdoutWriter , stderrWriter ,
66
+ verboseInfoFn ,
67
+ verboseStdoutFn , verboseStderrFn ,
68
+ progress , progressCB ,
69
+ )
49
70
if err != nil {
50
- return errors .WithStack (err )
71
+ return nil , errors .WithStack (err )
51
72
}
52
73
53
- ctx .LibrariesObjectFiles = objectFiles
54
- return nil
74
+ return librariesObjectFiles , nil
55
75
}
56
76
57
77
func directoryContainsFile (folder * paths.Path ) bool {
@@ -62,12 +82,16 @@ func directoryContainsFile(folder *paths.Path) bool {
62
82
return false
63
83
}
64
84
65
- func findExpectedPrecompiledLibFolder (ctx * types.Context , library * libraries.Library ) * paths.Path {
66
- mcu := ctx .BuildProperties .Get (constants .BUILD_PROPERTIES_BUILD_MCU )
85
+ func findExpectedPrecompiledLibFolder (
86
+ library * libraries.Library ,
87
+ buildProperties * properties.Map ,
88
+ verboseInfoFn func (msg string ),
89
+ ) * paths.Path {
90
+ mcu := buildProperties .Get (constants .BUILD_PROPERTIES_BUILD_MCU )
67
91
// Add fpu specifications if they exist
68
92
// To do so, resolve recipe.cpp.o.pattern,
69
93
// search for -mfpu=xxx -mfloat-abi=yyy and add to a subfolder
70
- command , _ := utils .PrepareCommandForRecipe (ctx . BuildProperties , "recipe.cpp.o.pattern" , true )
94
+ command , _ := utils .PrepareCommandForRecipe (buildProperties , "recipe.cpp.o.pattern" , true )
71
95
fpuSpecs := ""
72
96
for _ , el := range command .GetArgs () {
73
97
if strings .Contains (el , FPU_CFLAG ) {
@@ -88,50 +112,85 @@ func findExpectedPrecompiledLibFolder(ctx *types.Context, library *libraries.Lib
88
112
}
89
113
}
90
114
91
- ctx . Info (tr ("Library %[1]s has been declared precompiled:" , library .Name ))
115
+ verboseInfoFn (tr ("Library %[1]s has been declared precompiled:" , library .Name ))
92
116
93
117
// Try directory with full fpuSpecs first, if available
94
118
if len (fpuSpecs ) > 0 {
95
119
fpuSpecs = strings .TrimRight (fpuSpecs , "-" )
96
120
fullPrecompDir := library .SourceDir .Join (mcu ).Join (fpuSpecs )
97
121
if fullPrecompDir .Exist () && directoryContainsFile (fullPrecompDir ) {
98
- ctx . Info (tr ("Using precompiled library in %[1]s" , fullPrecompDir ))
122
+ verboseInfoFn (tr ("Using precompiled library in %[1]s" , fullPrecompDir ))
99
123
return fullPrecompDir
100
124
}
101
- ctx . Info (tr (`Precompiled library in "%[1]s" not found` , fullPrecompDir ))
125
+ verboseInfoFn (tr (`Precompiled library in "%[1]s" not found` , fullPrecompDir ))
102
126
}
103
127
104
128
precompDir := library .SourceDir .Join (mcu )
105
129
if precompDir .Exist () && directoryContainsFile (precompDir ) {
106
- ctx . Info (tr ("Using precompiled library in %[1]s" , precompDir ))
130
+ verboseInfoFn (tr ("Using precompiled library in %[1]s" , precompDir ))
107
131
return precompDir
108
132
}
109
- ctx . Info (tr (`Precompiled library in "%[1]s" not found` , precompDir ))
133
+ verboseInfoFn (tr (`Precompiled library in "%[1]s" not found` , precompDir ))
110
134
return nil
111
135
}
112
136
113
- func compileLibraries (ctx * types.Context , libraries libraries.List , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
114
- ctx .Progress .AddSubSteps (len (libraries ))
115
- defer ctx .Progress .RemoveSubSteps ()
137
+ func compileLibraries (
138
+ libraries libraries.List , buildPath * paths.Path , buildProperties * properties.Map , includes []string ,
139
+ verbose , onlyUpdateCompilationDatabase bool ,
140
+ compilationDatabase * builder.CompilationDatabase ,
141
+ jobs int ,
142
+ warningsLevel string ,
143
+ stdoutWriter , stderrWriter io.Writer ,
144
+ verboseInfoFn func (msg string ),
145
+ verboseStdoutFn , verboseStderrFn func (data []byte ),
146
+ progress * progress.Struct , progressCB rpc.TaskProgressCB ,
147
+ ) (paths.PathList , error ) {
148
+ progress .AddSubSteps (len (libraries ))
149
+ defer progress .RemoveSubSteps ()
116
150
117
151
objectFiles := paths .NewPathList ()
118
152
for _ , library := range libraries {
119
- libraryObjectFiles , err := compileLibrary (ctx , library , buildPath , buildProperties , includes )
153
+ libraryObjectFiles , err := compileLibrary (
154
+ library , buildPath , buildProperties , includes ,
155
+ verbose , onlyUpdateCompilationDatabase ,
156
+ compilationDatabase ,
157
+ jobs ,
158
+ warningsLevel ,
159
+ stdoutWriter , stderrWriter ,
160
+ verboseInfoFn , verboseStdoutFn , verboseStderrFn ,
161
+ progress , progressCB ,
162
+ )
120
163
if err != nil {
121
164
return nil , errors .WithStack (err )
122
165
}
123
166
objectFiles .AddAll (libraryObjectFiles )
124
167
125
- ctx .Progress .CompleteStep ()
126
- ctx .PushProgress ()
168
+ progress .CompleteStep ()
169
+ // PushProgress
170
+ if progressCB != nil {
171
+ progressCB (& rpc.TaskProgress {
172
+ Percent : progress .Progress ,
173
+ Completed : progress .Progress >= 100.0 ,
174
+ })
175
+ }
127
176
}
128
177
129
178
return objectFiles , nil
130
179
}
131
180
132
- func compileLibrary (ctx * types.Context , library * libraries.Library , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
133
- if ctx .Verbose {
134
- ctx .Info (tr (`Compiling library "%[1]s"` , library .Name ))
181
+ func compileLibrary (
182
+ library * libraries.Library , buildPath * paths.Path , buildProperties * properties.Map , includes []string ,
183
+ verbose , onlyUpdateCompilationDatabase bool ,
184
+ compilationDatabase * builder.CompilationDatabase ,
185
+ jobs int ,
186
+ warningsLevel string ,
187
+ stdoutWriter , stderrWriter io.Writer ,
188
+ verboseInfoFn func (msg string ),
189
+ verboseStdoutFn , verboseStderrFn func (data []byte ),
190
+ progress * progress.Struct , progressCB rpc.TaskProgressCB ,
191
+ ) (paths.PathList , error ) {
192
+ if verbose {
193
+ verboseInfoFn (tr (`Compiling library "%[1]s"` , library .Name ))
135
194
}
136
195
libraryBuildPath := buildPath .Join (library .DirName )
137
196
@@ -142,11 +201,15 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
142
201
objectFiles := paths .NewPathList ()
143
202
144
203
if library .Precompiled {
145
- coreSupportPrecompiled := ctx .BuildProperties .ContainsKey ("compiler.libraries.ldflags" )
146
- precompiledPath := findExpectedPrecompiledLibFolder (ctx , library )
204
+ coreSupportPrecompiled := buildProperties .ContainsKey ("compiler.libraries.ldflags" )
205
+ precompiledPath := findExpectedPrecompiledLibFolder (
206
+ library ,
207
+ buildProperties ,
208
+ verboseInfoFn ,
209
+ )
147
210
148
211
if ! coreSupportPrecompiled {
149
- ctx . Info (tr ("The platform does not support '%[1]s' for precompiled libraries." , "compiler.libraries.ldflags" ))
212
+ verboseInfoFn (tr ("The platform does not support '%[1]s' for precompiled libraries." , "compiler.libraries.ldflags" ))
150
213
} else if precompiledPath != nil {
151
214
// Find all libraries in precompiledPath
152
215
libs , err := precompiledPath .ReadDir ()
@@ -165,8 +228,8 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
165
228
}
166
229
}
167
230
168
- currLDFlags := ctx . BuildProperties .Get ("compiler.libraries.ldflags" )
169
- ctx . BuildProperties .Set ("compiler.libraries.ldflags" , currLDFlags + " \" -L" + precompiledPath .String ()+ "\" " + libsCmd + " " )
231
+ currLDFlags := buildProperties .Get ("compiler.libraries.ldflags" )
232
+ buildProperties .Set ("compiler.libraries.ldflags" , currLDFlags + " \" -L" + precompiledPath .String ()+ "\" " + libsCmd + " " )
170
233
171
234
// TODO: This codepath is just taken for .a with unusual names that would
172
235
// be ignored by -L / -l methods.
@@ -188,28 +251,26 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
188
251
if library .Layout == libraries .RecursiveLayout {
189
252
libObjectFiles , err := utils .CompileFilesRecursive (
190
253
library .SourceDir , libraryBuildPath , buildProperties , includes ,
191
- ctx .OnlyUpdateCompilationDatabase ,
192
- ctx .CompilationDatabase ,
193
- ctx .Jobs ,
194
- ctx .Verbose ,
195
- ctx .WarningsLevel ,
196
- ctx .Stdout , ctx .Stderr ,
197
- func (msg string ) { ctx .Info (msg ) },
198
- func (data []byte ) { ctx .WriteStdout (data ) },
199
- func (data []byte ) { ctx .WriteStderr (data ) },
200
- & ctx .Progress , ctx .ProgressCB ,
254
+ onlyUpdateCompilationDatabase ,
255
+ compilationDatabase ,
256
+ jobs ,
257
+ verbose ,
258
+ warningsLevel ,
259
+ stdoutWriter , stderrWriter ,
260
+ verboseInfoFn , verboseStdoutFn , verboseStderrFn ,
261
+ progress , progressCB ,
201
262
)
202
263
if err != nil {
203
264
return nil , errors .WithStack (err )
204
265
}
205
266
if library .DotALinkage {
206
267
archiveFile , verboseInfo , err := utils .ArchiveCompiledFiles (
207
268
libraryBuildPath , paths .New (library .DirName + ".a" ), libObjectFiles , buildProperties ,
208
- ctx . OnlyUpdateCompilationDatabase , ctx . Verbose ,
209
- ctx . Stdout , ctx . Stderr ,
269
+ onlyUpdateCompilationDatabase , verbose ,
270
+ stdoutWriter , stderrWriter ,
210
271
)
211
- if ctx . Verbose {
212
- ctx . Info (string (verboseInfo ))
272
+ if verbose {
273
+ verboseInfoFn (string (verboseInfo ))
213
274
}
214
275
if err != nil {
215
276
return nil , errors .WithStack (err )
@@ -224,16 +285,14 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
224
285
}
225
286
libObjectFiles , err := utils .CompileFiles (
226
287
library .SourceDir , libraryBuildPath , buildProperties , includes ,
227
- ctx .OnlyUpdateCompilationDatabase ,
228
- ctx .CompilationDatabase ,
229
- ctx .Jobs ,
230
- ctx .Verbose ,
231
- ctx .WarningsLevel ,
232
- ctx .Stdout , ctx .Stderr ,
233
- func (msg string ) { ctx .Info (msg ) },
234
- func (data []byte ) { ctx .WriteStdout (data ) },
235
- func (data []byte ) { ctx .WriteStderr (data ) },
236
- & ctx .Progress , ctx .ProgressCB ,
288
+ onlyUpdateCompilationDatabase ,
289
+ compilationDatabase ,
290
+ jobs ,
291
+ verbose ,
292
+ warningsLevel ,
293
+ stdoutWriter , stderrWriter ,
294
+ verboseInfoFn , verboseStdoutFn , verboseStderrFn ,
295
+ progress , progressCB ,
237
296
)
238
297
if err != nil {
239
298
return nil , errors .WithStack (err )
@@ -244,16 +303,14 @@ func compileLibrary(ctx *types.Context, library *libraries.Library, buildPath *p
244
303
utilityBuildPath := libraryBuildPath .Join ("utility" )
245
304
utilityObjectFiles , err := utils .CompileFiles (
246
305
library .UtilityDir , utilityBuildPath , buildProperties , includes ,
247
- ctx .OnlyUpdateCompilationDatabase ,
248
- ctx .CompilationDatabase ,
249
- ctx .Jobs ,
250
- ctx .Verbose ,
251
- ctx .WarningsLevel ,
252
- ctx .Stdout , ctx .Stderr ,
253
- func (msg string ) { ctx .Info (msg ) },
254
- func (data []byte ) { ctx .WriteStdout (data ) },
255
- func (data []byte ) { ctx .WriteStderr (data ) },
256
- & ctx .Progress , ctx .ProgressCB ,
306
+ onlyUpdateCompilationDatabase ,
307
+ compilationDatabase ,
308
+ jobs ,
309
+ verbose ,
310
+ warningsLevel ,
311
+ stdoutWriter , stderrWriter ,
312
+ verboseInfoFn , verboseStdoutFn , verboseStderrFn ,
313
+ progress , progressCB ,
257
314
)
258
315
if err != nil {
259
316
return nil , errors .WithStack (err )
0 commit comments