Support searching for the raylib library on multiple paths. (#34)

Add a new environment variable "USE_EXTERNAL_RAYLIB" which will
alter the load behaviour to not include a directory prefix. This
lets us fallback onto the system library loading mechanism.
Remove specific raylib library filenames.
This ability is important for operating systems like Nix.
This commit is contained in:
Adam Griffiths 2021-06-22 22:03:05 +10:00 committed by GitHub
parent aac3a506a2
commit c26da5407a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 30 deletions

View file

@ -28,6 +28,9 @@ If yours isn't available then pip will attempt to build from source, so you will
See here for a Raspberry Pi build: https://github.com/electronstudio/raylib-python-cffi/issues/31#issuecomment-862078330
If your system already has the Raylib library installed, you can set the environment variable 'USE_EXTERNAL_RAYLIB' and it will
be used instead.
## Option 2: Build from source
If you're using a platform we don't have binary builds for yet
@ -261,4 +264,3 @@ You can create a standalone binary using the Nuitka compiler. For example, here
pip3 install nuitka
cd examples/textures
python3 -m nuitka --onefile --linux-onefile-icon resources/wabbit_alpha.png textures_bunnymark.py

View file

@ -3,35 +3,41 @@ This is an attempt at a CFFI dynamic (ABI) binding. It was failing in the exact
materials of a model. But now it __seems__ to work
"""
from cffi import FFI
import itertools
import os
import pathlib
import platform
# Probably unnecessary, just covering all bases in case people add or remove dlls
MAC_NAMES = ['libraylib.3.5.0.dylib', 'libraylib.301.dylib', 'libraylib.dylib']
LINUX_NAMES = ['libraylib.so.3.5.0','libraylib.so.3', 'libraylib.so']
WINDOWS_NAMES = ['libraylib.dll', 'raylib.dll','32bit/raylib.dll', '32bit/libraylib.dll']
if platform.system() == "Darwin":
NAMES_TO_TRY = MAC_NAMES
elif platform.system() == "Linux":
NAMES_TO_TRY = LINUX_NAMES
elif platform.system() == "Windows":
NAMES_TO_TRY = WINDOWS_NAMES
else:
NAMES_TO_TRY = MAC_NAMES + LINUX_NAMES + WINDOWS_NAMES
import pathlib
MODULE = pathlib.Path(__file__).parent.parent
from cffi import FFI
def raylib_library_path():
'''Return the full path of the raylib shared library
If the environment variable "USE_EXTERNAL_RAYLIB" is set (no value required)
then the library will be loaded from the system library paths.
'''
def so_path():
return str(MODULE / 'dynamic') if not 'USE_EXTERNAL_RAYLIB' in os.environ else ''
def so_name():
'''Returns the appropriate for the library on the current platform.'''
lib_filenames = {
'Windows': 'libraylib.dll',
'Linux': 'libraylib.so',
'Darwin': 'libraylib.dylib',
}
if platform.system() not in lib_filenames:
raise ValueError('Unrecognised system "{}"'.format(platform.system()))
return lib_filenames.get(platform.system())
return os.path.join(so_path(), so_name())
ffi = FFI()
ffi.cdef(open(MODULE / "raylib_modified.h").read().replace('RLAPI ', ''))
for name in NAMES_TO_TRY:
file = str(MODULE)+"/dynamic/"+name
try:
raylib = ffi.dlopen(file)
print("LOADED DYNAMICALLY SHARED LIB "+file)
break
except Exception as e:
print(e)
try:
raylib_fname = raylib_library_path()
raylib = ffi.dlopen(raylib_fname)
print('LOADED DYNAMICALLY SHARED LIB "{}"'.format(raylib_fname))
except Exception as e:
print(e)