Skip to content

Commit 8e226b3

Browse files
alessio-peruginicmaglie
authored andcommitted
Move Preprocess and Build in arduino/builder
1 parent 6d57ce6 commit 8e226b3

File tree

9 files changed

+250
-476
lines changed

9 files changed

+250
-476
lines changed

arduino/builder/builder.go

Lines changed: 237 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/arduino/arduino-cli/arduino/builder/compilation"
23+
"github.com/arduino/arduino-cli/arduino/builder/detector"
2324
"github.com/arduino/arduino-cli/arduino/builder/logger"
2425
"github.com/arduino/arduino-cli/arduino/builder/progress"
2526
"github.com/arduino/arduino-cli/arduino/cores"
@@ -223,14 +224,243 @@ func (b *Builder) ExecutableSectionsSize() ExecutablesFileSections {
223224
return b.executableSectionsSize
224225
}
225226

226-
// SaveCompilationDatabase fixdoc
227-
func (b *Builder) SaveCompilationDatabase() {
228-
if b.compilationDatabase != nil {
229-
b.compilationDatabase.SaveToFile()
230-
}
231-
}
232-
233227
// TargetPlatform fixdoc
234228
func (b *Builder) TargetPlatform() *cores.PlatformRelease {
235229
return b.targetPlatform
236230
}
231+
232+
// Preprocess fixdoc
233+
func (b *Builder) Preprocess(detector *detector.SketchLibrariesDetector) error {
234+
b.Progress.AddSubSteps(6)
235+
defer b.Progress.RemoveSubSteps()
236+
return b.preprocess(detector)
237+
}
238+
239+
func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error {
240+
if err := b.buildPath.MkdirAll(); err != nil {
241+
return err
242+
}
243+
244+
if err := b.BuildOptionsManager.WipeBuildPath(); err != nil {
245+
return err
246+
}
247+
b.Progress.CompleteStep()
248+
b.Progress.PushProgress()
249+
250+
if err := b.RunRecipe("recipe.hooks.prebuild", ".pattern", false); err != nil {
251+
return err
252+
}
253+
b.Progress.CompleteStep()
254+
b.Progress.PushProgress()
255+
256+
if err := b.PrepareSketchBuildPath(); err != nil {
257+
return err
258+
}
259+
b.Progress.CompleteStep()
260+
b.Progress.PushProgress()
261+
262+
b.logIfVerbose(false, tr("Detecting libraries used..."))
263+
err := detector.FindIncludes(
264+
b.GetBuildPath(),
265+
b.GetBuildProperties().GetPath("build.core.path"),
266+
b.GetBuildProperties().GetPath("build.variant.path"),
267+
b.GetSketchBuildPath(),
268+
b.Sketch(),
269+
b.GetLibrariesBuildPath(),
270+
b.GetBuildProperties(),
271+
b.TargetPlatform().Platform.Architecture,
272+
)
273+
if err != nil {
274+
return err
275+
}
276+
b.Progress.CompleteStep()
277+
b.Progress.PushProgress()
278+
279+
b.WarnAboutArchIncompatibleLibraries(detector.ImportedLibraries())
280+
b.Progress.CompleteStep()
281+
b.Progress.PushProgress()
282+
283+
b.logIfVerbose(false, tr("Generating function prototypes..."))
284+
if err := b.PreprocessSketch(detector.IncludeFolders()); err != nil {
285+
return err
286+
}
287+
b.Progress.CompleteStep()
288+
b.Progress.PushProgress()
289+
290+
// Output arduino-preprocessed source
291+
preprocessedSketch, err := b.sketchBuildPath.Join(b.sketch.MainFile.Base() + ".cpp").ReadFile()
292+
if err != nil {
293+
return err
294+
}
295+
b.logger.WriteStdout(preprocessedSketch)
296+
297+
return nil
298+
}
299+
300+
func (b *Builder) logIfVerbose(warn bool, msg string) {
301+
if !b.logger.Verbose() {
302+
return
303+
}
304+
if warn {
305+
b.logger.Warn(msg)
306+
return
307+
}
308+
b.logger.Info(msg)
309+
}
310+
311+
// Build fixdoc
312+
func (b *Builder) Build(detector *detector.SketchLibrariesDetector) error {
313+
b.Progress.AddSubSteps(6 /** preprocess **/ + 21 /** build **/)
314+
defer b.Progress.RemoveSubSteps()
315+
316+
if err := b.preprocess(detector); err != nil {
317+
return err
318+
}
319+
320+
buildErr := b.build(detector)
321+
322+
detector.PrintUsedAndNotUsedLibraries(buildErr != nil)
323+
b.Progress.CompleteStep()
324+
b.Progress.PushProgress()
325+
326+
b.PrintUsedLibraries(detector.ImportedLibraries())
327+
b.Progress.CompleteStep()
328+
b.Progress.PushProgress()
329+
330+
if buildErr != nil {
331+
return buildErr
332+
}
333+
if err := b.ExportProjectCMake(detector.ImportedLibraries(), detector.IncludeFolders()); err != nil {
334+
return err
335+
}
336+
b.Progress.CompleteStep()
337+
b.Progress.PushProgress()
338+
339+
if err := b.Size(); err != nil {
340+
return err
341+
}
342+
b.Progress.CompleteStep()
343+
b.Progress.PushProgress()
344+
345+
return nil
346+
}
347+
348+
// Build fixdoc
349+
func (b *Builder) build(detector *detector.SketchLibrariesDetector) error {
350+
b.logIfVerbose(false, tr("Compiling sketch..."))
351+
if err := b.RunRecipe("recipe.hooks.sketch.prebuild", ".pattern", false); err != nil {
352+
return err
353+
}
354+
b.Progress.CompleteStep()
355+
b.Progress.PushProgress()
356+
357+
if err := b.BuildSketch(detector.IncludeFolders()); err != nil {
358+
return err
359+
}
360+
b.Progress.CompleteStep()
361+
b.Progress.PushProgress()
362+
363+
if err := b.RunRecipe("recipe.hooks.sketch.postbuild", ".pattern", true); err != nil {
364+
return err
365+
}
366+
b.Progress.CompleteStep()
367+
b.Progress.PushProgress()
368+
369+
b.logIfVerbose(false, tr("Compiling libraries..."))
370+
if err := b.RunRecipe("recipe.hooks.libraries.prebuild", ".pattern", false); err != nil {
371+
return err
372+
}
373+
b.Progress.CompleteStep()
374+
b.Progress.PushProgress()
375+
376+
if err := b.RemoveUnusedCompiledLibraries(detector.ImportedLibraries()); err != nil {
377+
return err
378+
}
379+
b.Progress.CompleteStep()
380+
b.Progress.PushProgress()
381+
382+
if err := b.BuildLibraries(detector.IncludeFolders(), detector.ImportedLibraries()); err != nil {
383+
return err
384+
}
385+
b.Progress.CompleteStep()
386+
b.Progress.PushProgress()
387+
388+
if err := b.RunRecipe("recipe.hooks.libraries.postbuild", ".pattern", true); err != nil {
389+
return err
390+
}
391+
b.Progress.CompleteStep()
392+
b.Progress.PushProgress()
393+
394+
b.logIfVerbose(false, tr("Compiling core..."))
395+
if err := b.RunRecipe("recipe.hooks.core.prebuild", ".pattern", false); err != nil {
396+
return err
397+
}
398+
b.Progress.CompleteStep()
399+
b.Progress.PushProgress()
400+
401+
if err := b.BuildCore(); err != nil {
402+
return err
403+
}
404+
b.Progress.CompleteStep()
405+
b.Progress.PushProgress()
406+
407+
if err := b.RunRecipe("recipe.hooks.core.postbuild", ".pattern", true); err != nil {
408+
return err
409+
}
410+
b.Progress.CompleteStep()
411+
b.Progress.PushProgress()
412+
413+
b.logIfVerbose(false, tr("Linking everything together..."))
414+
if err := b.RunRecipe("recipe.hooks.linking.prelink", ".pattern", false); err != nil {
415+
return err
416+
}
417+
b.Progress.CompleteStep()
418+
b.Progress.PushProgress()
419+
420+
if err := b.Link(); err != nil {
421+
return err
422+
}
423+
b.Progress.CompleteStep()
424+
b.Progress.PushProgress()
425+
426+
if err := b.RunRecipe("recipe.hooks.linking.postlink", ".pattern", true); err != nil {
427+
return err
428+
}
429+
b.Progress.CompleteStep()
430+
b.Progress.PushProgress()
431+
432+
if err := b.RunRecipe("recipe.hooks.objcopy.preobjcopy", ".pattern", false); err != nil {
433+
return err
434+
}
435+
b.Progress.CompleteStep()
436+
b.Progress.PushProgress()
437+
438+
if err := b.RunRecipe("recipe.objcopy.", ".pattern", true); err != nil {
439+
return err
440+
}
441+
b.Progress.CompleteStep()
442+
b.Progress.PushProgress()
443+
444+
if err := b.RunRecipe("recipe.hooks.objcopy.postobjcopy", ".pattern", true); err != nil {
445+
return err
446+
}
447+
b.Progress.CompleteStep()
448+
b.Progress.PushProgress()
449+
450+
if err := b.MergeSketchWithBootloader(); err != nil {
451+
return err
452+
}
453+
b.Progress.CompleteStep()
454+
b.Progress.PushProgress()
455+
456+
if err := b.RunRecipe("recipe.hooks.postbuild", ".pattern", true); err != nil {
457+
return err
458+
}
459+
b.Progress.CompleteStep()
460+
b.Progress.PushProgress()
461+
462+
if b.compilationDatabase != nil {
463+
b.compilationDatabase.SaveToFile()
464+
}
465+
return nil
466+
}

