Skip to content

Commit bfec96b

Browse files
committed
Use single cache rather then reconfiguring/moving it
This was split out from the change to use a single sysroot (#13090). I think it cleaner this way: There is single cache, with single lock file and the root doesn't change, but the libraries live in sub-directories within the cache.
1 parent 91a177b commit bfec96b

26 files changed

+76
-88
lines changed

embuilder.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,9 @@ def main():
129129

130130
if args.lto:
131131
shared.Settings.LTO = "full"
132-
# Reconfigure the cache dir to reflect the change
133-
shared.reconfigure_cache()
134132

135133
if args.pic:
136134
shared.Settings.RELOCATABLE = 1
137-
# Reconfigure the cache dir to reflect the change
138-
shared.reconfigure_cache()
139135

140136
if args.force:
141137
force = True

emcc.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,10 +1331,6 @@ def filter_out_duplicate_dynamic_libs(inputs):
13311331
'_emscripten_stack_get_end',
13321332
'_emscripten_stack_set_limits']
13331333

1334-
# Reconfigure the cache now that settings have been applied. Some settings
1335-
# such as LTO and SIDE_MODULE/MAIN_MODULE effect which cache directory we use.
1336-
shared.reconfigure_cache()
1337-
13381334
if not compile_only and not options.post_link:
13391335
ldflags = shared.emsdk_ldflags(newargs)
13401336
for f in ldflags:

tests/test_sanity.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from tools.shared import EXPECTED_LLVM_VERSION, Cache
2222
from tools import shared, system_libs, utils
2323

24-
SANITY_FILE = shared.Cache.get_path('sanity.txt', root=True)
24+
SANITY_FILE = shared.Cache.get_path('sanity.txt')
2525
commands = [[EMCC], [PYTHON, path_from_root('tests', 'runner.py'), 'blahblah']]
2626

2727

@@ -411,31 +411,29 @@ def ensure_cache(self):
411411
self.do([EMCC, '-O2', path_from_root('tests', 'hello_world.c')])
412412

413413
def test_emcc_caching(self):
414-
BUILDING_MESSAGE = 'generating system library: X'
414+
BUILDING_MESSAGE = 'generating system library: %s'
415415

416416
restore_and_set_up()
417417
self.erase_cache()
418418

419419
# Building a file that *does* need something *should* trigger cache
420420
# generation, but only the first time
421-
libname = 'libc++'
421+
libname = Cache.get_lib_name('libc++.a')
422422
for i in range(3):
423423
print(i)
424424
self.clear()
425425
output = self.do([EMCC, '-O' + str(i), path_from_root('tests', 'hello_libcxx.cpp'), '-s', 'DISABLE_EXCEPTION_CATCHING=0'])
426426
print('\n\n\n', output)
427-
self.assertContainedIf(BUILDING_MESSAGE.replace('X', libname), output, i == 0)
427+
self.assertContainedIf(BUILDING_MESSAGE % libname, output, i == 0)
428428
self.assertContained('hello, world!', self.run_js('a.out.js'))
429429
self.assertExists(Cache.dirname)
430-
full_libname = libname + '.bc' if libname != 'libc++' else libname + '.a'
431-
self.assertExists(os.path.join(Cache.dirname, full_libname))
430+
self.assertExists(os.path.join(Cache.dirname, libname))
432431

