Skip to content

Commit cd488d9

Browse files
when environment variables are set will skip the use of pkg-config in the build
1 parent a79be2d commit cd488d9

1 file changed

Lines changed: 61 additions & 25 deletions

File tree

raylib/build.py

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
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
2120
from cffi import FFI
2221
import os
2322
import platform
24-
import sys
2523
import subprocess
2624
import time
2725
from pathlib import Path
@@ -30,28 +28,42 @@
3028
REPO_ROOT = THIS_DIR.parent
3129

3230

31+
# Environment variables you can set before build
32+
#
33+
# RAYLIB_PLATFORM: Any one of: Desktop, SDL, DRM, PLATFORM_COMMA
34+
# RAYLIB_LINK_ARGS: Arguments to pass to the linker rather than getting them from pkg-config.
35+
# e.g.: -L/usr/local/lib -lraylib
36+
# RAYLIB_INCLUDE_PATH: Directory to find raylib.h rather than getting from pkg-config.
37+
# e.g.: /usr/local/include
38+
# RAYGUI_INCLUDE_PATH: Directory to find raygui.h
39+
# e.g.: /usr/local/include
40+
# GLFW_INCLUDE_PATH: Directory to find glfw3.h
41+
# e.g.: /usr/local/include/GLFW
42+
# PHYSAC_INCLUDE_PATH: Directory to find physac.h
43+
# e.g.: /usr/local/include
44+
3345
RAYLIB_PLATFORM = os.getenv("RAYLIB_PLATFORM", "Desktop")
3446

35-
def check_raylib_installed():
47+
def check_raylib_pkgconfig_installed():
3648
# this should be 'pkg-config --exists raylib' but result is non-deterministic on old versions of pkg-config!
3749
return subprocess.run(['pkg-config', '--libs', 'raylib'], text=True, stdout=subprocess.PIPE).returncode == 0
3850

39-
def check_SDL_installed():
51+
def check_sdl_pkgconfig_installed():
4052
# this should be 'pkg-config --exists sdl2' but result is non-deterministic on old versions of pkg-config!
4153
return subprocess.run(['pkg-config', '--libs', 'sdl2'], text=True, stdout=subprocess.PIPE).returncode == 0
4254

43-
def get_the_include_path():
55+
def get_the_include_path_from_pkgconfig():
4456
return subprocess.run(['pkg-config', '--variable=includedir', 'raylib'], text=True,
4557
stdout=subprocess.PIPE).stdout.strip()
4658

4759

48-
def get_the_lib_path():
60+
def get_the_lib_path_from_pkgconfig():
4961
return subprocess.run(['pkg-config', '--variable=libdir', 'raylib'], text=True,
5062
stdout=subprocess.PIPE).stdout.strip()
5163

52-
def get_lib_flags():
64+
def get_lib_flags_from_pkgconfig():
5365
return subprocess.run(['pkg-config', '--libs', 'raylib'], text=True,
54-
stdout=subprocess.PIPE).stdout.strip().split()
66+
stdout=subprocess.PIPE).stdout.strip()
5567

5668
def pre_process_header(filename, remove_function_bodies=False):
5769
print("Pre-processing " + filename)
@@ -111,48 +123,62 @@ def check_header_exists(file):
111123

112124

113125
def build_unix():
114-
if not check_raylib_installed():
126+
if os.getenv("RAYLIB_LINK_ARGS") is None and not check_raylib_pkgconfig_installed():
115127
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.")
128+
raise Exception("ERROR: raylib not found by pkg-config. Please install pkg-config and Raylib"
129+
"or else set RAYLIB_LINK_ARGS env variable.")
117130

118-
if RAYLIB_PLATFORM=="SDL" and not check_SDL_installed():
131+
if RAYLIB_PLATFORM=="SDL" and os.getenv("RAYLIB_LINK_ARGS") is None and not check_sdl_pkgconfig_installed():
119132
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.")
133+
raise Exception("ERROR: SDL2 not found by pkg-config. Please install pkg-config and SDL2."
134+
"or else set RAYLIB_LINK_ARGS env variable.")
121135

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"
136+
raylib_include_path = os.getenv("RAYLIB_INCLUDE_PATH")
137+
if raylib_include_path is None:
138+
raylib_include_path = get_the_include_path_from_pkgconfig()
139+
raylib_h = raylib_include_path + "/raylib.h"
140+
rlgl_h = raylib_include_path + "/rlgl.h"
141+
raymath_h = raylib_include_path + "/raymath.h"
125142

126143
if not os.path.isfile(raylib_h):
127-
raise Exception("ERROR: " + raylib_h + " not found. Please install Raylib.")
144+
raise Exception("ERROR: " + raylib_h + " not found. Please install Raylib or set RAYLIB_INCLUDE_PATH.")
128145

