Skip to content

Commit 4cb3c90

Browse files
introduce BuilderArtifacts to better isolate write operations
1 parent 1c9864d commit 4cb3c90

File tree

7 files changed

+45
-55
lines changed

7 files changed

+45
-55
lines changed

Diff for: arduino/builder/builder.go

+17
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,24 @@ type Builder struct {
7272
// C++ Parsing
7373
lineOffset int
7474

75+
buildArtifacts *BuildArtifacts
76+
7577
*BuildOptionsManager
7678
}
7779

80+
// BuildArtifacts contains the result of various build
81+
type BuildArtifacts struct {
82+
// populated by BuildCore
83+
coreArchiveFilePath *paths.Path
84+
coreObjectsFiles paths.PathList
85+
86+
// populated by BuildLibraries
87+
librariesObjectFiles paths.PathList
88+
89+
// populated by BuildSketch
90+
sketchObjectFiles paths.PathList
91+
}
92+
7893
// NewBuilder creates a sketch Builder.
7994
func NewBuilder(
8095
sk *sketch.Sketch,
@@ -160,6 +175,8 @@ func NewBuilder(
160175
onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase,
161176
compilationDatabase: compilation.NewDatabase(buildPath.Join("compile_commands.json")),
162177
Progress: progressStats,
178+
executableSectionsSize: []ExecutableSectionSize{},
179+
buildArtifacts: &BuildArtifacts{},
163180
BuildOptionsManager: NewBuildOptionsManager(
164181
hardwareDirs, builtInToolsDirs, otherLibrariesDirs,
165182
builtInLibrariesDirs, buildPath,

Diff for: arduino/builder/core.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import (
3232
)
3333

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

4040
if b.coreBuildCachePath != nil {
@@ -45,16 +45,17 @@ func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) (paths.PathLi
4545
// compileCore function).
4646
b.coreBuildCachePath = nil
4747
} else if err := b.coreBuildCachePath.MkdirAll(); err != nil {
48-
return nil, nil, errors.WithStack(err)
48+
return errors.WithStack(err)
4949
}
5050
}
5151

5252
archiveFile, objectFiles, err := b.compileCore(actualPlatform)
5353
if err != nil {
54-
return nil, nil, errors.WithStack(err)
54+
return errors.WithStack(err)
5555
}
56-
57-
return objectFiles, archiveFile, nil
56+
b.buildArtifacts.coreObjectsFiles = objectFiles
57+
b.buildArtifacts.coreArchiveFilePath = archiveFile
58+
return nil
5859
}
5960

6061
func (b *Builder) compileCore(actualPlatform *cores.PlatformRelease) (*paths.Path, paths.PathList, error) {

Diff for: arduino/builder/libraries.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ var (
3737
)
3838

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

4444
if err := b.librariesBuildPath.MkdirAll(); err != nil {
45-
return nil, errors.WithStack(err)
45+
return errors.WithStack(err)
4646
}
4747

4848
librariesObjectFiles, err := b.compileLibraries(libs, includes)
4949
if err != nil {
50-
return nil, errors.WithStack(err)
50+
return errors.WithStack(err)
5151
}
52-
53-
return librariesObjectFiles, nil
52+
b.buildArtifacts.librariesObjectFiles = librariesObjectFiles
53+
return nil
5454
}
5555

5656
func directoryContainsFile(folder *paths.Path) bool {

Diff for: arduino/builder/linker.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

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

3636
// TODO can we remove this multiple assignations?
37-
objectFilesSketch := sketchObjectFiles
38-
objectFilesLibraries := librariesObjectFiles
39-
objectFilesCore := coreObjectsFiles
37+
objectFilesSketch := b.buildArtifacts.sketchObjectFiles
38+
objectFilesLibraries := b.buildArtifacts.librariesObjectFiles
39+
objectFilesCore := b.buildArtifacts.coreObjectsFiles
4040

4141
objectFiles := paths.NewPathList()
4242
objectFiles.AddAll(objectFilesSketch)
4343
objectFiles.AddAll(objectFilesLibraries)
4444
objectFiles.AddAll(objectFilesCore)
4545

46-
coreDotARelPath, err := b.buildPath.RelTo(coreArchiveFilePath)
46+
coreDotARelPath, err := b.buildPath.RelTo(b.buildArtifacts.coreArchiveFilePath)
4747
if err != nil {
4848
return errors.WithStack(err)
4949
}
5050

51-
if err := b.link(objectFiles, coreDotARelPath, coreArchiveFilePath); err != nil {
51+
if err := b.link(objectFiles, coreDotARelPath, b.buildArtifacts.coreArchiveFilePath); err != nil {
5252
return errors.WithStack(err)
5353
}
5454

Diff for: arduino/builder/sketch.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error {
181181
}
182182

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

187187
if err := b.sketchBuildPath.MkdirAll(); err != nil {
188-
return nil, errors.WithStack(err)
188+
return errors.WithStack(err)
189189
}
190190

191191
sketchObjectFiles, err := utils.CompileFiles(
@@ -197,7 +197,7 @@ func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, e
197197
b.Progress,
198198
)
199199
if err != nil {
200-
return nil, errors.WithStack(err)
200+
return errors.WithStack(err)
201201
}
202202

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

220-
return sketchObjectFiles, nil
220+
b.buildArtifacts.sketchObjectFiles = sketchObjectFiles
221+
return nil
221222
}
222223

223224
// MergeSketchWithBootloader fixdoc

Diff for: legacy/builder/builder.go

+4-28
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ func (s *Builder) Run(ctx *types.Context) error {
6363
}),
6464

6565
types.BareCommand(func(ctx *types.Context) error {
66-
sketchObjectFiles, err := ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders())
67-
if err != nil {
68-
return err
69-
}
70-
ctx.SketchObjectFiles = sketchObjectFiles
71-
return nil
66+
return ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders())
7267
}),
7368

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

