Skip to content

Commit c6bc245

Browse files
committed
Add SDL_MIXER feature flag for conditional audio mixing support
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 c6bc245

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

.github/workflows/main.yml

Lines changed: 16 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,8 @@ 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 || echo "Warning: sdl2_mixer installation failed, continuing without SDL_MIXER support"
346353
.ci/riscv-toolchain-install.sh
347354
echo "${{ github.workspace }}/toolchain/bin" >> $GITHUB_PATH
348355
echo "$(brew --prefix llvm@18)/bin" >> $GITHUB_PATH
@@ -489,8 +496,14 @@ jobs:
489496
- name: Architecture test
490497
env:
491498
CC: ${{ steps.install_cc.outputs.cc }}
492-
LATEST_RELEASE: dummy
493499
run: |
500+
. .ci/common.sh
501+
export LATEST_RELEASE=$(download_with_headers "https://api.github.com/repos/sysprog21/rv32emu-prebuilt/releases" \
502+
"Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
503+
| grep '"tag_name"' \
504+
| grep "sail" \
505+
| head -n 1 \
506+
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
494507
python3 -m venv venv
495508
. venv/bin/activate
496509
.ci/riscv-tests.sh

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,39 @@ ENABLE_FULL4G ?= 0
199199

200200
# Experimental SDL oriented system calls
201201
ENABLE_SDL ?= 1
202+
ENABLE_SDL_MIXER ?= 1
202203
ifneq ("$(CC_IS_EMCC)", "1") # note that emcc generates port SDL headers/library, so it does not requires system SDL headers/library
203204
ifeq ($(call has, SDL), 1)
204205
ifeq (, $(shell which sdl2-config))
205206
$(warning No sdl2-config in $$PATH. Check SDL2 installation in advance)
206207
override ENABLE_SDL := 0
207208
endif
208209
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
210+
$(warning No SDL2_mixer lib installed. SDL2_mixer support will be disabled)
211+
override ENABLE_SDL_MIXER := 0
212+
endif
211213
endif
214+
else
215+
# Disable SDL_MIXER for emscripten builds to avoid SDL2_mixer port compilation issues
216+
# The emscripten-ports/SDL2_mixer was archived in Jan 2024 with unfixable warnings
217+
override ENABLE_SDL_MIXER := 0
212218
endif
213219
$(call set-feature, SDL)
220+
$(call set-feature, SDL_MIXER)
214221
ifeq ($(call has, SDL), 1)
215222
OBJS_EXT += syscall_sdl.o
223+
ifneq ("$(CC_IS_EMCC)", "1")
216224
$(OUT)/syscall_sdl.o: CFLAGS += $(shell sdl2-config --cflags)
225+
endif
217226
# 4 GiB of memory is required to run video games.
218227
ENABLE_FULL4G := 1
228+
ifneq ("$(CC_IS_EMCC)", "1")
219229
LDFLAGS += $(shell sdl2-config --libs) -pthread
230+
ifeq ($(call has, SDL_MIXER), 1)
220231
LDFLAGS += $(shell pkg-config --libs SDL2_mixer)
221232
endif
222233
endif
234+
endif
223235

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

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)