Skip to content

Commit e5166dc

Browse files
committed
Unified GCCPreprocRunner* functions
There is no need to duplicate a function that basically does the same thing.
1 parent e5a6614 commit e5166dc

7 files changed

+26
-46
lines changed

legacy/builder/add_additional_entries_to_context.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
5353
ctx.WarningsLevel = DEFAULT_WARNINGS_LEVEL
5454
}
5555

56-
ctx.CollectedSourceFiles = &types.UniqueSourceFileQueue{}
57-
5856
ctx.LibrariesResolutionResults = map[string]types.LibraryResolutionResult{}
5957

6058
return nil

legacy/builder/container_add_prototypes.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
3333

3434
// Run preprocessor
3535
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
36-
if err := GCCPreprocRunner(ctx, sourceFile, targetFilePath, ctx.IncludeFolders); err != nil {
36+
stderr, err := GCCPreprocRunner(ctx, sourceFile, targetFilePath, ctx.IncludeFolders)
37+
ctx.WriteStderr(stderr)
38+
if err != nil {
3739
if !ctx.OnlyUpdateCompilationDatabase {
3840
return errors.WithStack(err)
3941
}

legacy/builder/container_find_includes.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,24 @@ func (s *ContainerFindIncludes) findIncludes(ctx *types.Context) error {
143143
appendIncludeFolder(ctx, cache, nil, "", ctx.BuildProperties.GetPath("build.variant.path"))
144144
}
145145

146+
sourceFileQueue := &types.UniqueSourceFileQueue{}
147+
146148
if !ctx.UseCachedLibrariesResolution {
147149
sketch := ctx.Sketch
148150
mergedfile, err := types.MakeSourceFile(ctx, sketch, paths.New(sketch.MainFile.Base()+".cpp"))
149151
if err != nil {
150152
return errors.WithStack(err)
151153
}
152-
ctx.CollectedSourceFiles.Push(mergedfile)
154+
sourceFileQueue.Push(mergedfile)
153155

154-
sourceFilePaths := ctx.CollectedSourceFiles
155-
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, ctx.SketchBuildPath, false /* recurse */)
156+
queueSourceFilesFromFolder(ctx, sourceFileQueue, sketch, ctx.SketchBuildPath, false /* recurse */)
156157
srcSubfolderPath := ctx.SketchBuildPath.Join("src")
157158
if srcSubfolderPath.IsDir() {
158-
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, srcSubfolderPath, true /* recurse */)
159+
queueSourceFilesFromFolder(ctx, sourceFileQueue, sketch, srcSubfolderPath, true /* recurse */)
159160
}
160161

161-
for !sourceFilePaths.Empty() {
162-
err := findIncludesUntilDone(ctx, cache, sourceFilePaths.Pop())
162+
for !sourceFileQueue.Empty() {
163+
err := findIncludesUntilDone(ctx, cache, sourceFileQueue)
163164
if err != nil {
164165
cachePath.Remove()
165166
return errors.WithStack(err)
@@ -314,7 +315,8 @@ func writeCache(cache *includeCache, path *paths.Path) error {
314315
return nil
315316
}
316317

317-
func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile types.SourceFile) error {
318+
func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQueue *types.UniqueSourceFileQueue) error {
319+
sourceFile := sourceFileQueue.Pop()
318320
sourcePath := sourceFile.SourcePath(ctx)
319321
targetFilePath := paths.NullPath()
320322
depPath := sourceFile.DepfilePath(ctx)
@@ -367,7 +369,7 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
367369
ctx.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
368370
}
369371
} else {
370-
preproc_stderr, preproc_err = GCCPreprocRunnerForDiscoveringIncludes(ctx, sourcePath, targetFilePath, includes)
372+
preproc_stderr, preproc_err = GCCPreprocRunner(ctx, sourcePath, targetFilePath, includes)
371373
// Unwrap error and see if it is an ExitError.
372374
_, is_exit_error := errors.Cause(preproc_err).(*exec.ExitError)
373375
if preproc_err == nil {
@@ -399,7 +401,7 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
399401
// return errors.WithStack(err)
400402
if preproc_err == nil || preproc_stderr == nil {
401403
// Filename came from cache, so run preprocessor to obtain error to show
402-
preproc_stderr, preproc_err = GCCPreprocRunnerForDiscoveringIncludes(ctx, sourcePath, targetFilePath, includes)
404+
preproc_stderr, preproc_err = GCCPreprocRunner(ctx, sourcePath, targetFilePath, includes)
403405
if preproc_err == nil {
404406
// If there is a missing #include in the cache, but running
405407
// gcc does not reproduce that, there is something wrong.
@@ -419,13 +421,13 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
419421
appendIncludeFolder(ctx, cache, sourcePath, include, library.SourceDir)
420422
sourceDirs := library.SourceDirs()
421423
for _, sourceDir := range sourceDirs {
422-
queueSourceFilesFromFolder(ctx, ctx.CollectedSourceFiles, library, sourceDir.Dir, sourceDir.Recurse)
424+
queueSourceFilesFromFolder(ctx, sourceFileQueue, library, sourceDir.Dir, sourceDir.Recurse)
423425
}
424426
first = false
425427
}
426428
}
427429

428-
func queueSourceFilesFromFolder(ctx *types.Context, queue *types.UniqueSourceFileQueue, origin interface{}, folder *paths.Path, recurse bool) error {
430+
func queueSourceFilesFromFolder(ctx *types.Context, sourceFileQueue *types.UniqueSourceFileQueue, origin interface{}, folder *paths.Path, recurse bool) error {
429431
sourceFileExtensions := []string{}
430432
for k := range globals.SourceFilesValidExtensions {
431433
sourceFileExtensions = append(sourceFileExtensions, k)
@@ -440,7 +442,7 @@ func queueSourceFilesFromFolder(ctx *types.Context, queue *types.UniqueSourceFil
440442
if err != nil {
441443
return errors.WithStack(err)
442444
}
443-
queue.Push(sourceFile)
445+
sourceFileQueue.Push(sourceFile)
444446
}
445447

446448
return nil

legacy/builder/gcc_preproc_runner.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,13 @@ import (
2727
"github.com/pkg/errors"
2828
)
2929

30-
func GCCPreprocRunner(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) error {
30+
func GCCPreprocRunner(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) ([]byte, error) {
3131
cmd, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
3232
if err != nil {
33-
return errors.WithStack(err)
33+
return nil, err
3434
}
35-
36-
_, _, err = utils.ExecCommand(ctx, cmd /* stdout */, utils.ShowIfVerbose /* stderr */, utils.Show)
37-
if err != nil {
38-
return errors.WithStack(err)
39-
}
40-
41-
return nil
42-
}
43-
44-
func GCCPreprocRunnerForDiscoveringIncludes(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) ([]byte, error) {
45-
cmd, err := prepareGCCPreprocRecipeProperties(ctx, sourceFilePath, targetFilePath, includes)
46-
if err != nil {
47-
return nil, errors.WithStack(err)
48-
}
49-
50-
_, stderr, err := utils.ExecCommand(ctx, cmd /* stdout */, utils.ShowIfVerbose /* stderr */, utils.Capture)
51-
if err != nil {
52-
return stderr, errors.WithStack(err)
53-
}
54-
55-
return stderr, nil
35+
_, stderr, err := utils.ExecCommand(ctx, cmd, utils.ShowIfVerbose /* stdout */, utils.Capture /* stderr */)
36+
return stderr, err
5637
}
5738

5839
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) (*exec.Cmd, error) {

legacy/builder/preprocess_sketch.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error {
3636

3737
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
3838
targetFile := ctx.PreprocPath.Join("sketch_merged.cpp")
39-
GCCPreprocRunner(ctx, sourceFile, targetFile, ctx.IncludeFolders)
40-
39+
stderr, err := GCCPreprocRunner(ctx, sourceFile, targetFile, ctx.IncludeFolders)
40+
ctx.WriteStderr(stderr)
41+
if err != nil {
42+
return err
43+
}
4144
buildProperties := properties.NewMap()
4245
buildProperties.Set("tools.arduino-preprocessor.path", "{runtime.tools.arduino-preprocessor.path}")
4346
buildProperties.Set("tools.arduino-preprocessor.cmd.path", "{path}/arduino-preprocessor")

legacy/builder/test/add_additional_entries_to_context_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ func TestAddAdditionalEntriesToContextNoBuildPath(t *testing.T) {
3838

3939
require.NotNil(t, ctx.WarningsLevel)
4040

41-
require.True(t, ctx.CollectedSourceFiles.Empty())
42-
4341
require.Equal(t, 0, len(ctx.LibrariesResolutionResults))
4442
}
4543

@@ -57,7 +55,5 @@ func TestAddAdditionalEntriesToContextWithBuildPath(t *testing.T) {
5755

5856
require.NotNil(t, ctx.WarningsLevel)
5957

60-
require.True(t, ctx.CollectedSourceFiles.Empty())
61-
6258
require.Equal(t, 0, len(ctx.LibrariesResolutionResults))
6359
}

legacy/builder/types/context.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ type Context struct {
102102
SketchObjectFiles paths.PathList
103103
IgnoreSketchFolderNameErrors bool
104104

105-
CollectedSourceFiles *UniqueSourceFileQueue
106-
107105
Sketch *sketch.Sketch
108106
WarningsLevel string
109107

0 commit comments

Comments
 (0)