Skip to content

Commit

Permalink
introduce BuilderArtifacts to better isolate write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent 1c9864d commit 4cb3c90
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 55 deletions.
17 changes: 17 additions & 0 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,24 @@ type Builder struct {
// C++ Parsing
lineOffset int

buildArtifacts *BuildArtifacts

*BuildOptionsManager
}

// BuildArtifacts contains the result of various build
type BuildArtifacts struct {
// populated by BuildCore
coreArchiveFilePath *paths.Path
coreObjectsFiles paths.PathList

// populated by BuildLibraries
librariesObjectFiles paths.PathList

// populated by BuildSketch
sketchObjectFiles paths.PathList
}

// NewBuilder creates a sketch Builder.
func NewBuilder(
sk *sketch.Sketch,
Expand Down Expand Up @@ -160,6 +175,8 @@ func NewBuilder(
onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase,
compilationDatabase: compilation.NewDatabase(buildPath.Join("compile_commands.json")),
Progress: progressStats,
executableSectionsSize: []ExecutableSectionSize{},
buildArtifacts: &BuildArtifacts{},
BuildOptionsManager: NewBuildOptionsManager(
hardwareDirs, builtInToolsDirs, otherLibrariesDirs,
builtInLibrariesDirs, buildPath,
Expand Down
13 changes: 7 additions & 6 deletions arduino/builder/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
)

// BuildCore fixdoc
func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) (paths.PathList, *paths.Path, error) {
func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) error {
if err := b.coreBuildPath.MkdirAll(); err != nil {
return nil, nil, errors.WithStack(err)
return errors.WithStack(err)
}

if b.coreBuildCachePath != nil {
Expand All @@ -45,16 +45,17 @@ func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) (paths.PathLi
// compileCore function).
b.coreBuildCachePath = nil
} else if err := b.coreBuildCachePath.MkdirAll(); err != nil {
return nil, nil, errors.WithStack(err)
return errors.WithStack(err)
}
}

archiveFile, objectFiles, err := b.compileCore(actualPlatform)
if err != nil {
return nil, nil, errors.WithStack(err)
return errors.WithStack(err)
}

return objectFiles, archiveFile, nil
b.buildArtifacts.coreObjectsFiles = objectFiles
b.buildArtifacts.coreArchiveFilePath = archiveFile
return nil
}

