Skip to content

Commit 5b37e1b

Browse files
make the CoreBuilder command a function
1 parent 72e4d1d commit 5b37e1b

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed

legacy/builder/builder.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,29 @@ func (s *Builder) Run(ctx *types.Context) error {
7272

7373
utils.LogIfVerbose(false, tr("Compiling core...")),
7474
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.prebuild", Suffix: ".pattern"},
75-
&phases.CoreBuilder{},
75+
76+
types.BareCommand(func(ctx *types.Context) error {
77+
objectFiles, archiveFile, coreBuildCachePath,
78+
normalOut, verboseOut, err := phases.CoreBuilder(
79+
ctx,
80+
ctx.BuildPath, ctx.CoreBuildPath, ctx.CoreBuildCachePath,
81+
ctx.BuildProperties,
82+
ctx.ActualPlatform,
83+
ctx.Verbose, ctx.OnlyUpdateCompilationDatabase, ctx.Clean,
84+
)
85+
86+
ctx.CoreObjectsFiles = objectFiles
87+
ctx.CoreArchiveFilePath = archiveFile
88+
ctx.CoreBuildCachePath = coreBuildCachePath
89+
90+
ctx.Info(string(normalOut))
91+
if ctx.Verbose {
92+
ctx.Info(string(verboseOut))
93+
}
94+
95+
return err
96+
}),
97+
7698
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},
7799

78100
utils.LogIfVerbose(false, tr("Linking everything together...")),

legacy/builder/phases/core_builder.go

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
package phases
1717

