| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
import ast |
| 5 |
import json |
| 6 |
import os |
| 7 |
import re |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
|
| 11 |
from bypy.constants import LIBDIR, PREFIX, PYTHON, build_dir, islinux, ismacos, worker_env |
| 12 |
from bypy.constants import SRC as CALIBRE_DIR |
| 13 |
from bypy.utils import run_shell |
| 14 |
|
| 15 |
dlls = [ |
| 16 |
'Core', |
| 17 |
'Concurrent', |
| 18 |
'Gui', |
| 19 |
'Network', |
| 20 |
# 'NetworkAuth', |
| 21 |
'PrintSupport', |
| 22 |
'WebChannel', |
| 23 |
# 'WebSockets', |
| 24 |
# 'WebView', |
| 25 |
'Positioning', |
| 26 |
'Sensors', |
| 27 |
'SpatialAudio', |
| 28 |
'Sql', |
| 29 |
'Svg', |
| 30 |
'TextToSpeech', |
| 31 |
'WebChannel', |
| 32 |
'WebEngineCore', |
| 33 |
'WebEngineWidgets', |
| 34 |
'Widgets', |
| 35 |
'Multimedia', |
| 36 |
'MultimediaWidgets', |
| 37 |
'OpenGL', |
| 38 |
'OpenGLWidgets', |
| 39 |
'Quick', |
| 40 |
'QuickWidgets', |
| 41 |
'Qml', |
| 42 |
'QmlMeta', |
| 43 |
'QmlModels', |
| 44 |
'QmlWorkerScript', |
| 45 |
'Xml', |
| 46 |
# 'XmlPatterns', |
| 47 |
] |
| 48 |
|
| 49 |
if islinux: |
| 50 |
dlls += ['XcbQpa', 'WaylandClient', 'DBus'] |
| 51 |
elif ismacos: |
| 52 |
dlls += ['DBus'] |
| 53 |
|
| 54 |
QT_MAJOR = 6 |
| 55 |
QT_DLLS = frozenset( |
| 56 |
f'Qt{QT_MAJOR}' + x for x in dlls |
| 57 |
) |
| 58 |
|
| 59 |
QT_PLUGINS = [ |
| 60 |
'imageformats', |
| 61 |
'iconengines', |
| 62 |
'tls', |
| 63 |
'multimedia', |
| 64 |
'texttospeech', |
| 65 |
# 'mediaservice', |
| 66 |
'platforms', |
| 67 |
# 'playlistformats', |
| 68 |
'sqldrivers', |
| 69 |
# 'webview', |
| 70 |
# 'audio', 'printsupport', 'bearer', 'position', |
| 71 |
] |
| 72 |
|
| 73 |
if islinux: |
| 74 |
QT_PLUGINS += [ |
| 75 |
'egldeviceintegrations', |
| 76 |
'platforminputcontexts', |
| 77 |
'platformthemes', |
| 78 |
'wayland-decoration-client', |
| 79 |
'wayland-graphics-integration-client', |
| 80 |
'wayland-shell-integration', |
| 81 |
'xcbglintegrations', |
| 82 |
] |
| 83 |
else: |
| 84 |
QT_PLUGINS.append('styles') |
| 85 |
|
| 86 |
PYQT_MODULES = ( |
| 87 |
'Qt', |
| 88 |
'QtCore', |
| 89 |
'QtGui', |
| 90 |
'QtNetwork', |
| 91 |
'QtMultimedia', 'QtMultimediaWidgets', |
| 92 |
'QtTextToSpeech', |
| 93 |
'QtPrintSupport', |
| 94 |
'QtSensors', |
| 95 |
'QtSvg', |
| 96 |
'QtWidgets', |
| 97 |
'QtOpenGL', |
| 98 |
'QtOpenGLWidgets', |
| 99 |
'QtWebEngine', |
| 100 |
'QtWebEngineCore', |
| 101 |
'QtWebEngineWidgets', |
| 102 |
'QtWebChannel', |
| 103 |
) |
| 104 |
del dlls |
| 105 |
|
| 106 |
|
| 107 |
def read_cal_file(name): |
| 108 |
with open(os.path.join(CALIBRE_DIR, 'src', 'calibre', name), 'rb') as f: |
| 109 |
return f.read().decode('utf-8') |
| 110 |
|
| 111 |
|
| 112 |
def initialize_constants(): |
| 113 |
calibre_constants = {} |
| 114 |
src = read_cal_file('constants.py') |
| 115 |
nv = re.search(r'numeric_version\s+=\s+\((\d+), (\d+), (\d+)\)', src) |
| 116 |
assert nv is not None |
| 117 |
calibre_constants['version' |
| 118 |
] = '%s.%s.%s' % (nv.group(1), nv.group(2), nv.group(3)) |
| 119 |
def get_str_assign(which): |
| 120 |
pat = r'__appname__\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]'.replace('__appname__', which) |
| 121 |
m = re.search(pat, src) |
| 122 |
assert m is not None |
| 123 |
return m.group(2) |
| 124 |
|
| 125 |
calibre_constants['appname'] = get_str_assign('__appname__') |
| 126 |
calibre_constants['MAIN_APP_UID'] = get_str_assign('MAIN_APP_UID') |
| 127 |
calibre_constants['VIEWER_APP_UID'] = get_str_assign('VIEWER_APP_UID') |
| 128 |
calibre_constants['EDITOR_APP_UID'] = get_str_assign('EDITOR_APP_UID') |
| 129 |
ep_match = re.compile(r'entry_points = (\{.*?\})', |
| 130 |
re.DOTALL).search(read_cal_file('linux.py')) |
| 131 |
assert ep_match is not None |
| 132 |
epsrc = ep_match.group(1) |
| 133 |
epsrc = epsrc.replace('__appname__ +', repr(calibre_constants['appname'])) |
| 134 |
try: |
| 135 |
entry_points = ast.literal_eval(epsrc) |
| 136 |
except Exception as e: |
| 137 |
raise ValueError(f'could not evaluate {epsrc}') from e |
| 138 |
|
| 139 |
def e2b(ep): |
| 140 |
m = re.search(r'\s*(.*?)\s*=', ep) |
| 141 |
assert m is not None |
| 142 |
return m.group(1).strip() |
| 143 |
|
| 144 |
def e2s(ep, base='src'): |
| 145 |
m = re.search(r'.*=\s*(.*?):', ep) |
| 146 |
assert m is not None |
| 147 |
return (base + os.path.sep + m.group(1).replace('.', '/') + '.py').strip() |
| 148 |
|
| 149 |
def e2m(ep): |
| 150 |
m = re.search(r'.*=\s*(.*?)\s*:', ep) |
| 151 |
assert m is not None |
| 152 |
return m.group(1).strip() |
| 153 |
|
| 154 |
def e2f(ep): |
| 155 |
return ep[ep.rindex(':') + 1:].strip() |
| 156 |
|
| 157 |
calibre_constants['basenames'] = basenames = {} |
| 158 |
calibre_constants['functions'] = functions = {} |
| 159 |
calibre_constants['modules'] = modules = {} |
| 160 |
calibre_constants['scripts'] = scripts = {} |
| 161 |
for x in ('console', 'gui'): |
| 162 |
y = x + '_scripts' |
| 163 |
basenames[x] = list(map(e2b, entry_points[y])) |
| 164 |
functions[x] = list(map(e2f, entry_points[y])) |
| 165 |
modules[x] = list(map(e2m, entry_points[y])) |
| 166 |
scripts[x] = list(map(e2s, entry_points[y])) |
| 167 |
|
| 168 |
src = read_cal_file('ebooks/__init__.py') |
| 169 |
be_match = re.search( |
| 170 |
r'^BOOK_EXTENSIONS\s*=\s*(\[.+?\])', src, flags=re.DOTALL | re.MULTILINE |
| 171 |
) |
| 172 |
assert be_match is not None |
| 173 |
be = be_match.group(1) |
| 174 |
calibre_constants['book_extensions'] = ast.literal_eval(be) |
| 175 |
return calibre_constants |
| 176 |
|
| 177 |
|
| 178 |
def run(*args, **extra_env): |
| 179 |
env = os.environ.copy() |
| 180 |
env.update(worker_env) |
| 181 |
env.update(extra_env) |
| 182 |
env['SW'] = PREFIX |
| 183 |
env['LD_LIBRARY_PATH'] = LIBDIR |
| 184 |
env['SIP_BIN'] = os.path.join(PREFIX, 'bin', 'sip') |
| 185 |
env['QMAKE'] = os.path.join(PREFIX, 'qt', 'bin', 'qmake') |
| 186 |
return subprocess.call(list(args), env=env, cwd=CALIBRE_DIR) |
| 187 |
|
| 188 |
|
| 189 |
def build_c_extensions(ext_dir, args): |
| 190 |
bdir = os.path.join(build_dir(), 'calibre-extension-objects') |
| 191 |
cmd = [ |
| 192 |
PYTHON, 'setup.py', 'build', |
| 193 |
'--output-dir', ext_dir, '--build-dir', bdir, |
| 194 |
] |
| 195 |
if args.build_only: |
| 196 |
cmd.extend(('--only', args.build_only)) |
| 197 |
rc = run(*cmd, COMPILER_CWD=bdir) |
| 198 |
if rc != 0: |
| 199 |
print('Building of calibre C extensions failed, returncode:', rc, 'Failing cmd:', cmd, file=sys.stderr) |
| 200 |
os.chdir(CALIBRE_DIR) |
| 201 |
run_shell() |
| 202 |
raise SystemExit('Building of calibre C extensions failed') |
| 203 |
return ext_dir |
| 204 |
|
| 205 |
|
| 206 |
def run_tests(path_to_calibre_debug, cwd_on_failure): |
| 207 |
ret = run(path_to_calibre_debug, '--test-build') |
| 208 |
if ret != 0: |
| 209 |
os.chdir(cwd_on_failure) |
| 210 |
print( |
| 211 |
'running calibre build tests failed with return code:', ret, 'and exe:', path_to_calibre_debug, file=sys.stderr) |
| 212 |
run_shell() |
| 213 |
raise SystemExit('running calibre build tests failed') |
| 214 |
|
| 215 |
|
| 216 |
if __name__ == 'program': |
| 217 |
calibre_constants = initialize_constants() |