Skip to content

Commit 563d451

Browse files
committed
Removed dependency on builder ctx in GCCPreprocRunner
1 parent 117f079 commit 563d451

File tree

4 files changed

+57
-26
lines changed

4 files changed

+57
-26
lines changed

legacy/builder/container_add_prototypes.go

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

3535
// Run preprocessor
3636
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
37-
stderr, err := GCCPreprocRunner(ctx, sourceFile, targetFilePath, ctx.IncludeFolders)
38-
ctx.WriteStderr(stderr)
37+
gccStdout, gccStderr, err := GCCPreprocRunner(sourceFile, targetFilePath, ctx.IncludeFolders, ctx.BuildProperties)
38+
if ctx.Verbose {
39+
ctx.WriteStdout(gccStdout)
40+
ctx.WriteStderr(gccStderr)
41+
}
3942
if err != nil {
4043
if !ctx.OnlyUpdateCompilationDatabase {
4144
return errors.WithStack(err)

legacy/builder/container_find_includes.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,12 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQu
369369
ctx.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
370370
}
371371
} else {
372-
preproc_stderr, preproc_err = GCCPreprocRunner(ctx, sourcePath, targetFilePath, includes)
372+
var preproc_stdout []byte
373+
preproc_stdout, preproc_stderr, preproc_err = GCCPreprocRunner(sourcePath, targetFilePath, includes, ctx.BuildProperties)
374+
if ctx.Verbose {
375+
ctx.WriteStdout(preproc_stdout)
376+
ctx.WriteStdout(preproc_stderr)
377+
}
373378
// Unwrap error and see if it is an ExitError.
374379
_, is_exit_error := errors.Cause(preproc_err).(*exec.ExitError)
375380
if preproc_err == nil {
@@ -397,11 +402,13 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQu
397402
library := ResolveLibrary(ctx, include)
398403
if library == nil {
399404
// Library could not be resolved, show error
400-
// err := runCommand(ctx, &GCCPreprocRunner{SourceFilePath: sourcePath, TargetFileName: paths.New(constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E), Includes: includes})
401-
// return errors.WithStack(err)
402405
if preproc_err == nil || preproc_stderr == nil {
403406
// Filename came from cache, so run preprocessor to obtain error to show
404-
preproc_stderr, preproc_err = GCCPreprocRunner(ctx, sourcePath, targetFilePath, includes)
407+
var preproc_stdout []byte
408+
preproc_stdout, preproc_stderr, preproc_err = GCCPreprocRunner(sourcePath, targetFilePath, includes, ctx.BuildProperties)
409+
if ctx.Verbose {
410+
ctx.WriteStdout(preproc_stdout)
411+
}
405412
if preproc_err == nil {
406413
// If there is a missing #include in the cache, but running
407414
// gcc does not reproduce that, there is something wrong.

legacy/builder/gcc_preproc_runner.go

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,64 @@
1616
package builder
1717

1818
import (
19+
"context"
20+
"fmt"
1921
"strings"
2022

23+
"github.com/arduino/arduino-cli/executils"
2124
f "github.com/arduino/arduino-cli/internal/algorithms"
22-
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
23-
"github.com/arduino/arduino-cli/legacy/builder/types"
2425
"github.com/arduino/arduino-cli/legacy/builder/utils"
2526
"github.com/arduino/go-paths-helper"
2627
properties "github.com/arduino/go-properties-orderedmap"
28+
"github.com/pkg/errors"
2729
)
2830

29-
func GCCPreprocRunner(ctx *types.Context, sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList) ([]byte, error) {
30-
buildProperties := properties.NewMap()
31-
buildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")
32-
buildProperties.Merge(ctx.BuildProperties)
33-
buildProperties.Set("build.library_discovery_phase", "1")
34-
buildProperties.SetPath("source_file", sourceFilePath)
35-
buildProperties.SetPath("preprocessed_file_path", targetFilePath)
31+
func GCCPreprocRunner(sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList, buildProperties *properties.Map) ([]byte, []byte, error) {
32+
gccBuildProperties := properties.NewMap()
33+
gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")
34+
gccBuildProperties.Merge(buildProperties)
35+
gccBuildProperties.Set("build.library_discovery_phase", "1")
36+
gccBuildProperties.SetPath("source_file", sourceFilePath)
37+
gccBuildProperties.SetPath("preprocessed_file_path", targetFilePath)
3638

3739
includesStrings := f.Map(includes.AsStrings(), utils.WrapWithHyphenI)
38-
buildProperties.Set("includes", strings.Join(includesStrings, " "))
40+
gccBuildProperties.Set("includes", strings.Join(includesStrings, " "))
3941

40-
if buildProperties.Get("recipe.preproc.macros") == "" {
42+
const GCC_PATTERN_PROPERTY = "recipe.preproc.macros"
43+
if gccBuildProperties.Get(GCC_PATTERN_PROPERTY) == "" {
4144
// autogenerate preprocess macros recipe from compile recipe
42-
preprocPattern := buildProperties.Get("recipe.cpp.o.pattern")
45+
preprocPattern := gccBuildProperties.Get("recipe.cpp.o.pattern")
4346
// add {preproc.macros.flags} to {compiler.cpp.flags}
4447
preprocPattern = strings.Replace(preprocPattern, "{compiler.cpp.flags}", "{compiler.cpp.flags} {preproc.macros.flags}", 1)
4548
// replace "{object_file}" with "{preprocessed_file_path}"
4649
preprocPattern = strings.Replace(preprocPattern, "{object_file}", "{preprocessed_file_path}", 1)
4750

48-
buildProperties.Set("recipe.preproc.macros", preprocPattern)
51+
gccBuildProperties.Set(GCC_PATTERN_PROPERTY, preprocPattern)
4952
}
5053

51-
cmd, err := builder_utils.PrepareCommandForRecipe(buildProperties, "recipe.preproc.macros", true, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
54+
pattern := gccBuildProperties.Get(GCC_PATTERN_PROPERTY)
55+
if pattern == "" {
56+
return nil, nil, errors.Errorf(tr("%s pattern is missing"), GCC_PATTERN_PROPERTY)
57+
}
58+
59+
commandLine := gccBuildProperties.ExpandPropsInString(pattern)
60+
args, err := properties.SplitQuotedString(commandLine, `"'`, false)
5261
if err != nil {
53-
return nil, err
62+
return nil, nil, err
5463
}
5564

5665
// Remove -MMD argument if present. Leaving it will make gcc try
5766
// to create a /dev/null.d dependency file, which won't work.
58-
cmd.Args = f.Filter(cmd.Args, f.NotEquals("-MMD"))
67+
args = f.Filter(args, f.NotEquals("-MMD"))
68+
69+
proc, err := executils.NewProcess(nil, args...)
70+
if err != nil {
71+
return nil, nil, err
72+
}
73+
stdout, stderr, err := proc.RunAndCaptureOutput(context.Background())
74+
75+
// Append gcc arguments to stdout
76+
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
5977

60-
_, stderr, err := utils.ExecCommand(ctx, cmd, utils.ShowIfVerbose /* stdout */, utils.Capture /* stderr */)
61-
return stderr, err
78+
return stdout, stderr, err
6279
}

legacy/builder/preprocess_sketch.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ 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-
stderr, err := GCCPreprocRunner(ctx, sourceFile, targetFile, ctx.IncludeFolders)
40-
ctx.WriteStderr(stderr)
39+
gccStdout, gccStderr, err := GCCPreprocRunner(sourceFile, targetFile, ctx.IncludeFolders, ctx.BuildProperties)
40+
if ctx.Verbose {
41+
ctx.WriteStdout(gccStdout)
42+
ctx.WriteStderr(gccStderr)
43+
}
4144
if err != nil {
4245
return err
4346
}
47+
4448
buildProperties := properties.NewMap()
4549
buildProperties.Set("tools.arduino-preprocessor.path", "{runtime.tools.arduino-preprocessor.path}")
4650
buildProperties.Set("tools.arduino-preprocessor.cmd.path", "{path}/arduino-preprocessor")

0 commit comments

Comments
 (0)