Skip to content

Commit 5d06855

Browse files
committed
Add SDL_MIXER feature for conditional audio mixing
This introduces ENABLE_SDL_MIXER feature flag to conditionally compile SDL2_mixer-dependent audio code. This allows SDL2 graphics support without the problematic SDL2_mixer library in emscripten builds. emscripten-ports/SDL2_mixer (archived in 2024) has unfixable compilation warnings in music_modplug.c. The port system enforces -sSTRICT -Werror which cannot be overridden.
1 parent f4e521d commit 5d06855

File tree

5 files changed

+97
-13
lines changed

5 files changed

+97
-13
lines changed

.github/workflows/main.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,14 @@ jobs:
283283
- name: Architecture test
284284
env:
285285
CC: ${{ steps.install_cc.outputs.cc }}
286-
LATEST_RELEASE: dummy
287286
run: |
287+
. .ci/common.sh
288+
export LATEST_RELEASE=$(download_with_headers "https://api.github.com/repos/sysprog21/rv32emu-prebuilt/releases" \
289+
"Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
290+
| grep '"tag_name"' \
291+
| grep "sail" \
292+
| head -n 1 \
293+
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
288294
.ci/riscv-tests.sh
289295
if: ${{ always() }}
290296

@@ -342,7 +348,11 @@ jobs:
342348
- uses: actions/checkout@v4
343349
- name: install-dependencies
344350
run: |
345-
brew install make dtc expect sdl2 sdl2_mixer bc e2fsprogs p7zip llvm@18 dcfldd
351+
brew install make dtc expect sdl2 bc e2fsprogs p7zip llvm@18 dcfldd
352+
brew install sdl2_mixer || {
353+
echo "Warning: sdl2_mixer installation failed, continuing without SDL_MIXER support"
354+
echo "ENABLE_SDL_MIXER=0" >> $GITHUB_ENV
355+
}
346356
.ci/riscv-toolchain-install.sh
347357
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
348358
echo "$(brew --prefix llvm@18)/bin" >> $GITHUB_PATH
@@ -489,8 +499,14 @@ jobs:
489499
- name: Architecture test
490500
env:
491501
CC: ${{ steps.install_cc.outputs.cc }}
492-
LATEST_RELEASE: dummy
493502
run: |
503+
. .ci/common.sh
504+
export LATEST_RELEASE=$(download_with_headers "https://api.github.com/repos/sysprog21/rv32emu-prebuilt/releases" \
505+
"Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
506+
| grep '"tag_name"' \
507+
| grep "sail" \
508+
| head -n 1 \
509+
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
494510
python3 -m venv venv
495511
. venv/bin/activate
496512
.ci/riscv-tests.sh

Makefile

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,58 @@ ENABLE_FULL4G ?= 0
199199

200200
# Experimental SDL oriented system calls
201201
ENABLE_SDL ?= 1
202-
ifneq ("$(CC_IS_EMCC)", "1") # note that emcc generates port SDL headers/library, so it does not requires system SDL headers/library
203-
ifeq ($(call has, SDL), 1)
204-
ifeq (, $(shell which sdl2-config))
202+
ENABLE_SDL_MIXER ?= 1
203+
204+
# Step 1: Detect SDL availability
205+
ifneq ("$(CC_IS_EMCC)", "1")
206+
# Native build: require sdl2-config for SDL support
207+
ifeq (, $(shell command -v sdl2-config))
205208
$(warning No sdl2-config in $$PATH. Check SDL2 installation in advance)
206209
override ENABLE_SDL := 0
210+
override ENABLE_SDL_MIXER := 0
211+
endif
212+
else
213+
# Emscripten build: SDL is available via ports, but SDL_MIXER has unfixable warnings
214+
# The emscripten-ports/SDL2_mixer was archived in Jan 2024 with compilation issues
215+
override ENABLE_SDL_MIXER := 0
216+
endif
217+
218+
# Enforce dependency: SDL_MIXER requires SDL
219+
ifeq ($(ENABLE_SDL), 0)
220+
override ENABLE_SDL_MIXER := 0
221+
endif
222+
223+
# Step 2: If SDL is enabled on native builds, check for SDL_MIXER
224+
ifneq ("$(CC_IS_EMCC)", "1")
225+
ifeq ($(ENABLE_SDL), 1)
226+
ifneq (, $(shell command -v pkg-config))
227+
ifneq (0, $(shell pkg-config --exists SDL2_mixer; echo $$?))
228+
$(warning No SDL2_mixer lib installed. SDL2_mixer support will be disabled)
229+
override ENABLE_SDL_MIXER := 0
230+
endif
231+
else
232+
$(warning No pkg-config found. Disabling SDL2_mixer support)
233+
override ENABLE_SDL_MIXER := 0
207234
endif
208-
ifeq (1, $(shell pkg-config --exists SDL2_mixer; echo $$?))
209-
$(warning No SDL2_mixer lib installed. Check SDL2_mixer installation in advance)
210-
override ENABLE_SDL := 0
211235
endif
212236
endif
237+
213238
$(call set-feature, SDL)
239+
$(call set-feature, SDL_MIXER)
214240
ifeq ($(call has, SDL), 1)
215241
OBJS_EXT += syscall_sdl.o
242+
ifneq ("$(CC_IS_EMCC)", "1")
216243
$(OUT)/syscall_sdl.o: CFLAGS += $(shell sdl2-config --cflags)
244+
endif
217245
# 4 GiB of memory is required to run video games.
218246
ENABLE_FULL4G := 1
247+
ifneq ("$(CC_IS_EMCC)", "1")
219248
LDFLAGS += $(shell sdl2-config --libs) -pthread
249+
ifeq ($(call has, SDL_MIXER), 1)
220250
LDFLAGS += $(shell pkg-config --libs SDL2_mixer)
221251
endif
222252
endif
253+
endif
223254

224255
# If SYSTEM is enabled and ELF_LOADER is not, then skip FULL4G bacause guestOS
225256
# has dedicated memory mapping range.

mk/wasm.mk

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ CFLAGS += -mtail-call
1818

1919
# Build emscripten-port SDL
2020
ifeq ($(call has, SDL), 1)
21-
# Disable STRICT mode to avoid -Werror in SDL2_mixer port compilation.
22-
# The emscripten-ports/SDL2_mixer was archived in Jan 2024 and has warnings
23-
# in music_modplug.c that become fatal errors under STRICT mode.
24-
CFLAGS_emcc += -sSTRICT=0 -sUSE_SDL=2 -sSDL2_MIXER_FORMATS=wav,mid -sUSE_SDL_MIXER=2
21+
CFLAGS_emcc += -sUSE_SDL=2
2522
OBJS_EXT += syscall_sdl.o
2623
LDFLAGS += -pthread
24+
# SDL_MIXER is disabled by default for emscripten due to archived port with warnings
25+
ifeq ($(call has, SDL_MIXER), 1)
26+
# Note: Enabling SDL_MIXER requires -sSTRICT=0 due to unfixable warnings in music_modplug.c
27+
# The emscripten-ports/SDL2_mixer was archived in Jan 2024
28+
CFLAGS_emcc += -sSTRICT=0 -sSDL2_MIXER_FORMATS=wav,mid -sUSE_SDL_MIXER=2
29+
endif
2730
endif
2831

2932
# More build flags

src/feature.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@
6767
#define RV32_FEATURE_SDL 1
6868
#endif
6969

70+
/* SDL2_mixer audio support (requires SDL) */
71+
#ifndef RV32_FEATURE_SDL_MIXER
72+
#define RV32_FEATURE_SDL_MIXER 1
73+
#endif
74+
75+
/* Enforce dependency: SDL_MIXER requires SDL */
76+
#if RV32_FEATURE_SDL_MIXER && !RV32_FEATURE_SDL
77+
#undef RV32_FEATURE_SDL_MIXER
78+
#define RV32_FEATURE_SDL_MIXER 0
79+
#endif
80+
7081
/* GDB remote debugging */
7182
#ifndef RV32_FEATURE_GDBSTUB
7283
#define RV32_FEATURE_GDBSTUB 0

src/syscall_sdl.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include <unistd.h>
1616

1717
#include <SDL.h>
18+
#if RV32_HAS(SDL_MIXER)
1819
#include <SDL_mixer.h>
20+
#endif
1921

2022
#include "riscv.h"
2123
#include "riscv_private.h"
@@ -92,6 +94,7 @@ typedef struct sound {
9294
} sound_t;
9395

9496
/* SDL-mixer-related and music-related variables */
97+
#if RV32_HAS(SDL_MIXER)
9598
static pthread_t music_thread;
9699
static uint8_t *music_midi_data;
97100
static Mix_Music *mid;
@@ -107,6 +110,7 @@ static int chan;
107110
static bool audio_init = false;
108111
static bool sfx_thread_init = false;
109112
static bool music_thread_init = false;
113+
#endif
110114

111115
typedef struct {
112116
void *data;
@@ -718,6 +722,7 @@ uint8_t *mus2midi(uint8_t *data, int *length)
718722
return midi_data;
719723
}
720724

725+
#if RV32_HAS(SDL_MIXER)
721726
static void *sfx_handler(void *arg)
722727
{
723728
sound_t *sfx = (sound_t *) arg;
@@ -916,7 +921,9 @@ static void set_music_volume(riscv_t *rv)
916921
/* multiplied by 8 because volume's max is 15 */
917922
Mix_VolumeMusic(volume * 8);
918923
}
924+
#endif /* RV32_HAS(SDL_MIXER) */
919925

926+
#if RV32_HAS(SDL_MIXER)
920927
static void init_audio(void)
921928
{
922929
if (!(SDL_WasInit(-1) & SDL_INIT_AUDIO)) {
@@ -977,13 +984,16 @@ static void shutdown_audio()
977984

978985
audio_init = sfx_thread_init = music_thread_init = false;
979986
}
987+
#endif
980988

981989
void sdl_video_audio_cleanup()
982990
{
983991
if (window) {
984992
SDL_DestroyWindow(window);
985993
window = NULL;
986994
}
995+
996+
#if RV32_HAS(SDL_MIXER)
987997
/*
988998
* The sfx_or_music_thread_init flag might not be set if a quick ctrl-c
989999
* occurs while the audio configuration is being initialized. Therefore,
@@ -992,6 +1002,7 @@ void sdl_video_audio_cleanup()
9921002
bool sfx_or_music_thread_init = sfx_thread_init | music_thread_init;
9931003
if (sfx_or_music_thread_init || (!sfx_or_music_thread_init && audio_init))
9941004
shutdown_audio();
1005+
#endif
9951006
SDL_Quit();
9961007
}
9971008

@@ -1002,10 +1013,14 @@ void syscall_setup_audio(riscv_t *rv)
10021013

10031014
switch (request) {
10041015
case INIT_AUDIO:
1016+
#if RV32_HAS(SDL_MIXER)
10051017
init_audio();
1018+
#endif
10061019
break;
10071020
case SHUTDOWN_AUDIO:
1021+
#if RV32_HAS(SDL_MIXER)
10081022
shutdown_audio();
1023+
#endif
10091024
break;
10101025
default:
10111026
rv_log_error("Unknown sound request: %d", request);
@@ -1020,16 +1035,24 @@ void syscall_control_audio(riscv_t *rv)
10201035

10211036
switch (request) {
10221037
case PLAY_MUSIC:
1038+
#if RV32_HAS(SDL_MIXER)
10231039
play_music(rv);
1040+
#endif
10241041
break;
10251042
case PLAY_SFX:
1043+
#if RV32_HAS(SDL_MIXER)
10261044
play_sfx(rv);
1045+
#endif
10271046
break;
10281047
case SET_MUSIC_VOLUME:
1048+
#if RV32_HAS(SDL_MIXER)
10291049
set_music_volume(rv);
1050+
#endif
10301051
break;
10311052
case STOP_MUSIC:
1053+
#if RV32_HAS(SDL_MIXER)
10321054
stop_music();
1055+
#endif
10331056
break;
10341057
default:
10351058
rv_log_error("Unknown sound control request: %d", request);

0 commit comments

Comments
 (0)