Skip to content

Commit 191966a

Browse files
authored
Support formats option in sdl2 mixer (#24158)
This PR adds `handle_options` function in the SDL2_mixer port so one can build SDL2_mixer with specific formats with embuilder, such as ``` embuilder build sdl2_mixer:formats:mid,ogg,mp3 ``` I found this syntax was added in #21345, but it was only applied to SDL2_image.
1 parent 2328a81 commit 191966a

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

test/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,7 @@ def test_sdl_get_key_name(self):
24692469
def test_sdl2_mixer_wav(self):
24702470
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['-sUSE_SDL_MIXER=2'], output_filename='a.out.js')
24712471
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['--use-port=sdl2_mixer'], output_filename='a.out.js')
2472+
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['--use-port=sdl2_mixer:formats=ogg'], output_filename='a.out.js')
24722473

24732474
def test_sdl2_linkable(self):
24742475
# Ensure that SDL2 can be built with LINKABLE. This implies there are no undefined

tools/ports/sdl2_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def needed(settings):
3434

3535

3636
def get_formats(settings):
37-
return set(settings.SDL2_IMAGE_FORMATS).union(opts['formats'])
37+
return opts['formats'].union(settings.SDL2_IMAGE_FORMATS)
3838

3939

4040
def get_lib_name(settings):

tools/ports/sdl2_mixer.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# found in the LICENSE file.
55

66
import os
7+
from typing import Dict, Set
78

89
TAG = 'release-2.8.0'
910
HASH = '494ccd74540f74e717f7e4f1dc7f96398c0f4b1883ab00c4a76b0c7239bd2c185cb4358a35ef47819c49e7c14dac7c37b98a29c7b5237478121571f5e7ac4dfc'
@@ -16,14 +17,28 @@
1617
'sdl2_mixer-none-mt': {'SDL2_MIXER_FORMATS': [], 'PTHREADS': 1},
1718
}
1819

20+
OPTIONS = {
21+
'formats': 'A comma separated list of formats (ex: --use-port=sdl2_mixer:formats=ogg,mp3)'
22+
}
23+
24+
SUPPORTED_FORMATS = {'ogg', 'mp3', 'mod', 'mid'}
25+
26+
# user options (from --use-port)
27+
opts: Dict[str, Set] = {
28+
'formats': set()
29+
}
30+
1931

2032
def needed(settings):
2133
return settings.USE_SDL_MIXER == 2
2234

2335

36+
def get_formats(settings):
37+
return opts['formats'].union(settings.SDL2_MIXER_FORMATS)
38+
39+
2440
def get_lib_name(settings):
25-
settings.SDL2_MIXER_FORMATS.sort()
26-
formats = '-'.join(settings.SDL2_MIXER_FORMATS)
41+
formats = '-'.join(sorted(get_formats(settings)))
2742

2843
libname = 'libSDL2_mixer'
2944
if formats != '':
@@ -41,30 +56,33 @@ def get(ports, settings, shared):
4156

4257
def create(final):
4358
source_path = ports.get_dir('sdl2_mixer', 'SDL_mixer-' + TAG)
59+
60+
formats = get_formats(settings)
61+
4462
flags = [
4563
'-sUSE_SDL=2',
4664
'-DMUSIC_WAV',
4765
]
4866

49-
if "ogg" in settings.SDL2_MIXER_FORMATS:
67+
if "ogg" in formats:
5068
flags += [
5169
'-sUSE_VORBIS',
5270
'-DMUSIC_OGG',
5371
]
5472

55-
if "mp3" in settings.SDL2_MIXER_FORMATS:
73+
if "mp3" in formats:
5674
flags += [
5775
'-sUSE_MPG123',
5876
'-DMUSIC_MP3_MPG123',
5977
]
6078

61-
if "mod" in settings.SDL2_MIXER_FORMATS:
79+
if "mod" in formats:
6280
flags += [
6381
'-sUSE_MODPLUG',
6482
'-DMUSIC_MOD_MODPLUG',
6583
]
6684

67-
if "mid" in settings.SDL2_MIXER_FORMATS:
85+
if "mid" in formats:
6886
flags += [
6987
'-DMUSIC_MID_TIMIDITY',
7088
]
@@ -107,16 +125,27 @@ def clear(ports, settings, shared):
107125

108126
def process_dependencies(settings):
109127
settings.USE_SDL = 2
110-
if "ogg" in settings.SDL2_MIXER_FORMATS:
128+
formats = get_formats(settings)
129+
if "ogg" in formats:
111130
deps.append('vorbis')
112131
settings.USE_VORBIS = 1
113-
if "mp3" in settings.SDL2_MIXER_FORMATS:
132+
if "mp3" in formats:
114133
deps.append('mpg123')
115134
settings.USE_MPG123 = 1
116-
if "mod" in settings.SDL2_MIXER_FORMATS:
135+
if "mod" in formats:
117136
deps.append('libmodplug')
118137
settings.USE_MODPLUG = 1
119138

120139

140+
def handle_options(options, error_handler):
141+
formats = options['formats'].split(',')
142+
for format in formats:
143+
format = format.lower().strip()
144+
if format not in SUPPORTED_FORMATS:
145+
error_handler(f'{format} is not a supported format')
146+
else:
147+
opts['formats'].add(format)
148+
149+
121150
def show():
122151
return 'sdl2_mixer (-sUSE_SDL_MIXER=2 or --use-port=sdl2_mixer; zlib license)'

0 commit comments

Comments
 (0)