1515# Assumes raylib, GL, etc are all already installed as system libraries. We dont distribute them.
1616# Raylib must be installed and compiled with: cmake -DWITH_PIC=ON -DSHARED=ON -DSTATIC=ON ..
1717
18- # We use /usr/local/lib/libraylib.a to ensure we link to static version
19- import re
2018
19+ import re
2120from cffi import FFI
2221import os
2322import platform
24- import sys
2523import subprocess
2624import time
2725from pathlib import Path
2826
2927THIS_DIR = Path (__file__ ).resolve ().parent
3028REPO_ROOT = THIS_DIR .parent
3129
30+ # TODO: I think the customisation is making this file overly complex and probably isn't used
31+ # by many/any users, see discussion at https://github.com/electronstudio/raylib-python-cffi/pull/172
32+ #
33+ # Environment variables you can set before build
34+ #
35+ # RAYLIB_PLATFORM: Any one of: Desktop, SDL, DRM, PLATFORM_COMMA
36+ # RAYLIB_LINK_ARGS: Arguments to pass to the linker rather than getting them from pkg-config.
37+ # e.g.: -L/usr/local/lib -lraylib
38+ # RAYLIB_INCLUDE_PATH: Directory to find raylib.h rather than getting from pkg-config.
39+ # e.g.: /usr/local/include
40+ # RAYGUI_INCLUDE_PATH: Directory to find raygui.h
41+ # e.g.: /usr/local/include
42+ # GLFW_INCLUDE_PATH: Directory to find glfw3.h
43+ # e.g.: /usr/local/include/GLFW
44+ # PHYSAC_INCLUDE_PATH: Directory to find physac.h
45+ # e.g.: /usr/local/include
46+ # LIBFFI_INCLUDE_PATH:
47+ # e.g.: /usr/local/include
48+ #
49+ # PKG_CONFIG_LIB_raylib: the package to request from pkg-config for raylib include files, default 'raylib'
50+ # PKG_CONFIG_LIB_raygui: the package to request from pkg-config for raygui include files
51+ # set to 'raygui' if you have a separate raygui package, else unset for default 'raylib'
52+ # PKG_CONFIG_LIB_physac: the package to request from pkg-config for physac indlude files
53+ # set to 'physac' if you have a separate raygui physac, else unset for default 'raylib'
54+ # PKG_CONFIG_LIB_glfw3: the package to request from pkg-config for glfw3 include files
55+ # set to 'glfw3' if you have a separate glfw3 package, else unset for default 'raylib'
56+ # PKG_CONFIG_LIB_libffi: the package to request from pkg-config for libffi include files
3257
3358RAYLIB_PLATFORM = os .getenv ("RAYLIB_PLATFORM" , "Desktop" )
3459
35- def check_raylib_installed ():
60+ def check_raylib_pkgconfig_installed ():
3661 # this should be 'pkg-config --exists raylib' but result is non-deterministic on old versions of pkg-config!
3762 return subprocess .run (['pkg-config' , '--libs' , 'raylib' ], text = True , stdout = subprocess .PIPE ).returncode == 0
3863
39- def check_SDL_installed ():
64+ def check_sdl_pkgconfig_installed ():
4065 # this should be 'pkg-config --exists sdl2' but result is non-deterministic on old versions of pkg-config!
4166 return subprocess .run (['pkg-config' , '--libs' , 'sdl2' ], text = True , stdout = subprocess .PIPE ).returncode == 0
4267
43- def get_the_include_path ():
44- return subprocess .run (['pkg-config' , '--variable=includedir' , 'raylib' ], text = True ,
45- stdout = subprocess .PIPE ).stdout .strip ()
68+
69+ def get_the_include_path_from_pkgconfig (libname ):
70+ return subprocess .run (
71+ ['pkg-config' , '--variable=includedir' , os .environ .get ("PKG_CONFIG_LIB_" + libname , 'raylib' )], text = True ,
72+ stdout = subprocess .PIPE ).stdout .strip ()
4673
4774
48- def get_the_lib_path ():
75+ def get_the_lib_path_from_pkgconfig ():
4976 return subprocess .run (['pkg-config' , '--variable=libdir' , 'raylib' ], text = True ,
5077 stdout = subprocess .PIPE ).stdout .strip ()
5178
52- def get_lib_flags ():
79+ def get_lib_flags_from_pkgconfig ():
5380 return subprocess .run (['pkg-config' , '--libs' , 'raylib' ], text = True ,
54- stdout = subprocess .PIPE ).stdout .strip (). split ()
81+ stdout = subprocess .PIPE ).stdout .strip ()
5582
5683def pre_process_header (filename , remove_function_bodies = False ):
5784 print ("Pre-processing " + filename )
@@ -111,54 +138,72 @@ def check_header_exists(file):
111138
112139
113140def build_unix ():
114- if not check_raylib_installed ():
141+ if os . getenv ( "RAYLIB_LINK_ARGS" ) is None and not check_raylib_pkgconfig_installed ():
115142 print ("PKG_CONFIG_PATH is set to: " + os .getenv ("PKG_CONFIG_PATH" ))
116- raise Exception ("ERROR: raylib not found by pkg-config. Please install pkg-config and Raylib." )
143+ raise Exception ("ERROR: raylib not found by pkg-config. Please install pkg-config and Raylib"
144+ "or else set RAYLIB_LINK_ARGS env variable." )
117145
118- if RAYLIB_PLATFORM == "SDL" and not check_SDL_installed ():
146+ if RAYLIB_PLATFORM == "SDL" and os . getenv ( "RAYLIB_LINK_ARGS" ) is None and not check_sdl_pkgconfig_installed ():
119147 print ("PKG_CONFIG_PATH is set to: " + os .getenv ("PKG_CONFIG_PATH" ))
120- raise Exception ("ERROR: SDL2 not found by pkg-config. Please install pkg-config and SDL2." )
148+ raise Exception ("ERROR: SDL2 not found by pkg-config. Please install pkg-config and SDL2."
149+ "or else set RAYLIB_LINK_ARGS env variable." )
121150
122- raylib_h = get_the_include_path () + "/raylib.h"
123- rlgl_h = get_the_include_path () + "/rlgl.h"
124- raymath_h = get_the_include_path () + "/raymath.h"
151+ raylib_include_path = os .getenv ("RAYLIB_INCLUDE_PATH" )
152+ if raylib_include_path is None :
153+ raylib_include_path = get_the_include_path_from_pkgconfig ("raylib" )
154+ raylib_h = raylib_include_path + "/raylib.h"
155+ rlgl_h = raylib_include_path + "/rlgl.h"
156+ raymath_h = raylib_include_path + "/raymath.h"
125157
126158 if not os .path .isfile (raylib_h ):
127- raise Exception ("ERROR: " + raylib_h + " not found. Please install Raylib." )
159+ raise Exception ("ERROR: " + raylib_h + " not found. Please install Raylib or set RAYLIB_INCLUDE_PATH ." )
128160
129161 if not os .path .isfile (rlgl_h ):
130- raise Exception ("ERROR: " + rlgl_h + " not found. Please install Raylib." )
162+ raise Exception ("ERROR: " + rlgl_h + " not found. Please install Raylib or set RAYLIB_INCLUDE_PATH ." )
131163
132164 if not os .path .isfile (raymath_h ):
133- raise Exception ("ERROR: " + raylib_h + " not found. Please install Raylib." )
165+ raise Exception ("ERROR: " + raylib_h + " not found. Please install Raylib or set RAYLIB_INCLUDE_PATH ." )
134166
135167 ffi_includes = """
136168 #include "raylib.h"
137169 #include "rlgl.h"
138170 #include "raymath.h"
139171 """
140172
141- glfw3_h = get_the_include_path () + "/GLFW/glfw3.h"
173+ glfw_include_path = os .getenv ("GLFW_INCLUDE_PATH" )
174+ if glfw_include_path is None :
175+ glfw_include_path = get_the_include_path_from_pkgconfig ("glfw3" )
176+ glfw3_h = glfw_include_path + "/GLFW/glfw3.h"
142177 if RAYLIB_PLATFORM == "Desktop" and check_header_exists (glfw3_h ):
143178 ffi_includes += """
144179 #include "GLFW/glfw3.h"
145180 """
146181
147- raygui_h = get_the_include_path () + "/raygui.h"
182+ raygui_include_path = os .getenv ("RAYGUI_INCLUDE_PATH" )
183+ if raygui_include_path is None :
184+ raygui_include_path = get_the_include_path_from_pkgconfig ("raygui" )
185+ raygui_h = raygui_include_path + "/raygui.h"
148186 if check_header_exists (raygui_h ):
149187 ffi_includes += """
150188 #define RAYGUI_IMPLEMENTATION
151189 #define RAYGUI_SUPPORT_RICONS
152190 #include "raygui.h"
153191 """
154192
155- physac_h = get_the_include_path () + "/physac.h"
193+ physac_include_path = os .getenv ("PHYSAC_INCLUDE_PATH" )
194+ if physac_include_path is None :
195+ physac_include_path = get_the_include_path_from_pkgconfig ("physac" )
196+ physac_h = physac_include_path + "/physac.h"
156197 if check_header_exists (physac_h ):
157198 ffi_includes += """
158199 #define PHYSAC_IMPLEMENTATION
159200 #include "physac.h"
160201 """
161202
203+ libffi_include_path = os .getenv ("LIBFFI_INCLUDE_PATH" )
204+ if libffi_include_path is None :
205+ libffi_include_path = get_the_include_path_from_pkgconfig ("libffi" )
206+
162207 ffibuilder .cdef (pre_process_header (raylib_h ))
163208 ffibuilder .cdef (pre_process_header (rlgl_h ))
164209 ffibuilder .cdef (pre_process_header (raymath_h , True ))
@@ -173,7 +218,11 @@ def build_unix():
173218
174219 if platform .system () == "Darwin" :
175220 print ("BUILDING FOR MAC" )
176- extra_link_args = [get_the_lib_path () + '/libraylib.a' , '-framework' , 'OpenGL' , '-framework' , 'Cocoa' ,
221+ flags = os .getenv ("RAYLIB_LINK_ARGS" )
222+ if flags is None :
223+ flags = get_the_lib_path_from_pkgconfig () + '/libraylib.a'
224+ # We use /usr/local/lib/libraylib.a to ensure we link to static version
225+ extra_link_args = flags .split () + ['-framework' , 'OpenGL' , '-framework' , 'Cocoa' ,
177226 '-framework' , 'IOKit' , '-framework' , 'CoreFoundation' , '-framework' ,
178227 'CoreVideo' ]
179228 if RAYLIB_PLATFORM == "SDL" :
@@ -183,12 +232,18 @@ def build_unix():
183232 extra_compile_args = ["-Wno-error=incompatible-function-pointer-types" , "-D_CFFI_NO_LIMITED_API" ]
184233 else : #platform.system() == "Linux":
185234 print ("BUILDING FOR LINUX" )
186- extra_link_args = get_lib_flags () + [ '-lm' , '-lpthread' , '-lGL' ,
235+ flags = os .getenv ("RAYLIB_LINK_ARGS" )
236+ if flags is None :
237+ flags = get_lib_flags_from_pkgconfig ()
238+ extra_link_args = flags .split () + [ '-lm' , '-lpthread' , '-lGL' ,
187239 '-lrt' , '-lm' , '-ldl' , '-lpthread' , '-latomic' ]
188240 if RAYLIB_PLATFORM == "SDL" :
189241 extra_link_args += ['-lX11' ,'-lSDL2' ]
190242 elif RAYLIB_PLATFORM == "DRM" :
191243 extra_link_args += ['-lEGL' , '-lgbm' ]
244+ elif RAYLIB_PLATFORM == "PLATFORM_COMMA" :
245+ extra_link_args .remove ('-lGL' )
246+ extra_link_args += ['-lGLESv2' , '-lEGL' , '-lwayland-client' , '-lwayland-egl' ]
192247 else :
193248 extra_link_args += ['-lX11' ]
194249 extra_compile_args = ["-Wno-incompatible-pointer-types" , "-D_CFFI_NO_LIMITED_API" ]
@@ -201,7 +256,8 @@ def build_unix():
201256 ffibuilder .set_source ("raylib._raylib_cffi" ,
202257 ffi_includes ,
203258 py_limited_api = False ,
204- include_dirs = [get_the_include_path ()],
259+ include_dirs = [raylib_include_path , raygui_include_path , physac_include_path , glfw_include_path ,
260+ libffi_include_path ],
205261 extra_link_args = extra_link_args ,
206262 extra_compile_args = extra_compile_args ,
207263 libraries = libraries )
0 commit comments