Skip to content

legacy: Builder refactorization (part 2...) #2298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ac23e58
remove unused LibraryDir from legacy context
alessio-perugini Sep 5, 2023
8c92b30
remove unused WatchedLocation from legacy context
alessio-perugini Sep 5, 2023
2847034
remove unused IgnoreSketchFolderNameErrors from legacy context
alessio-perugini Sep 5, 2023
0b4aee2
remove CanUseCachedTools from legacy context
alessio-perugini Sep 5, 2023
402de11
remove UseArduinoPreprocessor from legacy context
alessio-perugini Sep 5, 2023
e82e52e
make the CoreBuilder command a function
alessio-perugini Sep 6, 2023
b41be55
remove the use of context from builder_utils
alessio-perugini Sep 6, 2023
407bc42
mvoe types.ProgressStruct in a dedicated pkg
alessio-perugini Sep 6, 2023
c3ffb4f
move ExecCommand under arduino/utils
alessio-perugini Sep 6, 2023
a2a9b99
move LogIfVerbose from utils to legacy builder
alessio-perugini Sep 6, 2023
3ab6d54
move some legacy constans in builder package
alessio-perugini Sep 6, 2023
5b6ed66
move builder_utils under arduino/builder/utils pkg
alessio-perugini Sep 6, 2023
f410804
appease golint
alessio-perugini Sep 6, 2023
3bd6c7f
move coreBuildCachePath in the arduino Builder
alessio-perugini Sep 6, 2023
447419d
refactor Linker command in a function
alessio-perugini Sep 7, 2023
a688f45
refactor SketchBuilder in a function
alessio-perugini Sep 7, 2023
a05fa12
refactor LibrariesBuilder in a function
alessio-perugini Sep 7, 2023
70a018f
refactor Sizer in a function
alessio-perugini Sep 7, 2023
d4454f5
remove empty file
alessio-perugini Sep 7, 2023
f57fbeb
remove unused struct FailIfBuildPathEqualsSketchPath
alessio-perugini Sep 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make the CoreBuilder command a function
  • Loading branch information
