22import os
33import sys
44import warnings
5+ from platform import machine as cpu_arch
56from ctypes import CDLL , POINTER , Structure , c_uint8 , cast , addressof
67from ctypes .util import find_library
78
@@ -94,6 +95,38 @@ def _preload_deps(libname, dllpath):
9495 return preloaded
9596
9697
98+ def _finds_libs_at_path (libnames , path , patterns ):
99+
100+ # Adding the potential 'd' suffix that is present on the library
101+ # when built in debug configuration
102+ searchfor = libnames + [libname + 'd' for libname in libnames ]
103+ results = []
104+
105+ # First, find any libraries matching pattern exactly within given path
106+ for libname in searchfor :
107+ for subpath in str .split (path , os .pathsep ):
108+ for pattern in patterns :
109+ dllfile = os .path .join (subpath , pattern .format (libname ))
110+ if os .path .exists (dllfile ):
111+ results .append (dllfile )
112+
113+ # Next, on Linux and similar, find any libraries with version suffixes matching
114+ # pattern (e.g. libSDL2.so.2) at path and add them in descending version order
115+ # (i.e. newest first)
116+ if sys .platform not in ("win32" , "darwin" ):
117+ versioned = []
118+ files = os .listdir (path )
119+ for f in files :
120+ for libname in searchfor :
121+ dllname = "lib{0}.so" .format (libname )
122+ if dllname in f and not (dllname == f or f .startswith ("." )):
123+ versioned .append (os .path .join (path , f ))
124+ versioned .sort (key = _so_version_num , reverse = True )
125+ results = results + versioned
126+
127+ return results
128+
129+
97130def _findlib (libnames , path = None ):
98131 """Finds SDL2 libraries and returns them in a list, with libraries found in the directory
99132 optionally specified by 'path' being first (taking precedence) and libraries found in system
@@ -108,33 +141,22 @@ def _findlib(libnames, path=None):
108141 else :
109142 patterns = ["lib{0}.so" ]
110143
144+ # On Apple Silicon Macs, search the non-standard Homebrew library path if no other
145+ # path explicitly set
146+ arm_brewpath = "/opt/Homebrew/lib"
147+ if not path and platform == "darwin" and os .path .exists (arm_brewpath ):
148+ path = arm_brewpath
149+
111150 # Adding the potential 'd' suffix that is present on the library
112151 # when built in debug configuration
113152 searchfor = libnames + [libname + 'd' for libname in libnames ]
153+
154+ # First, find any matching libraries at the given path (if specified)
114155 results = []
115156 if path and path .lower () != "system" :
116- # First, find any libraries matching pattern exactly within given path
117- for libname in searchfor :
118- for subpath in str .split (path , os .pathsep ):
119- for pattern in patterns :
120- dllfile = os .path .join (subpath , pattern .format (libname ))
121- if os .path .exists (dllfile ):
122- results .append (dllfile )
123-
124- # Next, on Linux and similar, find any libraries with version suffixes matching pattern
125- # (e.g. libSDL2.so.2) at path and add them in descending version order (i.e. newest first)
126- if platform not in ("win32" , "darwin" ):
127- versioned = []
128- files = os .listdir (path )
129- for f in files :
130- for libname in searchfor :
131- dllname = "lib{0}.so" .format (libname )
132- if dllname in f and not (dllname == f or f .startswith ("." )):
133- versioned .append (os .path .join (path , f ))
134- versioned .sort (key = _so_version_num , reverse = True )
135- results = results + versioned
136-
137- # Finally, search for library in system library search paths
157+ results = _finds_libs_at_path (libnames , path , patterns )
158+
159+ # Next, search for library in system library search paths
138160 for libname in searchfor :
139161 dllfile = find_library (libname )
140162 if dllfile :
@@ -143,6 +165,12 @@ def _findlib(libnames, path=None):
143165 dllfile = "./" + dllfile
144166 results .append (dllfile )
145167
168+ # On ARM64 Macs, search the non-standard brew library path as a fallback
169+ arm_brewpath = "/opt/Homebrew/lib"
170+ is_apple_silicon = platform == "darwin" and cpu_arch () == "arm64"
171+ if is_apple_silicon and os .path .exists (arm_brewpath ):
172+ results += _finds_libs_at_path (libnames , arm_brewpath , patterns )
173+
146174 return results
147175
148176
0 commit comments