-
-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathcython_setup.py
421 lines (370 loc) · 14.3 KB
/
cython_setup.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Copyright (c) 2017 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
"""
cython_setup.py is for internal use only - called by build.py.
This is Cython's setup for building the cefpython module
"""
# Use setuptools so that "Visual C++ compiler for Python 2.7" tools
# can be used. Otherwise "Unable to find vcvarsall.bat" error occurs.
try:
from setuptools import setup
from setuptools import Extension
except ImportError:
from distutils.core import setup
from distutils.extension import Extension
# Use "Extension" from Cython.Distutils so that "cython_directives" works
from Cython.Distutils import build_ext, Extension
from Cython.Compiler import Options
# noinspection PyUnresolvedReferences
from Cython.Compiler.ModuleNode import ModuleNode
from common import *
import sys
import platform
import Cython
import os
# Must monkey patch Cython's ModuleNode to inject custom C++ code
# in the generated cefpython.cpp. This is a fix for an error on Mac:
# > ImportError: dynamic module does not define init function
# To get rid of CEF's undefined symbol error when importing module
# it was required to pass "-fvisibility=hidden" and "-Wl,-dead_strip"
# flags. However these flags cause the "initcefpython_py27" Python
# Module Initialization function to be hidden as well. To workaround
# this it is required to add default visibility attribute to the
# signature of that init function.
#
# Original definition in Python 2.7:
# | https://github.com/python/cpython/blob/2.7/Include/pyport.h
# > define PyMODINIT_FUNC extern "C" __declspec(dllexport) void
#
# Original definition in Python 3.4 / 3.5 / 3.6:
# > define PyMODINIT_FUNC extern "C" __declspec(dllexport) PyObject*
if MAC:
g_generate_extern_c_macro_definition_old = (
ModuleNode.generate_extern_c_macro_definition)
def generate_extern_c_macro_definition(self, code):
# This code is written by Cython to both cefpython API header file
# and cefpython module cpp file.
g_generate_extern_c_macro_definition_old(self, code)
code.putln("// Added by: cefpython/tools/cython_setup.py")
code.putln("#undef PyMODINIT_FUNC")
if sys.version_info[:2] == (2, 7):
code.putln("#define PyMODINIT_FUNC extern \"C\""
" __attribute__((visibility(\"default\"))) void")
else:
code.putln("#define PyMODINIT_FUNC extern \"C\""
" __attribute__((visibility(\"default\"))) PyObject*")
# Overwrite Cython function
ModuleNode.generate_extern_c_macro_definition = (
generate_extern_c_macro_definition)
# Constants
FAST_FLAG = False
# Cython options. Stop on first error, otherwise hundreds
# of errors appear in the console.
Options.fast_fail = True
def main():
print("[cython_setup.py] Python version: {ver} {arch}"
.format(ver=platform.python_version(), arch=ARCH_STR))
print("[cython_setup.py] Python executable: %s" % sys.executable)
print("[cython_setup.py] Cython version: %s" % Cython.__version__)
global FAST_FLAG
if len(sys.argv) > 1 and "--fast" in sys.argv:
# Fast mode disables optimization flags
print("[cython_setup.py] FAST mode enabled")
FAST_FLAG = True
sys.argv.remove("--fast")
if len(sys.argv) <= 1:
print(__doc__)
sys.exit(1)
compile_time_constants()
options = dict()
set_compiler_options(options)
options["include_dirs"] = get_include_dirs()
options["library_dirs"] = get_library_dirs()
options["libraries"] = get_libraries()
print("[cython_setup.py] Execute setup()")
setup(
name="cefpython_py{pyver}".format(pyver=PYVERSION),
cmdclass={"build_ext": build_ext},
ext_modules=get_ext_modules(options)
)
def get_winsdk_lib():
print("[cython_setup.py] Detect Windows SDK library directory")
ret = ""
if WINDOWS:
if ARCH32:
winsdk_libs = [
r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Lib",
r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Lib",
]
elif ARCH64:
winsdk_libs = [
r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Lib\\x64",
r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Lib\\x64",
]
else:
raise Exception("Unknown architecture")
for lib in winsdk_libs:
if os.path.exists(lib):
ret = lib
break
if not ret:
ret = winsdk_libs[0]
if not os.path.exists(ret):
raise Exception("Windows SDK Lib directory not found: %s"
% ret)
return ret
def set_compiler_options(options):
"""Extends options and also sets environment variables."""
print("[cython_setup.py] Set compiler options")
extra_compile_args = list()
extra_link_args = list()
if WINDOWS:
# /EHsc - using STL string, multimap and others that use
# C++ exceptions.
#
# /ignore:4217 - disable warnings such as this:
#
# client_handler_py27_32bit.lib(client_handler.obj): warning LNK4217:
# locally defined symbol _RemovePythonCallbacksForFrame imported in
# function "public: virtual bool __thiscall
# ClientHandler::OnProcessMessageReceived
#
# The above warning LNK4217 is caused by the warning below which occurs
# when building the client_handler.lib static library:
extra_compile_args.extend(["/EHsc"])
extra_link_args.extend(["/ignore:4217"])
if LINUX or MAC:
# Compiler flags
if FAST_FLAG:
extra_compile_args.append("-O0")
else:
extra_compile_args.append("-O3")
extra_compile_args.extend([
"-DNDEBUG",
"-std=gnu++11",
])
if LINUX:
os.environ["CC"] = "g++"
os.environ["CXX"] = "g++"
extra_compile_args.extend(["-std=gnu++11"])
# Fix "ImportError ... undefined symbol ..." caused by CEF's
# include/base/ headers by adding the -flto flag (Issue #230).
# Unfortunately -flto prolongs compilation time significantly.
# More on the other flags: https://stackoverflow.com/questions/
# 6687630/ .
if FAST_FLAG:
extra_compile_args.extend(["-flto"])
extra_link_args.extend(["-flto"])
else:
extra_compile_args.extend(["-flto",
"-fdata-sections",
"-ffunction-sections"])
extra_link_args.extend(["-flto",
"-Wl,--gc-sections"])
if MAC:
# Compiler environment variables
os.environ["CC"] = "c++"
os.environ["CXX"] = "c++"
# COMPILER ARGS
# -Wno-return-type-c-linkage to ignore:
# > warning: 'somefunc' has C-linkage specified, but returns
# > user-defined type 'sometype' which is incompatible with C
#
# -Wno-constant-logical-operand to ignore:
# > warning: use of logical '||' with constant operand
extra_compile_args.extend([
# Compile against libc++ otherwise error "symbol not found"
# with cef::logging::LogMessage symbol. Also include -lc++
# and -lc++abi libraries.
"-stdlib=libc++",
"-Wno-return-type-c-linkage",
"-Wno-constant-logical-operand",
])
# From upstream CEF cefclient
extra_compile_args.extend([
"-fno-strict-aliasing",
"-fno-rtti",
"-fno-threadsafe-statics",
"-fobjc-call-cxx-cdtors",
# Visibility of symbols:
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
])
# Visibility of symbols
extra_compile_args.extend([
# "-flto",
# "-fdata-sections",
# "-ffunction-sections",
])
# LINKER ARGS
extra_link_args.extend([
"-mmacosx-version-min=10.9",
"-Wl,-search_paths_first",
"-F"+os.path.join(CEF_BINARIES_LIBRARIES, "bin"),
"-framework", "Chromium Embedded Framework",
"-Wl,-rpath,@loader_path/", # ending slash is crucial!
])
if not FAST_FLAG:
extra_link_args.extend([
# "-force_flat_namespace",
# "-flto",
"-Wl,-dead_strip",
])
options["extra_compile_args"] = extra_compile_args
options["extra_link_args"] = extra_link_args
def get_include_dirs():
print("[cython_setup.py] Prepare include directories")
include_dirs = list()
common_include_dirs = [
SRC_DIR,
os.path.join(SRC_DIR, "common"),
os.path.join(SRC_DIR, "extern"),
os.path.join(SRC_DIR, "extern", "cef")
]
if WINDOWS:
include_dirs.extend([WINDOWS_DIR])
include_dirs.extend(common_include_dirs)
elif MAC:
include_dirs.extend([MAC_DIR])
include_dirs.extend(common_include_dirs)
# TODO: Check these directories, are these really required on Mac?
include_dirs.extend([
'/usr/include/gtk-2.0',
'/usr/include/glib-2.0',
'/usr/include/gtk-unix-print-2.0',
'/usr/include/cairo',
'/usr/include/pango-1.0',
'/usr/include/gdk-pixbuf-2.0',
'/usr/include/atk-1.0',
# Fedora
'/usr/lib64/gtk-2.0/include',
'/usr/lib64/gtk-unix-print-2.0',
'/usr/lib64/glib-2.0/include',
'/usr/lib/gtk-2.0/include',
'/usr/lib/gtk-2.0/gtk-unix-print-2.0',
'/usr/lib/glib-2.0/include',
])
elif LINUX:
include_dirs.extend([LINUX_DIR])
include_dirs.extend(common_include_dirs)
include_dirs.extend([
'/usr/include/gtk-2.0',
'/usr/include/glib-2.0',
'/usr/include/gtk-unix-print-2.0',
'/usr/include/cairo',
'/usr/include/pango-1.0',
'/usr/include/gdk-pixbuf-2.0',
'/usr/include/atk-1.0',
# Ubuntu
'/usr/lib/x86_64-linux-gnu/gtk-2.0/include',
'/usr/lib/x86_64-linux-gnu/gtk-unix-print-2.0',
'/usr/lib/x86_64-linux-gnu/glib-2.0/include',
'/usr/lib/i386-linux-gnu/gtk-2.0/include',
'/usr/lib/i386-linux-gnu/gtk-unix-print-2.0',
'/usr/lib/i386-linux-gnu/glib-2.0/include',
# Fedora
'/usr/lib64/gtk-2.0/include',
'/usr/lib64/gtk-unix-print-2.0',
'/usr/lib64/glib-2.0/include',
'/usr/lib/gtk-2.0/include',
'/usr/lib/gtk-2.0/gtk-unix-print-2.0',
'/usr/lib/glib-2.0/include',
])
return include_dirs
def get_library_dirs():
print("[cython_setup.py] Prepare library directories")
library_dirs = [
os.path.join(CEF_BINARIES_LIBRARIES, "lib"),
os.path.join(CEF_BINARIES_LIBRARIES, "lib",
get_msvs_for_python(vs_prefix=True)),
]
if WINDOWS:
library_dirs.extend([
get_winsdk_lib(),
BUILD_CEFPYTHON_APP,
BUILD_CLIENT_HANDLER,
BUILD_CPP_UTILS,
])
if MAC:
library_dirs.append(os.path.join(CEF_BINARIES_LIBRARIES, "bin"))
if MAC or LINUX:
library_dirs.extend([
os.path.join(SRC_DIR, "client_handler"),
os.path.join(SRC_DIR, "subprocess"), # libcefpythonapp
os.path.join(SRC_DIR, "cpp_utils"),
])
return library_dirs
def get_libraries():
print("[cython_setup.py] Prepare libraries")
libraries = list()
if WINDOWS:
libraries.extend([
"libcef",
"libcef_dll_wrapper_MD",
"User32",
"cefpython_app",
"cpp_utils",
"client_handler",
])
elif MAC:
libraries.extend([
"c++",
"c++abi",
"cef_dll_wrapper",
"cefpythonapp",
"client_handler",
"cpp_utils",
])
elif LINUX:
libraries.extend([
"X11",
"gobject-2.0",
"glib-2.0",
"gtk-x11-2.0",
# CEF and CEF Python libraries
"cef_dll_wrapper",
"cefpythonapp",
"client_handler",
"cpp_utils",
])
return libraries
def get_ext_modules(options):
ext_modules = [Extension(
name=MODULE_NAME_NOEXT,
sources=["cefpython_py{pyver}.pyx".format(pyver=PYVERSION)],
# Ignore the warning in the console:
# > C:\Python27\lib\distutils\extension.py:133: UserWarning:
# > Unknown Extension options: 'cython_directives' warnings.warn(msg)
cython_directives={
# Any conversion to unicode must be explicit using .decode().
"c_string_type": "bytes",
"c_string_encoding": "utf-8",
},
language="c++",
include_dirs=options["include_dirs"],
library_dirs=options["library_dirs"],
# Static libraries only. Order is important, if library A depends on B,
# then B must be included before A.
libraries=options["libraries"],
# When you put "./" in here, loading of libcef.so will only work when
# running scripts from the same directory that libcef.so resides in.
# runtime_library_dirs=[
# './'
# ],
extra_compile_args=options["extra_compile_args"],
extra_link_args=options["extra_link_args"],
# Defining macros:
# define_macros = [("UNICODE","1"), ("_UNICODE","1"), ]
)]
return ext_modules
def compile_time_constants():
print("[cython_setup.py] Generate compile_time_constants.pxi")
with open(os.path.join(SRC_DIR, "compile_time_constants.pxi"), "wb") as fo:
contents = "# This file was generated by setup.py\n"
# A way around Python 3.2 bug: UNAME_SYSNAME is not set
contents += 'DEF UNAME_SYSNAME = "%s"\n' % platform.uname()[0]
contents += 'DEF PY_MAJOR_VERSION = %s\n' % sys.version_info.major
fo.write(contents.encode("utf-8"))
if __name__ == "__main__":
main()