Skip to content

Commit fde32ac

Browse files
committed
Test Pyodide builds
Simple import test
1 parent 669de49 commit fde32ac

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

.github/workflows/python-package.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,16 @@ jobs:
335335
fetch-depth: ${{ env.git-depth }}
336336
- name: Checkout submodules
337337
run: git submodule update --init --recursive --depth 1
338-
- uses: HexDecimal/my-setup-sdl-action@v1.0.0
339-
with:
340-
install-linux-dependencies: true
341-
build-type: "Debug"
342-
version: "3.2.4" # Should be equal or less than the version used by Emscripten
338+
#- uses: HexDecimal/my-setup-sdl-action@v1.0.0
339+
# with:
340+
# install-linux-dependencies: true
341+
# build-type: "Debug"
342+
# version: "3.2.4" # Should be equal or less than the version used by Emscripten
343343
- uses: pypa/cibuildwheel@v3.1.4
344344
env:
345345
CIBW_BUILD: cp313-pyodide_wasm32
346346
CIBW_PLATFORM: pyodide
347+
CIBW_TEST_COMMAND: python -c "import tcod.context"
347348
- name: Archive wheel
348349
uses: actions/upload-artifact@v4
349350
with:

build_sdl.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
# The SDL version to include in binary distributions.
3838
SDL_BUNDLE_VERSION = os.environ.get("SDL_VERSION", "3.2.16")
3939

40-
4140
# Used to remove excessive newlines in debug outputs.
4241
RE_NEWLINES = re.compile(r"\n\n+")
4342
# Functions using va_list need to be culled.
@@ -124,6 +123,9 @@
124123
)
125124
)
126125

126+
CMAKE_CMD = ("emcmake", "cmake") if "PYODIDE" in os.environ else ("cmake",)
127+
CMAKE_FIND_SDL_CMD = (*CMAKE_CMD, "--find-package", "-D", "NAME=SDL3", "-D", "COMPILER_ID=GNU", "-D", "LANGUAGE=C")
128+
127129

128130
def check_sdl_version() -> None:
129131
"""Check the local SDL3 version on Linux distributions."""
@@ -271,7 +273,21 @@ def get_emscripten_include_dir() -> Path:
271273
raise AssertionError(os.environ["PATH"])
272274

273275

274-
check_sdl_version()
276+
include_dirs: list[str] = []
277+
extra_compile_args: list[str] = []
278+
extra_link_args: list[str] = []
279+
280+
libraries: list[str] = []
281+
library_dirs: list[str] = []
282+
283+
284+
if "PYODIDE" in os.environ:
285+
with TemporaryDirectory() as tmp_dir:
286+
blank_source = Path(tmp_dir, "blank.c")
287+
blank_source.write_text("")
288+
subprocess.run(["emcc", "--use-port=sdl3", blank_source], check=True)
289+
290+
# check_sdl_version()
275291

276292
SDL_PARSE_PATH: Path | None = None
277293
SDL_BUNDLE_PATH: Path | None = None
@@ -285,10 +301,29 @@ def get_emscripten_include_dir() -> Path:
285301
elif sys.platform == "darwin" and SDL_PARSE_PATH is not None:
286302
SDL_INCLUDE = SDL_PARSE_PATH / "Versions/A/Headers"
287303
else: # Unix
288-
matches = re.findall(
289-
r"-I(\S+)",
290-
subprocess.check_output(["pkg-config", "sdl3", "--cflags"], universal_newlines=True),
291-
)
304+
matches = []
305+
try:
306+
out = subprocess.check_output(["pkg-config", "sdl3", "--cflags"], universal_newlines=True)
307+
except Exception:
308+
if "PYODIDE" in os.environ:
309+
emcc_stdout = subprocess.check_output(["emcc", "--use-port=sdl3", "--cflags"], text=True)
310+
print(f"""EMCC CFLAGS: {emcc_stdout}""")
311+
matches = re.findall(r"--sysroot=(\S+)", emcc_stdout)
312+
print(f"{matches=}")
313+
(sysroot,) = matches
314+
matches = [str(Path(sysroot, "include"))]
315+
library_dirs.append(str(Path(sysroot, "lib/wasm32-emscripten")))
316+
out = ""
317+
else:
318+
cmake_out = subprocess.run(
319+
(*CMAKE_FIND_SDL_CMD, "-D", "MODE=COMPILE"), check=False, text=True, stdout=subprocess.PIPE
320+
)
321+
print(f"{cmake_out.stdout=}")
322+
cmake_out.check_returncode()
323+
324+
out = subprocess.check_output((*CMAKE_FIND_SDL_CMD, "-D", "MODE=COMPILE"), text=True)
325+
if not matches:
326+
matches = re.findall(r"-I(\S+)", out)
292327
if not matches:
293328
matches = ["/usr/include"]
294329

@@ -366,18 +401,16 @@ def get_cdef() -> tuple[str, dict[str, str]]:
366401
)
367402
for name in FLEXIBLE_STRUCTS:
368403
sdl_cdef = sdl_cdef.replace(f"}} {name};", f"...;}} {name};")
369-
return sdl_cdef + EXTRA_CDEF, parser.known_string_defines
370404

405+
# Remove variable arg functions
406+
RE_VA_ARG_FUNC = re.compile(r"^.*?\([^()]*\.\.\.\)\s*;\s*$", re.MULTILINE)
407+
sdl_cdef = RE_VA_ARG_FUNC.sub("", sdl_cdef)
371408

372-
include_dirs: list[str] = []
373-
extra_compile_args: list[str] = []
374-
extra_link_args: list[str] = []
409+
return sdl_cdef + EXTRA_CDEF, parser.known_string_defines
375410

376-
libraries: list[str] = []
377-
library_dirs: list[str] = []
378411

379412
if "PYODIDE" in os.environ:
380-
pass
413+
libraries += ["SDL3"]
381414
elif sys.platform == "darwin":
382415
extra_link_args += ["-framework", "SDL3"]
383416
else:
@@ -406,6 +439,13 @@ def get_cdef() -> tuple[str, dict[str, str]]:
406439

407440
if "PYODIDE" in os.environ:
408441
extra_compile_args += ["--use-port=sdl3"]
442+
extra_link_args += ["--use-port=sdl3"]
443+
# extra_compile_args += (
444+
# subprocess.check_output((*CMAKE_FIND_SDL_CMD, "-D", "MODE=COMPILE"), text=True).strip().split()
445+
# )
446+
# extra_link_args += subprocess.check_output((*CMAKE_FIND_SDL_CMD, "-D", "MODE=LINK"), text=True).strip().split()
447+
# print(f"{extra_compile_args=}")
448+
# print(f"{extra_link_args=}")
409449
elif sys.platform not in ["win32", "darwin"]:
410450
# Use sdl-config to link to SDL on Linux.
411451
extra_compile_args += (

tcod/cffi.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
REQUIRED_SDL_VERSION = (3, 2, 0)
1919

20-
ffi_check = cffi.FFI()
21-
ffi_check.cdef(
22-
"""
23-
int SDL_GetVersion(void);
24-
"""
25-
)
26-
2720

2821
def verify_dependencies() -> None:
2922
"""Try to make sure dependencies exist on this system."""
23+
ffi_check = cffi.FFI()
24+
ffi_check.cdef(
25+
"""
26+
int SDL_GetVersion(void);
27+
"""
28+
)
3029
if sys.platform == "win32":
3130
lib_test: Any = ffi_check.dlopen("SDL3.dll") # Make sure SDL3.dll is here.
3231
int_version = lib_test.SDL_GetVersion() # Need to check this version.

0 commit comments

Comments
 (0)