2222import platform
2323import sys
2424import subprocess
25+ import time
2526
2627ffibuilder = FFI ()
2728
29+ def check_raylib_installed ():
30+ return subprocess .run (['pkg-config' , '--exists' , 'raylib' ], text = True , stdout = subprocess .PIPE ).returncode == 0
2831
2932def get_the_include_path ():
30- return subprocess .run (['pkg-config' , '--variable=includedir' , 'raylib' ], stdout = subprocess .PIPE ).stdout .decode (
31- 'utf-8' ).strip ()
33+ return subprocess .run (['pkg-config' , '--variable=includedir' , 'raylib' ], text = True , stdout = subprocess .PIPE ).stdout .strip ()
3234
3335
3436def get_the_lib_path ():
35- return subprocess .run (['pkg-config' , '--variable=libdir' , 'raylib' ], stdout = subprocess .PIPE ).stdout .decode (
36- 'utf-8' ).strip ()
37+ return subprocess .run (['pkg-config' , '--variable=libdir' , 'raylib' ], text = True , stdout = subprocess .PIPE ).stdout .strip ()
38+
39+
40+ def pre_process_header (filename ):
41+ print ("Pre-processing " + filename )
42+ file = open (filename , "r" )
43+ filetext = "" .join ([ line for line in file if '#include' not in line ])
44+ command = ['gcc' , '-CC' , '-P' ,'-undef' , '-nostdinc' , '-DRLAPI=' , '-DPHYSACDEF=' , '-DRAYGUIDEF=' ,
45+ '-dDI' , '-E' , '-' ]
46+ filetext2 = subprocess .run (command , text = True , input = filetext , stdout = subprocess .PIPE ).stdout
47+ filetext3 = filetext2 .replace ("va_list" , "void *" )
48+ filetext4 = "\n " .join ([ line for line in filetext3 .splitlines () if not line .startswith ("#" )])
49+ #print(r)
50+ return filetext4
51+
52+ def check_header_exists (file ):
53+ if not os .path .isfile (file ):
54+ print ("\n \n *************** WARNING ***************\n \n " )
55+ print (file + " not found. Build will not contain these extra functions.\n \n Please copy file from src/extras to " + file + " if you want them.\n \n " )
56+ print ("**************************************\n \n " )
57+ time .sleep (1 )
58+ return False
59+ return True
60+
61+ def mangle (string ):
62+ return string
63+
64+ if not check_raylib_installed ():
65+ raise Exception ("ERROR: raylib not found by pkg-config. Please install pkg-config and Raylib." )
66+
67+ raylib_h = get_the_include_path () + "/raylib.h"
68+
69+ if not os .path .isfile (raylib_h ):
70+ raise Exception ("ERROR: " + raylib_h + " not found. Please install Raylib." )
71+
72+
3773
3874
3975ffi_includes = """
4076#include "raylib.h"
41- #define RAYGUI_IMPLEMENTATION
42- #define RAYGUI_SUPPORT_RICONS
43- #include "raygui.h"
44- #define PHYSAC_IMPLEMENTATION
45- #include "physac.h"
4677"""
4778
79+ raygui_h = get_the_include_path () + "/raygui.h"
80+ if check_header_exists (raygui_h ):
81+ ffi_includes += """
82+ #define RAYGUI_IMPLEMENTATION
83+ #define RAYGUI_SUPPORT_RICONS
84+ #include "raygui.h"
85+ """
86+
87+
88+ physac_h = get_the_include_path () + "/physac.h"
89+ if check_header_exists (physac_h ):
90+ ffi_includes += """
91+ #define PHYSAC_IMPLEMENTATION
92+ #include "physac.h"
93+ """
94+
95+
96+
97+
4898
49- def mangle (file ):
50- result = ""
51- skip = False
52- for line in open (file ):
53- line = line .strip ().replace ("va_list" , "void *" ) + "\n "
54- if skip :
55- if line .startswith ("#endif" ):
56- skip = False
57- continue
58- if line .startswith ("#if defined(__cplusplus)" ):
59- skip = True
60- continue
61- if line .startswith ("#endif // RAYGUI_H" ):
62- break
63- if line .startswith ("#" ):
64- continue
65- if line .startswith ("RLAPI" ):
66- line = line .replace ('RLAPI ' , '' )
67- if line .startswith ("RAYGUIDEF" ):
68- line = line .replace ('RAYGUIDEF ' , '' )
69- if line .startswith ("PHYSACDEF" ):
70- line = line .replace ('PHYSACDEF ' , '' )
71- result += line
72- # print(line)
73- return result
7499
75100
76101def build_linux ():
77102 print ("BUILDING FOR LINUX" )
78- ffibuilder .cdef (mangle (get_the_include_path () + "/raylib.h" ))
79- ffibuilder .cdef (open ("raylib/raygui_modified.h" ).read ().replace ('RAYGUIDEF ' , '' ))
80- ffibuilder .cdef (open ("raylib/physac_modified.h" ).read ().replace ('PHYSACDEF ' , '' ))
103+ ffibuilder .cdef (pre_process_header (raylib_h ))
104+ if os .path .isfile (raygui_h ):
105+ ffibuilder .cdef (pre_process_header (raygui_h ))
106+ if os .path .isfile (physac_h ):
107+ ffibuilder .cdef (pre_process_header (physac_h ))
81108 ffibuilder .set_source ("raylib._raylib_cffi" , ffi_includes ,
82109 extra_link_args = [get_the_lib_path () + '/libraylib.a' , '-lm' , '-lpthread' , '-lGLU' , '-lGL' ,
83110 '-lrt' ,
@@ -92,8 +119,8 @@ def build_linux():
92119def build_windows ():
93120 print ("BUILDING FOR WINDOWS" )
94121 ffibuilder .cdef (mangle ("raylib/raylib.h" ))
95- ffibuilder .cdef (open ("raylib/raygui_modified.h" ).read ().replace ('RAYGUIDEF ' , '' ). replace ( 'bool' , 'int' ) )
96- ffibuilder .cdef (open ("raylib/physac_modified.h" ).read ().replace ('PHYSACDEF ' , '' ). replace ( 'bool' , 'int' ) )
122+ ffibuilder .cdef (open ("raylib/raygui_modified.h" ).read ().replace ('RAYGUIDEF ' , '' ))
123+ ffibuilder .cdef (open ("raylib/physac_modified.h" ).read ().replace ('PHYSACDEF ' , '' ))
97124 ffibuilder .set_source ("raylib._raylib_cffi" , ffi_includes ,
98125 extra_link_args = ['/NODEFAULTLIB:MSVCRTD' ],
99126 libraries = ['raylib' , 'gdi32' , 'shell32' , 'user32' , 'OpenGL32' , 'winmm' ],
@@ -105,9 +132,11 @@ def build_windows():
105132
106133def build_mac ():
107134 print ("BUILDING FOR MAC" )
108- ffibuilder .cdef (mangle (get_the_include_path () + "/raylib.h" ))
109- ffibuilder .cdef (open ("raylib/raygui_modified.h" ).read ().replace ('RAYGUIDEF ' , '' ))
110- ffibuilder .cdef (open ("raylib/physac_modified.h" ).read ().replace ('PHYSACDEF ' , '' ))
135+ ffibuilder .cdef (pre_process_header (raylib_h ))
136+ if os .path .isfile (raygui_h ):
137+ ffibuilder .cdef (pre_process_header (raygui_h ))
138+ if os .path .isfile (physac_h ):
139+ ffibuilder .cdef (pre_process_header (physac_h ))
111140 ffibuilder .set_source ("raylib._raylib_cffi" , ffi_includes ,
112141 extra_link_args = [get_the_lib_path () + '/libraylib.a' , '-framework' , 'OpenGL' , '-framework' , 'Cocoa' ,
113142 '-framework' , 'IOKit' , '-framework' , 'CoreFoundation' , '-framework' ,
@@ -121,9 +150,11 @@ def build_mac():
121150
122151def build_rpi_nox ():
123152 print ("BUILDING FOR RASPBERRY PI" )
124- ffibuilder .cdef (mangle (get_the_include_path () + "/raylib.h" ))
125- ffibuilder .cdef (open ("raylib/raygui_modified.h" ).read ().replace ('RAYGUIDEF ' , '' ))
126- ffibuilder .cdef (open ("raylib/physac_modified.h" ).read ().replace ('PHYSACDEF ' , '' ))
153+ ffibuilder .cdef (pre_process_header (raylib_h ))
154+ if os .path .isfile (raygui_h ):
155+ ffibuilder .cdef (pre_process_header (raygui_h ))
156+ if os .path .isfile (physac_h ):
157+ ffibuilder .cdef (pre_process_header (physac_h ))
127158 ffibuilder .set_source ("raylib._raylib_cffi" , ffi_includes ,
128159 extra_link_args = [get_the_lib_path () + '/libraylib.a' ,
129160 '/opt/vc/lib/libEGL_static.a' , '/opt/vc/lib/libGLESv2_static.a' ,
0 commit comments