Skip to content

Commit 26b1978

Browse files
authored
Add multi-threaded versions of sdl2_image and sdl2_ttf (#22946)
Because some possible dependencies of these libraries have `-mt` variants (specifically png and harfbuzz) we also needs to declare `-mt` variant of these libraries. The reason for this is a little complex: When we find the set of transitive dependencies of given library we select the correct variant of each library. This means that if we are building with `-pthread` then we select and include the `-mt` variants of all dependencies. However if libsdl2_image or libsdl2_ttf themselves then need to be built we end up building them without `-pthread` which means that emcc subprocesses will try to select and build the single threaded variants of the dependencies. This is mostly a serious problem because we don't allow for nested calls to emcc (we assume all dependencies have been already built before we try to build a given library and the we error out with `EM_CACHE_IS_LOCKED` if that is not the case). Testing this fix requires the cache to be setup just right so I'm not sure its worth it. Fixes: #22941, #20204
1 parent 2df6ad4 commit 26b1978

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

test/test_sanity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def test_embuilder_wildcards(self):
752752
def test_embuilder_with_use_port_syntax(self):
753753
restore_and_set_up()
754754
self.run_process([EMBUILDER, 'build', 'sdl2_image:formats=png,jpg', '--force'])
755-
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'libSDL2_image_jpg-png.a'))
755+
self.assertExists(os.path.join(config.CACHE, 'sysroot', 'lib', 'wasm32-emscripten', 'libSDL2_image-jpg-png.a'))
756756
self.assertContained('error building port `sdl2_image:formats=invalid` | invalid is not a supported format', self.do([EMBUILDER, 'build', 'sdl2_image:formats=invalid', '--force']))
757757

758758
def test_embuilder_external_ports(self):

tools/ports/sdl2_image.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
deps = ['sdl2']
1313
variants = {
14-
'sdl2_image_jpg': {'SDL2_IMAGE_FORMATS': ["jpg"]},
15-
'sdl2_image_png': {'SDL2_IMAGE_FORMATS': ["png"]},
14+
'sdl2_image-jpg': {'SDL2_IMAGE_FORMATS': ["jpg"]},
15+
'sdl2_image-png': {'SDL2_IMAGE_FORMATS': ["png"]},
16+
'sdl2_image-jpg-mt': {'SDL2_IMAGE_FORMATS': ["jpg"], 'PTHREADS': 1},
17+
'sdl2_image-png-mt': {'SDL2_IMAGE_FORMATS': ["png"], 'PTHREADS': 1},
1618
}
1719

1820
OPTIONS = {
@@ -41,7 +43,9 @@ def get_lib_name(settings):
4143

4244
libname = 'libSDL2_image'
4345
if formats != '':
44-
libname += '_' + formats
46+
libname += '-' + formats
47+
if settings.PTHREADS:
48+
libname += '-mt'
4549
return libname + '.a'
4650

4751

@@ -58,20 +62,23 @@ def create(final):
5862
IMG_tif.c IMG_xcf.c IMG_xpm.c IMG_xv.c IMG_webp.c IMG_ImageIO.m
5963
IMG_avif.c IMG_jxl.c IMG_svg.c IMG_qoi.c'''.split()
6064

61-
defs = ['-O2', '-sUSE_SDL=2', '-Wno-format-security']
65+
flags = ['-O2', '-sUSE_SDL=2', '-Wno-format-security']
6266

6367
formats = get_formats(settings)
6468

6569
for fmt in formats:
66-
defs.append('-DLOAD_' + fmt.upper())
70+
flags.append('-DLOAD_' + fmt.upper())
6771

6872
if 'png' in formats:
69-
defs += ['-sUSE_LIBPNG']
73+
flags += ['-sUSE_LIBPNG']
7074

7175
if 'jpg' in formats:
72-
defs += ['-sUSE_LIBJPEG']
76+
flags += ['-sUSE_LIBJPEG']
7377

74-
ports.build_port(src_dir, final, 'sdl2_image', flags=defs, srcs=srcs)
78+
if settings.PTHREADS:
79+
flags += ['-pthread']
80+
81+
ports.build_port(src_dir, final, 'sdl2_image', flags=flags, srcs=srcs)
7582

7683
return [shared.cache.get_lib(libname, create, what='port')]
7784

tools/ports/sdl2_ttf.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@
88

99
deps = ['freetype', 'sdl2', 'harfbuzz']
1010

11+
variants = {'sdl2_ttf-mt': {'PTHREADS': 1}}
12+
1113

1214
def needed(settings):
1315
return settings.USE_SDL_TTF == 2
1416

1517

18+
def get_lib_name(settings):
19+
return 'libSDL2_ttf' + ('-mt' if settings.PTHREADS else '') + '.a'
20+
21+
1622
def get(ports, settings, shared):
1723
ports.fetch_project('sdl2_ttf', f'https://github.com/libsdl-org/SDL_ttf/archive/{TAG}.zip', sha512hash=HASH)
1824

1925
def create(final):
2026
src_root = ports.get_dir('sdl2_ttf', 'SDL_ttf-' + TAG)
2127
ports.install_headers(src_root, target='SDL2')
2228
flags = ['-DTTF_USE_HARFBUZZ=1', '-sUSE_SDL=2', '-sUSE_FREETYPE', '-sUSE_HARFBUZZ']
29+
if settings.PTHREADS:
30+
flags += ['-pthread']
2331
ports.build_port(src_root, final, 'sdl2_ttf', flags=flags, srcs=['SDL_ttf.c'])
2432

25-
return [shared.cache.get_lib('libSDL2_ttf.a', create, what='port')]
33+
return [shared.cache.get_lib(get_lib_name(settings), create, what='port')]
2634

2735

2836
def clear(ports, settings, shared):
29-
shared.cache.erase_lib('libSDL2_ttf.a')
37+
shared.cache.erase_lib(get_lib_name(settings))
3038

3139

3240
def process_dependencies(settings):

0 commit comments

Comments
 (0)