|
16 | 16 | package builder
|
17 | 17 |
|
18 | 18 | import (
|
19 |
| - "fmt" |
20 |
| - "os" |
21 |
| - "os/exec" |
| 19 | + "bytes" |
| 20 | + "context" |
22 | 21 | "path/filepath"
|
23 | 22 | "runtime"
|
24 | 23 |
|
25 | 24 | bldr "github.com/arduino/arduino-cli/arduino/builder"
|
26 | 25 | "github.com/arduino/arduino-cli/arduino/builder/preprocessor"
|
27 |
| - "github.com/arduino/arduino-cli/legacy/builder/types" |
| 26 | + "github.com/arduino/arduino-cli/arduino/sketch" |
| 27 | + "github.com/arduino/arduino-cli/executils" |
28 | 28 | "github.com/arduino/arduino-cli/legacy/builder/utils"
|
| 29 | + "github.com/arduino/go-paths-helper" |
29 | 30 | properties "github.com/arduino/go-properties-orderedmap"
|
30 | 31 | "github.com/pkg/errors"
|
31 | 32 | )
|
32 | 33 |
|
33 |
| -func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error { |
34 |
| - if err := ctx.BuildPath.Join("preproc").MkdirAll(); err != nil { |
35 |
| - return errors.WithStack(err) |
| 34 | +func PreprocessSketchWithArduinoPreprocessor(sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList, sketchBuildPath *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) { |
| 35 | + verboseOut := &bytes.Buffer{} |
| 36 | + normalOut := &bytes.Buffer{} |
| 37 | + if err := buildPath.Join("preproc").MkdirAll(); err != nil { |
| 38 | + return nil, nil, err |
36 | 39 | }
|
37 | 40 |
|
38 |
| - sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp") |
39 |
| - targetFile := ctx.BuildPath.Join("preproc", "sketch_merged.cpp") |
40 |
| - gccStdout, gccStderr, err := preprocessor.GCC(sourceFile, targetFile, ctx.IncludeFolders, ctx.BuildProperties) |
41 |
| - if ctx.Verbose { |
42 |
| - ctx.WriteStdout(gccStdout) |
43 |
| - ctx.WriteStderr(gccStderr) |
44 |
| - } |
| 41 | + sourceFile := sketchBuildPath.Join(sk.MainFile.Base() + ".cpp") |
| 42 | + targetFile := buildPath.Join("preproc", "sketch_merged.cpp") |
| 43 | + gccStdout, gccStderr, err := preprocessor.GCC(sourceFile, targetFile, includeFolders, buildProperties) |
| 44 | + verboseOut.Write(gccStdout) |
| 45 | + verboseOut.Write(gccStderr) |
45 | 46 | if err != nil {
|
46 |
| - return err |
| 47 | + return nil, nil, err |
47 | 48 | }
|
48 | 49 |
|
49 |
| - buildProperties := properties.NewMap() |
50 |
| - buildProperties.Set("tools.arduino-preprocessor.path", "{runtime.tools.arduino-preprocessor.path}") |
51 |
| - buildProperties.Set("tools.arduino-preprocessor.cmd.path", "{path}/arduino-preprocessor") |
52 |
| - buildProperties.Set("tools.arduino-preprocessor.pattern", `"{cmd.path}" "{source_file}" -- -std=gnu++11`) |
53 |
| - buildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC") |
54 |
| - buildProperties.Merge(ctx.BuildProperties) |
55 |
| - buildProperties.Merge(buildProperties.SubTree("tools").SubTree("arduino-preprocessor")) |
56 |
| - buildProperties.SetPath("source_file", targetFile) |
57 |
| - |
58 |
| - pattern := buildProperties.Get("pattern") |
| 50 | + arduiniPreprocessorProperties := properties.NewMap() |
| 51 | + arduiniPreprocessorProperties.Set("tools.arduino-preprocessor.path", "{runtime.tools.arduino-preprocessor.path}") |
| 52 | + arduiniPreprocessorProperties.Set("tools.arduino-preprocessor.cmd.path", "{path}/arduino-preprocessor") |
| 53 | + arduiniPreprocessorProperties.Set("tools.arduino-preprocessor.pattern", `"{cmd.path}" "{source_file}" -- -std=gnu++11`) |
| 54 | + arduiniPreprocessorProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC") |
| 55 | + arduiniPreprocessorProperties.Merge(buildProperties) |
| 56 | + arduiniPreprocessorProperties.Merge(arduiniPreprocessorProperties.SubTree("tools").SubTree("arduino-preprocessor")) |
| 57 | + arduiniPreprocessorProperties.SetPath("source_file", targetFile) |
| 58 | + pattern := arduiniPreprocessorProperties.Get("pattern") |
59 | 59 | if pattern == "" {
|
60 |
| - return errors.New(tr("arduino-preprocessor pattern is missing")) |
| 60 | + return nil, nil, errors.New(tr("arduino-preprocessor pattern is missing")) |
61 | 61 | }
|
62 | 62 |
|
63 |
| - commandLine := buildProperties.ExpandPropsInString(pattern) |
| 63 | + commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern) |
64 | 64 | parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
|
65 | 65 | if err != nil {
|
66 |
| - return errors.WithStack(err) |
| 66 | + return nil, nil, errors.WithStack(err) |
67 | 67 | }
|
68 |
| - command := exec.Command(parts[0], parts[1:]...) |
69 |
| - command.Env = append(os.Environ(), ctx.PackageManager.GetEnvVarsForSpawnedProcess()...) |
70 | 68 |
|
| 69 | + command, err := executils.NewProcess(nil, parts...) |
| 70 | + if err != nil { |
| 71 | + return nil, nil, err |
| 72 | + } |
71 | 73 | if runtime.GOOS == "windows" {
|
72 | 74 | // chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
|
73 |
| - command.Dir = filepath.VolumeName(command.Args[0]) + "/" |
74 |
| - //command.Args[0], _ = filepath.Rel(command.Dir, command.Args[0]) |
75 |
| - } |
76 |
| - |
77 |
| - verbose := ctx.Verbose |
78 |
| - if verbose { |
79 |
| - fmt.Println(commandLine) |
| 75 | + command.SetDir(filepath.VolumeName(parts[0]) + "/") |
80 | 76 | }
|
81 | 77 |
|
82 |
| - buf, err := command.Output() |
| 78 | + verboseOut.WriteString(commandLine) |
| 79 | + commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background()) |
| 80 | + verboseOut.Write(commandStdErr) |
83 | 81 | if err != nil {
|
84 |
| - return errors.New(errors.WithStack(err).Error() + string(err.(*exec.ExitError).Stderr)) |
| 82 | + return normalOut.Bytes(), verboseOut.Bytes(), err |
85 | 83 | }
|
| 84 | + result := utils.NormalizeUTF8(commandStdOut) |
86 | 85 |
|
87 |
| - result := utils.NormalizeUTF8(buf) |
88 |
| - return bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, result, ctx.SketchBuildPath) |
| 86 | + return normalOut.Bytes(), verboseOut.Bytes(), bldr.SketchSaveItemCpp(sk.MainFile, result, sketchBuildPath) |
89 | 87 | }
|
0 commit comments