Skip to content

Commit

Permalink
remove Progress from Context
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent d3d4ed6 commit 9715450
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 96 deletions.
10 changes: 10 additions & 0 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/arduino/arduino-cli/arduino/builder/logger"
"github.com/arduino/arduino-cli/arduino/builder/progress"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -59,6 +60,9 @@ type Builder struct {
// Set to true to skip build and produce only Compilation Database
onlyUpdateCompilationDatabase bool

// Progress of all various steps
Progress *progress.Struct

*BuildOptionsManager
}

Expand All @@ -78,6 +82,7 @@ func NewBuilder(
sourceOverrides map[string]string,
onlyUpdateCompilationDatabase bool,
logger *logger.BuilderLogger,
progressStats *progress.Struct,
) (*Builder, error) {
buildProperties := properties.NewMap()
if boardBuildProperties != nil {
Expand Down Expand Up @@ -126,6 +131,10 @@ func NewBuilder(
return nil, ErrSketchCannotBeLocatedInBuildPath
}

if progressStats == nil {
progressStats = progress.New(nil)
}

return &Builder{
sketch: sk,
buildProperties: buildProperties,
Expand All @@ -140,6 +149,7 @@ func NewBuilder(
clean: clean,
sourceOverrides: sourceOverrides,
onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase,
Progress: progressStats,
BuildOptionsManager: NewBuildOptionsManager(
hardwareDirs, builtInToolsDirs, otherLibrariesDirs,
builtInLibrariesDirs, buildPath,
Expand Down
14 changes: 3 additions & 11 deletions arduino/builder/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ import (

"github.com/arduino/arduino-cli/arduino/builder/compilation"
"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/builder/progress"
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/buildcache"
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/pkg/errors"
)
Expand All @@ -38,7 +36,6 @@ import (
func (b *Builder) BuildCore(
actualPlatform *cores.PlatformRelease,
compilationDatabase *compilation.Database,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, *paths.Path, error) {
if err := b.coreBuildPath.MkdirAll(); err != nil {
return nil, nil, errors.WithStack(err)
Expand All @@ -56,11 +53,7 @@ func (b *Builder) BuildCore(
}
}

archiveFile, objectFiles, err := b.compileCore(
actualPlatform,
compilationDatabase,
progress, progressCB,
)
archiveFile, objectFiles, err := b.compileCore(actualPlatform, compilationDatabase)
if err != nil {
return nil, nil, errors.WithStack(err)
}
Expand All @@ -71,7 +64,6 @@ func (b *Builder) BuildCore(
func (b *Builder) compileCore(
actualPlatform *cores.PlatformRelease,
compilationDatabase *compilation.Database,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (*paths.Path, paths.PathList, error) {
coreFolder := b.buildProperties.GetPath("build.core.path")
variantFolder := b.buildProperties.GetPath("build.variant.path")
Expand All @@ -92,7 +84,7 @@ func (b *Builder) compileCore(
compilationDatabase,
b.jobs,
b.logger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, nil, errors.WithStack(err)
Expand Down Expand Up @@ -143,7 +135,7 @@ func (b *Builder) compileCore(
compilationDatabase,
b.jobs,
b.logger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, nil, errors.WithStack(err)
Expand Down
37 changes: 9 additions & 28 deletions arduino/builder/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import (

"github.com/arduino/arduino-cli/arduino/builder/compilation"
"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/builder/progress"
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/libraries"
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"
Expand All @@ -44,7 +42,6 @@ func (b *Builder) BuildLibraries(
includesFolders paths.PathList,
importedLibraries libraries.List,
compilationDatabase *compilation.Database,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)
libs := importedLibraries
Expand All @@ -53,11 +50,7 @@ func (b *Builder) BuildLibraries(
return nil, errors.WithStack(err)
}

librariesObjectFiles, err := b.compileLibraries(
libs, includes,
compilationDatabase,
progress, progressCB,
)
librariesObjectFiles, err := b.compileLibraries(libs, includes, compilationDatabase)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down Expand Up @@ -124,34 +117,23 @@ func (b *Builder) findExpectedPrecompiledLibFolder(
return nil
}

func (b *Builder) compileLibraries(
libraries libraries.List, includes []string,
compilationDatabase *compilation.Database,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
progress.AddSubSteps(len(libraries))
defer progress.RemoveSubSteps()
func (b *Builder) compileLibraries(libraries libraries.List, includes []string, compilationDatabase *compilation.Database) (paths.PathList, error) {
b.Progress.AddSubSteps(len(libraries))
defer b.Progress.RemoveSubSteps()

objectFiles := paths.NewPathList()
for _, library := range libraries {
libraryObjectFiles, err := b.compileLibrary(
library, includes,
compilationDatabase,
progress, progressCB,
)
if err != nil {
return nil, errors.WithStack(err)
}
objectFiles.AddAll(libraryObjectFiles)

progress.CompleteStep()
// PushProgress
if progressCB != nil {
progressCB(&rpc.TaskProgress{
Percent: progress.Progress,
Completed: progress.Progress >= 100.0,
})
}
b.Progress.CompleteStep()
b.Progress.PushProgress()
}

return objectFiles, nil
Expand All @@ -160,7 +142,6 @@ func (b *Builder) compileLibraries(
func (b *Builder) compileLibrary(
library *libraries.Library, includes []string,
compilationDatabase *compilation.Database,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
if b.logger.Verbose() {
b.logger.Info(tr(`Compiling library "%[1]s"`, library.Name))
Expand Down Expand Up @@ -227,7 +208,7 @@ func (b *Builder) compileLibrary(
compilationDatabase,
b.jobs,
b.logger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -258,7 +239,7 @@ func (b *Builder) compileLibrary(
compilationDatabase,
b.jobs,
b.logger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -273,7 +254,7 @@ func (b *Builder) compileLibrary(
compilationDatabase,
b.jobs,
b.logger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
18 changes: 18 additions & 0 deletions arduino/builder/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@

package progress

import rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"

// Struct fixdoc
type Struct struct {
Progress float32
StepAmount float32
Parent *Struct
callback rpc.TaskProgressCB
}

// New fixdoc
func New(callback rpc.TaskProgressCB) *Struct {
return &Struct{callback: callback}
}

// AddSubSteps fixdoc
Expand All @@ -46,3 +54,13 @@ func (p *Struct) RemoveSubSteps() {
func (p *Struct) CompleteStep() {
p.Progress += p.StepAmount
}

// PushProgress fixdoc
func (p *Struct) PushProgress() {
if p.callback != nil {
p.callback(&rpc.TaskProgress{
Percent: p.Progress,
Completed: p.Progress >= 100.0,
})
}
}
7 changes: 2 additions & 5 deletions arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ import (

"github.com/arduino/arduino-cli/arduino/builder/compilation"
"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/builder/progress"
"github.com/arduino/arduino-cli/arduino/builder/utils"
"github.com/arduino/arduino-cli/arduino/sketch"
"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/marcinbor85/gohex"

Expand Down Expand Up @@ -185,7 +183,6 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error {
func (b *Builder) BuildSketch(
includesFolders paths.PathList,
compilationDatabase *compilation.Database,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)

Expand All @@ -199,7 +196,7 @@ func (b *Builder) BuildSketch(
compilationDatabase,
b.jobs,
b.builderLogger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -214,7 +211,7 @@ func (b *Builder) BuildSketch(
compilationDatabase,
b.jobs,
b.builderLogger,
progress, progressCB,
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
6 changes: 3 additions & 3 deletions arduino/builder/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestMergeSketchSources(t *testing.T) {

b, err := NewBuilder(
sk, nil, paths.New("testdata"), false, nil, 0, nil,
nil, nil, nil, nil, fqbn, false, nil, false, nil)
nil, nil, nil, nil, fqbn, false, nil, false, nil, nil)
require.NoError(t, err)

offset, source, err := b.sketchMergeSources(nil)
Expand All @@ -73,7 +73,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) {

// ensure not to include Arduino.h when it's already there
b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil,
nil, nil, nil, nil, fqbn, false, nil, false, nil)
nil, nil, nil, nil, fqbn, false, nil, false, nil, nil)
require.NoError(t, err)

_, source, err := b.sketchMergeSources(nil)
Expand All @@ -95,7 +95,7 @@ func TestCopyAdditionalFiles(t *testing.T) {
require.NoError(t, err)

b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil,
nil, nil, nil, nil, fqbn, false, nil, false, nil)
nil, nil, nil, nil, fqbn, false, nil, false, nil, nil)
require.NoError(t, err)

// copy the sketch over, create a fake main file we don't care about it
Expand Down
18 changes: 5 additions & 13 deletions arduino/builder/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"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"
Expand Down Expand Up @@ -358,7 +357,7 @@ func CompileFiles(
compilationDatabase *compilation.Database,
jobs int,
builderLogger *logger.BuilderLogger,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
progress *progress.Struct,
) (paths.PathList, error) {
return compileFiles(
onlyUpdateCompilationDatabase,
Expand All @@ -368,7 +367,7 @@ func CompileFiles(
false,
buildPath, buildProperties, includes,
builderLogger,
progress, progressCB,
progress,
)
}

Expand All @@ -381,7 +380,7 @@ func CompileFilesRecursive(
compilationDatabase *compilation.Database,
jobs int,
builderLogger *logger.BuilderLogger,
progress *progress.Struct, progressCB rpc.TaskProgressCB,
progress *progress.Struct,
) (paths.PathList, error) {
return compileFiles(
onlyUpdateCompilationDatabase,
Expand All @@ -391,7 +390,7 @@ func CompileFilesRecursive(
true,
buildPath, buildProperties, includes,
builderLogger,
progress, progressCB,
progress,
)
}

Expand All @@ -406,7 +405,6 @@ func compileFiles(
includes []string,
builderLogger *logger.BuilderLogger,
progress *progress.Struct,
progressCB rpc.TaskProgressCB,
) (paths.PathList, error) {
validExtensions := []string{}
for ext := range globals.SourceFilesValidExtensions {
Expand Down Expand Up @@ -483,13 +481,7 @@ func compileFiles(
queue <- source

progress.CompleteStep()
// PushProgress
if progressCB != nil {
progressCB(&rpc.TaskProgress{
Percent: progress.Progress,
Completed: progress.Progress >= 100.0,
})
}
progress.PushProgress()
}
close(queue)
wg.Wait()
Expand Down
Loading

0 comments on commit 9715450

Please sign in to comment.