Skip to content

Commit 4de40f9

Browse files
committed
Fix issue with transitive/recursive building of PIC libraries
This issue was introduces in #25522 when `-sRELOCATABLE` was removed from `get_base_cflags. This is a somewhat tricky to reproduce / understand issue with transitive library building: 1. User attempts to compile program using SDL2 + SDL2_GFX. 2. `emcc` attempts to builds all needed libraries in reverse dependency order. And ends up with the follows sequence: 1. libsdl2-pic 2. libsdl2_gfx-pic 3. `emcc` builds libsdl2-pic 4. `emcc` builds libsdl2_gfx-pic. The command that it constructs when building each source file looks like `emcc -fPIC gfx.c -sUSE_SDL=2`. However because neither `-sMAIN_MODULE` nor `-sRELOCATABLE` were specified this `emcc` command will not try to build the non-PIC version of libsdl2. At this point the compiler will crash with: `AssertionError: attempt to lock the cache while a parent process is holding the lock (sysroot/lib/wasm32-emscripten/libSDL2.a)` This issue is hard to reproduce because if libsdl2 is already built it will not trigger the issue.
1 parent 5da1ffa commit 4de40f9

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

test/test_sanity.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,12 @@ def test_embuilder_external_ports_options(self):
780780
self.run_process([EMBUILDER, 'build', f'{external_port_path}:dependency=sdl2', '--force'])
781781
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'lib_external-sdl2.a'))
782782

783+
def test_embuilder_transitive_pic(self):
784+
restore_and_set_up()
785+
self.run_process([EMBUILDER, 'clear', 'sdl2*'])
786+
self.run_process([EMBUILDER, '--pic', 'clear', 'sdl2*'])
787+
self.run_process([EMCC, '-sMAIN_MODULE=2', '-sUSE_SDL=2', '-sUSE_SDL_GFX=2', test_file('hello_world.c')])
788+
783789
def test_binaryen_version(self):
784790
restore_and_set_up()
785791
with open(EM_CONFIG, 'a') as f:

tools/system_libs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ def get_base_cflags(build_dir, force_object_files=False, preprocess=True):
6363
if settings.LTO and not force_object_files:
6464
flags += ['-flto=' + settings.LTO]
6565
if settings.RELOCATABLE or settings.MAIN_MODULE:
66-
flags += ['-fPIC']
66+
# Explictly include `-sRELOCATABLE` when building system libraries.
67+
# `-fPIC` alone is not enough to configure trigger the building and
68+
# caching of `pic` libraries (see `get_lib_dir` in `cache.py`)
69+
# FIXME(sbc): `-fPIC` should really be enough here.
70+
flags += ['-fPIC', '-sRELOCATABLE']
6771
if preprocess:
6872
flags += ['-DEMSCRIPTEN_DYNAMIC_LINKING']
6973
if settings.MEMORY64:

0 commit comments

Comments
 (0)