183183import filecmp
184184from shutil import copyfile
185185
186+ # Need to work on signature line used for match to avoid conflicts with
187+ # existing embedded documentation methods.
188+ build_opt_signature = "/*@create-file:build.opt@"
189+
190+
186191def print_msg (* args , ** kwargs ):
187192 print (* args , flush = True , ** kwargs )
188193
@@ -233,13 +238,13 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
233238 path/core/build.opt. The subdirectory path must already exist as well as the
234239 copy of SketchName.ino.globals.h.
235240 """
241+ global build_opt_signature
242+
236243 build_opt = open (build_opt_fqfn , 'w' )
237244 if not os .path .exists (globals_h_fqfn ) or (0 == os .path .getsize (globals_h_fqfn )):
238245 build_opt .close ()
239246 return
240- # Need to work on signature line used for match to avoid conflicts with
241- # existing embedded documentation methods.
242- build_opt_signature = "/*@create-file:build.opt@"
247+
243248 complete_comment = False
244249 build_opt_error = False
245250 line_no = 0
@@ -296,42 +301,42 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
296301 # Don't let the failure get hidden by a spew of nonsensical error
297302 # messages that will follow. Bring things to a halt.
298303 sys .exit (1 )
299- return
304+ return False
300305 elif complete_comment :
301306 print_msg ("Created compiler command-line options file " + build_opt_fqfn )
302307 build_opt .close ()
308+ return complete_comment
303309
304-
305- def get_sketchbook_globals (build_path , sketchbook_globals_path , rebuild_opt_file , build_opt_fqfn ):
310+ def get_sketchbook_globals (build_path , sketchbook_globals_path , build_opt_fqfn ):
306311 """
307312 Construct path to sketchbook globals using relative path from users home directory.
308- Append to build options only if recomposing build options .
313+ Append to build options.
309314 """
310315 source_fqfn = os .path .expanduser ('~/' + sketchbook_globals_path )
311316 notused , file_name = os .path .split (source_fqfn )
312317 build_target_fqfn = os .path .join (build_path , file_name )
313318 copy_create_build_file (source_fqfn , build_target_fqfn )
314- # The old build.opt will be fine since we did a add_include last time.
315- if rebuild_opt_file :
316- add_include_line (build_opt_fqfn , build_target_fqfn )
319+ add_include_line (build_opt_fqfn , build_target_fqfn )
317320
318321
319322def main ():
323+ global build_opt_signature
324+
320325 if len (sys .argv ) >= 4 :
321- source_globals_h_fqfn = sys .argv [1 ]
326+ source_globals_h_fqfn = os . path . normpath ( sys .argv [1 ])
322327 globals_name = os .path .basename (source_globals_h_fqfn )
323- globals_h_fqfn = sys .argv [2 ]
328+ globals_h_fqfn = os . path . normpath ( sys .argv [2 ])
324329 build_path = os .path .dirname (globals_h_fqfn )
325- build_opt_fqfn = sys .argv [3 ]
330+ build_opt_fqfn = os . path . normpath ( sys .argv [3 ])
326331 # Assumption: globals_h_fqfn and build_opt_fqfn have the same dirname
327332
328333 if len (sys .argv ) >= 5 :
329- # Hidden option for more advanced programmers
334+ # Hidden option for advanced programmers
330335 # Very few things need to be made available globaly to *all* Sketches
331336 # This option can create obfuscation when not used wisely.
332337 # Omit from documentation, assume that only an advanced programmer
333338 # will discover and use this.
334- sketchbook_globals_path = sys .argv [4 ]
339+ sketchbook_globals_path = os . path . normpath ( sys .argv [4 ])
335340 num_include_lines = 2
336341 else :
337342 sketchbook_globals_path = ""
@@ -349,42 +354,30 @@ def main():
349354 if os .path .getsize (globals_h_fqfn ) and len (os .listdir (build_path )) < 20 :
350355 print_err ("Aggressive caching of core.a might be enabled. This may create build errors." )
351356 print_err ("Suggest turning off in preferences.txt: \" compiler.cache_core=false\" " )
352- # TODO Revise message with proper URL
353- print_err ("Add URL to topic in docs." )
354357 else :
355358 # Info: When platform.txt, platform.local.txt, or IDE Tools are
356359 # changed, our build path directory was cleaned. Note,
357360 # makecorever.py may have run before us and recreaded the directory.
358361 if not os .path .exists (build_path ):
359362 os .makedirs (build_path )
360- print_msg ("Clean build, created dir " + build_path ) # dev debug print
363+ print_msg ("Clean build, created dir " + build_path )
361364
362365 if os .path .exists (source_globals_h_fqfn ):
363366 print_msg ("Using global defines from " + source_globals_h_fqfn )
364367
365- extract_fallback_build_opt = \
366368 copy_create_build_file (source_globals_h_fqfn , globals_h_fqfn )
367369
368- # At this point we have a SketchName.ino.globals.h and build.opt file in
369- # build path/core/ directory. They may be empty at this stage.
370- # Reuse old build.opt or extract new one from SketchName.ino.globals.h
371- if extract_fallback_build_opt :
372- extract_create_build_opt_file (globals_h_fqfn , globals_name , build_opt_fqfn )
373- elif os .path .exists (globals_h_fqfn ) and os .path .getsize (globals_h_fqfn ):
374- num_lines = sum (1 for line in open (build_opt_fqfn ))
375- if num_lines > num_include_lines :
376- print_msg ("Using extracted compiler command-line options in " + build_opt_fqfn )
377- else :
378- # SketchName.ino.globals.h may have been deleted in the sketch
379- # directory or is just empty. Recompose build.opt
380- open (build_opt_fqfn , 'w' ).close ()
381- extract_fallback_build_opt = True
370+ # globals_h_fqfn timestamp was only updated if the source changed. This
371+ # controls the rebuild on change. We can always extact a new build.opt
372+ # w/o triggering a needless rebuild.
373+ embedded_options = extract_create_build_opt_file (globals_h_fqfn , globals_name , build_opt_fqfn )
374+ if not embedded_options and os .path .exists (source_globals_h_fqfn ):
375+ print_msg ("To add embedded compiler options, include them in a block comment starting with '" + build_opt_signature + "'." )
382376
383- if extract_fallback_build_opt :
384- add_include_line (build_opt_fqfn , globals_h_fqfn )
377+ add_include_line (build_opt_fqfn , globals_h_fqfn )
385378
386379 if len (sketchbook_globals_path ):
387- get_sketchbook_globals (build_path , sketchbook_globals_path , extract_fallback_build_opt , build_opt_fqfn )
380+ get_sketchbook_globals (build_path , sketchbook_globals_path , build_opt_fqfn )
388381
389382 else :
390383 print_err ("Too few arguments. Required arguments:" )
0 commit comments