183
183
import filecmp
184
184
from shutil import copyfile
185
185
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
+
186
191
def print_msg (* args , ** kwargs ):
187
192
print (* args , flush = True , ** kwargs )
188
193
@@ -233,13 +238,13 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
233
238
path/core/build.opt. The subdirectory path must already exist as well as the
234
239
copy of SketchName.ino.globals.h.
235
240
"""
241
+ global build_opt_signature
242
+
236
243
build_opt = open (build_opt_fqfn , 'w' )
237
244
if not os .path .exists (globals_h_fqfn ) or (0 == os .path .getsize (globals_h_fqfn )):
238
245
build_opt .close ()
239
246
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
+
243
248
complete_comment = False
244
249
build_opt_error = False
245
250
line_no = 0
@@ -296,42 +301,42 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
296
301
# Don't let the failure get hidden by a spew of nonsensical error
297
302
# messages that will follow. Bring things to a halt.
298
303
sys .exit (1 )
299
- return
304
+ return False
300
305
elif complete_comment :
301
306
print_msg ("Created compiler command-line options file " + build_opt_fqfn )
302
307
build_opt .close ()
308
+ return complete_comment
303
309
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 ):
306
311
"""
307
312
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.
309
314
"""
310
315
source_fqfn = os .path .expanduser ('~/' + sketchbook_globals_path )
311
316
notused , file_name = os .path .split (source_fqfn )
312
317
build_target_fqfn = os .path .join (build_path , file_name )
313
318
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 )
317
320
318
321
319
322
def main ():
323
+ global build_opt_signature
324
+
320
325
if len (sys .argv ) >= 4 :
321
- source_globals_h_fqfn = sys .argv [1 ]
326
+ source_globals_h_fqfn = os . path . normpath ( sys .argv [1 ])
322
327
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 ])
324
329
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 ])
326
331
# Assumption: globals_h_fqfn and build_opt_fqfn have the same dirname
327
332
328
333
if len (sys .argv ) >= 5 :
329
- # Hidden option for more advanced programmers
334
+ # Hidden option for advanced programmers
330
335
# Very few things need to be made available globaly to *all* Sketches
331
336
# This option can create obfuscation when not used wisely.
332
337
# Omit from documentation, assume that only an advanced programmer
333
338
# will discover and use this.
334
- sketchbook_globals_path = sys .argv [4 ]
339
+ sketchbook_globals_path = os . path . normpath ( sys .argv [4 ])
335
340
num_include_lines = 2
336
341
else :
337
342
sketchbook_globals_path = ""
@@ -349,42 +354,30 @@ def main():
349
354
if os .path .getsize (globals_h_fqfn ) and len (os .listdir (build_path )) < 20 :
350
355
print_err ("Aggressive caching of core.a might be enabled. This may create build errors." )
351
356
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." )
354
357
else :
355
358
# Info: When platform.txt, platform.local.txt, or IDE Tools are
356
359
# changed, our build path directory was cleaned. Note,
357
360
# makecorever.py may have run before us and recreaded the directory.
358
361
if not os .path .exists (build_path ):
359
362
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 )
361
364
362
365
if os .path .exists (source_globals_h_fqfn ):
363
366
print_msg ("Using global defines from " + source_globals_h_fqfn )
364
367
365
- extract_fallback_build_opt = \
366
368
copy_create_build_file (source_globals_h_fqfn , globals_h_fqfn )
367
369
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 + "'." )
382
376
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 )
385
378
386
379
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 )
388
381
389
382
else :
390
383
print_err ("Too few arguments. Required arguments:" )
0 commit comments