Skip to content

Commit f119fea

Browse files
committed
expunged sketchbook global
added workaround for aggressive caching
1 parent 4fdfd3b commit f119fea

File tree

3 files changed

+78
-53
lines changed

3 files changed

+78
-53
lines changed

doc/faq/a06-global-build-options.rst

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,29 @@ Global ``.h`` file: ``LowWatermark.ino.globals.h``
9292
Aggressive Caching of ``core.a``
9393
================================
9494

95-
Using global defines or compiler command-line options will lead to bad
96-
builds when the **Aggressively cache compiled core** feature is enabled.
97-
When ``#define`` changes require rebuilding ``core.a`` and multiple
98-
Sketches are open, they can no longer reliably share one cached
99-
``core.a``. In a simple case: The 1st Sketch to be built has its version
100-
of ``core.a`` cached. Other sketches will use this cached version for
101-
their builds.
102-
103-
To turn this off, you need to find the location of ``preferences.txt``.
104-
From the Arduino IDE, go to *File->Preferences*. Make note of the path
105-
to ``prefereces.txt``. You cannot edit the file while the Arduino IDE is
106-
running. Close all Arduino IDE windows and edit the file
107-
``preferences.txt``. Change ``compiler.cache_core=true`` to
108-
``compiler.cache_core=false`` and save. Then each sketch will maintain
109-
its *own* copy of ``core.a``. The alternative when using
110-
``compiler.cache_core=true``, is to close all Arduino IDE sketch
111-
windows. Start and run *only* one instance of the IDE, while building a
112-
Sketch that uses global defines.
95+
Without mediation, using global defines or compiler command-line options
96+
could lead to bad builds when the “Aggressively cache compiled core”
97+
feature is enabled. When ``#define`` changes require rebuilding
98+
``core.a`` and multiple Sketches are open, they can no longer reliably
99+
share one cached ``core.a``. In a simple case: The 1st Sketch to be
100+
built has its version of ``core.a`` cached. Other sketches will use this
101+
cached version for their builds.
102+
103+
When the “Aggressively cache compiled core” feature is enabled and a
104+
global define file is detected, a workaround will turn on and stay on.
105+
When you switch between Sketch windows, core will be recompiled and
106+
cache updated.
107+
108+
To turn the “Aggressively cache compiled core” feature off, you need to
109+
find the location of ``preferences.txt``. From the Arduino IDE, go to
110+
*File->Preferences*. Make note of the path to ``prefereces.txt``. You
111+
cannot edit the file while the Arduino IDE is running. Close all Arduino
112+
IDE windows and edit the file ``preferences.txt``. Change
113+
``compiler.cache_core=true`` to ``compiler.cache_core=false`` and save.
114+
Then each sketch will maintain its *own* copy of ``core.a``. The
115+
alternative when using ``compiler.cache_core=true``, is to close all
116+
Arduino IDE sketch windows. Start and run *only* one instance of the
117+
IDE, while building a Sketch that uses global defines.
113118

114119
Other build confusion
115120
=====================
@@ -126,6 +131,6 @@ Other build confusion
126131
modules. That module will continue to use the stale version of the
127132
``.h`` until you restart the IDE or other major changes that would
128133
cause the IDE to delete and recopy the contents from the source
129-
Sketch directory. Changes on the IDE Tools selection may cause a
134+
Sketch directory. Changes on the IDE Tools board settings may cause a
130135
complete rebuild, clearing the problem. This may be the culprit for
131136
“What! It built fine last night!”

platform.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ globals.h.source.fqfn={build.source.path}/{build.project_name}.globals.h
6464
build.globals.path={build.path}/core
6565
globals.h.fqfn={build.globals.path}/{build.project_name}.globals.h
6666
build.opt.fqfn={build.globals.path}/build.opt
67-
sketchbook.globals.h.rfn=
67+
commonhfile.fqfn={build.core.path}/CommonHFile.h
6868

6969
compiler.path={runtime.tools.xtensa-lx106-elf-gcc.path}/bin/
7070
compiler.sdk.path={runtime.platform.path}/tools/sdk
@@ -118,7 +118,7 @@ recipe.hooks.sketch.prebuild.pattern="{runtime.tools.python3.path}/python3" -I "
118118
recipe.hooks.prebuild.1.pattern="{runtime.tools.python3.path}/python3" -I "{runtime.tools.makecorever}" --build_path "{build.path}" --platform_path "{runtime.platform.path}" --version "{version}"
119119

