@@ -45,28 +45,19 @@ func PreprocessSketchWithCtags(
4545 lineOffset int , buildProperties * properties.Map ,
4646 onlyUpdateCompilationDatabase , verbose bool ,
4747) (* runner.Result , error ) {
48+ stdout , stderr := & bytes.Buffer {}, & bytes.Buffer {}
49+ unpreprocessedSourceFile := buildPath .Join ("sketch" , sketch .MainFile .Base ()+ ".cpp.merged" )
50+ preprocessedSourceFile := buildPath .Join ("sketch" , sketch .MainFile .Base ()+ ".cpp" )
51+
4852 // Create a temporary working directory
4953 tmpDir , err := paths .MkTempDir ("" , "" )
5054 if err != nil {
5155 return nil , err
5256 }
5357 defer tmpDir .RemoveAll ()
54- ctagsTarget := tmpDir .Join ("sketch_merged.cpp" )
55-
56- stdout , stderr := & bytes.Buffer {}, & bytes.Buffer {}
57-
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- }
6858
6959 // Run GCC preprocessor
60+ ctagsTarget := tmpDir .Join ("sketch_merged.cpp" )
7061 result := GCC (unpreprocessedSourceFile , ctagsTarget , includes , buildProperties , nil ).Run (ctx )
7162 stdout .Write (result .Stdout )
7263 stderr .Write (result .Stderr )
@@ -145,6 +136,13 @@ func PreprocessSketchWithCtags(
145136 fmt .Println ("#END OF PREPROCESSED SOURCE" )
146137 }
147138
139+ // Read the existing preprocessed file to check if it's already up-to-date.
140+ oldPreprocessedSource , _ := preprocessedSourceFile .ReadFile ()
141+ if bytes .Equal ([]byte (preprocessedSource ), oldPreprocessedSource ) {
142+ // No changes, do nothing
143+ return & runner.Result {Args : result .Args , Stdout : stdout .Bytes (), Stderr : stderr .Bytes ()}, nil
144+ }
145+
148146 // Write back arduino-preprocess output to the sourceFile
149147 err = preprocessedSourceFile .WriteFile ([]byte (preprocessedSource ))
150148 return & runner.Result {Args : result .Args , Stdout : stdout .Bytes (), Stderr : stderr .Bytes ()}, err
0 commit comments