433432
def test_cache_clearing_manual(self):
434433
# Manual cache clearing
435434
restore_and_set_up()
436435
self.ensure_cache()
437436
self.assertTrue(os.path.exists(Cache.dirname))
438-
self.assertTrue(os.path.exists(Cache.root_dirname))
439437
output = self.do([EMCC, '--clear-cache'])
440438
self.assertIn('clearing cache', output)
441439
self.assertIn(SANITY_MESSAGE, output)
@@ -459,12 +457,10 @@ def test_FROZEN_CACHE(self):
459457
self.erase_cache()
460458
self.ensure_cache()
461459
self.assertTrue(os.path.exists(Cache.dirname))
462-
self.assertTrue(os.path.exists(Cache.root_dirname))
463460
# changing config file should not clear cache
464461
add_to_config('FROZEN_CACHE = True')
465462
self.do([EMCC])
466463
self.assertTrue(os.path.exists(Cache.dirname))
467-
self.assertTrue(os.path.exists(Cache.root_dirname))
468464
# building libraries is disallowed
469465
output = self.do([EMBUILDER, 'build', 'libemmalloc'])
470466
self.assertIn('FROZEN_CACHE disallows building system libs', output)
@@ -483,6 +479,7 @@ def test_emcc_multiprocess_cache_access(self):
483479
}
484480
''')
485481
cache_dir_name = self.in_dir('test_cache')
482+
libname = Cache.get_lib_name('libc.a')
486483
with env_modify({'EM_CACHE': cache_dir_name}):
487484
tasks = []
488485
num_times_libc_was_built = 0
@@ -491,13 +488,13 @@ def test_emcc_multiprocess_cache_access(self):
491488
tasks += [p]
492489
for p in tasks:
493490
print('stdout:\n', p.stdout)
494-
if 'generating system library: libc' in p.stdout:
491+
if 'generating system library: ' + libname in p.stdout:
495492
num_times_libc_was_built += 1
496493

497494
# The cache directory must exist after the build
498495
self.assertTrue(os.path.exists(cache_dir_name))
499496
# The cache directory must contain a built libc
500-
self.assertTrue(os.path.exists(os.path.join(cache_dir_name, 'wasm', 'libc.a')))
497+
self.assertTrue(os.path.exists(os.path.join(cache_dir_name, libname)))
501498
# Exactly one child process should have triggered libc build!
502499
self.assertEqual(num_times_libc_was_built, 1)
503500

@@ -531,12 +528,11 @@ def test_emcc_ports(self):
531528
restore_and_set_up()
532529

533530
# listing ports
534-
535531
out = self.do([EMCC, '--show-ports'])
536-
assert 'Available ports:' in out, out
537-
assert 'SDL2' in out, out
538-
assert 'SDL2_image' in out, out
539-
assert 'SDL2_net' in out, out
532+
self.assertContained('Available ports:', out)
533+
self.assertContained('SDL2', out)
534+
self.assertContained('SDL2_image', out)
535+
self.assertContained('SDL2_net', out)
540536

541537
# using ports
542538
RETRIEVING_MESSAGE = 'retrieving port'
@@ -551,27 +547,27 @@ def test_emcc_ports(self):
551547
try_delete(PORTS_DIR)
552548
else:
553549
self.do([EMCC, '--clear-ports'])
554-
assert not os.path.exists(PORTS_DIR)
550+
self.assertNotExists(PORTS_DIR)
555551

556552
# Building a file that doesn't need ports should not trigger anything
557553
output = self.do([EMCC, path_from_root('tests', 'hello_world_sdl.cpp')])
558554
assert RETRIEVING_MESSAGE not in output, output
559555
assert BUILDING_MESSAGE not in output
560556
print('no', output)
561-
assert not os.path.exists(PORTS_DIR)
557+
self.assertNotExists(PORTS_DIR)
562558

563559
def first_use():
564560
output = self.do([EMCC, path_from_root('tests', 'hello_world_sdl.cpp'), '-s', 'USE_SDL=2'])
565-
assert RETRIEVING_MESSAGE in output, output
566-
assert BUILDING_MESSAGE in output, output
561+
self.assertContained(RETRIEVING_MESSAGE, output)
562+
self.assertContained(BUILDING_MESSAGE, output)
567563
self.assertExists(PORTS_DIR)
568564
print('yes', output)
569565

570566
def second_use():
571567
# Using it again avoids retrieve and build
572568
output = self.do([EMCC, path_from_root('tests', 'hello_world_sdl.cpp'), '-s', 'USE_SDL=2'])
573-
assert RETRIEVING_MESSAGE not in output, output
574-
assert BUILDING_MESSAGE not in output, output
569+
self.assertNotContained(RETRIEVING_MESSAGE, output)
570+
self.assertNotContained(BUILDING_MESSAGE, output)
575571

576572
# Building a file that need a port does trigger stuff
577573
first_use()

tools/cache.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,9 @@ class Cache:
2424
# acquired.
2525
EM_EXCLUSIVE_CACHE_ACCESS = int(os.environ.get('EM_EXCLUSIVE_CACHE_ACCESS', '0'))
2626

27-
def __init__(self, dirname, use_subdir=True):
27+
def __init__(self, dirname):
2828
# figure out the root directory for all caching
2929
dirname = os.path.normpath(dirname)
30-
self.root_dirname = dirname
31-
32-
# if relevant, use a subdir of the cache
33-
if use_subdir:
34-
subdir = 'wasm'
35-
if shared.Settings.LTO:
36-
subdir += '-lto'
37-
if shared.Settings.RELOCATABLE:
38-
subdir += '-pic'
39-
if shared.Settings.MEMORY64:
40-
subdir += '-memory64'
41-
dirname = os.path.join(dirname, subdir)
42-
4330
self.dirname = dirname
4431
self.acquired_count = 0
4532

@@ -90,28 +77,46 @@ def ensure(self):
9077

9178
def erase(self):
9279
with self.lock():
93-
if os.path.exists(self.root_dirname):
94-
for f in os.listdir(self.root_dirname):
95-
tempfiles.try_delete(os.path.join(self.root_dirname, f))
80+
if os.path.exists(self.dirname):
81+
for f in os.listdir(self.dirname):
82+
tempfiles.try_delete(os.path.join(self.dirname, f))
83+
84+
def get_path(self, name):
85+
return os.path.join(self.dirname, name)
86+
87+
def get_include_dir(self):
88+
return os.path.join(self.dirname, 'include')
9689

97-
def get_path(self, shortname, root=False):
98-
if root:
99-
return os.path.join(self.root_dirname, shortname)
100-
return os.path.join(self.dirname, shortname)
90+
def get_lib_dir(self):
91+
subdir = 'wasm'
92+
if shared.Settings.LTO:
93+
subdir += '-lto'
94+
if shared.Settings.RELOCATABLE:
95+
subdir += '-pic'
96+
if shared.Settings.MEMORY64:
97+
subdir += '-memory64'
98+
return subdir
99+
100+
def get_lib_name(self, name):
101+
return os.path.join(self.get_lib_dir(), name)
102+
103+
def erase_lib(self, name):
104+
self.erase_file(self.get_lib_name(name))
101105

102106
def erase_file(self, shortname):
103107
name = os.path.join(self.dirname, shortname)
104108
if os.path.exists(name):
105109
logging.info('Cache: deleting cached file: %s', name)
106110
tempfiles.try_delete(name)
107111

112+
def get_lib(self, libname, *args, **kwargs):
113+
name = self.get_lib_name(libname)
114+
return self.get(name, *args, **kwargs)
115+
108116
# Request a cached file. If it isn't in the cache, it will be created with
109117
# the given creator function
110-
def get(self, shortname, creator, what=None, force=False, root=False):
111-
if root:
112-
cachename = os.path.join(self.root_dirname, shortname)
113-
else:
114-
cachename = os.path.join(self.dirname, shortname)
118+
def get(self, shortname, creator, what=None, force=False):
119+
cachename = os.path.join(self.dirname, shortname)
115120
cachename = os.path.abspath(cachename)
116121
# Check for existence before taking the lock in case we can avoid the
117122
# lock completely.

tools/ports/boost_headers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def create():
4848
ports.create_lib(final, o_s)
4949
return final
5050

51-
return [shared.Cache.get(libname, create, what='port')]
51+
return [shared.Cache.get_lib(libname, create, what='port')]
5252

5353

5454
def clear(ports, settings, shared):

tools/ports/bullet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def create():
5050
ports.build_port(src_path, final, includes=includes, exclude_dirs=['MiniCL'])
5151
return final
5252

53-
return [shared.Cache.get(libname, create)]
53+
return [shared.Cache.get_lib(libname, create)]
5454

5555

5656
def clear(ports, settings, shared):

tools/ports/bzip2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def create():
4747
ports.install_headers(source_path)
4848
return final
4949

50-
return [shared.Cache.get('libbz2.a', create, what='port')]
50+
return [shared.Cache.get_lib('libbz2.a', create, what='port')]
5151

5252

5353
def clear(ports, settings, shared):

tools/ports/cocos2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def create():
7474

7575
return final
7676

77-
return [shared.Cache.get(libname, create, what='port')]
77+
return [shared.Cache.get_lib(libname, create, what='port')]
7878

7979

8080
def clear(ports, settings, shared):

tools/ports/freetype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def create():
109109
target=os.path.join('freetype2', 'freetype'))
110110
return final
111111

112-
return [shared.Cache.get('libfreetype.a', create, what='port')]
112+
return [shared.Cache.get_lib('libfreetype.a', create, what='port')]
113113

114114

115115
def clear(ports, settings, shared):

tools/ports/harfbuzz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def create():
3232
source_path = os.path.join(ports.get_dir(), 'harfbuzz', 'harfbuzz-' + TAG)
3333
dest_path = os.path.join(ports.get_build_dir(), 'harfbuzz')
3434

35-
freetype_lib = shared.Cache.get_path('libfreetype.a')
35+
freetype_lib = shared.Cache.get_path(shared.Cache.get_lib_name('libfreetype.a'))
3636
freetype_include = os.path.join(ports.get_include_dir(), 'freetype2', 'freetype')
3737
freetype_include_dirs = freetype_include + ';' + os.path.join(freetype_include, 'config')
3838

@@ -67,7 +67,7 @@ def create():
6767

6868
return os.path.join(dest_path, 'libharfbuzz.a')
6969

70-
return [shared.Cache.get(get_lib_name(settings), create, what='port')]
70+
return [shared.Cache.get_lib(get_lib_name(settings), create, what='port')]
7171

7272

7373
def clear(ports, settings, shared):

tools/ports/icu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def create():
3636
ports.install_header_dir(os.path.join(dest_path, 'source', 'common', 'unicode'))
3737
return final
3838

39-
return [shared.Cache.get(libname, create)]
39+
return [shared.Cache.get_lib(libname, create)]
4040

4141

4242
def clear(ports, settings, shared):

tools/ports/libjpeg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create():
4343
)
4444
return final
4545

46-
return [shared.Cache.get(libname, create, what='port')]
46+
return [shared.Cache.get_lib(libname, create, what='port')]
4747

4848

4949
def clear(ports, settings, shared):

tools/ports/libpng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def create():
3838
ports.build_port(dest_path, final, flags=['-s', 'USE_ZLIB=1'], exclude_files=['pngtest'], exclude_dirs=['scripts', 'contrib'])
3939
return final
4040

41-
return [shared.Cache.get(libname, create, what='port')]
41+
return [shared.Cache.get_lib(libname, create, what='port')]
4242

4343

4444
def clear(ports, settings, shared):

tools/ports/mpg123.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def create():
9393
ports.install_headers(libmpg123_path, pattern="*123.h", target='')
9494
return output_path
9595

96-
return [shared.Cache.get(libname, create, what='port')]
96+
return [shared.Cache.get_lib(libname, create, what='port')]
9797

9898

9999
def clear(ports, settings, shared):

tools/ports/ogg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def create():
3939
ports.build_port(os.path.join(dest_path, 'src'), final)
4040
return final
4141

42-
return [shared.Cache.get(libname, create)]
42+
return [shared.Cache.get_lib(libname, create)]
4343

4444

4545
def clear(ports, settings, shared):

tools/ports/regal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def create():
138138
ports.create_lib(final, o_s)
139139
return final
140140

141-
return [shared.Cache.get(get_lib_name(settings), create, what='port')]
141+
return [shared.Cache.get_lib(get_lib_name(settings), create, what='port')]
142142

143143

144144
def clear(ports, settings, shared):

tools/ports/sdl2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ def create():
8686
ports.create_lib(final, o_s)
8787
return final
8888

89-
return [shared.Cache.get(libname, create, what='port')]
89+
return [shared.Cache.get_lib(libname, create, what='port')]
9090

9191

9292
def clear(ports, settings, shared):
93-
shared.Cache.erase_file(get_lib_name(settings))
93+
shared.Cache.erase_lib(get_lib_name(settings))
9494

9595

9696
def process_dependencies(settings):

tools/ports/sdl2_gfx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def create():
3535
ports.install_headers(source_path, target='SDL2')
3636
return final
3737

38-
return [shared.Cache.get(libname, create)]
38+
return [shared.Cache.get_lib(libname, create)]
3939

4040

4141
def clear(ports, settings, shared):

tools/ports/sdl2_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def create():
5555
ports.create_lib(final, o_s)
5656
return final
5757

58-
return [shared.Cache.get(libname, create, what='port')]
58+
return [shared.Cache.get_lib(libname, create, what='port')]
5959

6060

6161
def clear(ports, settings, shared):

tools/ports/sdl2_mixer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def create():
7676
ports.install_headers(source_path, pattern='SDL_*.h', target='SDL2')
7777
return final
7878

79-
return [shared.Cache.get(libname, create, what='port')]
79+
return [shared.Cache.get_lib(libname, create, what='port')]
8080

8181

8282
def clear(ports, settings, shared):

0 commit comments

Comments
 (0)