Skip to content

Commit af6a641

Browse files
remove the use of context from builder_utils
1 parent 5b37e1b commit af6a641

File tree

9 files changed

+343
-102
lines changed

9 files changed

+343
-102
lines changed

legacy/builder/builder.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,25 @@ func (s *Builder) Run(ctx *types.Context) error {
7474
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.prebuild", Suffix: ".pattern"},
7575

7676
types.BareCommand(func(ctx *types.Context) error {
77-
objectFiles, archiveFile, coreBuildCachePath,
78-
normalOut, verboseOut, err := phases.CoreBuilder(
79-
ctx,
77+
objectFiles, archiveFile, coreBuildCachePath, err := phases.CoreBuilder(
8078
ctx.BuildPath, ctx.CoreBuildPath, ctx.CoreBuildCachePath,
8179
ctx.BuildProperties,
8280
ctx.ActualPlatform,
8381
ctx.Verbose, ctx.OnlyUpdateCompilationDatabase, ctx.Clean,
82+
ctx.CompilationDatabase,
83+
ctx.Jobs,
84+
ctx.WarningsLevel,
85+
ctx.Stdout, ctx.Stderr,
86+
func(msg string) { ctx.Info(msg) },
87+
func(data []byte) { ctx.WriteStdout(data) },
88+
func(data []byte) { ctx.WriteStderr(data) },
89+
&ctx.Progress, ctx.ProgressCB,
8490
)
8591

8692
ctx.CoreObjectsFiles = objectFiles
8793
ctx.CoreArchiveFilePath = archiveFile
8894
ctx.CoreBuildCachePath = coreBuildCachePath
8995

90-
ctx.Info(string(normalOut))
91-
if ctx.Verbose {
92-
ctx.Info(string(verboseOut))
93-
}
94-
9596
return err
9697
}),
9798

legacy/builder/builder_utils/utils.go

Lines changed: 151 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@
1616
package builder_utils
1717

1818
import (
19+
"bytes"
1920
"fmt"
21+
"io"
2022
"os"
2123
"path/filepath"
2224
"runtime"
2325
"strings"
2426
"sync"
2527

28+
"github.com/arduino/arduino-cli/arduino/builder"
2629
bUtils "github.com/arduino/arduino-cli/arduino/builder/utils"
2730
"github.com/arduino/arduino-cli/arduino/globals"
2831
"github.com/arduino/arduino-cli/executils"
2932
"github.com/arduino/arduino-cli/i18n"
3033
"github.com/arduino/arduino-cli/legacy/builder/constants"
3134
"github.com/arduino/arduino-cli/legacy/builder/types"
3235
"github.com/arduino/arduino-cli/legacy/builder/utils"
36+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3337
"github.com/arduino/go-paths-helper"
3438
"github.com/arduino/go-properties-orderedmap"
3539
"github.com/pkg/errors"
@@ -63,15 +67,81 @@ func DirContentIsOlderThan(dir *paths.Path, target *paths.Path, extensions ...st
6367
return true, nil
6468
}
6569

66-
func CompileFiles(ctx *types.Context, sourceDir *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
67-
return compileFiles(ctx, sourceDir, false, buildPath, buildProperties, includes)
70+
func CompileFiles(
71+
sourceDir, buildPath *paths.Path,
72+
buildProperties *properties.Map,
73+
includes []string,
74+
onlyUpdateCompilationDatabase bool,
75+
compilationDatabase *builder.CompilationDatabase,
76+
jobs int,
77+
verbose bool,
78+
warningsLevel string,
79+
stdoutWriter, stderrWriter io.Writer,
80+
verboseInfoFn func(msg string),
81+
verboseStdoutFn, verboseStderrFn func(data []byte),
82+
progress *types.ProgressStruct, progressCB rpc.TaskProgressCB,
83+
) (paths.PathList, error) {
84+
return compileFiles(
85+
onlyUpdateCompilationDatabase,
86+
compilationDatabase,
87+
jobs,
88+
sourceDir,
89+
false,
90+
buildPath, buildProperties, includes,
91+
verbose,
92+
warningsLevel,
93+
stdoutWriter, stderrWriter,
94+
verboseInfoFn, verboseStdoutFn, verboseStderrFn,
95+
progress, progressCB,
96+
)
6897
}
6998

70-
func CompileFilesRecursive(ctx *types.Context, sourceDir *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
71-
return compileFiles(ctx, sourceDir, true, buildPath, buildProperties, includes)
99+
func CompileFilesRecursive(
100+
sourceDir, buildPath *paths.Path,
101+
buildProperties *properties.Map,
102+
includes []string,
103+
onlyUpdateCompilationDatabase bool,
104+
compilationDatabase *builder.CompilationDatabase,
105+
jobs int,
106+
verbose bool,
107+
warningsLevel string,
108+
stdoutWriter, stderrWriter io.Writer,
109+
verboseInfoFn func(msg string),
110+
verboseStdoutFn, verboseStderrFn func(data []byte),
111+
progress *types.ProgressStruct, progressCB rpc.TaskProgressCB,
112+
) (paths.PathList, error) {
113+
return compileFiles(
114+
onlyUpdateCompilationDatabase,
115+
compilationDatabase,
116+
jobs,
117+
sourceDir,
118+
true,
119+
buildPath, buildProperties, includes,
120+
verbose,
121+
warningsLevel,
122+
stdoutWriter, stderrWriter,
123+
verboseInfoFn, verboseStdoutFn, verboseStderrFn,
124+
progress, progressCB,
125+
)
72126
}
73127

74-
func compileFiles(ctx *types.Context, sourceDir *paths.Path, recurse bool, buildPath *paths.Path, buildProperties *properties.Map, includes []string) (paths.PathList, error) {
128+
func compileFiles(
129+
onlyUpdateCompilationDatabase bool,
130+
compilationDatabase *builder.CompilationDatabase,
131+
jobs int,
132+
sourceDir *paths.Path,
133+
recurse bool,
134+
buildPath *paths.Path,
135+
buildProperties *properties.Map,
136+
includes []string,
137+
verbose bool,
138+
warningsLevel string,
139+
stdoutWriter, stderrWriter io.Writer,
140+
verboseInfoFn func(msg string),
141+
verboseStdoutFn, verboseStderrFn func(data []byte),
142+
progress *types.ProgressStruct,
143+
progressCB rpc.TaskProgressCB,
144+
) (paths.PathList, error) {
75145
validExtensions := []string{}
76146
for ext := range globals.SourceFilesValidExtensions {
77147
validExtensions = append(validExtensions, ext)
@@ -82,8 +152,8 @@ func compileFiles(ctx *types.Context, sourceDir *paths.Path, recurse bool, build
82152
return nil, err
83153
}
84154

85-
ctx.Progress.AddSubSteps(len(sources))
86-
defer ctx.Progress.RemoveSubSteps()
155+
progress.AddSubSteps(len(sources))
156+
defer progress.RemoveSubSteps()
87157

88158
objectFiles := paths.NewPathList()
89159
var objectFilesMux sync.Mutex
@@ -99,7 +169,19 @@ func compileFiles(ctx *types.Context, sourceDir *paths.Path, recurse bool, build
99169
if !buildProperties.ContainsKey(recipe) {
100170
recipe = fmt.Sprintf("recipe%s.o.pattern", globals.SourceFilesValidExtensions[source.Ext()])
101171
}
102-
objectFile, err := compileFileWithRecipe(ctx, sourceDir, source, buildPath, buildProperties, includes, recipe)
172+
objectFile, verboseInfo, verboseStdout, stderr, err := compileFileWithRecipe(
173+
stdoutWriter, stderrWriter,
174+
warningsLevel,
175+
compilationDatabase,
176+
verbose,
177+
onlyUpdateCompilationDatabase,
178+
sourceDir, source, buildPath, buildProperties, includes, recipe,
179+
)
180+
if verbose {
181+
verboseStdoutFn(verboseStdout)
182+
verboseInfoFn(string(verboseInfo))
183+
}
184+
verboseStderrFn(stderr)
103185
if err != nil {
104186
errorsMux.Lock()
105187
errorsList = append(errorsList, err)
@@ -113,7 +195,6 @@ func compileFiles(ctx *types.Context, sourceDir *paths.Path, recurse bool, build
113195

114196
// Spawn jobs runners
115197
var wg sync.WaitGroup
116-
jobs := ctx.Jobs
117198
if jobs == 0 {
118199
jobs = runtime.NumCPU()
119200
}
@@ -137,8 +218,14 @@ func compileFiles(ctx *types.Context, sourceDir *paths.Path, recurse bool, build
137218
}
138219
queue <- source
139220

140-
ctx.Progress.CompleteStep()
141-
ctx.PushProgress()
221+
progress.CompleteStep()
222+
// PushProgress
223+
if progressCB != nil {
224+
progressCB(&rpc.TaskProgress{
225+
Percent: progress.Progress,
226+
Completed: progress.Progress >= 100.0,
227+
})
228+
}
142229
}
143230
close(queue)
144231
wg.Wait()
@@ -150,68 +237,87 @@ func compileFiles(ctx *types.Context, sourceDir *paths.Path, recurse bool, build
150237
return objectFiles, nil
151238
}
152239

153-
func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string, recipe string) (*paths.Path, error) {
240+
func compileFileWithRecipe(
241+
stdoutWriter, stderrWriter io.Writer,
242+
warningsLevel string,
243+
compilationDatabase *builder.CompilationDatabase,
244+
verbose, onlyUpdateCompilationDatabase bool,
245+
sourcePath *paths.Path,
246+
source *paths.Path,
247+
buildPath *paths.Path,
248+
buildProperties *properties.Map,
249+
includes []string,
250+
recipe string,
251+
) (*paths.Path, []byte, []byte, []byte, error) {
252+
verboseStdout, verboseInfo, errOut := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}
253+
154254
properties := buildProperties.Clone()
155-
properties.Set(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS, properties.Get(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+ctx.WarningsLevel))
255+
properties.Set(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS, properties.Get(constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+warningsLevel))
156256
properties.Set(constants.BUILD_PROPERTIES_INCLUDES, strings.Join(includes, constants.SPACE))
157257
properties.SetPath("source_file", source)
158258
relativeSource, err := sourcePath.RelTo(source)
159259
if err != nil {
160-
return nil, errors.WithStack(err)
260+
return nil, nil, nil, nil, errors.WithStack(err)
161261
}
162262
depsFile := buildPath.Join(relativeSource.String() + ".d")
163263
objectFile := buildPath.Join(relativeSource.String() + ".o")
164264

165265
properties.SetPath(constants.BUILD_PROPERTIES_OBJECT_FILE, objectFile)
166266
err = objectFile.Parent().MkdirAll()
167267
if err != nil {
168-
return nil, errors.WithStack(err)
268+
return nil, nil, nil, nil, errors.WithStack(err)
169269
}
170270

171271
objIsUpToDate, err := bUtils.ObjFileIsUpToDate(source, objectFile, depsFile)
172272
if err != nil {
173-
return nil, errors.WithStack(err)
273+
return nil, nil, nil, nil, errors.WithStack(err)
174274
}
175275

176276
command, err := PrepareCommandForRecipe(properties, recipe, false)
177277
if err != nil {
178-
return nil, errors.WithStack(err)
278+
return nil, nil, nil, nil, errors.WithStack(err)
179279
}
180-
if ctx.CompilationDatabase != nil {
181-
ctx.CompilationDatabase.Add(source, command)
280+
if compilationDatabase != nil {
281+
compilationDatabase.Add(source, command)
182282
}
183-
if !objIsUpToDate && !ctx.OnlyUpdateCompilationDatabase {
283+
if !objIsUpToDate && !onlyUpdateCompilationDatabase {
184284
// Since this compile could be multithreaded, we first capture the command output
185-
stdout, stderr, err := utils.ExecCommand(ctx, command, utils.Capture, utils.Capture)
285+
info, stdout, stderr, err := utils.ExecCommand(verbose, stdoutWriter, stderrWriter, command, utils.Capture, utils.Capture)
186286
// and transfer all at once at the end...
187-
if ctx.Verbose {
188-
ctx.WriteStdout(stdout)
287+
if verbose {
288+
verboseInfo.Write(info)
289+
verboseStdout.Write(stdout)
189290
}
190-
ctx.WriteStderr(stderr)
291+
errOut.Write(stderr)
191292

192293
// ...and then return the error
193294
if err != nil {
194-
return nil, errors.WithStack(err)
295+
return nil, verboseInfo.Bytes(), verboseStdout.Bytes(), errOut.Bytes(), errors.WithStack(err)
195296
}
196-
} else if ctx.Verbose {
297+
} else if verbose {
197298
if objIsUpToDate {
198-
ctx.Info(tr("Using previously compiled file: %[1]s", objectFile))
299+
verboseInfo.WriteString(tr("Using previously compiled file: %[1]s", objectFile))
199300
} else {
200-
ctx.Info(tr("Skipping compile of: %[1]s", objectFile))
301+
verboseInfo.WriteString(tr("Skipping compile of: %[1]s", objectFile))
201302
}
202303
}
203304

204-
return objectFile, nil
305+
return objectFile, verboseInfo.Bytes(), verboseStdout.Bytes(), errOut.Bytes(), nil
205306
}
206307

207-
func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile *paths.Path, objectFilesToArchive paths.PathList, buildProperties *properties.Map) (*paths.Path, error) {
308+
func ArchiveCompiledFiles(
309+
buildPath *paths.Path, archiveFile *paths.Path, objectFilesToArchive paths.PathList, buildProperties *properties.Map,
310+
onlyUpdateCompilationDatabase, verbose bool,
311+
stdoutWriter, stderrWriter io.Writer,
312+
) (*paths.Path, []byte, error) {
313+
verboseInfobuf := &bytes.Buffer{}
208314
archiveFilePath := buildPath.JoinPath(archiveFile)
209315

210-
if ctx.OnlyUpdateCompilationDatabase {
211-
if ctx.Verbose {
212-
ctx.Info(tr("Skipping archive creation of: %[1]s", archiveFilePath))
316+
if onlyUpdateCompilationDatabase {
317+
if verbose {
318+
verboseInfobuf.WriteString(tr("Skipping archive creation of: %[1]s", archiveFilePath))
213319
}
214-
return archiveFilePath, nil
320+
return archiveFilePath, verboseInfobuf.Bytes(), nil
215321
}
216322

217323
if archiveFileStat, err := archiveFilePath.Stat(); err == nil {
@@ -228,13 +334,13 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
228334
// something changed, rebuild the core archive
229335
if rebuildArchive {
230336
if err := archiveFilePath.Remove(); err != nil {
231-
return nil, errors.WithStack(err)
337+
return nil, nil, errors.WithStack(err)
232338
}
233339
} else {
234-
if ctx.Verbose {
235-
ctx.Info(tr("Using previously compiled file: %[1]s", archiveFilePath))
340+
if verbose {
341+
verboseInfobuf.WriteString(tr("Using previously compiled file: %[1]s", archiveFilePath))
236342
}
237-
return archiveFilePath, nil
343+
return archiveFilePath, verboseInfobuf.Bytes(), nil
238344
}
239345
}
240346

@@ -246,16 +352,19 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
246352

247353
command, err := PrepareCommandForRecipe(properties, constants.RECIPE_AR_PATTERN, false)
248354
if err != nil {
249-
return nil, errors.WithStack(err)
355+
return nil, verboseInfobuf.Bytes(), errors.WithStack(err)
250356
}
251357

252-
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
358+
verboseInfo, _, _, err := utils.ExecCommand(verbose, stdoutWriter, stderrWriter, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)
359+
if verbose {
360+
verboseInfobuf.WriteString(string(verboseInfo))
361+
}
253362
if err != nil {
254-
return nil, errors.WithStack(err)
363+
return nil, verboseInfobuf.Bytes(), errors.WithStack(err)
255364
}
256365
}
257366

258-
return archiveFilePath, nil
367+
return archiveFilePath, verboseInfobuf.Bytes(), nil
259368
}
260369

261370
const COMMANDLINE_LIMIT = 30000

0 commit comments

Comments
 (0)