renpy / pygame_sdl2

Reimplementation of portions of the pygame API using SDL2.
GNU Lesser General Public License v2.1
325 stars 63 forks source link

Emscripten support - compilation #115

Closed Beuc closed 4 years ago

Beuc commented 5 years ago

Hi,

Pygame_SDL2 needs few changes to work with Emscripten. I mostly disabled a few thread-related lines for now, and added a call to emscripten_sleep/async (not necessarily the right place to do so).

The main issue I'd like to discuss is compiling Pygame_SDL2 as static modules. There are a few steps to make this work:

  1. Python2 needs to be slightly patched to support static submodules (Not related to Pygame_SDL2)

  2. The Cython output needs to be slightly patched to support static submodules I added something like:

            # Fix-up source for static loading
            if len(split_name) > 1:
                parent_module = '.'.join(split_name[:-1])
                parent_module_identifier = parent_module.replace('.', '_')
                with open(c_fn, 'r') as f: ccode = f.read()
                ccode = re.sub('Py_InitModule4\("([^"]+)"', 'Py_InitModule4("'+parent_module+'.\\1"', ccode)
                ccode = re.sub('^__Pyx_PyMODINIT_FUNC init', '__Pyx_PyMODINIT_FUNC init'+parent_module_identifier+'_', ccode, 0, re.MULTILINE)  # Cython 0.28.2
                ccode = re.sub('^PyMODINIT_FUNC init', 'PyMODINIT_FUNC init'+parent_module_identifier+'_', ccode, 0, re.MULTILINE)  # Cython 0.25.2
                with open(c_fn, 'w') as f: f.write(ccode)

    after the subprocess call to cython. I need the same for the Ren'Py modules.

  3. Last, I need a way to insert a call to emscripten_sleep conditionally and #include <emscripten.h> (if I keep it in Pygame_SDL2, depending on the performance / maintainability of moving this annoying async call elsewhere). There's a way to specific values for conditional compilation in Cython, but only through the distutils cython_compile_time_env (e.g. (1)), and not through the command line, which is unfortunate. Alternatively we could implement a mini-preprocessor with a #ifdef/#endif-style syntax in the .pyx file. (No need for this in Ren'Py modules for now.)

So, Pygame_SDL2 (and Ren'Py)'s setuplib.py has a pretty specific way to compile Cython modules (compared to other Python libs that use distutils). For 2) this is great because we can easily modify the generate .c script. For 3) this is a blocker because we can't introduce conditional compilation.

I wonder what are your thoughts on this.

Beuc commented 5 years ago

I moved out 3. out of pygame_sdl2 so that sub-part is not an immediate issue.

Beuc commented 4 years ago

Done -- cf. commits during April.