Skip to content

Commit 1c1236b

Browse files
committed
Add cleanup logic and identify 1st run after IDE restart
1 parent 1e0bf97 commit 1c1236b

File tree

3 files changed

+65
-49
lines changed

3 files changed

+65
-49
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,16 @@ cached version for their builds.
110110
When the “Aggressively cache compiled core” feature is enabled and a
111111
global define file is detected, a workaround will turn on and stay on.
112112
When you switch between Sketch windows, core will be recompiled and the
113-
cache updated.
113+
cache updated. The workaround logic is reset when Arduino IDE is
114+
completely shutdown and restarted. Some operating systems are better at
115+
cleaning up their temp space than others at reboot after a crash. At
116+
least for Windows you may need to manually delete the Arduino temp files
117+
and directories after a crash. Otherwise the workaround logic may be
118+
left on.
119+
120+
For some Windows systems the temp directory can be found near
121+
``C:\Users\<user id>\AppData\Local\Temp\arduino*``. Note ``AppData`` is
122+
a hidden directory.
114123

115124
If you think your workflow performance would benefit from keeping a per
116125
Sketch copy of ``core.a``, you can turn off the “Aggressively cache

platform.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ build.spiffs_start=
5959
build.spiffs_end=
6060
build.spiffs_blocksize=
6161

62-
# Fully qualified and relative file names for processing sketch global options
62+
# Fully qualified file names for processing sketch global options
6363
globals.h.source.fqfn={build.source.path}/{build.project_name}.globals.h
64-
build.globals.path={build.path}/core
65-
globals.h.fqfn={build.globals.path}/{build.project_name}.globals.h
66-
build.opt.fqfn={build.globals.path}/build.opt
6764
commonhfile.fqfn={build.core.path}/CommonHFile.h
65+
build.opt.fqfn={build.path}/core/build.opt
6866

6967
compiler.path={runtime.tools.xtensa-lx106-elf-gcc.path}/bin/
7068
compiler.sdk.path={runtime.platform.path}/tools/sdk
@@ -118,7 +116,7 @@ recipe.hooks.sketch.prebuild.pattern="{runtime.tools.python3.path}/python3" -I "
118116
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}"
119117

120118
# 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}" "{commonhfile.fqfn}" "{runtime.ide.path}"
119+
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{runtime.ide.path}" "{build.path}" "{build.opt.fqfn}" "{globals.h.source.fqfn}" "{commonhfile.fqfn}"
122120

123121

124122
## Build the app.ld linker file

tools/mkbuildoptglobals.py

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@
7373
7474
runtime.tools.mkbuildoptglobals={runtime.platform.path}/tools/mkbuildoptglobals.py
7575
76-
# Fully qualified and relative file names for processing sketch global options
76+
# Fully qualified file names for processing sketch global options
7777
globals.h.source.fqfn={build.source.path}/{build.project_name}.globals.h
78-
build.globals.path={build.path}/core
79-
globals.h.fqfn={build.globals.path}/{build.project_name}.globals.h
80-
build.opt.fqfn={build.globals.path}/build.opt
78+
commonhfile.fqfn={build.core.path}/CommonHFile.h
79+
build.opt.fqfn={build.path}/core/build.opt
8180
82-
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{globals.h.source.fqfn}" "{globals.h.fqfn}" "{build.opt.fqfn}"
81+
recipe.hooks.prebuild.2.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.mkbuildoptglobals}" "{runtime.ide.path}" "{build.path}" "{build.opt.fqfn}" "{globals.h.source.fqfn}" "{commonhfile.fqfn}"
8382
8483
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"
8584
"""
@@ -180,12 +179,12 @@
180179
Build does not work as expected. This does not fail often. Maybe PIC NIC.
181180
"""
182181

182+
from shutil import copyfile
183+
import glob
183184
import os
185+
import platform
184186
import sys
185-
import filecmp
186187
import time
187-
import platform
188-
from shutil import copyfile
189188

190189
# Need to work on signature line used for match to avoid conflicts with
191190
# existing embedded documentation methods.
@@ -323,10 +322,36 @@ def extract_create_build_opt_file(globals_h_fqfn, file_name, build_opt_fqfn):
323322

324323

