Skip to content

Commit f9cb4ba

Browse files
committed
Moved ctag preprocessor into proper location
1 parent 84f2702 commit f9cb4ba

11 files changed

+92
-122
lines changed

Diff for: arduino/builder/preprocessor/ctags.go

+55-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package preprocessor
1717

1818
import (
19+
"bytes"
1920
"context"
2021
"fmt"
2122
"strconv"
2223
"strings"
2324

25+
"github.com/arduino/arduino-cli/arduino/builder"
2426
"github.com/arduino/arduino-cli/arduino/builder/cpp"
2527
"github.com/arduino/arduino-cli/arduino/builder/preprocessor/ctags"
2628
"github.com/arduino/arduino-cli/arduino/sketch"
@@ -34,30 +36,73 @@ import (
3436
var tr = i18n.Tr
3537
var DebugPreprocessor bool
3638

37-
func CTags(sourceFile *paths.Path, targetFile *paths.Path, sketch *sketch.Sketch, lineOffset int, buildProperties *properties.Map) ([]byte, error) {
38-
ctagsOutput, ctagsStdErr, err := RunCTags(sourceFile, buildProperties)
39+
func PreprocessSketchWithCtags(sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool) ([]byte, []byte, error) {
40+
// Create a temporary working directory
41+
tmpDir, err := paths.MkTempDir("", "")
3942
if err != nil {
40-
return ctagsStdErr, err
43+
return nil, nil, err
44+
}
45+
defer tmpDir.RemoveAll()
46+
ctagsTarget := tmpDir.Join("sketch_merged.cpp")
47+
48+
normalOutput := &bytes.Buffer{}
49+
verboseOutput := &bytes.Buffer{}
50+
51+
// Run GCC preprocessor
52+
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
53+
gccStdout, gccStderr, err := GCC(sourceFile, ctagsTarget, includes, buildProperties)
54+
verboseOutput.Write(gccStdout)
55+
verboseOutput.Write(gccStderr)
56+
normalOutput.Write(gccStderr)
57+
if err != nil {
58+
if !onlyUpdateCompilationDatabase {
59+
return normalOutput.Bytes(), verboseOutput.Bytes(), errors.WithStack(err)
60+
}
61+
62+
// Do not bail out if we are generating the compile commands database
63+
normalOutput.WriteString(fmt.Sprintf("%s: %s",
64+
tr("An error occurred adding prototypes"),
65+
tr("the compilation database may be incomplete or inaccurate")))
66+
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
67+
return normalOutput.Bytes(), verboseOutput.Bytes(), errors.WithStack(err)
68+
}
69+
}
70+
71+
if src, err := ctagsTarget.ReadFile(); err != nil {
72+
return normalOutput.Bytes(), verboseOutput.Bytes(), err
73+
} else {
74+
filteredSource := builder.FilterSketchSource(sketch, bytes.NewReader(src), false)
75+
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
76+
return normalOutput.Bytes(), verboseOutput.Bytes(), err
77+
}
78+
}
79+
80+
// Run CTags on gcc-preprocessed source
81+
ctagsOutput, ctagsStdErr, err := RunCTags(ctagsTarget, buildProperties)
82+
verboseOutput.Write(ctagsStdErr)
83+
if err != nil {
84+
return normalOutput.Bytes(), verboseOutput.Bytes(), err
4185
}
4286

43-
// func PrototypesAdder(sketch *sketch.Sketch, source string, ctagsStdout []byte, lineOffset int) string {
87+
// Parse CTags output
4488
parser := &ctags.CTagsParser{}
4589
prototypes, firstFunctionLine := parser.Parse(ctagsOutput, sketch.MainFile)
4690
if firstFunctionLine == -1 {
4791
firstFunctionLine = 0
4892
}
4993

94+
// Add prototypes to the original sketch source
5095
var source string
51-
if sourceData, err := targetFile.ReadFile(); err != nil {
52-
return nil, err
96+
if sourceData, err := sourceFile.ReadFile(); err != nil {
97+
return normalOutput.Bytes(), verboseOutput.Bytes(), err
5398
} else {
5499
source = string(sourceData)
55100
}
56101
source = strings.Replace(source, "\r\n", "\n", -1)
57102
source = strings.Replace(source, "\r", "\n", -1)
58103
sourceRows := strings.Split(source, "\n")
59104
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
60-
return nil, nil
105+
return normalOutput.Bytes(), verboseOutput.Bytes(), nil
61106
}
62107

63108
insertionLine := firstFunctionLine + lineOffset - 1
@@ -81,8 +126,9 @@ func CTags(sourceFile *paths.Path, targetFile *paths.Path, sketch *sketch.Sketch
81126
fmt.Println("#END OF PREPROCESSED SOURCE")
82127
}
83128

84-
err = targetFile.WriteFile([]byte(preprocessedSource))
85-
return ctagsStdErr, err
129+
// Write back arduino-preprocess output to the sourceFile
130+
err = sourceFile.WriteFile([]byte(preprocessedSource))
131+
return normalOutput.Bytes(), verboseOutput.Bytes(), err
86132
}
87133

88134
func composePrototypeSection(line int, prototypes []*ctags.Prototype) string {

Diff for: legacy/builder/add_additional_entries_to_context.go

-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ type AddAdditionalEntriesToContext struct{}
2626
func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
2727
if ctx.BuildPath != nil {
2828
buildPath := ctx.BuildPath
29-
preprocPath, err := buildPath.Join(constants.FOLDER_PREPROC).Abs()
30-
if err != nil {
31-
return errors.WithStack(err)
32-
}
3329
sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs()
3430
if err != nil {
3531
return errors.WithStack(err)
@@ -43,7 +39,6 @@ func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
4339
return errors.WithStack(err)
4440
}
4541

46-
ctx.PreprocPath = preprocPath
4742
ctx.SketchBuildPath = sketchBuildPath
4843
ctx.LibrariesBuildPath = librariesBuildPath
4944
ctx.CoreBuildPath = coreBuildPath

Diff for: legacy/builder/builder.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"github.com/arduino/arduino-cli/arduino/builder"
23+
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
2324
"github.com/arduino/arduino-cli/i18n"
2425
"github.com/arduino/arduino-cli/legacy/builder/phases"
2526
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -59,7 +60,7 @@ func (s *Builder) Run(ctx *types.Context) error {
5960
&WarnAboutArchIncompatibleLibraries{},
6061

6162
utils.LogIfVerbose(false, tr("Generating function prototypes...")),
62-
&PreprocessSketch{},
63+
types.BareCommand(PreprocessSketch),
6364

6465
utils.LogIfVerbose(false, tr("Compiling sketch...")),
6566
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.sketch.prebuild", Suffix: ".pattern"},
@@ -115,13 +116,19 @@ func (s *Builder) Run(ctx *types.Context) error {
115116
return otherErr
116117
}
117118

118-
type PreprocessSketch struct{}
119-
120-
func (s *PreprocessSketch) Run(ctx *types.Context) error {
119+
func PreprocessSketch(ctx *types.Context) error {
121120
if ctx.UseArduinoPreprocessor {
122121
return PreprocessSketchWithArduinoPreprocessor(ctx)
123122
} else {
124-
return PreprocessSketchWithCtags(ctx)
123+
normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags(
124+
ctx.Sketch, ctx.BuildPath, ctx.IncludeFolders, ctx.LineOffset,
125+
ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase)
126+
if ctx.Verbose {
127+
ctx.WriteStdout(verboseOutput)
128+
} else {
129+
ctx.WriteStdout(normalOutput)
130+
}
131+
return err
125132
}
126133
}
127134

@@ -149,7 +156,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
149156

150157
&WarnAboutArchIncompatibleLibraries{},
151158

152-
&PreprocessSketch{},
159+
types.BareCommand(PreprocessSketch),
153160
}
154161

155162
if err := runCommands(ctx, commands); err != nil {

Diff for: legacy/builder/constants/constants.go

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const BUILD_PROPERTIES_RUNTIME_PLATFORM_PATH = "runtime.platform.path"
3434
const EMPTY_STRING = ""
3535
const FOLDER_BOOTLOADERS = "bootloaders"
3636
const FOLDER_CORE = "core"
37-
const FOLDER_PREPROC = "preproc"
3837
const FOLDER_SKETCH = "sketch"
3938
const FOLDER_TOOLS = "tools"
4039
const FOLDER_LIBRARIES = "libraries"

Diff for: legacy/builder/container_add_prototypes.go

-56
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,3 @@
1414
// To purchase a commercial license, send an email to [email protected].
1515

1616
package builder
17-
18-
import (
19-
"bytes"
20-
"fmt"
21-
22-
bldr "github.com/arduino/arduino-cli/arduino/builder"
23-
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
24-
"github.com/arduino/arduino-cli/legacy/builder/types"
25-
"github.com/pkg/errors"
26-
)
27-
28-
func PreprocessSketchWithCtags(ctx *types.Context) error {
29-
// Generate the full pathname for the preproc output file
30-
if err := ctx.PreprocPath.MkdirAll(); err != nil {
31-
return errors.WithStack(err)
32-
}
33-
targetFilePath := ctx.PreprocPath.Join("sketch_merged.cpp")
34-
35-
// Run preprocessor
36-
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
37-
gccStdout, gccStderr, err := preprocessor.GCC(sourceFile, targetFilePath, ctx.IncludeFolders, ctx.BuildProperties)
38-
if ctx.Verbose {
39-
ctx.WriteStdout(gccStdout)
40-
ctx.WriteStderr(gccStderr)
41-
}
42-
if err != nil {
43-
if !ctx.OnlyUpdateCompilationDatabase {
44-
return errors.WithStack(err)
45-
}
46-
47-
// Do not bail out if we are generating the compile commands database
48-
ctx.Info(
49-
fmt.Sprintf("%s: %s",
50-
tr("An error occurred adding prototypes"),
51-
tr("the compilation database may be incomplete or inaccurate")))
52-
if err := sourceFile.CopyTo(targetFilePath); err != nil {
53-
return errors.WithStack(err)
54-
}
55-
}
56-
57-
if src, err := targetFilePath.ReadFile(); err != nil {
58-
return err
59-
} else {
60-
filteredSource := bldr.FilterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
61-
if err := targetFilePath.WriteFile([]byte(filteredSource)); err != nil {
62-
return err
63-
}
64-
}
65-
66-
sketchCpp := ctx.SketchBuildPath.Join(fmt.Sprintf("%s.cpp", ctx.Sketch.MainFile.Base()))
67-
ctagsStderr, err := preprocessor.CTags(targetFilePath, sketchCpp, ctx.Sketch, ctx.LineOffset, ctx.BuildProperties)
68-
if ctx.Verbose {
69-
ctx.WriteStderr(ctagsStderr)
70-
}
71-
return err
72-
}

Diff for: legacy/builder/create_cmake_rule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
115115
fmt.Println(err)
116116
}
117117

118-
if err := PreprocessSketchWithCtags(ctx); err != nil {
118+
if err := PreprocessSketch(ctx); err != nil {
119119
return err
120120
}
121121

Diff for: legacy/builder/preprocess_sketch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ import (
3131
)
3232

3333
func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error {
34-
if err := ctx.PreprocPath.MkdirAll(); err != nil {
34+
if err := ctx.BuildPath.Join("preproc").MkdirAll(); err != nil {
3535
return errors.WithStack(err)
3636
}
3737

3838
sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp")
39-
targetFile := ctx.PreprocPath.Join("sketch_merged.cpp")
39+
targetFile := ctx.BuildPath.Join("preproc", "sketch_merged.cpp")
4040
gccStdout, gccStderr, err := preprocessor.GCC(sourceFile, targetFile, ctx.IncludeFolders, ctx.BuildProperties)
4141
if ctx.Verbose {
4242
ctx.WriteStdout(gccStdout)

Diff for: legacy/builder/test/add_additional_entries_to_context_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func TestAddAdditionalEntriesToContextNoBuildPath(t *testing.T) {
3131
command := builder.AddAdditionalEntriesToContext{}
3232
NoError(t, command.Run(ctx))
3333

34-
require.Empty(t, ctx.PreprocPath)
3534
require.Empty(t, ctx.SketchBuildPath)
3635
require.Empty(t, ctx.LibrariesBuildPath)
3736
require.Empty(t, ctx.CoreBuildPath)
@@ -48,7 +47,6 @@ func TestAddAdditionalEntriesToContextWithBuildPath(t *testing.T) {
4847
command := builder.AddAdditionalEntriesToContext{}
4948
NoError(t, command.Run(ctx))
5049

51-
require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_PREPROC)), ctx.PreprocPath)
5250
require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_SKETCH)), ctx.SketchBuildPath)
5351
require.Equal(t, Abs(t, paths.New("folder", "libraries")), ctx.LibrariesBuildPath)
5452
require.Equal(t, Abs(t, paths.New("folder", constants.FOLDER_CORE)), ctx.CoreBuildPath)

Diff for: legacy/builder/test/builder_test.go

-18
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ func TestBuilderEmptySketch(t *testing.T) {
6161
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
6262
NoError(t, err)
6363
require.True(t, exist)
64-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck()
65-
NoError(t, err)
66-
require.True(t, exist)
6764
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch1.ino.cpp.o").ExistCheck()
6865
NoError(t, err)
6966
require.True(t, exist)
@@ -91,9 +88,6 @@ func TestBuilderBridge(t *testing.T) {
9188
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
9289
NoError(t, err)
9390
require.True(t, exist)
94-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck()
95-
NoError(t, err)
96-
require.True(t, exist)
9791
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
9892
NoError(t, err)
9993
require.True(t, exist)
@@ -124,9 +118,6 @@ func TestBuilderSketchWithConfig(t *testing.T) {
124118
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
125119
NoError(t, err)
126120
require.True(t, exist)
127-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck()
128-
NoError(t, err)
129-
require.True(t, exist)
130121
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch_with_config.ino.cpp.o").ExistCheck()
131122
NoError(t, err)
132123
require.True(t, exist)
@@ -162,9 +153,6 @@ func TestBuilderBridgeTwice(t *testing.T) {
162153
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
163154
NoError(t, err)
164155
require.True(t, exist)
165-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck()
166-
NoError(t, err)
167-
require.True(t, exist)
168156
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
169157
NoError(t, err)
170158
require.True(t, exist)
@@ -202,9 +190,6 @@ func TestBuilderBridgeSAM(t *testing.T) {
202190
exist, err = buildPath.Join(constants.FOLDER_CORE, "avr", "dtostrf.c.d").ExistCheck()
203191
NoError(t, err)
204192
require.True(t, exist)
205-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck()
206-
NoError(t, err)
207-
require.True(t, exist)
208193
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
209194
NoError(t, err)
210195
require.True(t, exist)
@@ -242,9 +227,6 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
242227
exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck()
243228
NoError(t, err)
244229
require.True(t, exist)
245-
exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck()
246-
NoError(t, err)
247-
require.True(t, exist)
248230
exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck()
249231
NoError(t, err)
250232
require.True(t, exist)

0 commit comments

Comments
 (0)