mkxp-z/subprojects/packagefiles/openal-soft-emscripten.patch
刘皓 4a730f5e8b
Use a newer version of OpenAL Soft in libretro builds
The Emscripten build was crashing because of some unaligned memory
access in OpenAL Soft. Hopefully this fixes it.
2025-01-31 17:28:34 -05:00

165 lines
5.2 KiB
Diff

# This patch applies C++ name mangling to all of OpenAL Soft's functions so that they don't conflict with the functions in Emscripten's OpenAL implementation.
# Otherwise, the Emscripten build crashes on startup because the Emscripten version of RetroArch also depends on OpenAL, and it will incorrectly try to use our OpenAL implementation instead of Emscripten's.
# Also stops OpenAL Soft's build system from unconditionally enabling the `-pthread` compiler flag when targeting Emscripten because it causes problems if the rest of mkxp-z isn't also building with pthread support.
# Also fixes a compilation error in althreads.h when targeting Emscripten without pthread support.
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -561,7 +561,10 @@ if(NOT WIN32)
message(FATAL_ERROR "PThreads is required for non-Windows builds!")
endif()
- check_c_compiler_flag(-pthread HAVE_PTHREAD)
+ string(TOLOWER ${CMAKE_SYSTEM_NAME} SYSTEM_LOWER)
+ if(NOT SYSTEM_LOWER STREQUAL "emscripten")
+ check_c_compiler_flag(-pthread HAVE_PTHREAD)
+ endif()
if(HAVE_PTHREAD)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -pthread")
set(C_FLAGS ${C_FLAGS} -pthread)
diff --git a/common/althreads.h b/common/althreads.h
--- a/common/althreads.h
+++ b/common/althreads.h
@@ -107,29 +107,57 @@ public:
#else
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+ void **mTss;
+#else
tss_t mTss{};
+#endif /* defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) */
public:
tss()
{
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+ if ((mTss = (void **)std::malloc(sizeof *mTss)) == NULL)
+ throw std::runtime_error{"al::tss::tss()"};
+ *mTss = nullptr;
+#else
if(int res{tss_create(&mTss, nullptr)}; res != thrd_success)
throw std::runtime_error{"al::tss::tss()"};
+#endif /* defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) */
}
explicit tss(const T &init) : tss{}
{
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+ if ((mTss = (void **)std::malloc(sizeof *mTss)) == NULL)
+ throw std::runtime_error{"al::tss::tss()"};
+ *mTss = to_ptr(init);
+#else
if(int res{tss_set(mTss, to_ptr(init))}; res != thrd_success)
throw std::runtime_error{"al::tss::tss(T)"};
+#endif /* defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) */
}
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+ ~tss() { std::free(mTss); }
+#else
~tss() { tss_delete(mTss); }
+#endif /* defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) */
void set(const T &value) const
{
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+ *mTss = to_ptr(value);
+#else
if(int res{tss_set(mTss, to_ptr(value))}; res != thrd_success)
throw std::runtime_error{"al::tss::set(T)"};
+#endif /* defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) */
}
[[nodiscard]]
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+ auto get() const noexcept -> T { return from_ptr(*mTss); }
+#else
auto get() const noexcept -> T { return from_ptr(tss_get(mTss)); }
+#endif /* defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) */
#endif /* _WIN32 */
tss(const tss&) = delete;
diff --git a/include/AL/al.h b/include/AL/al.h
--- a/include/AL/al.h
+++ b/include/AL/al.h
@@ -3,7 +3,7 @@
/* NOLINTBEGIN */
#ifdef __cplusplus
-extern "C" {
+
#ifdef _MSVC_LANG
#define AL_CPLUSPLUS _MSVC_LANG
@@ -700,7 +700,7 @@ typedef void (AL_APIENTRY *LPALSPEEDOFSOUND)(ALfloat value) AL_API_NOEX
typedef void (AL_APIENTRY *LPALDISTANCEMODEL)(ALenum distanceModel) AL_API_NOEXCEPT17;
#ifdef __cplusplus
-} /* extern "C" */
+
#endif
/* NOLINTEND */
diff --git a/include/AL/alc.h b/include/AL/alc.h
--- a/include/AL/alc.h
+++ b/include/AL/alc.h
@@ -3,7 +3,7 @@
/* NOLINTBEGIN */
#ifdef __cplusplus
-extern "C" {
+
#ifdef _MSVC_LANG
#define ALC_CPLUSPLUS _MSVC_LANG
@@ -300,7 +300,7 @@ typedef void (ALC_APIENTRY *LPALCCAPTURESTOP)(ALCdevice *device) ALC_A
typedef void (ALC_APIENTRY *LPALCCAPTURESAMPLES)(ALCdevice *device, ALCvoid *buffer, ALCsizei samples) ALC_API_NOEXCEPT17;
#ifdef __cplusplus
-} /* extern "C" */
+
#endif
/* NOLINTEND */
diff --git a/include/AL/alext.h b/include/AL/alext.h
--- a/include/AL/alext.h
+++ b/include/AL/alext.h
@@ -23,7 +23,7 @@ typedef uint64_t _alsoft_uint64_t;
#include "al.h"
#ifdef __cplusplus
-extern "C" {
+
#endif
struct _GUID;
@@ -1085,7 +1085,7 @@ ALenum AL_APIENTRY EAXGetBufferModeDirect(ALCcontext *context, ALuint buffer, AL
#endif
#ifdef __cplusplus
-}
+
#endif
/* NOLINTEND */
diff --git a/include/AL/efx.h b/include/AL/efx.h
--- a/include/AL/efx.h
+++ b/include/AL/efx.h
@@ -8,7 +8,7 @@
#include "al.h"
#ifdef __cplusplus
-extern "C" {
+
#endif
#define ALC_EXT_EFX_NAME "ALC_EXT_EFX"
@@ -757,7 +757,7 @@ AL_API void AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum par
#ifdef __cplusplus
-} /* extern "C" */
+
#endif
/* NOLINTEND */