325324
def enable_override(enable, commonhfile_fqfn):
325+
# Reduce disk IO writes
326+
if os.path.exists(commonhfile_fqfn):
327+
if os.path.getsize(commonhfile_fqfn): # workaround active
328+
if enable:
329+
return
330+
elif not enable:
331+
return
326332
with open(commonhfile_fqfn, 'w') as file:
327333
if enable:
328334
file.write("//Override aggressive caching\n")
329-
# enabled when getsize(commonhfile_fqfn) is non-zero, disabled when zero
335+
# enable workaround when getsize(commonhfile_fqfn) is non-zero, disabled when zero
336+
337+
338+
def discover_1st_time_run(build_path):
339+
# Need to know if this is the 1ST compile of the Arduino IDE starting.
340+
# Use empty cache directory as an indicator for 1ST compile.
341+
# Arduino IDE 2.0 RC5 does not cleanup on exist like 1.6.19. Probably for
342+
# debugging like the irregular version number 10607. For RC5 this indicator
343+
# will be true after a reboot instead of a 1ST compile of the IDE starting.
344+
tmp_path, build = os.path.split(build_path)
345+
ide_2_0 = 'arduino-sketch-'
346+
if ide_2_0 == build[:len(ide_2_0)]:
347+
search_path = os.path.join(tmp_path, 'arduino-core-cache/*') # Arduino IDE 2.0
348+
else:
349+
search_path = os.path.join(tmp_path, 'arduino_cache_*/*') # Arduino IDE 1.6.x and up
350+
351+
count = 0
352+
for dirname in glob.glob(search_path):
353+
count += 1
354+
return 0 == count
330355

331356

332357
def find_preferences_txt(runtime_ide_path):
@@ -423,45 +448,27 @@ def main():
423448
num_include_lines = 1
424449

425450
if len(sys.argv) >= 6:
426-
source_globals_h_fqfn = os.path.normpath(sys.argv[1])
427-
globals_name = os.path.basename(source_globals_h_fqfn)
428-
globals_h_fqfn = os.path.normpath(sys.argv[2])
429-
build_path = os.path.dirname(globals_h_fqfn)
430-
# Assumption: globals_h_fqfn and build_opt_fqfn have the same dirname
451+
runtime_ide_path = os.path.normpath(sys.argv[1])
452+
build_path = os.path.normpath(sys.argv[2])
431453
build_opt_fqfn = os.path.normpath(sys.argv[3])
432-
commonhfile_fqfn = os.path.normpath(sys.argv[4])
433-
runtime_ide_path = os.path.normpath(sys.argv[5])
454+
source_globals_h_fqfn = os.path.normpath(sys.argv[4])
455+
commonhfile_fqfn = os.path.normpath(sys.argv[5])
456+
457+
globals_name = os.path.basename(source_globals_h_fqfn)
458+
build_path_core, build_opt_name = os.path.split(build_opt_fqfn)
459+
globals_h_fqfn = os.path.join(build_path_core, globals_name)
460+
461+
first_time = discover_1st_time_run(build_path)
434462
use_aggressive_caching_workaround = check_preferences_txt(runtime_ide_path)
435463

436-
if os.path.exists(commonhfile_fqfn):
437-
if os.path.getsize(commonhfile_fqfn) and \
438-
not use_aggressive_caching_workaround:
439-
enable_override(False, commonhfile_fqfn)
440-
else:
464+
if first_time or \
465+
not use_aggressive_caching_workaround or \
466+
not os.path.exists(commonhfile_fqfn):
441467
enable_override(False, commonhfile_fqfn)
442468

443-
if os.path.exists(globals_h_fqfn):
444-
# Check for signs of "Aggressive Caching core.a"
445-
# 1ST time run, build path/core will not exist or be nearly empty,
446-
# nothing can be learned. The presence of globals_h_fqfn in the
447-
# build path/core helps distinguish 1st time run from rebuild.
448-
# This method does not report in all scenarios; however, it does
449-
# report often enough to draw attention to the issue. Some aborted
450-
# builds with incomplete ./core compiles may later produce false
451-
# positives. Only report when globals.h is being used.
452-
if not use_aggressive_caching_workaround and \
453-
os.path.getsize(globals_h_fqfn) and \
454-
len(os.listdir(build_path)) < 20:
455-
print_err("Aggressive caching of core.a might be enabled. This may create build errors.")
456-
print_err(" Suggest turning off in preferences.txt: \"compiler.cache_core=false\"")
457-
print_err(" Read more at " + docs_url)
458-
else:
459-
# Info: When platform.txt, platform.local.txt, or IDE Tools board
460-
# settings are changed, our build path directory was cleaned. Note,
461-
# makecorever.py may have run before us and recreaded the directory.
462-
if not os.path.exists(build_path):
463-
os.makedirs(build_path)
464-
print_msg("Clean build, created dir " + build_path)
469+
if not os.path.exists(build_path_core):
470+
os.makedirs(build_path_core)
471+
print_msg("Clean build, created dir " + build_path_core)
465472

466473
if os.path.exists(source_globals_h_fqfn):
467474
print_msg("Using global defines from " + source_globals_h_fqfn)
@@ -478,6 +485,8 @@ def main():
478485
# that exactly matches the timestamp of "CommonHFile.h" in the
479486
# platform source tree, it owns the core cache. If not, or
480487
# "Sketch.ino.globals.h" has changed, rebuild core.
488+
# A non-zero file size for commonhfile_fqfn, means we have seen a
489+
# globals.h file before and workaround is active.
481490
if os.path.getsize(commonhfile_fqfn):
482491
if (os.path.getmtime(globals_h_fqfn) != os.path.getmtime(commonhfile_fqfn)):
483492
# Need to rebuild core.a

0 commit comments

Comments
 (0)