From 669de49b77a4167eb0fe9d10820b2d5eda5ba469 Mon Sep 17 00:00:00 2001 From: Kyle Benesch <4b796c65+github@gmail.com> Date: Tue, 19 Aug 2025 21:58:12 -0700 Subject: [PATCH 1/2] Temporarily narrow CI jobs for testing Pyodide To be reverted later --- .github/workflows/python-package.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index a9847a08..4229c639 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -68,6 +68,7 @@ jobs: # This makes sure that the latest versions of the SDL headers parse correctly. parse_sdl: + if: false needs: [ruff, mypy] runs-on: ${{ matrix.os }} strategy: @@ -92,6 +93,7 @@ jobs: SDL_VERSION: ${{ matrix.sdl-version }} build: + if: false needs: [ruff, mypy, sdist] runs-on: ${{ matrix.os }} strategy: @@ -165,6 +167,7 @@ jobs: compression-level: 0 test-docs: + if: false needs: [ruff, mypy, sdist] runs-on: ubuntu-latest steps: @@ -192,6 +195,7 @@ jobs: run: python -m sphinx -T -E -W --keep-going . _build/html tox: + if: false needs: [ruff, sdist] runs-on: ${{ matrix.os }} strategy: @@ -221,6 +225,7 @@ jobs: tox -vv linux-wheels: + if: false needs: [ruff, mypy, sdist] runs-on: ${{ matrix.arch == 'aarch64' && 'ubuntu-24.04-arm' || 'ubuntu-latest'}} strategy: @@ -276,6 +281,7 @@ jobs: compression-level: 0 build-macos: + if: false needs: [ruff, mypy, sdist] runs-on: "macos-14" strategy: @@ -322,7 +328,6 @@ jobs: compression-level: 0 pyodide: - needs: [ruff, mypy, sdist] runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 From fde32ac14089676675aa4075697b69751fdc7afa Mon Sep 17 00:00:00 2001 From: Kyle Benesch <4b796c65+github@gmail.com> Date: Wed, 25 Jun 2025 18:43:01 -0700 Subject: [PATCH 2/2] Test Pyodide builds Simple import test --- .github/workflows/python-package.yml | 11 ++--- build_sdl.py | 66 ++++++++++++++++++++++------ tcod/cffi.py | 13 +++--- 3 files changed, 65 insertions(+), 25 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 4229c639..0cb33385 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -335,15 +335,16 @@ jobs: fetch-depth: ${{ env.git-depth }} - name: Checkout submodules run: git submodule update --init --recursive --depth 1 - - uses: HexDecimal/my-setup-sdl-action@v1.0.0 - with: - install-linux-dependencies: true - build-type: "Debug" - version: "3.2.4" # Should be equal or less than the version used by Emscripten + #- uses: HexDecimal/my-setup-sdl-action@v1.0.0 + # with: + # install-linux-dependencies: true + # build-type: "Debug" + # version: "3.2.4" # Should be equal or less than the version used by Emscripten - uses: pypa/cibuildwheel@v3.1.4 env: CIBW_BUILD: cp313-pyodide_wasm32 CIBW_PLATFORM: pyodide + CIBW_TEST_COMMAND: python -c "import tcod.context" - name: Archive wheel uses: actions/upload-artifact@v4 with: diff --git a/build_sdl.py b/build_sdl.py index aca9fc25..c404a408 100755 --- a/build_sdl.py +++ b/build_sdl.py @@ -37,7 +37,6 @@ # The SDL version to include in binary distributions. SDL_BUNDLE_VERSION = os.environ.get("SDL_VERSION", "3.2.16") - # Used to remove excessive newlines in debug outputs. RE_NEWLINES = re.compile(r"\n\n+") # Functions using va_list need to be culled. @@ -124,6 +123,9 @@ ) ) +CMAKE_CMD = ("emcmake", "cmake") if "PYODIDE" in os.environ else ("cmake",) +CMAKE_FIND_SDL_CMD = (*CMAKE_CMD, "--find-package", "-D", "NAME=SDL3", "-D", "COMPILER_ID=GNU", "-D", "LANGUAGE=C") + def check_sdl_version() -> None: """Check the local SDL3 version on Linux distributions.""" @@ -271,7 +273,21 @@ def get_emscripten_include_dir() -> Path: raise AssertionError(os.environ["PATH"]) -check_sdl_version() +include_dirs: list[str] = [] +extra_compile_args: list[str] = [] +extra_link_args: list[str] = [] + +libraries: list[str] = [] +library_dirs: list[str] = [] + + +if "PYODIDE" in os.environ: + with TemporaryDirectory() as tmp_dir: + blank_source = Path(tmp_dir, "blank.c") + blank_source.write_text("") + subprocess.run(["emcc", "--use-port=sdl3", blank_source], check=True) + +# check_sdl_version() SDL_PARSE_PATH: Path | None = None SDL_BUNDLE_PATH: Path | None = None @@ -285,10 +301,29 @@ def get_emscripten_include_dir() -> Path: elif sys.platform == "darwin" and SDL_PARSE_PATH is not None: SDL_INCLUDE = SDL_PARSE_PATH / "Versions/A/Headers" else: # Unix - matches = re.findall( - r"-I(\S+)", - subprocess.check_output(["pkg-config", "sdl3", "--cflags"], universal_newlines=True), - ) + matches = [] + try: + out = subprocess.check_output(["pkg-config", "sdl3", "--cflags"], universal_newlines=True) + except Exception: + if "PYODIDE" in os.environ: + emcc_stdout = subprocess.check_output(["emcc", "--use-port=sdl3", "--cflags"], text=True) + print(f"""EMCC CFLAGS: {emcc_stdout}""") + matches = re.findall(r"--sysroot=(\S+)", emcc_stdout) + print(f"{matches=}") + (sysroot,) = matches + matches = [str(Path(sysroot, "include"))] + library_dirs.append(str(Path(sysroot, "lib/wasm32-emscripten"))) + out = "" + else: + cmake_out = subprocess.run( + (*CMAKE_FIND_SDL_CMD, "-D", "MODE=COMPILE"), check=False, text=True, stdout=subprocess.PIPE + ) + print(f"{cmake_out.stdout=}") + cmake_out.check_returncode() + + out = subprocess.check_output((*CMAKE_FIND_SDL_CMD, "-D", "MODE=COMPILE"), text=True) + if not matches: + matches = re.findall(r"-I(\S+)", out) if not matches: matches = ["/usr/include"] @@ -366,18 +401,16 @@ def get_cdef() -> tuple[str, dict[str, str]]: ) for name in FLEXIBLE_STRUCTS: sdl_cdef = sdl_cdef.replace(f"}} {name};", f"...;}} {name};") - return sdl_cdef + EXTRA_CDEF, parser.known_string_defines + # Remove variable arg functions + RE_VA_ARG_FUNC = re.compile(r"^.*?\([^()]*\.\.\.\)\s*;\s*$", re.MULTILINE) + sdl_cdef = RE_VA_ARG_FUNC.sub("", sdl_cdef) -include_dirs: list[str] = [] -extra_compile_args: list[str] = [] -extra_link_args: list[str] = [] + return sdl_cdef + EXTRA_CDEF, parser.known_string_defines -libraries: list[str] = [] -library_dirs: list[str] = [] if "PYODIDE" in os.environ: - pass + libraries += ["SDL3"] elif sys.platform == "darwin": extra_link_args += ["-framework", "SDL3"] else: @@ -406,6 +439,13 @@ def get_cdef() -> tuple[str, dict[str, str]]: if "PYODIDE" in os.environ: extra_compile_args += ["--use-port=sdl3"] + extra_link_args += ["--use-port=sdl3"] + # extra_compile_args += ( + # subprocess.check_output((*CMAKE_FIND_SDL_CMD, "-D", "MODE=COMPILE"), text=True).strip().split() + # ) + # extra_link_args += subprocess.check_output((*CMAKE_FIND_SDL_CMD, "-D", "MODE=LINK"), text=True).strip().split() + # print(f"{extra_compile_args=}") + # print(f"{extra_link_args=}") elif sys.platform not in ["win32", "darwin"]: # Use sdl-config to link to SDL on Linux. extra_compile_args += ( diff --git a/tcod/cffi.py b/tcod/cffi.py index b0590d0e..863e2219 100644 --- a/tcod/cffi.py +++ b/tcod/cffi.py @@ -17,16 +17,15 @@ REQUIRED_SDL_VERSION = (3, 2, 0) -ffi_check = cffi.FFI() -ffi_check.cdef( - """ -int SDL_GetVersion(void); -""" -) - def verify_dependencies() -> None: """Try to make sure dependencies exist on this system.""" + ffi_check = cffi.FFI() + ffi_check.cdef( + """ + int SDL_GetVersion(void); + """ + ) if sys.platform == "win32": lib_test: Any = ffi_check.dlopen("SDL3.dll") # Make sure SDL3.dll is here. int_version = lib_test.SDL_GetVersion() # Need to check this version.