8984
types.BareCommand(func(ctx *types.Context) error {
90-
librariesObjectFiles, err := ctx.Builder.BuildLibraries(
91-
ctx.SketchLibrariesDetector.IncludeFolders(),
92-
ctx.SketchLibrariesDetector.ImportedLibraries(),
93-
)
94-
if err != nil {
95-
return err
96-
}
97-
ctx.LibrariesObjectFiles = librariesObjectFiles
98-
99-
return nil
85+
return ctx.Builder.BuildLibraries(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries())
10086
}),
10187
types.BareCommand(func(ctx *types.Context) error {
10288
return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.libraries.postbuild", ".pattern", true)
@@ -108,12 +94,7 @@ func (s *Builder) Run(ctx *types.Context) error {
10894
}),
10995

11096
types.BareCommand(func(ctx *types.Context) error {
111-
objectFiles, archiveFile, err := ctx.Builder.BuildCore(ctx.ActualPlatform)
112-
113-
ctx.CoreObjectsFiles = objectFiles
114-
ctx.CoreArchiveFilePath = archiveFile
115-
116-
return err
97+
return ctx.Builder.BuildCore(ctx.ActualPlatform)
11798
}),
11899

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

128109
types.BareCommand(func(ctx *types.Context) error {
129-
return ctx.Builder.Link(
130-
ctx.SketchObjectFiles,
131-
ctx.LibrariesObjectFiles,
132-
ctx.CoreObjectsFiles,
133-
ctx.CoreArchiveFilePath,
134-
)
110+
return ctx.Builder.Link()
135111
}),
136112

137113
types.BareCommand(func(ctx *types.Context) error {

Diff for: legacy/builder/types/context.go

-5
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,4 @@ type Context struct {
3939
PackageManager *packagemanager.Explorer
4040
TargetPlatform *cores.PlatformRelease
4141
ActualPlatform *cores.PlatformRelease
42-
43-
CoreArchiveFilePath *paths.Path
44-
CoreObjectsFiles paths.PathList
45-
LibrariesObjectFiles paths.PathList
46-
SketchObjectFiles paths.PathList
4742
}

0 commit comments

Comments
 (0)