1616package builder
1717
1818import (
19- "bytes"
2019 "strings"
2120
22- "github.com/arduino/arduino-cli/arduino/builder/logger"
2321 "github.com/arduino/arduino-cli/arduino/builder/utils"
2422 f "github.com/arduino/arduino-cli/internal/algorithms"
2523 "github.com/arduino/go-paths-helper"
26- "github.com/arduino/go-properties-orderedmap"
2724 "github.com/pkg/errors"
2825)
2926
30- // Linker fixdoc
31- func Linker (
27+ // Link fixdoc
28+ func ( b * Builder ) Link (
3229 onlyUpdateCompilationDatabase bool ,
3330 sketchObjectFiles , librariesObjectFiles , coreObjectsFiles paths.PathList ,
34- coreArchiveFilePath , buildPath * paths.Path ,
35- buildProperties * properties.Map ,
36- builderLogger * logger.BuilderLogger ,
37- ) ([]byte , error ) {
38- verboseInfo := & bytes.Buffer {}
31+ coreArchiveFilePath * paths.Path ,
32+ ) error {
3933 if onlyUpdateCompilationDatabase {
40- if builderLogger .Verbose () {
41- verboseInfo . WriteString (tr ("Skip linking of final executable." ))
34+ if b . logger .Verbose () {
35+ b . logger . Info (tr ("Skip linking of final executable." ))
4236 }
43- return verboseInfo . Bytes (), nil
37+ return nil
4438 }
4539
40+ // TODO can we remove this multiple assignations?
4641 objectFilesSketch := sketchObjectFiles
4742 objectFilesLibraries := librariesObjectFiles
4843 objectFilesCore := coreObjectsFiles
@@ -52,25 +47,19 @@ func Linker(
5247 objectFiles .AddAll (objectFilesLibraries )
5348 objectFiles .AddAll (objectFilesCore )
5449
55- coreDotARelPath , err := buildPath .RelTo (coreArchiveFilePath )
50+ coreDotARelPath , err := b . buildPath .RelTo (coreArchiveFilePath )
5651 if err != nil {
57- return nil , errors .WithStack (err )
52+ return errors .WithStack (err )
5853 }
5954
60- verboseInfoOut , err := link (objectFiles , coreDotARelPath , coreArchiveFilePath , buildProperties , builderLogger )
61- verboseInfo .Write (verboseInfoOut )
62- if err != nil {
63- return verboseInfo .Bytes (), errors .WithStack (err )
55+ if err := b .link (objectFiles , coreDotARelPath , coreArchiveFilePath ); err != nil {
56+ return errors .WithStack (err )
6457 }
6558
66- return verboseInfo . Bytes (), nil
59+ return nil
6760}
6861
69- func link (
70- objectFiles paths.PathList , coreDotARelPath * paths.Path , coreArchiveFilePath * paths.Path , buildProperties * properties.Map ,
71- builderLogger * logger.BuilderLogger ,
72- ) ([]byte , error ) {
73- verboseBuffer := & bytes.Buffer {}
62+ func (b * Builder ) link (objectFiles paths.PathList , coreDotARelPath * paths.Path , coreArchiveFilePath * paths.Path ) error {
7463 wrapWithDoubleQuotes := func (value string ) string { return "\" " + value + "\" " }
7564 objectFileList := strings .Join (f .Map (objectFiles .AsStrings (), wrapWithDoubleQuotes ), " " )
7665
@@ -83,7 +72,7 @@ func link(
8372 // it may happen that a subdir/spi.o inside the archive may be overwritten by a anotherdir/spi.o
8473 // because thery are both named spi.o.
8574
86- properties := buildProperties .Clone ()
75+ properties := b . buildProperties .Clone ()
8776 archives := paths .NewPathList ()
8877 for _ , object := range objectFiles {
8978 if object .HasSuffix (".a" ) {
@@ -102,36 +91,36 @@ func link(
10291
10392 command , err := utils .PrepareCommandForRecipe (properties , "recipe.ar.pattern" , false )
10493 if err != nil {
105- return nil , errors .WithStack (err )
94+ return errors .WithStack (err )
10695 }
10796
108- if verboseInfo , _ , _ , err := utils .ExecCommand (builderLogger . Verbose (), builderLogger . Stdout (), builderLogger .Stderr (), command , utils .ShowIfVerbose /* stdout */ , utils .Show /* stderr */ ); err != nil {
109- if builderLogger .Verbose () {
110- verboseBuffer . WriteString (string (verboseInfo ))
97+ if verboseInfo , _ , _ , err := utils .ExecCommand (b . logger . Verbose (), b . logger . Stdout (), b . logger .Stderr (), command , utils .ShowIfVerbose /* stdout */ , utils .Show /* stderr */ ); err != nil {
98+ if b . logger .Verbose () {
99+ b . logger . Info (string (verboseInfo ))
111100 }
112- return verboseBuffer . Bytes (), errors .WithStack (err )
101+ return errors .WithStack (err )
113102 }
114103 }
115104
116105 objectFileList = strings .Join (f .Map (archives .AsStrings (), wrapWithDoubleQuotes ), " " )
117106 objectFileList = "-Wl,--whole-archive " + objectFileList + " -Wl,--no-whole-archive"
118107 }
119108
120- properties := buildProperties .Clone ()
109+ properties := b . buildProperties .Clone ()
121110 properties .Set ("compiler.c.elf.flags" , properties .Get ("compiler.c.elf.flags" ))
122- properties .Set ("compiler.warning_flags" , properties .Get ("compiler.warning_flags." + builderLogger .WarningsLevel ()))
111+ properties .Set ("compiler.warning_flags" , properties .Get ("compiler.warning_flags." + b . logger .WarningsLevel ()))
123112 properties .Set ("archive_file" , coreDotARelPath .String ())
124113 properties .Set ("archive_file_path" , coreArchiveFilePath .String ())
125114 properties .Set ("object_files" , objectFileList )
126115
127116 command , err := utils .PrepareCommandForRecipe (properties , "recipe.c.combine.pattern" , false )
128117 if err != nil {
129- return verboseBuffer . Bytes (), err
118+ return err
130119 }
131120
132- verboseInfo , _ , _ , err := utils .ExecCommand (builderLogger . Verbose (), builderLogger . Stdout (), builderLogger .Stderr (), command , utils .ShowIfVerbose /* stdout */ , utils .Show /* stderr */ )
133- if builderLogger .Verbose () {
134- verboseBuffer . WriteString (string (verboseInfo ))
121+ verboseInfo , _ , _ , err := utils .ExecCommand (b . logger . Verbose (), b . logger . Stdout (), b . logger .Stderr (), command , utils .ShowIfVerbose /* stdout */ , utils .Show /* stderr */ )
122+ if b . logger .Verbose () {
123+ b . logger . Info (string (verboseInfo ))
135124 }
136- return verboseBuffer . Bytes (), err
125+ return err
137126}
0 commit comments