Skip to content

Commit

Permalink
make Linker a method recevier of arduino/builder
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent da308d8 commit 02df09c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 62 deletions.
15 changes: 0 additions & 15 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,6 @@ func (b *Builder) GetBuildProperties() *properties.Map {
return b.buildProperties
}

// Jobs number of parallel processes
func (b *Builder) Jobs() int {
return b.jobs
}

// CustomBuildProperties returns user provided custom build properties
func (b *Builder) CustomBuildProperties() []string {
return b.customBuildProperties
}

// GetBuildPath returns the build path
func (b *Builder) GetBuildPath() *paths.Path {
return b.buildPath
Expand All @@ -175,11 +165,6 @@ func (b *Builder) GetSketchBuildPath() *paths.Path {
return b.sketchBuildPath
}

// GetCoreBuildPath returns the core build path
func (b *Builder) GetCoreBuildPath() *paths.Path {
return b.coreBuildPath
}

// GetLibrariesBuildPath returns the libraries build path
func (b *Builder) GetLibrariesBuildPath() *paths.Path {
return b.librariesBuildPath
Expand Down
65 changes: 27 additions & 38 deletions arduino/builder/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@
package builder

import (
"bytes"
"strings"

"github.com/arduino/arduino-cli/arduino/builder/logger"
"github.com/arduino/arduino-cli/arduino/builder/utils"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)

// Linker fixdoc
func Linker(
// Link fixdoc
func (b *Builder) Link(
onlyUpdateCompilationDatabase bool,
sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList,
coreArchiveFilePath, buildPath *paths.Path,
buildProperties *properties.Map,
builderLogger *logger.BuilderLogger,
) ([]byte, error) {
verboseInfo := &bytes.Buffer{}
coreArchiveFilePath *paths.Path,
) error {
if onlyUpdateCompilationDatabase {
if builderLogger.Verbose() {
verboseInfo.WriteString(tr("Skip linking of final executable."))
if b.logger.Verbose() {
b.logger.Info(tr("Skip linking of final executable."))
}
return verboseInfo.Bytes(), nil
return nil
}

// TODO can we remove this multiple assignations?
objectFilesSketch := sketchObjectFiles
objectFilesLibraries := librariesObjectFiles
objectFilesCore := coreObjectsFiles
Expand All @@ -52,25 +47,19 @@ func Linker(
objectFiles.AddAll(objectFilesLibraries)
objectFiles.AddAll(objectFilesCore)

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

verboseInfoOut, err := link(objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, builderLogger)
verboseInfo.Write(verboseInfoOut)
if err != nil {
return verboseInfo.Bytes(), errors.WithStack(err)
if err := b.link(objectFiles, coreDotARelPath, coreArchiveFilePath); err != nil {
return errors.WithStack(err)
}

return verboseInfo.Bytes(), nil
return nil
}

func link(
objectFiles paths.PathList, coreDotARelPath *paths.Path, coreArchiveFilePath *paths.Path, buildProperties *properties.Map,
builderLogger *logger.BuilderLogger,
) ([]byte, error) {
verboseBuffer := &bytes.Buffer{}
func (b *Builder) link(objectFiles paths.PathList, coreDotARelPath *paths.Path, coreArchiveFilePath *paths.Path) error {
wrapWithDoubleQuotes := func(value string) string { return "\"" + value + "\"" }
objectFileList := strings.Join(f.Map(objectFiles.AsStrings(), wrapWithDoubleQuotes), " ")

Expand All @@ -83,7 +72,7 @@ func link(
// it may happen that a subdir/spi.o inside the archive may be overwritten by a anotherdir/spi.o
// because thery are both named spi.o.

properties := buildProperties.Clone()
properties := b.buildProperties.Clone()
archives := paths.NewPathList()
for _, object := range objectFiles {
if object.HasSuffix(".a") {
Expand All @@ -102,36 +91,36 @@ func link(

command, err := utils.PrepareCommandForRecipe(properties, "recipe.ar.pattern", false)
if err != nil {
return nil, errors.WithStack(err)
return errors.WithStack(err)
}

if verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
if builderLogger.Verbose() {
verboseBuffer.WriteString(string(verboseInfo))
if verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil {
if b.logger.Verbose() {
b.logger.Info(string(verboseInfo))
}
return verboseBuffer.Bytes(), errors.WithStack(err)
return errors.WithStack(err)
}
}

objectFileList = strings.Join(f.Map(archives.AsStrings(), wrapWithDoubleQuotes), " ")
objectFileList = "-Wl,--whole-archive " + objectFileList + " -Wl,--no-whole-archive"
}

properties := buildProperties.Clone()
properties := b.buildProperties.Clone()
properties.Set("compiler.c.elf.flags", properties.Get("compiler.c.elf.flags"))
properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel()))
properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+b.logger.WarningsLevel()))
properties.Set("archive_file", coreDotARelPath.String())
properties.Set("archive_file_path", coreArchiveFilePath.String())
properties.Set("object_files", objectFileList)

command, err := utils.PrepareCommandForRecipe(properties, "recipe.c.combine.pattern", false)
if err != nil {
return verboseBuffer.Bytes(), err
return err
}

verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if builderLogger.Verbose() {
verboseBuffer.WriteString(string(verboseInfo))
verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
if b.logger.Verbose() {
b.logger.Info(string(verboseInfo))
}
return verboseBuffer.Bytes(), err
return err
}
10 changes: 1 addition & 9 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"reflect"
"time"

"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/builder/sizer"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/legacy/builder/types"
Expand Down Expand Up @@ -142,20 +141,13 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
verboseInfoOut, err := builder.Linker(
return ctx.Builder.Link(
ctx.OnlyUpdateCompilationDatabase,
ctx.SketchObjectFiles,
ctx.LibrariesObjectFiles,
ctx.CoreObjectsFiles,
ctx.CoreArchiveFilePath,
ctx.Builder.GetBuildPath(),
ctx.Builder.GetBuildProperties(),
ctx.BuilderLogger,
)
if ctx.BuilderLogger.Verbose() {
ctx.BuilderLogger.Info(string(verboseInfoOut))
}
return err
}),

types.BareCommand(func(ctx *types.Context) error {
Expand Down

0 comments on commit 02df09c

Please sign in to comment.