129146
if not os.path.isfile(rlgl_h):
130-
raise Exception("ERROR: " + rlgl_h + " not found. Please install Raylib.")
147+
raise Exception("ERROR: " + rlgl_h + " not found. Please install Raylib or set RAYLIB_INCLUDE_PATH.")
131148

132149
if not os.path.isfile(raymath_h):
133-
raise Exception("ERROR: " + raylib_h + " not found. Please install Raylib.")
150+
raise Exception("ERROR: " + raylib_h + " not found. Please install Raylib or set RAYLIB_INCLUDE_PATH.")
134151

135152
ffi_includes = """
136153
#include "raylib.h"
137154
#include "rlgl.h"
138155
#include "raymath.h"
139156
"""
140157

141-
glfw3_h = get_the_include_path() + "/GLFW/glfw3.h"
158+
glfw_include_path = os.getenv("GLFW_INCLUDE_PATH")
159+
if glfw_include_path is None:
160+
glfw_include_path = get_the_include_path_from_pkgconfig()
161+
glfw3_h = glfw_include_path + "/GLFW/glfw3.h"
142162
if RAYLIB_PLATFORM=="Desktop" and check_header_exists(glfw3_h):
143163
ffi_includes += """
144164
#include "GLFW/glfw3.h"
145165
"""
146166

147-
raygui_h = get_the_include_path() + "/raygui.h"
167+
raygui_include_path = os.getenv("RAYGUI_INCLUDE_PATH")
168+
if raygui_include_path is None:
169+
raygui_include_path = get_the_include_path_from_pkgconfig()
170+
raygui_h = raygui_include_path + "/raygui.h"
148171
if check_header_exists(raygui_h):
149172
ffi_includes += """
150173
#define RAYGUI_IMPLEMENTATION
151174
#define RAYGUI_SUPPORT_RICONS
152175
#include "raygui.h"
153176
"""
154177

155-
physac_h = get_the_include_path() + "/physac.h"
178+
physac_include_path = os.getenv("PHYSAC_INCLUDE_PATH")
179+
if physac_include_path is None:
180+
physac_include_path = get_the_include_path_from_pkgconfig()
181+
physac_h = physac_include_path + "/physac.h"
156182
if check_header_exists(physac_h):
157183
ffi_includes += """
158184
#define PHYSAC_IMPLEMENTATION
@@ -173,7 +199,11 @@ def build_unix():
173199

174200
if platform.system() == "Darwin":
175201
print("BUILDING FOR MAC")
176-
extra_link_args = [get_the_lib_path() + '/libraylib.a', '-framework', 'OpenGL', '-framework', 'Cocoa',
202+
flags = os.getenv("RAYLIB_LINK_ARGS")
203+
if flags is None:
204+
flags = get_the_lib_path_from_pkgconfig() + '/libraylib.a'
205+
# We use /usr/local/lib/libraylib.a to ensure we link to static version
206+
extra_link_args = flags.split() + ['-framework', 'OpenGL', '-framework', 'Cocoa',
177207
'-framework', 'IOKit', '-framework', 'CoreFoundation', '-framework',
178208
'CoreVideo']
179209
if RAYLIB_PLATFORM=="SDL":
@@ -183,12 +213,18 @@ def build_unix():
183213
extra_compile_args = ["-Wno-error=incompatible-function-pointer-types", "-D_CFFI_NO_LIMITED_API"]
184214
else: #platform.system() == "Linux":
185215
print("BUILDING FOR LINUX")
186-
extra_link_args = get_lib_flags() + [ '-lm', '-lpthread', '-lGL',
216+
flags = os.getenv("RAYLIB_LINK_ARGS")
217+
if flags is None:
218+
flags = get_lib_flags_from_pkgconfig()
219+
extra_link_args = flags.split() + [ '-lm', '-lpthread', '-lGL',
187220
'-lrt', '-lm', '-ldl', '-lpthread', '-latomic']
188221
if RAYLIB_PLATFORM=="SDL":
189222
extra_link_args += ['-lX11','-lSDL2']
190223
elif RAYLIB_PLATFORM=="DRM":
191224
extra_link_args += ['-lEGL', '-lgbm']
225+
elif RAYLIB_PLATFORM=="PLATFORM_COMMA":
226+
extra_link_args.remove('-lGL')
227+
extra_link_args += ['-lGLESv2', '-lEGL', '-lwayland-client', '-lwayland-egl']
192228
else:
193229
extra_link_args += ['-lX11']
194230
extra_compile_args = ["-Wno-incompatible-pointer-types", "-D_CFFI_NO_LIMITED_API"]
@@ -201,7 +237,7 @@ def build_unix():
201237
ffibuilder.set_source("raylib._raylib_cffi",
202238
ffi_includes,
203239
py_limited_api=False,
204-
include_dirs=[get_the_include_path()],
240+
include_dirs=[get_the_include_path_from_pkgconfig()],
205241
extra_link_args=extra_link_args,
206242
extra_compile_args=extra_compile_args,
207243
libraries=libraries)

0 commit comments

Comments
 (0)