Skip to content

Commit 48a06a4

Browse files
committed
Avoid rebuilding the sketch if the sketch file is unchanged.
1 parent 1907cb3 commit 48a06a4

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

internal/arduino/builder/internal/preprocessor/ctags.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,19 @@ func PreprocessSketchWithCtags(
5555

5656
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
5757

58+
// Check if the preprocessed file is already up-to-date
59+
unpreprocessedSourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp.merged")
60+
preprocessedSourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
61+
if unpreprocessedStat, err := unpreprocessedSourceFile.Stat(); err != nil {
62+
return nil, fmt.Errorf("%s: %w", i18n.Tr("unable to open unpreprocessed source file"), err)
63+
} else if sourceStat, err := preprocessedSourceFile.Stat(); err == nil && unpreprocessedStat.ModTime().Before(sourceStat.ModTime()) {
64+
fmt.Fprintln(stdout, i18n.Tr("Sketch is unchanged, skipping preprocessing."))
65+
res := &runner.Result{Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}
66+
return res, nil
67+
}
68+
5869
// Run GCC preprocessor
59-
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
60-
result := GCC(sourceFile, ctagsTarget, includes, buildProperties).Run(ctx)
70+
result := GCC(unpreprocessedSourceFile, ctagsTarget, includes, buildProperties).Run(ctx)
6171
stdout.Write(result.Stdout)
6272
stderr.Write(result.Stderr)
6373
if err := result.Error; err != nil {
@@ -69,7 +79,7 @@ func PreprocessSketchWithCtags(
6979
fmt.Fprintf(stderr, "%s: %s",
7080
i18n.Tr("An error occurred adding prototypes"),
7181
i18n.Tr("the compilation database may be incomplete or inaccurate"))
72-
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
82+
if err := unpreprocessedSourceFile.CopyTo(ctagsTarget); err != nil {
7383
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
7484
}
7585
}
@@ -102,7 +112,7 @@ func PreprocessSketchWithCtags(
102112

103113
// Add prototypes to the original sketch source
104114
var source string
105-
if sourceData, err := sourceFile.ReadFile(); err == nil {
115+
if sourceData, err := unpreprocessedSourceFile.ReadFile(); err == nil {
106116
source = string(sourceData)
107117
} else {
108118
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
@@ -136,7 +146,7 @@ func PreprocessSketchWithCtags(
136146
}
137147

138148
// Write back arduino-preprocess output to the sourceFile
139-
err = sourceFile.WriteFile([]byte(preprocessedSource))
149+
err = preprocessedSourceFile.WriteFile([]byte(preprocessedSource))
140150
return &runner.Result{Args: result.Args, Stdout: stdout.Bytes(), Stderr: stderr.Bytes()}, err
141151
}
142152

internal/arduino/builder/sketch.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,20 @@ func (b *Builder) prepareSketchBuildPath() error {
5858
return err
5959
}
6060

61+
// Save the unpreprocessed merged source to a file named sketch.cpp.merged.
62+
destFileUnpreprocessed := b.sketchBuildPath.Join(b.sketch.MainFile.Base() + ".cpp.merged")
6163
destFile := b.sketchBuildPath.Join(b.sketch.MainFile.Base() + ".cpp")
62-
if err := destFile.WriteFile([]byte(mergedSource)); err != nil {
63-
return err
64+
oldUnpreprocessedSource, _ := destFileUnpreprocessed.ReadFile()
65+
66+
// If the merged source is unchanged, skip writing it.
67+
// This avoids unnecessary rebuilds and keeps the build path clean.
68+
if !bytes.Equal(oldUnpreprocessedSource, []byte(mergedSource)) {
69+
if err := destFileUnpreprocessed.WriteFile([]byte(mergedSource)); err != nil {
70+
return err
71+
}
72+
if err := destFile.WriteFile([]byte(mergedSource)); err != nil {
73+
return err
74+
}
6475
}
6576

6677
if err := b.sketchCopyAdditionalFiles(b.sketchBuildPath, b.sourceOverrides); err != nil {

0 commit comments

Comments
 (0)