Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Emscripten) Modularize the JavaScript and clean up the web build #15688

Merged
merged 9 commits into from
Nov 2, 2023

Conversation

JoeOsborn
Copy link
Contributor

@JoeOsborn JoeOsborn commented Sep 11, 2023

This adds the -s MODULARIZE flag and some related flags to emscripten, and updates libretro.js and canvas targeting in C code to account for the fact that there is no longer a global Module object.

I want this patch for two reasons:

  1. To ease the use of RA in web applications that use bundlers, and generally to make the script more of a good citizen and not pollute the global namespace
  2. To allow embedding multiple instances of RA on a single page without iframes, for purposes like comparing rom hacks side-by-side.

@JoeOsborn JoeOsborn force-pushed the modularize-emscripten branch 2 times, most recently from 0e340c5 to 1b93c25 Compare September 13, 2023 22:27
@JoeOsborn
Copy link
Contributor Author

I rebased this onto #15695, since it depends on that change to work. If this is merged #15695 can be closed.

The current status of this patch is that it all works great, except on CI where the emsdk version is very old.

@RobLoach
Copy link
Member

This is looking great! So happy to have more people involved on emscripten. Looks like the github actions build is failing....


LD retroarch.js
shared:ERROR: Attempt to set a non-existent setting: 'STACK_SIZE'
 - did you mean one of ASYNCIFY_STACK_SIZE, WASM_TABLE_SIZE?
 - perhaps a typo in emcc's  -s X=Y  notation?
 - (see src/settings.js for valid values)
make: *** [retroarch.js] Error 1
Makefile.emscripten:157: recipe for target 'retroarch.js' failed

@JoeOsborn
Copy link
Contributor Author

Yes, this is because the emsdk version on the emscripten build container from gitlab is ancient. I’m using builds made from a modern emsdk and they seem fine. A bit more discussion is on the standalone build fix PR, but that’s included in this PR already: #15695

@JoeOsborn
Copy link
Contributor Author

There's a bug with the EXPORT_ES6 part which is due to the use of strict mode, I'll investigate.

@JoeOsborn
Copy link
Contributor Author

Fixed the mentioned bug, it prevented (among other things) fast-forward from working.

@JoeOsborn
Copy link
Contributor Author

This one also builds in the new container 👍 thanks!

@RobLoach RobLoach changed the title Modularize emscripten (Emscripten) Modularize the JavaScript and clean up the web build Sep 30, 2023
Copy link
Member

@RobLoach RobLoach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks so much. Happy to have more focus on the web build 👍

@JoeOsborn
Copy link
Contributor Author

Looks great! This one subsumes #15695 so if this is merged we can close that one.

@RobLoach
Copy link
Member

RobLoach commented Oct 2, 2023

@JoeOsborn Ran into some build errors on some cores after upgrading emscripten. Do you happen to know what's wrong with it?
https://git.libretro.com/libretro/beetle-lynx-libretro/-/jobs/4179483

@JoeOsborn
Copy link
Contributor Author

JoeOsborn commented Oct 2, 2023

My guess is something to do with symbol mangling if some cores are in C++ and some are in C, but that’s a wild guess. I’ll test tomorrow with the libretro super repo and try to compile that core.

Is it weird that beetle-vb works but beetle-lynx doesn’t?

@JoeOsborn
Copy link
Contributor Author

JoeOsborn commented Oct 2, 2023

The issue was that the lynx core was using em++ to combine objects into an archive file where it should have used emar, the patch to libretro-mednafen_lynx is like so (same for libretro-mednafen_pcfx, libretro-mednafen_vb, mednafen_supergrafx, ngp, pce, wswan):

diff --git a/Makefile b/Makefile
index 6e620df..2ab54fa 100644
--- a/Makefile
+++ b/Makefile
@@ -623,9 +623,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) -o $@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS)

For libretro-atari800:

diff --git a/Makefile b/Makefile
index f70ed3f..16e4781 100644
--- a/Makefile
+++ b/Makefile
@@ -370,6 +370,7 @@ else ifeq ($(platform), miyoo)
 # emscripten
 else ifeq ($(platform), emscripten)
        TARGET := $(TARGET_NAME)_libretro_emscripten.bc
+       STATIC_LINKING=1
 # cross Windows
 else ifeq ($(platform), wincross64)
        TARGET := $(TARGET_NAME)_libretro.dll