func (b *Builder) compileCore(actualPlatform *cores.PlatformRelease) (*paths.Path, paths.PathList, error) {
Expand Down
10 changes: 5 additions & 5 deletions arduino/builder/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ var (
)

// BuildLibraries fixdoc
func (b *Builder) BuildLibraries(includesFolders paths.PathList, importedLibraries libraries.List) (paths.PathList, error) {
func (b *Builder) BuildLibraries(includesFolders paths.PathList, importedLibraries libraries.List) error {
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)
libs := importedLibraries

if err := b.librariesBuildPath.MkdirAll(); err != nil {
return nil, errors.WithStack(err)
return errors.WithStack(err)
}

librariesObjectFiles, err := b.compileLibraries(libs, includes)
if err != nil {
return nil, errors.WithStack(err)
return errors.WithStack(err)
}

return librariesObjectFiles, nil
b.buildArtifacts.librariesObjectFiles = librariesObjectFiles
return nil
}

func directoryContainsFile(folder *paths.Path) bool {
Expand Down
12 changes: 6 additions & 6 deletions arduino/builder/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// Link fixdoc
func (b *Builder) Link(sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, coreArchiveFilePath *paths.Path) error {
func (b *Builder) Link() error {
if b.onlyUpdateCompilationDatabase {
if b.logger.Verbose() {
b.logger.Info(tr("Skip linking of final executable."))
Expand All @@ -34,21 +34,21 @@ func (b *Builder) Link(sketchObjectFiles, librariesObjectFiles, coreObjectsFiles
}

// TODO can we remove this multiple assignations?
objectFilesSketch := sketchObjectFiles
objectFilesLibraries := librariesObjectFiles
objectFilesCore := coreObjectsFiles
objectFilesSketch := b.buildArtifacts.sketchObjectFiles
objectFilesLibraries := b.buildArtifacts.librariesObjectFiles
objectFilesCore := b.buildArtifacts.coreObjectsFiles

objectFiles := paths.NewPathList()
objectFiles.AddAll(objectFilesSketch)
objectFiles.AddAll(objectFilesLibraries)
objectFiles.AddAll(objectFilesCore)

coreDotARelPath, err := b.buildPath.RelTo(coreArchiveFilePath)
coreDotARelPath, err := b.buildPath.RelTo(b.buildArtifacts.coreArchiveFilePath)
if err != nil {
return errors.WithStack(err)
}

if err := b.link(objectFiles, coreDotARelPath, coreArchiveFilePath); err != nil {
if err := b.link(objectFiles, coreDotARelPath, b.buildArtifacts.coreArchiveFilePath); err != nil {
return errors.WithStack(err)
}

Expand Down
11 changes: 6 additions & 5 deletions arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error {
}

// BuildSketch fixdoc
func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, error) {
func (b *Builder) BuildSketch(includesFolders paths.PathList) error {
includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI)

if err := b.sketchBuildPath.MkdirAll(); err != nil {
return nil, errors.WithStack(err)
return errors.WithStack(err)
}

sketchObjectFiles, err := utils.CompileFiles(
Expand All @@ -197,7 +197,7 @@ func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, e
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
return errors.WithStack(err)
}

// The "src/" subdirectory of a sketch is compiled recursively
Expand All @@ -212,12 +212,13 @@ func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, e
b.Progress,
)
if err != nil {
return nil, errors.WithStack(err)
return errors.WithStack(err)
}
sketchObjectFiles.AddAll(srcObjectFiles)
}

return sketchObjectFiles, nil
b.buildArtifacts.sketchObjectFiles = sketchObjectFiles
return nil
}

// MergeSketchWithBootloader fixdoc
Expand Down
32 changes: 4 additions & 28 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
sketchObjectFiles, err := ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders())
if err != nil {
return err
}
ctx.SketchObjectFiles = sketchObjectFiles
return nil
return ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders())
}),

types.BareCommand(func(ctx *types.Context) error {
Expand All @@ -87,16 +82,7 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
librariesObjectFiles, err := ctx.Builder.BuildLibraries(
ctx.SketchLibrariesDetector.IncludeFolders(),
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
if err != nil {
return err
}
ctx.LibrariesObjectFiles = librariesObjectFiles

return nil
return ctx.Builder.BuildLibraries(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries())
}),
types.BareCommand(func(ctx *types.Context) error {
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.libraries.postbuild", ".pattern", true)
Expand All @@ -108,12 +94,7 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
objectFiles, archiveFile, err := ctx.Builder.BuildCore(ctx.ActualPlatform)

ctx.CoreObjectsFiles = objectFiles
ctx.CoreArchiveFilePath = archiveFile

return err
return ctx.Builder.BuildCore(ctx.ActualPlatform)
}),

types.BareCommand(func(ctx *types.Context) error {
Expand All @@ -126,12 +107,7 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
return ctx.Builder.Link(
ctx.SketchObjectFiles,
ctx.LibrariesObjectFiles,
ctx.CoreObjectsFiles,
ctx.CoreArchiveFilePath,
)
return ctx.Builder.Link()
}),

types.BareCommand(func(ctx *types.Context) error {
Expand Down
5 changes: 0 additions & 5 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ type Context struct {
PackageManager *packagemanager.Explorer
TargetPlatform *cores.PlatformRelease
ActualPlatform *cores.PlatformRelease

CoreArchiveFilePath *paths.Path
CoreObjectsFiles paths.PathList
LibrariesObjectFiles paths.PathList
SketchObjectFiles paths.PathList
}

0 comments on commit 4cb3c90

Please sign in to comment.