arduino/builder/export_cmake.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ import (
3535
var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`)
3636

3737
// ExportProjectCMake fixdoc
38-
func (b *Builder) ExportProjectCMake(
39-
sketchError bool, // Was there an error while compiling the sketch?
40-
importedLibraries libraries.List,
41-
includeFolders paths.PathList,
42-
) error {
38+
func (b *Builder) ExportProjectCMake(importedLibraries libraries.List, includeFolders paths.PathList) error {
4339
// copies the contents of the file named src to the file named
4440
// by dst. The file will be created if it does not already exist. If the
4541
// destination file exists, all it's contents will be replaced by the contents
@@ -175,8 +171,8 @@ func (b *Builder) ExportProjectCMake(
175171
}
176172
var validStaticLibExtensions = []string{".a"}
177173

178-
// If sketch error or cannot export Cmake project
179-
if sketchError || b.buildProperties.Get("compiler.export_cmake") == "" {
174+
// If cannot export Cmake project
175+
if b.buildProperties.Get("compiler.export_cmake") == "" {
180176
return nil
181177
}
182178

arduino/builder/sizer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut
5151
}
5252

5353
// Size fixdoc
54-
func (b *Builder) Size(sketchError bool) error {
55-
if b.onlyUpdateCompilationDatabase || sketchError {
54+
func (b *Builder) Size() error {
55+
if b.onlyUpdateCompilationDatabase {
5656
return nil
5757
}
5858

commands/compile/compile.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/arduino/arduino-cli/arduino"
27-
bldr "github.com/arduino/arduino-cli/arduino/builder"
27+
"github.com/arduino/arduino-cli/arduino/builder"
2828
"github.com/arduino/arduino-cli/arduino/builder/detector"
2929
"github.com/arduino/arduino-cli/arduino/builder/logger"
3030
"github.com/arduino/arduino-cli/arduino/builder/progress"
@@ -37,8 +37,6 @@ import (
3737
"github.com/arduino/arduino-cli/configuration"
3838
"github.com/arduino/arduino-cli/i18n"
3939
"github.com/arduino/arduino-cli/internal/inventory"
40-
"github.com/arduino/arduino-cli/legacy/builder"
41-
"github.com/arduino/arduino-cli/legacy/builder/types"
4240
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
4341
paths "github.com/arduino/go-paths-helper"
4442
"github.com/sirupsen/logrus"
@@ -174,16 +172,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
174172
return nil, err
175173
}
176174

177-
builderCtx := &types.Context{}
178175
actualPlatform := buildPlatform
179176
builtinLibrariesDir := configuration.IDEBuiltinLibrariesDir(configuration.Settings)
180177
otherLibrariesDirs := paths.NewPathList(req.GetLibraries()...)
181178
otherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
182179

183180
builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings())
184-
builderCtx.BuilderLogger = builderLogger
185181

186-
sketchBuilder, err := bldr.NewBuilder(
182+
sketchBuilder, err := builder.NewBuilder(
187183
sk,
188184
boardBuildProperties,
189185
buildPath,
@@ -207,14 +203,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
207203
if strings.Contains(err.Error(), "invalid build properties") {
208204
return nil, &arduino.InvalidArgumentError{Message: tr("Invalid build properties"), Cause: err}
209205
}
210-
if errors.Is(err, bldr.ErrSketchCannotBeLocatedInBuildPath) {
206+
if errors.Is(err, builder.ErrSketchCannotBeLocatedInBuildPath) {
211207
return r, &arduino.CompileFailedError{
212208
Message: tr("Sketch cannot be located in build path. Please specify a different build path"),
213209
}
214210
}
215211
return r, &arduino.CompileFailedError{Message: err.Error()}
216212
}
217-
builderCtx.Builder = sketchBuilder
218213

219214
var libsManager *librariesmanager.LibrariesManager
220215
if pme.GetProfile() != nil {
@@ -235,7 +230,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
235230
builderLogger.Warn(string(verboseOut))
236231
}
237232

238-
builderCtx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector(
233+
sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
239234
libsManager, libsResolver,
240235
useCachedLibrariesResolution,
241236
req.GetCreateCompilationDatabaseOnly(),
@@ -270,7 +265,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
270265

271266
if req.GetPreprocess() {
272267
// Just output preprocessed source code and exit
273-
compileErr := builder.RunPreprocess(builderCtx)
268+
compileErr := sketchBuilder.Preprocess(sketchLibrariesDetector)
274269
if compileErr != nil {
275270
compileErr = &arduino.CompileFailedError{Message: compileErr.Error()}
276271
}
@@ -279,7 +274,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
279274

280275
defer func() {
281276
importedLibs := []*rpc.Library{}
282-
for _, lib := range builderCtx.SketchLibrariesDetector.ImportedLibraries() {
277+
for _, lib := range sketchLibrariesDetector.ImportedLibraries() {
283278
rpcLib, err := lib.ToRPCLibrary()
284279
if err != nil {
285280
msg := tr("Error getting information for library %s", lib.Name) + ": " + err.Error() + "\n"
@@ -315,7 +310,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
315310
targetBoard.String(), "'build.board'", sketchBuilder.GetBuildProperties().Get("build.board")) + "\n"))
316311
}
317312

318-
if err := builder.RunBuilder(builderCtx); err != nil {
313+
if err := sketchBuilder.Build(sketchLibrariesDetector); err != nil {
319314
return r, &arduino.CompileFailedError{Message: err.Error()}
320315
}
321316

internal/integrationtest/daemon/daemon_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func TestDaemonCompileOptions(t *testing.T) {
187187
// https://github.com/arduino/arduino-cli/issues/2016
188188
// assert that the task progress is increasing and doesn't contain multiple 100% values
189189
results := analyzer.Results[""]
190-
require.True(t, results[len(results)-1].Completed)
190+
require.True(t, results[len(results)-1].Completed, fmt.Sprintf("latest percent value: %v", results[len(results)-1].Percent))
191191
require.IsNonDecreasing(t, f.Map(results, (*commands.TaskProgress).GetPercent))
192192
}
193193

0 commit comments

Comments
 (0)