libretro-bsnes_performance:

diff --git a/Makefile b/Makefile
index 4d22b87a..f74b228a 100644
--- a/Makefile
+++ b/Makefile
@@ -391,8 +391,10 @@ else ifneq (,$(findstring windows_msvc2017,$(platform)))

 # Emscripten
 else ifeq ($(platform), emscripten)
-   TARGET := $(TARGET_NAME)_libretro_$(platform).bc
-   STATIC_LINKING = 1
+       TARGET := $(TARGET_NAME)_libretro_$(platform).bc
+       STATIC_LINKING = 1
+       CFLAGS += -D__linux__
+       CXXFLAGS += -D__linux__

 # Windows MSVC 2005 x86
 else ifeq ($(platform), windows_msvc2005_x86)
@@ -528,9 +530,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

libretro-81:

diff --git a/Makefile.libretro b/Makefile.libretro
index 165094b..e2e24c1 100644
--- a/Makefile.libretro
+++ b/Makefile.libretro
@@ -415,6 +415,7 @@ else ifneq (,$(findstring armv,$(platform)))
 # emscripten
 else ifeq ($(platform), emscripten)
        TARGET := $(TARGET_NAME)_libretro_$(platform).bc
+    STATIC_LINKING = 1

 # CTR (3DS)
 else ifeq ($(platform), ctr)
@@ -465,7 +467,7 @@ ifeq ($(DEBUG), 1)
        CXXFLAGS += -O0 -g
 else ifeq ($(platform), emscripten)
        CFLAGS += -O2
-       CXXFLAGS += -O2 -fno-exceptions -fno-rtti -DHAVE_STDINT_H
+       CXXFLAGS += -O2 -fno-exceptions -fno-rtti -DHAVE_STDINT_H -Dregister=""
 else ifeq ($(platform), retrofw)
        CFLAGS += -Ofast
        CXXFLAGS += -Ofast -fno-exceptions -fno-rtti -DHAVE_STDINT_H

libretro-bsnes_accuracy:

diff --git a/Makefile b/Makefile
index 4d22b87a..a473d087 100644
--- a/Makefile
+++ b/Makefile
@@ -393,6 +393,8 @@ else ifneq (,$(findstring windows_msvc2017,$(platform)))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_$(platform).bc
    STATIC_LINKING = 1
+   CFLAGS += -D__linux__
+   CXXFLAGS += -D__linux__

 # Windows MSVC 2005 x86
 else ifeq ($(platform), windows_msvc2005_x86)
@@ -528,9 +530,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

libretro-bsnes_balanced:

diff --git a/Makefile b/Makefile
index 4d22b87a..a473d087 100644
--- a/Makefile
+++ b/Makefile
@@ -393,6 +393,8 @@ else ifneq (,$(findstring windows_msvc2017,$(platform)))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_$(platform).bc
    STATIC_LINKING = 1
+   CFLAGS += -D__linux__
+   CXXFLAGS += -D__linux__

 # Windows MSVC 2005 x86
 else ifeq ($(platform), windows_msvc2005_x86)
@@ -528,9 +530,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

libretro-bsnes_cplusplus98:

diff --git a/Makefile b/Makefile
index 1731a75..3f650cf 100644
--- a/Makefile
+++ b/Makefile
@@ -386,6 +386,8 @@ else ifneq (,$(findstring windows_msvc2017,$(platform)))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_$(platform).bc
    STATIC_LINKING = 1
+   CFLAGS += -D__linux__
+   CXXFLAGS += -D__linux__

 # Windows MSVC 2005 x86
 else ifeq ($(platform), windows_msvc2005_x86)
@@ -521,9 +523,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

libretro-bsnes:

diff --git a/Makefile b/Makefile
index ff09da62..4cb6ce17 100644
--- a/Makefile
+++ b/Makefile
@@ -363,6 +363,8 @@ else ifneq (,$(findstring windows_msvc2017,$(platform)))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_$(platform).bc
    STATIC_LINKING = 1
+   CFLAGS += -D__linux__
+   CXXFLAGS += -D__linux__

 # Windows MSVC 2005 x86
 else ifeq ($(platform), windows_msvc2005_x86)
@@ -491,9 +493,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

This is getting kind of repetitive, all the bsnes/mercury cores need the same Makefile changes.

