Skip to content

Commit 3b6c91a

Browse files
committed
Use runner.Task in GCC preprocessor
It slightly simplifies code, but also provide the basis for the next commits.
1 parent 9991c8d commit 3b6c91a

File tree

4 files changed

+25
-37
lines changed

4 files changed

+25
-37
lines changed

internal/arduino/builder/internal/detector/detector.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ func (l *SketchLibrariesDetector) findIncludes(
284284
return nil
285285
}
286286

287+
func (l *SketchLibrariesDetector) gccPreprocessTask(sourceFile *sourceFile, buildProperties *properties.Map) *runner.Task {
288+
// Libraries may require the "utility" directory to be added to the include
289+
// search path, but only for the source code of the library, so we temporary
290+
// copy the current search path list and add the library' utility directory
291+
// if needed.
292+
includeFolders := l.includeFolders
293+
if extraInclude := sourceFile.ExtraIncludePath; extraInclude != nil {
294+
includeFolders = append(includeFolders, extraInclude)
295+
}
296+
297+
return preprocessor.GCC(sourceFile.SourcePath, paths.NullPath(), includeFolders, buildProperties)
298+
}
299+
287300
func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
288301
ctx context.Context,
289302
sourceFileQueue *uniqueSourceFileQueue,
@@ -315,15 +328,7 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
315328
for {
316329
l.cache.Expect(&detectorCacheEntry{Compile: sourceFile})
317330

318-
// Libraries may require the "utility" directory to be added to the include
319-
// search path, but only for the source code of the library, so we temporary
320-
// copy the current search path list and add the library' utility directory
321-
// if needed.
322-
includeFolders := l.includeFolders
323-
if extraInclude := sourceFile.ExtraIncludePath; extraInclude != nil {
324-
includeFolders = append(includeFolders, extraInclude)
325-
}
326-
331+
preprocTask := l.gccPreprocessTask(sourceFile, buildProperties)
327332
var preprocErr error
328333
var preprocResult *runner.Result
329334

@@ -335,7 +340,8 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
335340
}
336341
first = false
337342
} else {
338-
preprocResult, preprocErr = preprocessor.GCC(ctx, sourcePath, paths.NullPath(), includeFolders, buildProperties)
343+
preprocResult = preprocTask.Run(ctx)
344+
preprocErr = preprocResult.Error
339345
if l.logger.Verbose() {
340346
l.logger.WriteStdout(preprocResult.Stdout)
341347
}
@@ -368,7 +374,8 @@ func (l *SketchLibrariesDetector) findMissingIncludesInCompilationUnit(
368374

369375
// If preprocess result came from cache, run the preprocessor to obtain the actual error to show
370376
if preprocErr == nil || len(preprocResult.Stderr) == 0 {
371-
preprocResult, preprocErr = preprocessor.GCC(ctx, sourcePath, paths.NullPath(), includeFolders, buildProperties)
377+
preprocResult = preprocTask.Run(ctx)
378+
preprocErr = preprocResult.Error
372379
if l.logger.Verbose() {
373380
l.logger.WriteStdout(preprocResult.Stdout)
374381
}

internal/arduino/builder/internal/preprocessor/arduino_preprocessor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func PreprocessSketchWithArduinoPreprocessor(
4545

4646
sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
4747
targetFile := buildPath.Join("preproc", "sketch_merged.cpp")
48-
gccResult, err := GCC(ctx, sourceFile, targetFile, includeFolders, buildProperties)
48+
gccResult := GCC(sourceFile, targetFile, includeFolders, buildProperties).Run(ctx)
4949
verboseOut.Write(gccResult.Stdout)
5050
verboseOut.Write(gccResult.Stderr)
51-
if err != nil {
52-
return nil, err
51+
if gccResult.Error != nil {
52+
return nil, gccResult.Error
5353
}
5454

5555
arduinoPreprocessorProperties := properties.NewMap()

internal/arduino/builder/internal/preprocessor/ctags.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ func PreprocessSketchWithCtags(
5757

5858
// Run GCC preprocessor
5959
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
60-
result, err := GCC(ctx, sourceFile, ctagsTarget, includes, buildProperties)
60+
result := GCC(sourceFile, ctagsTarget, includes, buildProperties).Run(ctx)
6161
stdout.Write(result.Stdout)
6262
stderr.Write(result.Stderr)
63-
if err != nil {
63+
if err := result.Error; err != nil {
6464
if !onlyUpdateCompilationDatabase {
6565
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
6666
}

internal/arduino/builder/internal/preprocessor/gcc.go

+2-21
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,21 @@
1616
package preprocessor
1717

1818
import (
19-
"context"
20-
"errors"
21-
"fmt"
2219
"strings"
2320

2421
f "github.com/arduino/arduino-cli/internal/algorithms"
2522
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
2623
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/runner"
27-
"github.com/arduino/arduino-cli/internal/i18n"
2824
"github.com/arduino/go-paths-helper"
2925
"github.com/arduino/go-properties-orderedmap"
3026
)
3127

3228
// GCC performs a run of the gcc preprocess (macro/includes expansion). The function outputs the result
3329
// to targetFilePath. Returns the stdout/stderr of gcc if any.
3430
func GCC(
35-
ctx context.Context,
3631
sourceFilePath, targetFilePath *paths.Path,
3732
includes paths.PathList, buildProperties *properties.Map,
38-
) (*runner.Result, error) {
33+
) *runner.Task {
3934
gccBuildProperties := properties.NewMap()
4035
gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")
4136
gccBuildProperties.Merge(buildProperties)
@@ -59,26 +54,12 @@ func GCC(
5954
}
6055

6156
pattern := gccBuildProperties.Get(gccPreprocRecipeProperty)
62-
if pattern == "" {
63-
return nil, errors.New(i18n.Tr("%s pattern is missing", gccPreprocRecipeProperty))
64-
}
65-
6657
commandLine := gccBuildProperties.ExpandPropsInString(pattern)
6758
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
6859
args, _ := properties.SplitQuotedString(commandLine, `"'`, false)
6960

7061
// Remove -MMD argument if present. Leaving it will make gcc try
7162
// to create a /dev/null.d dependency file, which won't work.
7263
args = f.Filter(args, f.NotEquals("-MMD"))
73-
74-
proc, err := paths.NewProcess(nil, args...)
75-
if err != nil {
76-
return nil, err
77-
}
78-
stdout, stderr, err := proc.RunAndCaptureOutput(ctx)
79-
80-
// Append gcc arguments to stdout
81-
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
82-
83-
return &runner.Result{Args: proc.GetArgs(), Stdout: stdout, Stderr: stderr}, err
64+
return runner.NewTask(args...)
8465
}

0 commit comments

Comments
 (0)