forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_module.py
More file actions
299 lines (266 loc) · 10.2 KB
/
build_module.py
File metadata and controls
299 lines (266 loc) · 10.2 KB
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
# 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
from common import *
import sys
import platform
import Cython
import os
# Cython options. Stop on first error, otherwise hundreds
# of errors appear in the console.
Options.fast_fail = True
def get_winsdk_lib():
print("[build_module.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("[build_module.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:
#
# cefpython.h(36): warning C4190: 'RequestHandler_GetResourceHandler'
# has C-linkage specified, but returns UDT 'CefRefPtr<T>' which is
# incompatible with C
#
# The C4190 warning is disabled with pragma in cefpython.h, see the
# fix_cefpython_h() in the build.py script.
extra_compile_args.extend(['/EHsc'])
extra_link_args.extend(['/ignore:4217'])
if LINUX:
if len(sys.argv) > 1 and "--fast" in sys.argv:
sys.argv.remove("--fast")
# Fast mode disables optimization flags
print("[build_module.py] FAST mode On")
extra_compile_args.extend(['-flto', '-std=gnu++11'])
extra_link_args.extend(['-flto'])
else:
# 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/ .
extra_compile_args.extend(['-flto', '-fdata-sections',
'-ffunction-sections', '-std=gnu++11'])
extra_link_args.extend(['-flto', '-Wl,--gc-sections'])
if MAC:
os.environ["CC"] = "gcc"
os.environ["CXX"] = "g++"
options["extra_compile_args"] = extra_compile_args
options["extra_link_args"] = extra_link_args
def get_include_dirs():
print("[build_module.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("[build_module.py] Prepare library directories")
library_dirs = [
os.path.join(CEF_BINARY, "lib"),
]
if WINDOWS:
library_dirs.extend([
get_winsdk_lib(),
os.path.join(SRC_DIR, "client_handler",
"Release_py{pyver}_{os}"
.format(pyver=PYVERSION, os=OS_POSTFIX2)),
os.path.join(SRC_DIR, "subprocess",
"Release_{os}"
.format(os=OS_POSTFIX2)),
os.path.join(SRC_DIR, "cpp_utils",
"Release_py{os}"
.format(os=OS_POSTFIX2))
])
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("[build_module.py] Prepare libraries")
libraries = list()
if WINDOWS:
libraries.extend([
"libcef",
"libcef_dll_wrapper_md",
"User32",
"client_handler_py{pyver}_{os}".format(
pyver=PYVERSION, os=OS_POSTFIX2),
"libcefpythonapp_py{pyver}_{os}".format(
pyver=PYVERSION, os=OS_POSTFIX2),
"cpp_utils_{os}".format(
os=OS_POSTFIX2),
])
elif MAC:
libraries.extend([
'client_handler',
'cef_dll_wrapper',
'cefpythonapp',
'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(
"cefpython_py%s" % PYVERSION,
["cefpython.pyx"],
# 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("[build_module.py] Generate compile_time_constants.pxi")
with open(os.path.join(SRC_DIR, "compile_time_constants.pxi"), "w") as fd:
fd.write('# This file was generated by setup.py\n')
# A way around Python 3.2 bug: UNAME_SYSNAME is not set
fd.write('DEF UNAME_SYSNAME = "%s"\n' % platform.uname()[0])
fd.write('DEF PY_MAJOR_VERSION = %s\n' % sys.version_info.major)
def main():
print("[build_module.py] Cython version: %s" % Cython.__version__)
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("[build_module.py] Execute setup()")
setup(
name='cefpython_py%s' % PYVERSION,
cmdclass={'build_ext': build_ext},
ext_modules=get_ext_modules(options)
)
if __name__ == "__main__":
main()