@@ -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
0 commit comments