-
Notifications
You must be signed in to change notification settings - Fork 8
/
gyp_configure.py
executable file
·381 lines (323 loc) · 11.3 KB
/
gyp_configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python2
import glob
import platform
import os
import subprocess
import sys
import argparse
try:
import multiprocessing.synchronize
gyp_parallel_support = True
except ImportError:
gyp_parallel_support = False
CC = os.environ.get('CC', 'cc')
script_dir = os.path.dirname(__file__)
rig_root = os.path.normpath(script_dir)
output_dir = os.path.join(os.path.abspath(rig_root), 'out')
sys.path.insert(0, os.path.join(rig_root, 'build', 'gyp', 'pylib'))
try:
import gyp
except ImportError:
print('You need to install gyp in build/gyp first. See the README.')
sys.exit(42)
def host_arch():
machine = platform.machine()
if machine == 'i386': return 'ia32'
if machine == 'x86_64': return 'x64'
if machine.startswith('arm'): return 'arm'
return machine # Return as-is and hope for the best.
def pkg_config_exists(pkg_name):
try:
subprocess.check_call(["pkg-config", "--exists", pkg_name])
return True
except:
return False
def pkg_config_get(pkg_name, arg):
return subprocess.check_output(["pkg-config", arg, pkg_name])
def pkg_config_variable(pkg_name, variable):
return subprocess.check_output(["pkg-config", "--variable=" + variable, pkg_name]).strip()
def run_gyp(args):
rc = gyp.main(args)
if rc != 0:
print('Error running GYP')
sys.exit(rc)
def add_option(parser, name, opt):
if opt["enabled"] and "pkg-config" in opt and platform.system() == "Linux":
opt["enabled"] = pkg_config_exists(opt["pkg-config"])
if not opt["enabled"]:
enable_help = "Enable " + opt["help"]
disable_help = argparse.SUPPRESS
else:
enable_help = argparse.SUPPRESS
disable_help = "Disable " + opt["help"]
hyphen_name = name.replace('_', '-')
parser.add_argument("--enable-" + hyphen_name, dest="enable_" + name, action="store_true", default=opt["enabled"], help=enable_help)
parser.add_argument("--disable-" + hyphen_name, dest="enable_" + name, action="store_false", default=not opt["enabled"], help=disable_help)
parser = argparse.ArgumentParser()
parser.add_argument("--prefix",
help="install architecture-independent files in PREFIX",
default="/usr/local")
parser.add_argument("--host",
help="cross-compile to build programs to run on HOST")
parser.add_argument("--enable-shared", action='store_true',
help="build shared libraries")
subst = {
"CG_DEFINES": "",
}
enabled = {}
options = {
"_": {
"enabled": True,
"pkg-config": "libpng mozjs-24 gdk-pixbuf-2.0",
},
"debug": {
"help": "debug support",
"enabled": False,
"defines": [ "RIG_ENABLE_DEBUG", "C_DEBUG", "CG_GL_DEBUG", "CG_OBJECT_DEBUG", "CG_ENABLE_DEBUG" ]
},
# "opencv": {
# "help": "OpenCV support",
# "enabled": True,
# "pkg-config": "opencv >= 3.0.0",
# "defines": [ "USE_OPENCV" ]
# },
"oculus_rift": {
"help": "OculusRift support",
"enabled": True,
"defines": [ "ENABLE_OCULUS_RIFT" ],
},
"ffmpeg": {
"help": "ffmpeg support",
"enabled": True,
"pkg-config": "libavcodec libavformat libavutil libswscale libswresample",
"defines": [ "USE_FFMPEG" ]
},
"alsa": {
"help": "Alsa audio support",
"enabled": True,
"pkg-config": "alsa",
"defines": [ "USE_ALSA" ]
},
# "uv": {
# "help": "libuv support",
# "enabled": True,
# "pkg-config": "libuv",
# "public_defines": { "CG_HAS_UV_SUPPORT" },
# "defines": [ "USE_UV" ]
# },
"glib": {
"help": "GLib support",
"enabled": False,
"pkg-config": "glib-2.0",
"public_defines": { "CG_HAS_GLIB_SUPPORT" },
"defines": [ "USE_GLIB" ]
},
"x11": {
"enabled": True,
"pkg-config": "x11 xdamage xcomposite xfixes xext xrandr xkbcommon-x11 x11-xcb",
"public_defines": { "CG_HAS_XLIB_SUPPORT", "CG_HAS_X11_SUPPORT" },
"ldflags": [ "-lXi" ],
"defines": { "USE_X11" }
},
"glx": {
"requires": { "x11" }, #TODO check
"help": "GLX support",
"enabled": True,
"pkg-config": "gl",
"public_defines": { "CG_HAS_GLX_SUPPORT", "CG_HAS_GL_SUPPORT", "CG_HAS_XLIB_SUPPORT", "CG_HAS_X11_SUPPORT" },
"conditionals": { "enable_glx", "enable_gl" }
},
"egl": {
"help": "EGL support",
"enabled": True,
"pkg-config": "egl",
"public_defines": { "CG_HAS_EGL_SUPPORT" },
"conditionals": { "enable_gl" }
},
"egl_wayland": {
"help": "Wayland EGL support",
"enabled": True,
"pkg-config": "wayland-egl",
"public_defines": { "CG_HAS_EGL_PLATFORM_WAYLAND_SUPPORT" },
},
"egl_kms": {
"help": "Kernel Mode Setting EGL support",
"enabled": False,
"pkg-config": "egl gbm libdrm",
"public_defines": { "CG_HAS_EGL_PLATFORM_KMS_SUPPORT" },
},
"egl_xlib": {
"help": "XLib EGL support",
"enabled": True,
"pkg-config": "egl x11",
"public_defines": { "CG_HAS_EGL_PLATFORM_XLIB_SUPPORT" },
},
"sdl": {
"help": "SDL 2.0 support",
"enabled": False,
"pkg-config": "sdl2",
"public_defines": { "CG_HAS_SDL_SUPPORT" },
},
"gles2": {
"help": "GLES 2.0 support",
"enabled": True,
"pkg-config": "glesv2",
"public_defines": { "CG_HAS_GLES2_SUPPORT" },
},
"ncurses": {
"help": "ncurses debug console",
"enabled": True,
"pkg-config": "ncursesw",
"defines": [ "USE_NCURSES" ],
},
}
output = {
"cglib/cglib/cg-defines.h",
}
for name, opt in options.items():
if "help" in opt:
add_option(parser, name, opt)
if "defines" not in opt:
opt["defines"] = []
if "includes" not in opt:
opt["includes"] = []
if "ldflags" not in opt:
opt["ldflags"] = []
opt_args = parser.parse_args()
opt_args_dict = vars(opt_args)
gyp_args = []
options['_']["defines"].append("ICU_DATA_DIR=\"" + pkg_config_variable("icu-uc", "prefix") + "/share\"")
options['_']["defines"].append("RIG_BIN_DIR=\"" + opt_args.prefix + "/bin\"")
for name, opt in options.items():
if "enable_" + name in opt_args_dict:
if opt_args_dict["enable_" + name]:
if "pkg-config" in opt:
if not pkg_config_exists(opt["pkg-config"]):
sys.exit(opt["pkg-config"] + " required for enabling " + name + " support is missing")
opt["enabled"] = True
enabled[name] = opt
else:
if opt["enabled"]:
enabled[name] = opt
for name, opt in enabled.items():
if "pkg-config" in opt:
cflags = pkg_config_get(opt["pkg-config"], "--cflags")
tokens = cflags.split()
if len(tokens):
for tok in tokens:
if tok[:2] == "-I":
opt["includes"].append(tok[2:])
elif tok[:2] == "-D":
opt["defines"].append(tok[2:])
ldflags = pkg_config_get(opt["pkg-config"], "--libs")
tokens = ldflags.split()
if len(tokens):
for tok in tokens:
opt["ldflags"].append(tok)
gyp_config = open("config.gypi", "w")
gyp_config.write("{\n")
gyp_config.write(" 'variables': {\n")
for name, opt in options.items():
gyp_config.write(" 'enable_" + name + "%': '0',\n")
gyp_config.write(" },\n")
gyp_config.write("""
'target_defaults': {
'include_dirs': [
""")
for name, opt in enabled.items():
if "includes" in opt:
gyp_config.write(" # " + name + " includes...\n")
for include in opt["includes"]:
gyp_config.write(" '" + include + "',\n")
gyp_config.write("""
],
'defines': [
""")
for name, opt in enabled.items():
if "defines" in opt:
gyp_config.write(" # " + name + " defines...\n")
for define in opt["defines"]:
gyp_config.write(" '" + define + "',\n")
gyp_config.write("""
],
'ldflags': [
""")
for name, opt in enabled.items():
if "ldflags" in opt:
gyp_config.write(" # " + name + " ldflags...\n")
for flag in opt["ldflags"]:
gyp_config.write(" '" + flag + "',\n")
gyp_config.write("""
]}
""")
gyp_config.write("}\n")
for name, opt in enabled.items():
if "public_defines" in opt:
defines = opt["public_defines"]
for define in defines:
subst["CG_DEFINES"] = subst["CG_DEFINES"] + "#define " + define + "\n"
gyp_args.append(os.path.join(rig_root, 'rig.gyp'))
gyp_args.append('-Dis_nodejs_build=0')
gyp_args.append('-Dlibrary=shared_library')
gyp_args.append('--debug=all')
gyp_args.append('--check')
gyp_args.extend(['-I', "config.gypi"])
gyp_args.extend(['-I', "common.gypi"])
options_fn = os.path.join(rig_root, 'options.gypi')
if os.path.exists(options_fn):
gyp_args.extend(['-I', options_fn])
gyp_args.append('--depth=' + rig_root)
if opt_args.enable_shared:
gyp_args.append('-Drig_library=shared_library')
# There's a bug with windows which doesn't allow this feature.
if sys.platform != 'win32':
if '-f' not in gyp_args:
gyp_args.extend('-f ninja'.split())
if 'eclipse' not in gyp_args and 'ninja' not in gyp_args:
gyp_args.extend(['-Goutput_dir=' + output_dir])
gyp_args.extend(['--generator-output', output_dir])
if not any(a.startswith('-Dhost_arch=') for a in gyp_args):
gyp_args.append('-Dhost_arch=%s' % host_arch())
if not any(a.startswith('-Dtarget_arch=') for a in gyp_args):
gyp_args.append('-Dtarget_arch=%s' % host_arch())
if os.name == "posix":
gyp_args.append('-Dos_posix=1')
if sys.platform == 'win32':
# Override the Gyp default and require a half-modern C compiler...
if not os.environ.get('GYP_MSVS_VERSION'):
os.environ['GYP_MSVS_VERSION'] = '2015'
if opt_args.host != None and "emscripten" in opt_args.host:
gyp_args.append('-DOS=emscripten')
else:
gyp_args.append('-Denable_uv')
subst["CG_DEFINES"] = subst["CG_DEFINES"] + "#define CG_HAS_UV_SUPPORT\n"
for name in enabled:
gyp_args.append("-Denable_" + name)
gyp_config.close()
for file in output:
file_contents = None
with open(file + ".in", 'r') as fp:
file_contents = fp.read()
for key, val in subst.items():
file_contents = file_contents.replace("@" + key + "@", val)
try:
fp = open(file, 'r')
current_contents = fp.read()
except:
current_contents = ""
finally:
fp.close()
if current_contents == file_contents:
print(file + " unchanged")
else:
with open(file, 'w') as fp:
print("creating " + file)
fp.write(file_contents)
print("")
gyp_args = list(gyp_args)
print("Running gyp:")
print("$ gyp " + " ".join(gyp_args))
run_gyp(gyp_args)
if "ninja" in gyp_args:
print("\n\nReady to build:")
print("$ ninja -C ./out/Debug")