3737# The SDL version to include in binary distributions.
3838SDL_BUNDLE_VERSION = os .environ .get ("SDL_VERSION" , "3.2.16" )
3939
40-
4140# Used to remove excessive newlines in debug outputs.
4241RE_NEWLINES = re .compile (r"\n\n+" )
4342# Functions using va_list need to be culled.
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
128130def 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
276292SDL_PARSE_PATH : Path | None = None
277293SDL_BUNDLE_PATH : Path | None = None
@@ -285,10 +301,29 @@ def get_emscripten_include_dir() -> Path:
285301elif sys .platform == "darwin" and SDL_PARSE_PATH is not None :
286302 SDL_INCLUDE = SDL_PARSE_PATH / "Versions/A/Headers"
287303else : # 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
379412if "PYODIDE" in os .environ :
380- pass
413+ libraries += [ "SDL3" ]
381414elif sys .platform == "darwin" :
382415 extra_link_args += ["-framework" , "SDL3" ]
383416else :
@@ -406,6 +439,13 @@ def get_cdef() -> tuple[str, dict[str, str]]:
406439
407440if "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=}")
409449elif sys .platform not in ["win32" , "darwin" ]:
410450 # Use sdl-config to link to SDL on Linux.
411451 extra_compile_args += (
0 commit comments