1818
import (
19+
"bytes"
1920
"crypto/md5"
2021
"encoding/hex"
2122
"fmt"
2223
"os"
2324
"strings"
2425

2526
"github.com/arduino/arduino-cli/arduino/builder/cpp"
27+
"github.com/arduino/arduino-cli/arduino/cores"
2628
"github.com/arduino/arduino-cli/buildcache"
2729
"github.com/arduino/arduino-cli/i18n"
2830
f "github.com/arduino/arduino-cli/internal/algorithms"
@@ -34,61 +36,72 @@ import (
3436
"github.com/pkg/errors"
3537
)
3638

37-
type CoreBuilder struct{}
39+
// Trasformo in un command
40+
// provo a spostare builder_utils in arduino/Builder
3841

3942
var tr = i18n.Tr
4043

41-
func (s *CoreBuilder) Run(ctx *types.Context) error {
42-
coreBuildPath := ctx.CoreBuildPath
43-
coreBuildCachePath := ctx.CoreBuildCachePath
44-
buildProperties := ctx.BuildProperties
45-
44+
func CoreBuilder(
45+
ctx *types.Context,
46+
buildPath, coreBuildPath, coreBuildCachePath *paths.Path,
47+
buildProperties *properties.Map,
48+
actualPlatform *cores.PlatformRelease,
49+
verbose, onlyUpdateCompilationDatabase, clean bool,
50+
) (paths.PathList, *paths.Path, *paths.Path, []byte, []byte, error) {
51+
normalOut := &bytes.Buffer{}
4652
if err := coreBuildPath.MkdirAll(); err != nil {
47-
return errors.WithStack(err)
53+
return nil, nil, coreBuildCachePath, nil, nil, errors.WithStack(err)
4854
}
4955

5056
if coreBuildCachePath != nil {
51-
if _, err := coreBuildCachePath.RelTo(ctx.BuildPath); err != nil {
52-
ctx.Info(tr("Couldn't deeply cache core build: %[1]s", err))
53-
ctx.Info(tr("Running normal build of the core..."))
57+
if _, err := coreBuildCachePath.RelTo(buildPath); err != nil {
58+
normalOut.WriteString(tr("Couldn't deeply cache core build: %[1]s", err))
59+
normalOut.WriteString(tr("Running normal build of the core..."))
5460
coreBuildCachePath = nil
55-
ctx.CoreBuildCachePath = nil
5661
} else if err := coreBuildCachePath.MkdirAll(); err != nil {
57-
return errors.WithStack(err)
62+
return nil, nil, coreBuildCachePath, nil, nil, errors.WithStack(err)
5863
}
5964
}
6065

61-
archiveFile, objectFiles, err := compileCore(ctx, coreBuildPath, coreBuildCachePath, buildProperties)
66+
archiveFile, objectFiles, verboseOut, err := compileCore(
67+
ctx,
68+
verbose, onlyUpdateCompilationDatabase, clean,
69+
actualPlatform,
70+
coreBuildPath, coreBuildCachePath,
71+
buildProperties,
72+
)
6273
if err != nil {
63-
return errors.WithStack(err)
74+
return nil, nil, coreBuildCachePath, normalOut.Bytes(), verboseOut, errors.WithStack(err)
6475
}
6576

66-
ctx.CoreArchiveFilePath = archiveFile
67-
ctx.CoreObjectsFiles = objectFiles
68-
69-
return nil
77+
return objectFiles, archiveFile, coreBuildCachePath, normalOut.Bytes(), verboseOut, nil
7078
}
7179

72-
func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *paths.Path, buildProperties *properties.Map) (*paths.Path, paths.PathList, error) {
80+
func compileCore(
81+
ctx *types.Context,
82+
verbose, onlyUpdateCompilationDatabase, clean bool,
83+
actualPlatform *cores.PlatformRelease,
84+
buildPath, buildCachePath *paths.Path,
85+
buildProperties *properties.Map,
86+
) (*paths.Path, paths.PathList, []byte, error) {
87+
verboseOut := &bytes.Buffer{}
88+
7389
coreFolder := buildProperties.GetPath("build.core.path")
7490
variantFolder := buildProperties.GetPath("build.variant.path")
75-
7691
targetCoreFolder := buildProperties.GetPath(constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH)
7792

78-
includes := []string{}
79-
includes = append(includes, coreFolder.String())
93+
includes := []string{coreFolder.String()}
8094
if variantFolder != nil && variantFolder.IsDir() {
8195
includes = append(includes, variantFolder.String())
8296
}
8397
includes = f.Map(includes, cpp.WrapWithHyphenI)
8498

8599
var err error
86-
87100
variantObjectFiles := paths.NewPathList()
88101
if variantFolder != nil && variantFolder.IsDir() {
89102
variantObjectFiles, err = builder_utils.CompileFilesRecursive(ctx, variantFolder, buildPath, buildProperties, includes)
90103
if err != nil {
91-
return nil, nil, errors.WithStack(err)
104+
return nil, nil, verboseOut.Bytes(), errors.WithStack(err)
92105
}
93106
}
94107

@@ -98,15 +111,16 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
98111
archivedCoreName := GetCachedCoreArchiveDirName(
99112
buildProperties.Get("build.fqbn"),
100113
buildProperties.Get("compiler.optimization_flags"),
101-
realCoreFolder)
114+
realCoreFolder,
115+
)
102116
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")
103117

104118
if _, err := buildcache.New(buildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) {
105-
return nil, nil, fmt.Errorf(tr("creating core cache folder: %s", err))
119+
return nil, nil, verboseOut.Bytes(), fmt.Errorf(tr("creating core cache folder: %s", err))
106120
}
107121

108122
var canUseArchivedCore bool
109-
if ctx.OnlyUpdateCompilationDatabase || ctx.Clean {
123+
if onlyUpdateCompilationDatabase || clean {
110124
canUseArchivedCore = false
111125
} else if isOlder, err := builder_utils.DirContentIsOlderThan(realCoreFolder, targetArchivedCore); err != nil || !isOlder {
112126
// Recreate the archive if ANY of the core files (including platform.txt) has changed
@@ -122,47 +136,47 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
122136

123137
if canUseArchivedCore {
124138
// use archived core
125-
if ctx.Verbose {
126-
ctx.Info(tr("Using precompiled core: %[1]s", targetArchivedCore))
139+
if verbose {
140+
verboseOut.WriteString(tr("Using precompiled core: %[1]s", targetArchivedCore))
127141
}
128-
return targetArchivedCore, variantObjectFiles, nil
142+
return targetArchivedCore, variantObjectFiles, verboseOut.Bytes(), nil
129143
}
130144
}
131145

132146
coreObjectFiles, err := builder_utils.CompileFilesRecursive(ctx, coreFolder, buildPath, buildProperties, includes)
133147
if err != nil {
134-
return nil, nil, errors.WithStack(err)
148+
return nil, nil, verboseOut.Bytes(), errors.WithStack(err)
135149
}
136150

137151
archiveFile, err := builder_utils.ArchiveCompiledFiles(ctx, buildPath, paths.New("core.a"), coreObjectFiles, buildProperties)
138152
if err != nil {
139-
return nil, nil, errors.WithStack(err)
153+
return nil, nil, verboseOut.Bytes(), errors.WithStack(err)
140154
}
141155

142156
// archive core.a
143-
if targetArchivedCore != nil && !ctx.OnlyUpdateCompilationDatabase {
157+
if targetArchivedCore != nil && !onlyUpdateCompilationDatabase {
144158
err := archiveFile.CopyTo(targetArchivedCore)
145-
if ctx.Verbose {
159+
if verbose {
146160
if err == nil {
147-
ctx.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore))
161+
verboseOut.WriteString(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore))
148162
} else if os.IsNotExist(err) {
149-
ctx.Info(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s",
150-
ctx.ActualPlatform,
163+
verboseOut.WriteString(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s",
164+
actualPlatform,
151165
"https://arduino.github.io/arduino-cli/latest/platform-specification/#recipes-to-build-the-corea-archive-file"))
152166
} else {
153-
ctx.Info(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err))
167+
verboseOut.WriteString(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err))
154168
}
155169
}
156170
}
157171

158-
return archiveFile, variantObjectFiles, nil
172+
return archiveFile, variantObjectFiles, verboseOut.Bytes(), nil
159173
}
160174

161175
// GetCachedCoreArchiveDirName returns the directory name to be used to store
162176
// the global cached core.a.
163177
func GetCachedCoreArchiveDirName(fqbn string, optimizationFlags string, coreFolder *paths.Path) string {
164-
fqbnToUnderscore := strings.Replace(fqbn, ":", "_", -1)
165-
fqbnToUnderscore = strings.Replace(fqbnToUnderscore, "=", "_", -1)
178+
fqbnToUnderscore := strings.ReplaceAll(fqbn, ":", "_")
179+
fqbnToUnderscore = strings.ReplaceAll(fqbnToUnderscore, "=", "_")
166180
if absCoreFolder, err := coreFolder.Abs(); err == nil {
167181
coreFolder = absCoreFolder
168182
} // silently continue if absolute path can't be detected

0 commit comments

Comments
 (0)