120120
# Handle processing sketch global options
121-
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{globals.h.source.fqfn}" "{globals.h.fqfn}" "{build.opt.fqfn}" "{sketchbook.globals.h.rfn}"
121+
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{globals.h.source.fqfn}" "{globals.h.fqfn}" "{build.opt.fqfn}" "{commonhfile.fqfn}"
122122

123123

124124
## Build the app.ld linker file

tools/mkbuildoptglobals.py

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@
7878
build.globals.path={build.path}/core
7979
globals.h.fqfn={build.globals.path}/{build.project_name}.globals.h
8080
build.opt.fqfn={build.globals.path}/build.opt
81-
sketchbook.globals.h.rfn=
8281
83-
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{globals.h.source.fqfn}" "{globals.h.fqfn}" "{build.opt.fqfn}" "{sketchbook.globals.h.rfn}"
82+
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{globals.h.source.fqfn}" "{globals.h.fqfn}" "{build.opt.fqfn}"
8483
8584
compiler.cpreprocessor.flags=-D__ets__ -DICACHE_FLASH -U__STRICT_ANSI__ -D_GNU_SOURCE -DESP8266 @{build.opt.path} "-I{compiler.sdk.path}/include" "-I{compiler.sdk.path}/{build.lwip_include}" "-I{compiler.libc.path}/include" "-I{build.path}/core"
8685
"""
@@ -181,6 +180,7 @@
181180
import os
182181
import sys
183182
import filecmp
183+
import time
184184
from shutil import copyfile
185185

186186
# Need to work on signature line used for match to avoid conflicts with
@@ -228,6 +228,7 @@ def add_include_line(build_opt_fqfn, include_fqfn):
228228
if not os.path.exists(include_fqfn):
229229
# If file is missing, we need an place holder
230230
open(include_fqfn, 'w').close()
231+
print("add_include_line: Created " + include_fqfn)
231232
build_opt = open(build_opt_fqfn, 'a')
232233
build_opt.write('-include "' + include_fqfn.replace('\\', '\\\\') + '"\n')
233234
build_opt.close()
@@ -309,41 +310,49 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
309310
return complete_comment
310311

311312

312-
def get_sketchbook_globals(build_path, sketchbook_globals_path, build_opt_fqfn):
313-
"""
314-
Construct path to sketchbook globals using relative path from users home directory.
315-
Append to build options.
316-
"""
317-
source_fqfn = os.path.expanduser('~/' + sketchbook_globals_path)
318-
notused, file_name = os.path.split(source_fqfn)
319-
build_target_fqfn = os.path.join(build_path, file_name)
320-
copy_create_build_file(source_fqfn, build_target_fqfn)
321-
add_include_line(build_opt_fqfn, build_target_fqfn)
313+
def enable_override(enable, commonhfile_fqfn):
314+
file=open(commonhfile_fqfn, 'w')
315+
if enable:
316+
file.write("//Override aggressive caching\n")
317+
file.close()
318+
# enabled when getsize(commonhfile_fqfn) is non-zero, disabled when zero
319+
320+
321+
def touch(fname, times=None):
322+
with open(fname, 'a'):
323+
os.utime(fname, times)
324+
325+
326+
def time_sync(globals_h_fqfn, commonhfile_fqfn):
327+
ts = time.time()
328+
touch(globals_h_fqfn, (ts, ts))
329+
touch(commonhfile_fqfn, (ts, ts))
322330

323331

324332
def main():
325333
global build_opt_signature
326334
global docs_url
335+
num_include_lines = 1
336+
use_aggressive_caching_workaround = True
337+
# Enhancement: read preferences.txt and set use_aggressive_caching_workaround
338+
# https://www.arduino.cc/en/hacking/preferences
339+
# :( it can be in 9 different locations
327340

328-
if len(sys.argv) >= 4:
341+
if len(sys.argv) >= 5:
329342
source_globals_h_fqfn = os.path.normpath(sys.argv[1])
330343
globals_name = os.path.basename(source_globals_h_fqfn)
331344
globals_h_fqfn = os.path.normpath(sys.argv[2])
332345
build_path = os.path.dirname(globals_h_fqfn)
333346
build_opt_fqfn = os.path.normpath(sys.argv[3])
334347
# Assumption: globals_h_fqfn and build_opt_fqfn have the same dirname
348+
commonhfile_fqfn = os.path.normpath(sys.argv[4])
335349

336-
if len(sys.argv) >= 5:
337-
# Hidden option for advanced programmers
338-
# Very few things need to be made available globaly to *all* Sketches
339-
# This option can create obfuscation when not used wisely.
340-
# Omit from documentation, assume that only an advanced programmer
341-
# will discover and use this.
342-
sketchbook_globals_path = os.path.normpath(sys.argv[4])
343-
num_include_lines = 2
350+
if os.path.exists(commonhfile_fqfn):
351+
if os.path.getsize(commonhfile_fqfn) and \
352+
not use_aggressive_caching_workaround:
353+
enable_override(False, commonhfile_fqfn)
344354
else:
345-
sketchbook_globals_path = ""
346-
num_include_lines = 1
355+
enable_override(False, commonhfile_fqfn)
347356

348357
if os.path.exists(globals_h_fqfn):
349358
# Check for signs of "Aggressive Caching core.a"
@@ -354,14 +363,15 @@ def main():
354363
# report often enough to draw attention to the issue. Some aborted
355364
# builds with incomplete ./core compiles may later produce false
356365
# positives. Only report when globals.h is being used.
357-
if os.path.getsize(globals_h_fqfn) and len(os.listdir(build_path)) < 20:
366+
if not use_aggressive_caching_workaround and \
367+
os.path.getsize(globals_h_fqfn) and \
368+
len(os.listdir(build_path)) < 20:
358369
print_err("Aggressive caching of core.a might be enabled. This may create build errors.")
359370
print_err(" Suggest turning off in preferences.txt: \"compiler.cache_core=false\"")
360371
print_err(" Read more at " + docs_url)
361-
362372
else:
363-
# Info: When platform.txt, platform.local.txt, or IDE Tools are
364-
# changed, our build path directory was cleaned. Note,
373+
# Info: When platform.txt, platform.local.txt, or IDE Tools board
374+
# settings are changed, our build path directory was cleaned. Note,
365375
# makecorever.py may have run before us and recreaded the directory.
366376
if not os.path.exists(build_path):
367377
os.makedirs(build_path)
@@ -377,6 +387,21 @@ def main():
377387
# w/o triggering a needless rebuild.
378388
embedded_options = extract_create_build_opt_file(globals_h_fqfn, globals_name, build_opt_fqfn)
379389

390+
if use_aggressive_caching_workaround:
391+
if os.path.getsize(commonhfile_fqfn):
392+
print("os.path.getmtime(globals_h_fqfn) " + str(os.path.getmtime(globals_h_fqfn)))
393+
print("os.path.getmtime(commonhfile_fqfn) " + str(os.path.getmtime(commonhfile_fqfn)))
394+
if (os.path.getmtime(globals_h_fqfn) != os.path.getmtime(commonhfile_fqfn)):
395+
# Need to rebuild core.a
396+
# touching commonhfile_fqfn in the source core tree will cause rebuild.
397+
time_sync(globals_h_fqfn, commonhfile_fqfn)
398+
elif os.path.getsize(globals_h_fqfn):
399+
enable_override(True, commonhfile_fqfn)
400+
time_sync(globals_h_fqfn, commonhfile_fqfn)
401+
402+
add_include_line(build_opt_fqfn, commonhfile_fqfn)
403+
add_include_line(build_opt_fqfn, globals_h_fqfn)
404+
380405
# Provide context help for build option support.
381406
source_build_opt_h_fqfn = os.path.join(os.path.dirname(source_globals_h_fqfn), "build_opt.h")
382407
if os.path.exists(source_build_opt_h_fqfn) and not embedded_options:
@@ -389,11 +414,6 @@ def main():
389414
print_msg("Tip: Embedd compiler command-line options in a block comment starting with '" + build_opt_signature + "'.")
390415
print_msg(" Read more at " + docs_url)
391416

392-
add_include_line(build_opt_fqfn, globals_h_fqfn)
393-
394-
if len(sketchbook_globals_path):
395-
get_sketchbook_globals(build_path, sketchbook_globals_path, build_opt_fqfn)
396-
397417
else:
398418
print_err("Too few arguments. Add arguments:")
399419
print_err(" Source FQFN SketchName.ino.globals.h, Build FQFN SketchName.ino.globals.h, Build FQFN build.opt")

0 commit comments

Comments
 (0)