libretro-freechaf:

diff --git a/Makefile b/Makefile
index 5e89fa7..cc72f13 100644
--- a/Makefile
+++ b/Makefile
@@ -275,6 +275,7 @@ else ifeq ($(platform), emscripten)
        TARGET := $(TARGET_NAME)_libretro_$(platform).bc
        fpic := -fPIC
        SHARED := -shared -r
+       AR=emar
        STATIC_LINKING=1

 # PS2
@@ -582,9 +583,7 @@ endif
 all: $(TARGET)

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(LD) $(fpic) $(SHARED) $(LDFLAGS) $(LINKOUT)$@ $(OBJECTS) $(LIBS)
-else ifeq ($(STATIC_LINKING),1)
+ifeq ($(STATIC_LINKING),1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(fpic) $(SHARED) $(LDFLAGS) $(LINKOUT)$@ $(OBJECTS) $(LIBS)

libretro-cannonball:

diff --git a/Makefile b/Makefile
index f677020..3f978a3 100644
--- a/Makefile
+++ b/Makefile
@@ -266,6 +266,9 @@ else ifneq (,$(filter $(platform), ngc wii wiiu))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_$(platform).bc
    STATIC_LINKING = 1
+   CFLAGS += --std=c++11
+   CXXFLAGS += --std=c++11
+

 # GCW0 (OpenDingux and OpenDingux Beta)
 else ifeq ($(platform), gcw0)
@@ -574,9 +577,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

mame2000:

diff --git a/Makefile b/Makefile
index 57163e4..64d8115 100644
--- a/Makefile
+++ b/Makefile
@@ -309,6 +309,7 @@ else ifeq ($(platform), emscripten)
    DISABLE_ERROR_LOGGING := 1
    STATIC_LINKING := 1
    WANT_LIBCO := 0
+   CFLAGS += -Wno-int-conversion

 # GCW0
 else ifeq ($(platform), gcw0)
@@ -569,9 +570,7 @@ endif
 all: $(TARGET)

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CC) $(CFLAGS) $(OBJECTS) $(OBJOUT)$@
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(SHARED) $(LDFLAGS) $(OBJECTS) $(LIBS) $(LINKOUT)$@

mrboom:

diff --git a/Makefile b/Makefile
index 284023a..99b7653 100644
--- a/Makefile
+++ b/Makefile
@@ -188,6 +188,7 @@ else ifneq (,$(findstring qnx,$(platform)))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_emscripten.bc
    fpic := -fPIC
+   AR = emar
    SHARED := -shared
    STATIC_LINKING := 1
    CFLAGS += -DNO_NETWORK
@@ -741,9 +742,7 @@ endif
 all: $(TARGET)

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(fpic) -r $(SHARED) $(INCLUDES) -o $@ $(OBJECTS) $(LDFLAGS)
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else ifeq ($(platform),genode)
        $(LD) -o $@ $(OBJECTS) $(LDFLAGS)

neocd:

diff --git a/Makefile b/Makefile
index 4b918c6..015caac 100644
--- a/Makefile
+++ b/Makefile
@@ -141,6 +141,7 @@ else ifneq (,$(findstring qnx,$(platform)))
    SHARED := -shared -Wl,--version-script=$(CORE_DIR)/link.T -Wl,--no-undefined
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_emscripten.bc
+   AR=emar
    fpic := -fPIC
    SHARED :=
    CFLAGS += -DSYNC_CDROM=1
@@ -245,7 +246,7 @@ ifneq ($(GIT_VERSION)," unknown")
 endif

 CFLAGS += -DHAVE_ZLIB -D_7ZIP_ST -DHAVE_FLAC -DUSE_LIBRETRO_VFS
-CXXFLAGS += -std=c++11 -fno-exceptions -fno-rtti -DUSE_LIBRETRO_VFS
+CXXFLAGS += -std=c++14 -fno-exceptions -fno-rtti -DUSE_LIBRETRO_VFS

 include Makefile.common