alessio-perugini committed Sep 8, 2023
commit e82e52e81d1dd892618deff813fcfa70f75d2b43
24 changes: 23 additions & 1 deletion legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,29 @@ func (s *Builder) Run(ctx *types.Context) error {

utils.LogIfVerbose(false, tr("Compiling core...")),
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.prebuild", Suffix: ".pattern"},
&phases.CoreBuilder{},

types.BareCommand(func(ctx *types.Context) error {
objectFiles, archiveFile, coreBuildCachePath,
normalOut, verboseOut, err := phases.CoreBuilder(
ctx,
ctx.BuildPath, ctx.CoreBuildPath, ctx.CoreBuildCachePath,
ctx.BuildProperties,
ctx.ActualPlatform,
ctx.Verbose, ctx.OnlyUpdateCompilationDatabase, ctx.Clean,
)

ctx.CoreObjectsFiles = objectFiles
ctx.CoreArchiveFilePath = archiveFile
ctx.CoreBuildCachePath = coreBuildCachePath

ctx.Info(string(normalOut))
if ctx.Verbose {
ctx.Info(string(verboseOut))
}

return err
}),

&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.postbuild", Suffix: ".pattern", SkipIfOnlyUpdatingCompilationDatabase: true},

utils.LogIfVerbose(false, tr("Linking everything together...")),
Expand Down
96 changes: 55 additions & 41 deletions legacy/builder/phases/core_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package phases

import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"strings"

"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/buildcache"
"github.com/arduino/arduino-cli/i18n"
f "github.com/arduino/arduino-cli/internal/algorithms"
Expand All @@ -34,61 +36,72 @@ import (
"github.com/pkg/errors"
)

type CoreBuilder struct{}
// Trasformo in un command
// provo a spostare builder_utils in arduino/Builder

var tr = i18n.Tr

func (s *CoreBuilder) Run(ctx *types.Context) error {
coreBuildPath := ctx.CoreBuildPath
coreBuildCachePath := ctx.CoreBuildCachePath
buildProperties := ctx.BuildProperties

func CoreBuilder(
ctx *types.Context,
buildPath, coreBuildPath, coreBuildCachePath *paths.Path,
buildProperties *properties.Map,
actualPlatform *cores.PlatformRelease,
verbose, onlyUpdateCompilationDatabase, clean bool,
) (paths.PathList, *paths.Path, *paths.Path, []byte, []byte, error) {
normalOut := &bytes.Buffer{}
if err := coreBuildPath.MkdirAll(); err != nil {
return errors.WithStack(err)
return nil, nil, coreBuildCachePath, nil, nil, errors.WithStack(err)
}

if coreBuildCachePath != nil {
if _, err := coreBuildCachePath.RelTo(ctx.BuildPath); err != nil {
ctx.Info(tr("Couldn't deeply cache core build: %[1]s", err))
ctx.Info(tr("Running normal build of the core..."))
if _, err := coreBuildCachePath.RelTo(buildPath); err != nil {
normalOut.WriteString(tr("Couldn't deeply cache core build: %[1]s", err))
normalOut.WriteString(tr("Running normal build of the core..."))
coreBuildCachePath = nil
ctx.CoreBuildCachePath = nil
} else if err := coreBuildCachePath.MkdirAll(); err != nil {
return errors.WithStack(err)
return nil, nil, coreBuildCachePath, nil, nil, errors.WithStack(err)
}
}

archiveFile, objectFiles, err := compileCore(ctx, coreBuildPath, coreBuildCachePath, buildProperties)
archiveFile, objectFiles, verboseOut, err := compileCore(
ctx,
verbose, onlyUpdateCompilationDatabase, clean,
actualPlatform,
coreBuildPath, coreBuildCachePath,
buildProperties,
)
if err != nil {
return errors.WithStack(err)
return nil, nil, coreBuildCachePath, normalOut.Bytes(), verboseOut, errors.WithStack(err)
}

ctx.CoreArchiveFilePath = archiveFile
ctx.CoreObjectsFiles = objectFiles

return nil
return objectFiles, archiveFile, coreBuildCachePath, normalOut.Bytes(), verboseOut, nil
}

func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *paths.Path, buildProperties *properties.Map) (*paths.Path, paths.PathList, error) {
func compileCore(
ctx *types.Context,
verbose, onlyUpdateCompilationDatabase, clean bool,
actualPlatform *cores.PlatformRelease,
buildPath, buildCachePath *paths.Path,
buildProperties *properties.Map,
) (*paths.Path, paths.PathList, []byte, error) {
verboseOut := &bytes.Buffer{}

coreFolder := buildProperties.GetPath("build.core.path")
variantFolder := buildProperties.GetPath("build.variant.path")

targetCoreFolder := buildProperties.GetPath(constants.BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH)

includes := []string{}
includes = append(includes, coreFolder.String())
includes := []string{coreFolder.String()}
if variantFolder != nil && variantFolder.IsDir() {
includes = append(includes, variantFolder.String())
}
includes = f.Map(includes, cpp.WrapWithHyphenI)

var err error

variantObjectFiles := paths.NewPathList()
if variantFolder != nil && variantFolder.IsDir() {
variantObjectFiles, err = builder_utils.CompileFilesRecursive(ctx, variantFolder, buildPath, buildProperties, includes)
if err != nil {
return nil, nil, errors.WithStack(err)
return nil, nil, verboseOut.Bytes(), errors.WithStack(err)
}
}

Expand All @@ -98,15 +111,16 @@ func compileCore(ctx *types.Context, buildPath *paths.Path, buildCachePath *path
archivedCoreName := GetCachedCoreArchiveDirName(
buildProperties.Get("build.fqbn"),
buildProperties.Get("compiler.optimization_flags"),
realCoreFolder)
realCoreFolder,
)
targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a")

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

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

if canUseArchivedCore {
// use archived core
if ctx.Verbose {
ctx.Info(tr("Using precompiled core: %[1]s", targetArchivedCore))
if verbose {
verboseOut.WriteString(tr("Using precompiled core: %[1]s", targetArchivedCore))
}
return targetArchivedCore, variantObjectFiles, nil
return targetArchivedCore, variantObjectFiles, verboseOut.Bytes(), nil
}
}

coreObjectFiles, err := builder_utils.CompileFilesRecursive(ctx, coreFolder, buildPath, buildProperties, includes)
if err != nil {
return nil, nil, errors.WithStack(err)
return nil, nil, verboseOut.Bytes(), errors.WithStack(err)
}

archiveFile, err := builder_utils.ArchiveCompiledFiles(ctx, buildPath, paths.New("core.a"), coreObjectFiles, buildProperties)
if err != nil {
return nil, nil, errors.WithStack(err)
return nil, nil, verboseOut.Bytes(), errors.WithStack(err)
}

// archive core.a
if targetArchivedCore != nil && !ctx.OnlyUpdateCompilationDatabase {
if targetArchivedCore != nil && !onlyUpdateCompilationDatabase {
err := archiveFile.CopyTo(targetArchivedCore)
if ctx.Verbose {
if verbose {
if err == nil {
ctx.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore))
verboseOut.WriteString(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore))
} else if os.IsNotExist(err) {
ctx.Info(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s",
ctx.ActualPlatform,
verboseOut.WriteString(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s",
actualPlatform,
"https://arduino.github.io/arduino-cli/latest/platform-specification/#recipes-to-build-the-corea-archive-file"))
} else {
ctx.Info(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err))
verboseOut.WriteString(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err))
}
}
}

return archiveFile, variantObjectFiles, nil
return archiveFile, variantObjectFiles, verboseOut.Bytes(), nil
}

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