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

Build with Emscripten for WebAssembly #6

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

NeroBurner
Copy link
Collaborator

@NeroBurner NeroBurner commented Feb 24, 2022

InfiniTime PRs fixing Emscripten compile errors:

Need a patched SDL2 with fixes for use with -s USE_PTHREAD

I did modify the downloaded emsdk/upstream/emscripten/tools/ports/sdl2.py file to get the SDL2 patched version

import os

TAG = 'emscripten_pthread_fix'
- HASH = '67e1abe1183b04836b35d724fd495c83c9559b4530d4a5c9bcc89648af0ac7cc51c02f7055a1664fe5f5f90953d22a6c431fa8bc5cdd77c94a97f107c47e2d62'
+ HASH = 'b14f279aed10ec3acaf2cece5917ed62ff77a933ba9f03ea87484214b7b961f19f01acffab48ef3a63ec654765f159ba70a3ef638d4c548f88555bd6b1f6513e'
SUBDIR = 'SDL-' + TAG


def needed(settings):
  return settings.USE_SDL == 2


def get_lib_name(settings):
  return 'libSDL2' + ('-mt' if settings.USE_PTHREADS else '') + '.a'


def get(ports, settings, shared):
  # get the port
-  ports.fetch_project('sdl2', 'https://github.com/libsdl-org/SDL/archive/' + TAG + '.zip', SUBDIR, sha512hash=HASH)
+  ports.fetch_project('sdl2', 'https://github.com/NeroBurner/SDL/archive/refs/heads/emscripten_pthread_fix.zip', SUBDIR, sha512hash=HASH)

To build I used the following script named build_emscripten.sh :

#!/bin/bash
set -ex

SCRIPT_DIR=$(dirname "$(realpath "$0")")
source "/home/nero/repos/pinetime/emsdk/emsdk_env.sh"
emcmake cmake -S "${SCRIPT_DIR}" -B "${SCRIPT_DIR}/_build_em" -DCMAKE_BUILD_TYPE=Release
cmake --build "${SCRIPT_DIR}/_build_em" -j4

The created infinisim.html (and wasm) file can be provided to the browser using emrun _build/infinisim.html

or use minimal python server with the required headers set start_python_cors_server.py

#!/usr/bin/env python3
# from: https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
        self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
    test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)

And with all that InfiniSim in your browser! 🎉

InfiniSim wasm working

edit: update hash to branch changes

@NeroBurner
Copy link
Collaborator Author

PR libsdl-org/SDL#5365 got merged 🎉
we only need to update the tag and the sha512 hash ( no need to modify the ports.fetch_project url anymore 🎉

TAG = 'ea7d5307acfb1daf9af6104b60b75114b15bcd27' 
HASH = 'b7d58124f0d1145f23338abfdb6aa07855ac74ed80b3a5b613d23b4930c84d04d1794a62aab2ca2680ba369128ee2028ea51236fab4aaf70556036172fa59e6a' 
SUBDIR = 'SDL-' + TAG 
 
 
def needed(settings): 
  return settings.USE_SDL == 2 
 
 
def get_lib_name(settings): 
  return 'libSDL2' + ('-mt' if settings.USE_PTHREADS else '') + '.a' 
 
 
def get(ports, settings, shared): 
  # get the port 
  ports.fetch_project('sdl2', 'https://github.com/libsdl-org/SDL/archive/' + TAG + '.zip', SUBDIR, sha512hash=HASH)

To do that execute the following sed script in the emsdk folder

sed -i -e "s/^TAG =.*/TAG = 'ea7d5307acfb1daf9af6104b60b75114b15bcd27'/" -e "s/HASH =.*/HASH = 'b7d58124f0d1145f23338abfdb6aa07855ac74ed80b3a5b613d23b4930c84d04d1794a62aab2ca2680ba369128ee2028ea51236fab4aaf70556036172fa59e6a'/" upstream/emscripten/tools/ports/sdl2.py

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.

1 participant