forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
325 lines (268 loc) · 10.1 KB
/
compile.py
File metadata and controls
325 lines (268 loc) · 10.1 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
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
"""
Build the cefpython module, install package and run example.
Usage:
compile.py VERSION [--debug] [--fast]
Options:
VERSION Version in format xx.xx
--debug Debug mode
--fast Fast mode, don't delete C++ .o .a files, nor the setup/build/
directory, and disable optimization flags when building
the so/pyd module.
--kivy Run Kivy example
"""
# TODO: Check Cython version using info from tools/requirements.txt
import sys
import os
import glob
import shutil
import subprocess
import platform
import re
import struct
# raw_input() was renamed to input() in Python 3
try:
# noinspection PyUnresolvedReferences
# noinspection PyShadowingBuiltins
input = raw_input
except NameError:
pass
def check_cython_version():
with open("../../tools/requirements.txt", "r") as fileobj:
contents = fileobj.read()
match = re.search(r"cython\s*==\s*([\d.]+)", contents,
flags=re.IGNORECASE)
assert match, "cython package not found in requirements.txt"
require_version = match.group(1)
try:
import Cython
version = Cython.__version__
except ImportError:
# noinspection PyUnusedLocal
Cython = None
print("ERROR: Cython is not installed ({0} required)"
.format(require_version))
sys.exit(1)
if version != require_version:
print("ERROR: Wrong Cython version: {0}. Required: {1}"
.format(version, require_version))
sys.exit(1)
print("Cython version: {0}".format(version))
check_cython_version()
# This will not show "Segmentation fault" error message:
# | subprocess.call(["python", "./wxpython.py"])
# You need to call it with shell=True for this kind of
# error message to be shown:
# | subprocess.call("python wxpython.py", shell=True)
# How to debug:
# 1. Install "python-dbg" package
# 2. Install "python-wxgtk2.8-dbg" package
# 3. Run "python compile.py debug"
# 4. In cygdb type "cy run"
# 5. To display debug backtrace type "cy bt"
# 6. More commands: http://docs.cython.org/src/userguide/debugging.html
# -- debug flag
if len(sys.argv) > 1 and "--debug" in sys.argv:
DEBUG_FLAG = True
print("DEBUG mode On")
else:
DEBUG_FLAG = False
# --fast flag
if len(sys.argv) > 1 and "--fast" in sys.argv:
# Fast mode doesn't delete C++ .o .a files.
# Fast mode also disables optimization flags in setup/setup.py .
FAST_FLAG = True
print("FAST mode On")
else:
FAST_FLAG = False
# --kivy flag
if len(sys.argv) > 1 and "--kivy" in sys.argv:
KIVY_FLAG = True
print("KIVY_FLAG flag enabled")
else:
KIVY_FLAG = False
# version arg
if len(sys.argv) > 1 and re.search(r"^\d+\.\d+$", sys.argv[1]):
VERSION = sys.argv[1]
else:
print("[compile.py] ERROR: expected first argument to be version number")
print(" Allowed version format: \\d+\.\\d+")
sys.exit(1)
print("VERSION=%s" % VERSION)
# Architecture and OS postfixes
ARCH32 = (8 * struct.calcsize('P') == 32)
ARCH64 = (8 * struct.calcsize('P') == 64)
OS_POSTFIX = ("win" if platform.system() == "Windows" else
"linux" if platform.system() == "Linux" else
"mac" if platform.system() == "Darwin" else "unknown")
OS_POSTFIX2 = "unknown"
if OS_POSTFIX == "win":
OS_POSTFIX2 = "win32" if ARCH32 else "win64"
elif OS_POSTFIX == "mac":
OS_POSTFIX2 = "mac32" if ARCH32 else "mac64"
elif OS_POSTFIX == "linux":
OS_POSTFIX2 = "linux32" if ARCH32 else "linux64"
PYVERSION = str(sys.version_info[0])+str(sys.version_info[1])
print("PYVERSION = %s" % PYVERSION)
print("OS_POSTFIX2 = %s" % OS_POSTFIX2)
# Directories
LINUX_DIR = os.path.abspath(os.path.dirname(__file__))
SRC_DIR = os.path.abspath(os.path.join(LINUX_DIR, ".."))
CPP_UTILS_DIR = os.path.abspath(os.path.join(SRC_DIR, "cpp_utils"))
CLIENT_HANDLER_DIR = os.path.abspath(os.path.join(SRC_DIR, "client_handler"))
SUBPROCESS_DIR = os.path.abspath(os.path.join(SRC_DIR, "subprocess"))
SETUP_DIR = os.path.abspath(os.path.join(LINUX_DIR, "setup"))
CEFPYTHON_DIR = os.path.abspath(os.path.join(SRC_DIR, ".."))
BUILD_DIR = os.path.abspath(os.path.join(CEFPYTHON_DIR, "build"))
CEF_BINARY = os.path.abspath(os.path.join(BUILD_DIR, "cef_"+OS_POSTFIX2))
CEFPYTHON_BINARY = os.path.abspath(os.path.join(BUILD_DIR,
"cefpython_"+OS_POSTFIX2))
# Create directories if necessary
if not os.path.exists(CEFPYTHON_BINARY):
os.mkdir(CEFPYTHON_BINARY)
# Check directories
assert os.path.exists(CEF_BINARY)
assert os.path.exists(CEFPYTHON_BINARY)
print("Compiling C++ projects")
# Need to allow continuing even when make fails, as it may
# fail because the "public" function declaration is not yet
# in "cefpython.h", but for it to be generated we need to run
# cython compiling, so in this case you continue even when make
# fails and then run the compile.py script again and this time
# make should succeed.
# -------- CPP_UTILS_DIR
os.chdir(CPP_UTILS_DIR)
if not FAST_FLAG:
subprocess.call("rm -f *.o *.a", shell=True)
ret = subprocess.call("make -f Makefile", shell=True)
if ret != 0:
# noinspection PyUnboundLocalVariable
what = input("make failed, press 'y' to continue, 'n' to stop: ")
if what != "y":
sys.exit(1)
# -------- CLIENT_HANDLER_DIR
os.chdir(CLIENT_HANDLER_DIR)
if not FAST_FLAG:
subprocess.call("rm -f *.o *.a", shell=True)
ret = subprocess.call("make -f Makefile", shell=True)
if ret != 0:
what = input("make failed, press 'y' to continue, 'n' to stop: ")
if what != "y":
sys.exit(1)
# -------- SUBPROCESS_DIR
os.chdir(SUBPROCESS_DIR)
if not FAST_FLAG:
subprocess.call("rm -f *.o *.a", shell=True)
subprocess.call("rm -f subprocess", shell=True)
ret = subprocess.call("make -f Makefile-libcefpythonapp", shell=True)
if ret != 0:
what = input("make failed, press 'y' to continue, 'n' to stop: ")
if what != "y":
sys.exit(1)
ret = subprocess.call("make -f Makefile", shell=True)
if ret != 0:
what = input("make failed, press 'y' to continue, 'n' to stop: ")
if what != "y":
sys.exit(1)
subprocess_exe = os.path.join(CEFPYTHON_BINARY, "subprocess")
if os.path.exists("./subprocess"):
# .copy() will also copy Permission bits
shutil.copy("./subprocess", subprocess_exe)
# -------- LINUX_DIR
os.chdir(LINUX_DIR)
try:
cefpython_module = os.path.join(CEFPYTHON_BINARY,
"cefpython_py{0}.so".format(PYVERSION))
os.remove(cefpython_module)
except OSError:
pass
# -------- SETUP_DIR
os.chdir(SETUP_DIR)
os.system("rm -f ./cefpython_py*.so")
pyx_files = glob.glob("./*.pyx")
for f in pyx_files:
os.remove(f)
try:
if not FAST_FLAG:
shutil.rmtree(os.path.join(SETUP_DIR, "build"))
except OSError:
pass
ret = subprocess.call("{python} fix_pyx_files.py"
.format(python=sys.executable), shell=True)
if ret != 0:
sys.exit("ERROR")
# Create __version__.pyx after fix_pyx_files.py was called,
# as that script deletes old pyx files before copying new ones.
print("Creating __version__.pyx file")
with open("__version__.pyx", "w") as fo:
fo.write('__version__ = "{}"\n'.format(VERSION))
# if DEBUG_FLAG:
# ret = subprocess.call("python-dbg setup.py build_ext --inplace"
# " --cython-gdb", shell=True)
if FAST_FLAG:
ret = subprocess.call("{python} setup.py build_ext --inplace --fast"
.format(python=sys.executable), shell=True)
else:
ret = subprocess.call("{python} setup.py build_ext --inplace"
.format(python=sys.executable), shell=True)
# if DEBUG_FLAG:
# shutil.rmtree("./../binaries_%s/cython_debug/" % BITS,
# ignore_errors=True)
# shutil.copytree("./cython_debug/",
# "./../binaries_%s/cython_debug/" % BITS)
oldpyxfiles = glob.glob("./*.pyx")
print("Removing old pyx files in /setup/: %s" % oldpyxfiles)
for pyxfile in oldpyxfiles:
if os.path.exists(pyxfile):
os.remove(pyxfile)
if ret != 0:
sys.exit("ERROR")
exitcode = os.system("mv ./cefpython_py{pyver}*.so"
" {cefpython_binary}/cefpython_py{pyver}.so"
.format(pyver=PYVERSION,
cefpython_binary=CEFPYTHON_BINARY))
if exitcode:
raise RuntimeError("Failed to move the cefpython module")
print("DONE")
# -------- LINUX_DIR
os.chdir(LINUX_DIR)
# if DEBUG_FLAG:
# os.chdir("./binaries_%s" % BITS)
# subprocess.call("cygdb . --args python-dbg wxpython.py", shell=True)
print("Make installer and run setup.py install...")
# Clean installer directory from previous run
exit_code = os.system("rm -rf ./installer/cefpython3-{ver}-*-setup/"
.format(ver=VERSION))
if exit_code:
os.system("sudo rm -rf ./installer/cefpython3-{ver}-*-setup/"
.format(ver=VERSION))
# System python requires sudo when installing package
if sys.executable in ["/usr/bin/python", "/usr/bin/python3"]:
sudo = "sudo"
else:
sudo = ""
# Make installer, install, run examples and unit tests,
# and return to src/linux/ dir.
if KIVY_FLAG:
run_examples = " && {python} ../src/linux/deprecated_64bit/kivy_.py"
else:
run_examples = (" && {python} hello_world.py"
" && {python} wxpython.py"
" && {python} gtk2.py"
" && {python} gtk2.py --message-loop-timer"
# " && {python} gtk3.py"
" && {python} tkinter_.py"
" && {python} qt.py pyqt"
" && {python} qt.py pyside"
" && {python} ../src/linux/deprecated_64bit/kivy_.py")
commands = ("cd ./installer/"
" && {python} make-setup.py --version {ver}"
" && cd cefpython3-{ver}-*-setup/"
" && {sudo} {python} setup.py install"
" && cd ../"
" && {sudo} rm -rf ./cefpython3-{ver}-*-setup/"
" && cd ../../../unittests/"
" && {python} _test_runner.py"
" && cd ../examples/"
+ run_examples +
" && cd ../src/linux/")
os.system(commands.format(python=sys.executable, ver=VERSION, sudo=sudo))