Skip to content

Commit de3c000

Browse files
committed
Removed ctx dependency in PreprocessSketchWithArduinoPreprocessor
1 parent 2054bf1 commit de3c000

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

legacy/builder/builder.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,23 @@ func (s *Builder) Run(ctx *types.Context) error {
138138
}
139139

140140
func PreprocessSketch(ctx *types.Context) error {
141+
var normalOutput, verboseOutput []byte
142+
var err error
141143
if ctx.UseArduinoPreprocessor {
142-
return PreprocessSketchWithArduinoPreprocessor(ctx)
144+
normalOutput, verboseOutput, err = PreprocessSketchWithArduinoPreprocessor(
145+
ctx.Sketch, ctx.BuildPath, ctx.IncludeFolders, ctx.SketchBuildPath,
146+
ctx.BuildProperties)
143147
} else {
144-
normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags(
148+
normalOutput, verboseOutput, err = preprocessor.PreprocessSketchWithCtags(
145149
ctx.Sketch, ctx.BuildPath, ctx.IncludeFolders, ctx.LineOffset,
146150
ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase)
147-
if ctx.Verbose {
148-
ctx.WriteStdout(verboseOutput)
149-
} else {
150-
ctx.WriteStdout(normalOutput)
151-
}
152-
return err
153151
}
152+
if ctx.Verbose {
153+
ctx.WriteStdout(verboseOutput)
154+
} else {
155+
ctx.WriteStdout(normalOutput)
156+
}
157+
return err
154158
}
155159

156160
type Preprocess struct{}

legacy/builder/preprocess_sketch.go

+39-41
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,72 @@
1616
package builder
1717

1818
import (
19-
"fmt"
20-
"os"
21-
"os/exec"
19+
"bytes"
20+
"context"
2221
"path/filepath"
2322
"runtime"
2423

2524
bldr "github.com/arduino/arduino-cli/arduino/builder"
2625
"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"
2828
"github.com/arduino/arduino-cli/legacy/builder/utils"
29+
"github.com/arduino/go-paths-helper"
2930
properties "github.com/arduino/go-properties-orderedmap"
3031
"github.com/pkg/errors"
3132
)
3233

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
3639
}
3740

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)
4546
if err != nil {
46-
return err
47+
return nil, nil, err
4748
}
4849

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")
5959
if pattern == "" {
60-
return errors.New(tr("arduino-preprocessor pattern is missing"))
60+
return nil, nil, errors.New(tr("arduino-preprocessor pattern is missing"))
6161
}
6262

63-
commandLine := buildProperties.ExpandPropsInString(pattern)
63+
commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern)
6464
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
6565
if err != nil {
66-
return errors.WithStack(err)
66+
return nil, nil, errors.WithStack(err)
6767
}
68-
command := exec.Command(parts[0], parts[1:]...)
69-
command.Env = append(os.Environ(), ctx.PackageManager.GetEnvVarsForSpawnedProcess()...)
7068

69+
command, err := executils.NewProcess(nil, parts...)
70+
if err != nil {
71+
return nil, nil, err
72+
}
7173
if runtime.GOOS == "windows" {
7274
// 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]) + "/")
8076
}
8177

82-
buf, err := command.Output()
78+
verboseOut.WriteString(commandLine)
79+
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background())
80+
verboseOut.Write(commandStdErr)
8381
if err != nil {
84-
return errors.New(errors.WithStack(err).Error() + string(err.(*exec.ExitError).Stderr))
82+
return normalOut.Bytes(), verboseOut.Bytes(), err
8583
}
84+
result := utils.NormalizeUTF8(commandStdOut)
8685

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)
8987
}

0 commit comments

Comments
 (0)