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 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 ## Option 2: Build from source
If you're using a platform we don't have binary builds for yet If you're using a platform we don't have binary builds for yet
@ -61,7 +64,7 @@ Build and install Raylib from the raylib-c directory.
cd ..\.. cd ..\..
To update the dynamic libs, download the official release, e.g. https://github.com/raysan5/raylib/releases/download/3.7.0/raylib-3.7.0_win64_msvc16.zip and extract `raylib.dll` To update the dynamic libs, download the official release, e.g. https://github.com/raysan5/raylib/releases/download/3.7.0/raylib-3.7.0_win64_msvc16.zip and extract `raylib.dll`
into `raylib/dynamic`. Delete the files for other platforms, unless you want them in your distribution. into `raylib/dynamic`. Delete the files for other platforms, unless you want them in your distribution.
To build a binary wheel distribution: To build a binary wheel distribution:
@ -73,7 +76,7 @@ To build a binary wheel distribution:
and install it: and install it:
pip3 install dist\raylib-3.7.0-cp37-cp37m-win_amd64.whl pip3 install dist\raylib-3.7.0-cp37-cp37m-win_amd64.whl
(Note: your wheel's filename will probably be different than the one here.) (Note: your wheel's filename will probably be different than the one here.)
### Linux etc ### Linux etc
@ -100,13 +103,13 @@ Optional: Make a patched version of raylib header. (Not necessary if you've alr
patch -p0 <raylib_modified.h.patch patch -p0 <raylib_modified.h.patch
Build Build
pip3 install cffi pip3 install cffi
cd .. cd ..
rm -rf build raylib/static/_raylib_cffi.* rm -rf build raylib/static/_raylib_cffi.*
python3 raylib/static/build.py python3 raylib/static/build.py
To update the Linux dynamic libs (names will be different on other platfroms): To update the Linux dynamic libs (names will be different on other platfroms):
@ -261,4 +264,3 @@ You can create a standalone binary using the Nuitka compiler. For example, here
pip3 install nuitka pip3 install nuitka
cd examples/textures cd examples/textures
python3 -m nuitka --onefile --linux-onefile-icon resources/wabbit_alpha.png textures_bunnymark.py 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 materials of a model. But now it __seems__ to work
""" """
from cffi import FFI
import itertools
import os
import pathlib
import platform 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 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 = FFI()
ffi.cdef(open(MODULE / "raylib_modified.h").read().replace('RLAPI ', '')) ffi.cdef(open(MODULE / "raylib_modified.h").read().replace('RLAPI ', ''))
for name in NAMES_TO_TRY: try:
file = str(MODULE)+"/dynamic/"+name raylib_fname = raylib_library_path()
try: raylib = ffi.dlopen(raylib_fname)
raylib = ffi.dlopen(file) print('LOADED DYNAMICALLY SHARED LIB "{}"'.format(raylib_fname))
print("LOADED DYNAMICALLY SHARED LIB "+file) except Exception as e:
break print(e)
except Exception as e:
print(e)