|
16 | 16 | package builder
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "context" |
| 20 | + "fmt" |
19 | 21 | "strings"
|
20 | 22 |
|
| 23 | + "github.com/arduino/arduino-cli/executils" |
21 | 24 | 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" |
24 | 25 | "github.com/arduino/arduino-cli/legacy/builder/utils"
|
25 | 26 | "github.com/arduino/go-paths-helper"
|
26 | 27 | properties "github.com/arduino/go-properties-orderedmap"
|
| 28 | + "github.com/pkg/errors" |
27 | 29 | )
|
28 | 30 |
|
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) |
36 | 38 |
|
37 | 39 | includesStrings := f.Map(includes.AsStrings(), utils.WrapWithHyphenI)
|
38 |
| - buildProperties.Set("includes", strings.Join(includesStrings, " ")) |
| 40 | + gccBuildProperties.Set("includes", strings.Join(includesStrings, " ")) |
39 | 41 |
|
40 |
| - if buildProperties.Get("recipe.preproc.macros") == "" { |
| 42 | + const GCC_PATTERN_PROPERTY = "recipe.preproc.macros" |
| 43 | + if gccBuildProperties.Get(GCC_PATTERN_PROPERTY) == "" { |
41 | 44 | // autogenerate preprocess macros recipe from compile recipe
|
42 |
| - preprocPattern := buildProperties.Get("recipe.cpp.o.pattern") |
| 45 | + preprocPattern := gccBuildProperties.Get("recipe.cpp.o.pattern") |
43 | 46 | // add {preproc.macros.flags} to {compiler.cpp.flags}
|
44 | 47 | preprocPattern = strings.Replace(preprocPattern, "{compiler.cpp.flags}", "{compiler.cpp.flags} {preproc.macros.flags}", 1)
|
45 | 48 | // replace "{object_file}" with "{preprocessed_file_path}"
|
46 | 49 | preprocPattern = strings.Replace(preprocPattern, "{object_file}", "{preprocessed_file_path}", 1)
|
47 | 50 |
|
48 |
| - buildProperties.Set("recipe.preproc.macros", preprocPattern) |
| 51 | + gccBuildProperties.Set(GCC_PATTERN_PROPERTY, preprocPattern) |
49 | 52 | }
|
50 | 53 |
|
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) |
52 | 61 | if err != nil {
|
53 |
| - return nil, err |
| 62 | + return nil, nil, err |
54 | 63 | }
|
55 | 64 |
|
56 | 65 | // Remove -MMD argument if present. Leaving it will make gcc try
|
57 | 66 | // 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...) |
59 | 77 |
|
60 |
| - _, stderr, err := utils.ExecCommand(ctx, cmd, utils.ShowIfVerbose /* stdout */, utils.Capture /* stderr */) |
61 |
| - return stderr, err |
| 78 | + return stdout, stderr, err |
62 | 79 | }
|
0 commit comments