Skip to content

Commit f956130

Browse files
committed
Add multi-threaded versions of sdl2_image and sdl2_ttf
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 d4687ce commit f956130

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@
88

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

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

1213
def needed(settings):
1314
return settings.USE_SDL_TTF == 2
1415

1516

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

1924
def create(final):
2025
src_root = ports.get_dir('sdl2_ttf', 'SDL_ttf-' + TAG)
2126
ports.install_headers(src_root, target='SDL2')
2227
flags = ['-DTTF_USE_HARFBUZZ=1', '-sUSE_SDL=2', '-sUSE_FREETYPE', '-sUSE_HARFBUZZ']
28+
if settings.PTHREADS:
29+
flags += ['-pthread']
2330
ports.build_port(src_root, final, 'sdl2_ttf', flags=flags, srcs=['SDL_ttf.c'])
2431

2532
return [shared.cache.get_lib('libSDL2_ttf.a', create, what='port')]

0 commit comments

Comments
 (0)