@@ -257,10 +258,7 @@ CXXFLAGS += -Wall -D__LIBRETRO__ $(fpic) $(INCFLAGS)
 all: $(TARGET)

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       @$(if $(Q), $(shell echo echo LD $@),)
-       $(CXX) $(fpic) -r $(SHARED) -o $@ $(OBJECTS) $(LIBS) $(LDFLAGS)
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        @$(if $(Q), $(shell echo echo LD $@),)
diff --git a/src/audiobuffer.h b/src/audiobuffer.h
index e491479..ef94911 100644
--- a/src/audiobuffer.h
+++ b/src/audiobuffer.h
@@ -20,7 +20,7 @@ public:
     static constexpr double SAMPLES_PER_FRAME = static_cast<double>(SAMPLE_RATE) / Timer::FRAME_RATE;

     /// The audio buffer size, enough for one frame of audio.
-    static constexpr uint32_t CD_BUFFER_SIZE = round<int32_t>(SAMPLES_PER_FRAME + 1.0);
+    static constexpr uint32_t CD_BUFFER_SIZE = round<uint32_t, double>(SAMPLES_PER_FRAME + 1.0);

     /// We may occasionally generate one or two extra samples because of a long instruction (like DIVU)
     static constexpr uint32_t YM_BUFFER_SIZE = CD_BUFFER_SIZE + 2;
diff --git a/src/timer.h b/src/timer.h
index 90ce142..130b477 100644
--- a/src/timer.h
+++ b/src/timer.h
@@ -28,13 +28,13 @@ public:
     static constexpr int32_t VBL_RELOAD_X = ACTIVE_AREA_RIGHT - 63;
     static constexpr int32_t VBL_RELOAD_Y = ACTIVE_AREA_BOTTOM;

-    static constexpr int32_t WATCHDOG_DELAY = round<int32_t>(MASTER_CLOCK * 0.13516792);
-    static constexpr int32_t CDROM_64HZ_DELAY = round<int32_t>(MASTER_CLOCK / 64.64);
-    static constexpr int32_t CDROM_75HZ_DELAY = round<int32_t>(MASTER_CLOCK / 75.0);
-    static constexpr int32_t CDROM_150HZ_DELAY = round<int32_t>(MASTER_CLOCK / 150.0);
+    static constexpr int32_t WATCHDOG_DELAY = round<int32_t, double>(MASTER_CLOCK * 0.13516792);
+    static constexpr int32_t CDROM_64HZ_DELAY = round<int32_t, double>(MASTER_CLOCK / 64.64);
+    static constexpr int32_t CDROM_75HZ_DELAY = round<int32_t, double>(MASTER_CLOCK / 75.0);
+    static constexpr int32_t CDROM_150HZ_DELAY = round<int32_t, double>(MASTER_CLOCK / 150.0);

     static constexpr double FRAME_RATE = PIXEL_CLOCK / static_cast<double>(SCREEN_WIDTH * SCREEN_HEIGHT);
-    static constexpr int32_t CYCLES_PER_FRAME = round<int32_t>((MASTER_CLOCK / PIXEL_CLOCK) * SCREEN_WIDTH * SCREEN_HEIGHT);
+    static constexpr int32_t CYCLES_PER_FRAME = round<int32_t, double>((MASTER_CLOCK / PIXEL_CLOCK) * SCREEN_WIDTH * SCREEN_HEIGHT);

     typedef void(*Callback)(Timer* timer, uint32_t userData);

@@ -99,7 +99,7 @@ public:

     static inline constexpr int32_t masterToPixel(int32_t value)
     {
-        return round<int32_t>(static_cast<double>(value) / (MASTER_CLOCK / PIXEL_CLOCK));
+       return round<int32_t,double>(static_cast<double>(value) / (MASTER_CLOCK / PIXEL_CLOCK));
     }

     friend DataPacker& operator<<(DataPacker& out, const Timer& timer);

reminiscence:

diff --git a/Makefile b/Makefile
index a4f265b..5e23834 100644
--- a/Makefile
+++ b/Makefile
@@ -242,6 +242,7 @@ else ifneq (,$(filter $(platform), ngc wii wiiu))
 else ifeq ($(platform), emscripten)
    TARGET := $(TARGET_NAME)_libretro_$(platform).bc
    STATIC_LINKING = 1
+   CXXFLAGS += -Dregister=''

 # GCW Zero
 else ifeq ($(platform), gcw0)
@@ -540,9 +541,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

quasi88:

diff --git a/Makefile b/Makefile
index 3e2d187..02373b4 100644
--- a/Makefile
+++ b/Makefile
@@ -607,9 +607,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CXXFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS)

mame-2015:

diff --git a/Makefile b/Makefile
index 09353e17fe..9ed7eb794d 100644
--- a/Makefile
+++ b/Makefile
@@ -398,7 +398,7 @@ else ifeq ($(platform), emscripten)
    CC_AS = emcc
    CC = em++
    AR = emar
-   LD = em++
+   LD = emar
    FORCE_DRC_C_BACKEND = 1
    CCOMFLAGS += -DLSB_FIRST -fsigned-char -finline  -fno-common -fno-builtin
    ARFLAGS := rcs
@@ -412,8 +412,8 @@ else ifeq ($(platform), emscripten)
    PLATCFLAGS += -DALIGN_INTS -DALIGN_SHORTS
    CCOMFLAGS += $(PLATCFLAGS) #-ffast-math
    PTR64 = 0
-   CFLAGS +=  -s USE_ZLIB=1
-   CXXFLAGS += -s USE_ZLIB=1
+   CFLAGS +=  -s USE_ZLIB=1 -DNO_MEM_TRACKING
+   CXXFLAGS += -s USE_ZLIB=1 -DNO_MEM_TRACKING
    LDFLAGS += -s USE_ZLIB=1  $(EXCEPT_FLAGS)
    LDFLAGSEMULATOR +=

@@ -920,7 +920,7 @@ $(EMULATOR): $(VERSIONOBJ) $(EMULATOROBJ)
        @echo Linking $@...
 ifeq ($(TARGETOS),emscripten)
 # Emscripten's linker seems to be stricter about the ordering of files
-       $(LD) $(LDFLAGS) $(LDFLAGSEMULATOR) $(VERSIONOBJ) -Wl,--start-group $(EMULATOROBJ) -Wl,--end-group $(LIBS) -o $@
+       $(AR) rcs $@ $^
 else
        $(LD) $(LDFLAGS) $(LDFLAGSEMULATOR) $(VERSIONOBJ) $(EMULATOROBJ) $(LIBS) -o $@
 endif
@@ -937,7 +937,12 @@ endif
 $(EMULATOR): $(EMUINFOOBJ) $(DRIVLISTOBJ) $(DRVLIBS) $(LIBOSD) $(LIBBUS) $(LIBOPTIONAL) $(LIBEMU) $(LIBDASM) $(LIBUTIL) $(EXPAT) $(SOFTFLOAT) $(JPEG_LIB) $(FLAC_LIB) $(7Z_LIB) $(FORMATS_LIB) $(LUA_LIB) $(SQLITE3_LIB) $(WEB_LIB) $(ZLIB) $(LIBOCORE) $(MIDI_LIB) $(RESFILE)
        $(CC) $(CDEFS) $(CFLAGS) -c $(SRC)/version.c -o $(VERSIONOBJ)
        @echo Linking $(TARGETLIB)
+ifeq ($(TARGETOS),emscripten)
+# Emscripten's linker seems to be stricter about the ordering of files
+       $(AR) rcs $@ $^
+else
        $(LD) $(LDFLAGS) $(LDFLAGSEMULATOR) $(VERSIONOBJ) $^ $(LIBS) -o $(TARGETLIB)
+endif


 #-------------------------------------------------
@@ -971,8 +976,7 @@ ifeq ($(TARGETOS),emscripten)
 # Avoid using .a files with Emscripten, link to bitcode instead
 $(OBJ)/%.a:
        @echo Linking $@...
-       $(RM) $@
-       $(LD) $^ -o $@
+       $(AR) rcs $@ $^
 $(OBJ)/%.bc: $(OBJ)/%.a
        @cp $< $@
 else
diff --git a/src/lib/util/xmlfile.c b/src/lib/util/xmlfile.c
index e03893134f..d93aabe362 100644
--- a/src/lib/util/xmlfile.c
+++ b/src/lib/util/xmlfile.c
@@ -698,7 +698,7 @@ static void expat_data(void *data, const XML_Char *s, int len)
        if ((*curnode)->value != NULL)
        {
                memcpy(newdata, (*curnode)->value, oldlen);
-               free((*curnode)->value);
+               free((void *)((*curnode)->value));
        }
        (*curnode)->value = newdata;

smsplus:

diff --git a/Makefile.libretro b/Makefile.libretro
index 5e52c65..52ab93d 100644
--- a/Makefile.libretro
+++ b/Makefile.libretro
@@ -548,9 +548,7 @@ else
 endif

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CC) $(CFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(LIBS)

tyrquake:

diff --git a/Makefile b/Makefile
index bef904a..ef82d42 100644
--- a/Makefile
+++ b/Makefile
@@ -841,9 +841,7 @@ else
 all: $(TARGET)

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) $(CFLAGS) $(OBJOUT)$@ $^
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 else
        $(LD) $(fpic) $(SHARED) $(LINKOUT)$@ $(OBJECTS) $(LDFLAGS) $(WINSOCKS)

vice:

diff --git a/Makefile b/Makefile
index 5887c3b67..fe5408c01 100644
--- a/Makefile
+++ b/Makefile
@@ -448,9 +448,7 @@ all: $(TARGET)
 -include $(OBJECTS:.o=.d))

 $(TARGET): $(OBJECTS)
-ifeq ($(platform), emscripten)
-       $(CXX) -r $(SHARED) -o $@ $(OBJECTS) $(LDFLAGS)
-else ifeq ($(STATIC_LINKING), 1)
+ifeq ($(STATIC_LINKING), 1)
        $(AR) rcs $@ $(OBJECTS)
 #STATIC_LINKING=1 and additional Makefile.common sources are incompatible for PS3 environment
 else ifeq ($(platform), ps3)

@JoeOsborn
Copy link
Contributor Author

Phew. I think I've gone through all the cores that had special-case logic for emscripten and made sure they all compile under the latest SDK. The SDK has changed C++ standards/clang versions since then and has become more restrictive with respect to the use of em++/emcc vs emar for archive files. I didn't fork each repo and open a PR because ... it sounded like a lot of work. Sorry. But these patches should do it.

@RobLoach
Copy link
Member

RobLoach commented Oct 4, 2023

Wow, that's incredible work. Thanks so much, JoeOsborn. I can help push up some of these PRs. Do you think these patches will work on the older emscripten prior to our upgrade? Then we could make sure things are patched beforehand.

@JoeOsborn
Copy link
Contributor Author

Happy to do it—it scratches my own itch too.

I think these patches should work on the older sdk too, since some cores did use emar properly while others used emcc/em++. What would be the most expedient way to get these all applied? Should we split them half and half, or can you push PR branches directly to the repos, or what?

@RobLoach
Copy link
Member

RobLoach commented Oct 4, 2023

I am unable to merge patches, but can help on creating the Pull Requests.

@JoeOsborn
Copy link
Contributor Author

I'm going up from the bottom of the list; everything from freechaf through vice has open PRs now.

@JoeOsborn
Copy link
Contributor Author

That's the rest of them, once those PRs are merged I think every core that was compiling before under emscripten will compile under the new emscripten SDK.

@JoeOsborn
Copy link
Contributor Author

What is this waiting on for merging into master? Do the PRs on the core repositories need to be merged first?

@LibretroAdmin
Copy link
Contributor

Will this cause issues for our buildbot? What's the current state of teh deployed version on web.libretro.com?

@JoeOsborn
Copy link
Contributor Author

I don’t know how or when web.libretro.com is packaged/updated, I couldn’t tell from the gitlab-ci scripts. If this change and my per-core fixes (for the handful of cores with incorrect usage of emcc/em++ instead of emar) go through then it should be fine. This PR updates pkg/emscripten/index.html as needed.

@JoeOsborn JoeOsborn force-pushed the modularize-emscripten branch from c3cf594 to 428055c Compare November 2, 2023 19:30
@LibretroAdmin LibretroAdmin merged commit 862bebf into libretro:master Nov 2, 2023
22 checks passed
Sunderland93 pushed a commit to Sunderland93/RetroArch that referenced this pull request Dec 26, 2024
…bretro#15688)

* Increase emscripten stack size and decrease path size to fix emscripten builds broken since de45fc2

* use modularize flags for better-behaved javascript output

* makefile and loader changes

* use specialHTMLTargets to support modular access to canvas

* bind key events to canvas, not document

This way focus means focus and we can have multiple RA instances in
one page.

* Work around an emscripten bug in strict mode

* (Emscripten) Use console.error() for error messages

* increase asyncify stack size

* Fix `-lm` flag-related compile warnings in emscripten

---------

Co-authored-by: Rob Loach <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants