Debian Codegraph @

bypy/windows/__main__.py

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
4
5
6 import contextlib
7 import errno
8 import glob
9 import os
10 import re
11 import runpy
12 import shutil
13 import stat
14 import subprocess
15 import sys
16 import zipfile
17
18 from bypy.constants import CL, LINK, MT, PREFIX, RC, SIGNTOOL, SW, build_dir, python_major_minor_version, worker_env
19 from bypy.constants import SRC as CALIBRE_DIR
20 from bypy.freeze import cleanup_site_packages, extract_extension_modules, freeze_python, path_to_freeze_dir
21 from bypy.utils import mkdtemp, py_compile, run, walk
22 from bypy.sign_server import sign_file_in_client
23 from bypy.authenticode import has_signature
24
25 iv = globals()['init_env']
26 calibre_constants = iv['calibre_constants']
27 QT_PREFIX = os.path.join(PREFIX, 'qt')
28 QT_DLLS, QT_PLUGINS, PYQT_MODULES = iv['QT_DLLS'], iv['QT_PLUGINS'], iv['PYQT_MODULES']
29
30 APPNAME, VERSION = calibre_constants['appname'], calibre_constants['version']
31 WINVER = VERSION + '.0'
32 machine = 'X64'
33 j, d, a, b = os.path.join, os.path.dirname, os.path.abspath, os.path.basename
34 create_installer = runpy.run_path(
35 j(d(a(__file__)), 'wix.py'), {'calibre_constants': calibre_constants}
36 )['create_installer']
37 signatures = set()
38
39 DESCRIPTIONS = {
40 'calibre': 'The main calibre program',
41 'ebook-viewer': 'The calibre e-book viewer',
42 'ebook-edit': 'The calibre e-book editor',
43 'lrfviewer': 'Viewer for LRF files',
44 'ebook-convert': 'Command line interface to the conversion/news download system',
45 'ebook-meta': 'Command line interface for manipulating e-book metadata',
46 'calibredb': 'Command line interface to the calibre database',
47 'calibre-launcher': 'Utility functions common to all executables',
48 'calibre-debug': 'Command line interface for calibre debugging/development',
49 'calibre-customize': 'Command line interface to calibre plugin system',
50 'calibre-server': 'Standalone calibre content server',
51 'calibre-parallel': 'calibre worker process',
52 'calibre-smtp': 'Command line interface for sending books via email',
53 'calibre-eject': 'Helper program for ejecting connected reader devices',
54 'calibre-file-dialog': 'Helper program to show file open/save dialogs',
55 }
56
57 EXE_MANIFEST = '''\
58 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
59 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
60 <application xmlns="urn:schemas-microsoft-com:asm.v3">
61 <windowsSettings> <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware> </windowsSettings>
62 </application>
63 <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
64 <security>
65 <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
66 <requestedExecutionLevel level="asInvoker" uiAccess="false" />
67 </requestedPrivileges>
68 </security>
69 </trustInfo>
70 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
71 <application>
72 <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
73 </application>
74 </compatibility>
75 </assembly>
76 '''
77
78
79 def printf(*args, **kw):
80 print(*args, **kw)
81 sys.stdout.flush()
82
83
84 def run_compiler(env, *cmd):
85 run(*cmd, cwd=env.obj_dir)
86
87
88 def sign_file(path: str) -> None:
89 sign_file_in_client(path)
90 signatures.add(path)
91
92
93 class Env:
94
95 def __init__(self, build_dir):
96 self.python_base = os.path.join(PREFIX, 'private', 'python')
97 self.portable_uncompressed_size = 0
98 self.src_root = CALIBRE_DIR
99 self.base = j(build_dir, 'winfrozen')
100 self.app_base = j(self.base, 'app')
101 self.rc_template = j(d(a(__file__)), 'template.rc')
102 self.py_ver = '.'.join(map(str, python_major_minor_version()))
103 self.lib_dir = j(self.app_base, 'Lib')
104 self.pylib = j(self.app_base, 'pylib.zip')
105 self.dll_dir = j(self.app_base, 'bin')
106 self.share_dir = j(self.app_base, 'share')
107 self.portable_base = j(d(self.base), 'Calibre Portable')
108 self.obj_dir = j(build_dir, 'launcher')
109 self.installer_dir = j(build_dir, 'wix')
110 self.dist = j(SW, 'dist')
111
112
113 def initbase(env):
114 os.makedirs(env.app_base)
115 os.mkdir(env.dll_dir)
116 os.mkdir(env.share_dir)
117 try:
118 shutil.rmtree(env.dist)
119 except EnvironmentError as err:
120 if err.errno != errno.ENOENT:
121 raise
122 os.mkdir(env.dist)
123
124
125 def freeze(env, ext_dir, incdir):
126 shutil.copy2(j(env.src_root, 'LICENSE'), env.base)
127
128 printf('Adding resources...')
129 tgt = j(env.app_base, 'resources')
130 if os.path.exists(tgt):
131 shutil.rmtree(tgt)
132 shutil.copytree(j(env.src_root, 'resources'), tgt)
133
134 printf('\tAdding misc binary deps')
135
136 def copybin(x, dest=env.dll_dir):
137 shutil.copy2(x, dest)
138 with contextlib.suppress(FileNotFoundError):
139 shutil.copy2(x + '.manifest', dest)
140
141 bindir = os.path.join(PREFIX, 'bin')
142 libdir = os.path.join(PREFIX, 'lib')
143 for x in ('pdftohtml', 'pdfinfo', 'pdftoppm', 'pdftotext', 'jpegtran-calibre', 'cjpeg-calibre', 'optipng-calibre', 'cwebp-calibre', 'JXRDecApp-calibre'):
144 copybin(os.path.join(bindir, x + '.exe'))
145 # piper
146 for x in ('espeak-ng-data',):
147 shutil.copytree(os.path.join(PREFIX, 'share', x), os.path.join(env.share_dir, x))
148 for f in glob.glob(os.path.join(libdir, 'onnxruntime*.dll')):
149 copybin(f)
150 for f in glob.glob(os.path.join(libdir, 'DirectML*.dll')):
151 copybin(f)
152
153 for f in glob.glob(os.path.join(bindir, '*.dll')):
154 if re.search(r'(easylzma|icutest)', f.lower()) is None:
155 copybin(f)
156
157 ossm = os.path.join(env.dll_dir, 'ossl-modules')
158 os.mkdir(ossm)
159 for f in glob.glob(os.path.join(libdir, 'ossl-modules', '*.dll')):
160 copybin(f, ossm)
161 for f in glob.glob(os.path.join(PREFIX, 'ffmpeg', 'bin', '*.dll')):
162 copybin(f)
163
164 copybin(os.path.join(env.python_base, 'python%s.dll' % env.py_ver.replace('.', '')))
165 copybin(os.path.join(env.python_base, 'python%s.dll' % env.py_ver[0]))
166 for x in glob.glob(os.path.join(env.python_base, 'DLLs', '*.dll')): # dlls needed by python
167 copybin(x)
168 for f in walk(os.path.join(env.python_base, 'Lib')):
169 q = f.lower()
170 if q.endswith('.dll') and 'scintilla' not in q and 'pyqtbuild' not in q:
171 copybin(f)
172 ext_map = extract_extension_modules(ext_dir, env.dll_dir)
173 ext_map.update(extract_extension_modules(j(env.python_base, 'DLLs'), env.dll_dir, move=False))
174
175 printf('Adding Qt...')
176 for x in QT_DLLS:
177 copybin(os.path.join(QT_PREFIX, 'bin', x + '.dll'))
178 copybin(os.path.join(QT_PREFIX, 'bin', 'QtWebEngineProcess.exe'))
179 plugdir = j(QT_PREFIX, 'plugins')
180 tdir = j(env.app_base, 'plugins')
181 for d in QT_PLUGINS:
182 imfd = os.path.join(plugdir, d)
183 tg = os.path.join(tdir, d)
184 if os.path.exists(tg):
185 shutil.rmtree(tg)
186 shutil.copytree(imfd, tg)
187 for f in walk(tdir):
188 if not f.lower().endswith('.dll'):
189 os.remove(f)
190 for data_file in os.listdir(j(QT_PREFIX, 'resources')):
191 shutil.copy2(j(QT_PREFIX, 'resources', data_file), j(env.app_base, 'resources'))
192 shutil.copytree(j(QT_PREFIX, 'translations'), j(env.app_base, 'translations'))
193
194 printf('Adding python...')
195
196 def ignore_lib(root, items):
197 ans = []
198 for x in items:
199 ext = os.path.splitext(x)[1].lower()
200 if ext in ('.dll', '.chm', '.htm', '.txt'):
201 ans.append(x)
202 return ans
203
204 shutil.copytree(r'%s\Lib' % env.python_base, env.lib_dir, ignore=ignore_lib)
205 install_site_py(env)
206 sp_dir = j(env.lib_dir, 'site-packages')
207
208 printf('Adding calibre sources...')
209 for x in glob.glob(j(CALIBRE_DIR, 'src', '*')):
210 if os.path.isdir(x):
211 if os.path.exists(os.path.join(x, '__init__.py')):
212 shutil.copytree(x, j(sp_dir, b(x)), ignore=shutil.ignore_patterns('*.pyc', '*.pyo'))
213 else:
214 shutil.copy(x, j(sp_dir, b(x)))
215
216 ext_map.update(cleanup_site_packages(sp_dir))
217 for x in os.listdir(sp_dir):
218 os.rename(j(sp_dir, x), j(env.lib_dir, x))
219 os.rmdir(sp_dir)
220 printf('Extracting extension modules from', env.lib_dir, 'to', env.dll_dir)
221 ext_map.update(extract_extension_modules(env.lib_dir, env.dll_dir))
222
223 printf('Byte-compiling all python modules...')
224 py_compile(env.lib_dir.replace(os.sep, '/'))
225 # from bypy.utils import run_shell
226 # run_shell(cwd=env.lib_dir)
227 freeze_python(env.lib_dir, env.dll_dir, incdir, ext_map, develop_mode_env_var='CALIBRE_DEVELOP_FROM')
228 shutil.rmtree(env.lib_dir)
229
230
231 def embed_manifests(env):
232 printf('Embedding remaining manifests...')
233 for manifest in walk(env.base):
234 dll, ext = os.path.splitext(manifest)
235 if ext != '.manifest':
236 continue
237 res = 2
238 if os.path.splitext(dll)[1] == '.exe':
239 res = 1
240 if os.path.exists(dll) and open(manifest, 'rb').read().strip():
241 run(MT, '-manifest', manifest, '-outputresource:%s;%d' % (dll, res))
242 os.remove(manifest)
243
244
245 def embed_resources(env, module, desc=None, extra_data=None, product_description=None):
246 icon_base = j(env.src_root, 'icons')
247 icon_map = {
248 'calibre': 'library', 'ebook-viewer': 'viewer', 'ebook-edit': 'ebook-edit',
249 'lrfviewer': 'viewer',
250 }
251 file_type = 'DLL' if module.endswith('.dll') else 'APP'
252 with open(env.rc_template, 'rb') as f:
253 template = f.read().decode('utf-8')
254 bname = b(module)
255 internal_name = os.path.splitext(bname)[0]
256 icon = icon_map.get(internal_name.replace('-portable', ''), 'command-prompt')
257 if internal_name.startswith('calibre-portable-'):
258 icon = 'install'
259 icon = j(icon_base, icon + '.ico')
260 if desc is None:
261 defdesc = 'A dynamic link library' if file_type == 'DLL' else \
262 'An executable program'
263 desc = DESCRIPTIONS.get(internal_name, defdesc)
264 license = 'GNU GPL v3.0'
265
266 def e(val):
267 return val.replace('"', r'\"')
268 if product_description is None:
269 product_description = APPNAME + ' - E-book management'
270 rc = template.format(
271 icon=icon.replace('\\', '/'),
272 file_type=e(file_type),
273 file_version=e(WINVER.replace('.', ',')),
274 file_version_str=e(WINVER),
275 file_description=e(desc),
276 internal_name=e(internal_name),
277 original_filename=e(bname),
278 product_version=e(WINVER.replace('.', ',')),
279 product_version_str=e(VERSION),
280 product_name=e(APPNAME),
281 product_description=e(product_description),
282 legal_copyright=e(license),
283 legal_trademarks=e(APPNAME + ' is a registered U.S. trademark number 3,666,525')
284 )
285 if extra_data:
286 rc += '\nextra extra "%s"' % extra_data
287 tdir = env.obj_dir
288 rcf = j(tdir, bname + '.rc')
289 with open(rcf, 'w') as f:
290 f.write(rc)
291 res = j(tdir, bname + '.res')
292 run(RC, '/n', '/fo' + res, rcf)
293 return res
294
295
296 def install_site_py(env):
297 if not os.path.exists(env.lib_dir):
298 os.makedirs(env.lib_dir)
299 shutil.copy2(j(d(__file__), 'site.py'), env.lib_dir)
300
301
302 def build_portable_installer(env):
303 zf = a(j(env.dist, 'calibre-portable-%s.zip.lz' % VERSION)).replace(os.sep, '/')
304 usz = env.portable_uncompressed_size or os.path.getsize(zf)
305
306 def cc(src, obj):
307 cflags = '/c /EHsc /MT /W4 /Ox /nologo /D_UNICODE /DUNICODE /DPSAPI_VERSION=1'.split()
308 cflags.append(r'/I%s\include' % PREFIX)
309 cflags.append('/DUNCOMPRESSED_SIZE=%d' % usz)
310 printf('Compiling', obj)
311 cmd = [CL] + cflags + ['/Fo' + obj, src]
312 run_compiler(env, *cmd)
313
314 base = d(a(__file__))
315 src = j(base, 'portable-installer.cpp')
316 obj = j(env.obj_dir, b(src) + '.obj')
317 xsrc = j(base, 'XUnzip.cpp')
318 xobj = j(env.obj_dir, b(xsrc) + '.obj')
319 cc(src, obj)
320 cc(xsrc, xobj)
321
322 exe = j(env.dist, 'calibre-portable-installer-%s.exe' % VERSION)
323 printf('Linking', exe)
324 manifest = exe + '.manifest'
325 with open(manifest, 'wb') as f:
326 f.write(EXE_MANIFEST.encode('utf-8'))
327 cmd = [LINK] + [
328 '/INCREMENTAL:NO', '/MACHINE:' + machine,
329 '/LIBPATH:' + env.obj_dir, '/SUBSYSTEM:WINDOWS',
330 '/LIBPATH:' + (PREFIX + r'\lib'),
331 '/RELEASE', '/MANIFEST:EMBED', '/MANIFESTINPUT:' + manifest,
332 '/ENTRY:wWinMainCRTStartup',
333 '/OUT:' + exe, embed_resources(
334 env, exe, desc='Calibre Portable Installer', extra_data=zf, product_description='Calibre Portable Installer'),
335 xobj, obj, 'User32.lib', 'Shell32.lib', 'easylzma_s.lib',
336 'Ole32.lib', 'Shlwapi.lib', 'Kernel32.lib', 'Psapi.lib']
337 run(*cmd)
338 os.remove(zf)
339 os.remove(manifest)
340
341
342 def build_portable(env):
343 base = env.portable_base
344 if os.path.exists(base):
345 shutil.rmtree(base)
346 os.makedirs(base)
347 root = d(a(__file__))
348 src = j(root, 'portable.cpp')
349 obj = j(env.obj_dir, b(src) + '.obj')
350 cflags = '/c /EHsc /MT /W3 /Ox /nologo /D_UNICODE /DUNICODE'.split()
351 launchers = []
352
353 for exe_name in ('calibre.exe', 'ebook-viewer.exe', 'ebook-edit.exe'):
354 exe = j(base, exe_name.replace('.exe', '-portable.exe'))
355 printf('Compiling', exe)
356 cmd = [CL] + cflags + ['/Fo' + obj, '/Tp' + src]
357 run_compiler(env, *cmd)
358 printf('Linking', exe)
359 desc = {
360 'calibre.exe': 'Calibre Portable',
361 'ebook-viewer.exe': 'Calibre Portable Viewer',
362 'ebook-edit.exe': 'Calibre Portable Editor'
363 }[exe_name]
364 cmd = [LINK] + [
365 '/INCREMENTAL:NO', '/MACHINE:' + machine,
366 '/LIBPATH:' + env.obj_dir, '/SUBSYSTEM:WINDOWS',
367 '/RELEASE',
368 '/ENTRY:wWinMainCRTStartup',
369 '/OUT:' + exe, embed_resources(env, exe, desc=desc, product_description=desc),
370 obj, 'User32.lib', 'Shell32.lib']
371 run(*cmd)
372 sign_file(exe)
373
374 printf('Creating portable installer')
375 shutil.copytree(env.base, j(base, 'Calibre'))
376 os.mkdir(j(base, 'Calibre Library'))
377 os.mkdir(j(base, 'Calibre Settings'))
378
379 name = '%s-portable-%s.zip' % (APPNAME, VERSION)
380 name = j(env.dist, name)
381 with zipfile.ZipFile(name, 'w', zipfile.ZIP_STORED) as zf:
382 add_dir_to_zip(zf, base, 'Calibre Portable')
383
384 env.portable_uncompressed_size = os.path.getsize(name)
385 subprocess.check_call([PREFIX + r'\bin\elzma.exe', '-9', '--lzip', name])
386
387
388 def add_dir_to_zip(zf, path, prefix=''):
389 '''
390 Add a directory recursively to the zip file with an optional prefix.
391 '''
392 if prefix:
393 zi = zipfile.ZipInfo(prefix + '/')
394 zi.external_attr = 16
395 zf.writestr(zi, '')
396 cwd = os.path.abspath(os.getcwd())
397 try:
398 os.chdir(path)
399 fp = (prefix + ('/' if prefix else '')).replace('//', '/')
400 for f in os.listdir('.'):
401 arcname = fp + f
402 if os.path.isdir(f):
403 add_dir_to_zip(zf, f, prefix=arcname)
404 else:
405 zf.write(f, arcname)
406 finally:
407 os.chdir(cwd)
408
409
410 def build_utils(env):
411
412 def build(src, name, subsys='CONSOLE', libs='setupapi.lib'.split()):
413 printf('Building ' + name)
414 obj = j(env.obj_dir, os.path.basename(src) + '.obj')
415 cflags = '/c /EHsc /MD /W3 /Ox /nologo /D_UNICODE'.split()
416 ftype = '/T' + ('c' if src.endswith('.c') else 'p')
417 cmd = [CL] + cflags + ['/Fo' + obj, ftype + src]
418 run_compiler(env, *cmd)
419 exe = j(env.dll_dir, name)
420 mf = exe + '.manifest'
421 with open(mf, 'wb') as f:
422 f.write(EXE_MANIFEST.encode('utf-8'))
423 cmd = [LINK] + [
424 '/MACHINE:' + machine,
425 '/SUBSYSTEM:' + subsys, '/RELEASE', '/MANIFEST:EMBED', '/MANIFESTINPUT:' + mf,
426 '/OUT:' + exe] + [embed_resources(env, exe), obj] + libs
427 run(*cmd)
428 base = d(a(__file__))
429 build(j(base, 'file_dialogs.cpp'), 'calibre-file-dialog.exe', 'WINDOWS', 'Ole32.lib Shell32.lib'.split())
430 build(j(base, 'eject.c'), 'calibre-eject.exe')
431
432
433 def build_launchers(env, incdir, debug=False):
434 if not os.path.exists(env.obj_dir):
435 os.makedirs(env.obj_dir)
436 dflags = (['/Zi'] if debug else [])
437 dlflags = (['/DEBUG'] if debug else ['/INCREMENTAL:NO'])
438 base = d(a(__file__))
439 sources = [j(base, x) for x in ['util.c', ]]
440 objects = [j(env.obj_dir, b(x) + '.obj') for x in sources]
441 # Launcher DLL must be built aginast limit API to allow delay loading,
442 # which means defining PY_LIMITED_API and linking against python3.lib
443 # rather than python3.xx.lib
444 cflags = '/c /EHsc /W3 /Ox /nologo /D_UNICODE /DPY_LIMITED_API=0x030A0000L'.split()
445 cflags += ['/DPYDLL="python%s.dll"' % env.py_ver.replace('.', ''), '/I%s/include' % env.python_base]
446 cflags += [f'/I{path_to_freeze_dir()}', f'/I{incdir}']
447 for src, obj in zip(sources, objects):
448 cmd = [CL] + cflags + dflags + ['/MD', '/Fo' + obj, '/Tc' + src]
449 run_compiler(env, *cmd)
450
451 dll = j(env.obj_dir, 'calibre-launcher.dll')
452 ver = '.'.join(VERSION.split('.')[:2])
453 cmd = [LINK, '/DLL', '/VERSION:' + ver, '/LTCG', '/OUT:' + dll,
454 '/nologo', '/MACHINE:' + machine] + dlflags + objects + \
455 [embed_resources(env, dll),
456 '/LIBPATH:%s/libs' % env.python_base,
457 'delayimp.lib', 'user32.lib', 'shell32.lib',
458 'python%s.lib' % env.py_ver.split('.')[0],
459 '/delayload:python%s.dll' % env.py_ver.replace('.', '')]
460 printf('Linking calibre-launcher.dll')
461 run(*cmd)
462
463 src = j(base, 'main.c')
464 shutil.copy2(dll, env.dll_dir)
465 basenames, modules, functions = calibre_constants['basenames'], calibre_constants['modules'], calibre_constants['functions']
466 for typ in ('console', 'gui', ):
467 printf('Processing %s launchers' % typ)
468 subsys = 'WINDOWS' if typ == 'gui' else 'CONSOLE'
469 for mod, bname, func in zip(modules[typ], basenames[typ], functions[typ]):
470 cflags = '/c /EHsc /MT /W3 /O1 /nologo /D_UNICODE /DUNICODE /GS-'.split()
471 if typ == 'gui':
472 cflags += ['/DGUI_APP=']
473
474 cflags += ['/DMODULE=L"%s"' % mod, '/DBASENAME=L"%s"' % bname,
475 '/DFUNCTION=L"%s"' % func]
476 dest = j(env.obj_dir, bname + '.obj')
477 printf('Compiling', bname)
478 cmd = [CL] + cflags + dflags + ['/Tc' + src, '/Fo' + dest]
479 run_compiler(env, *cmd)
480 exe = j(env.base, bname + '.exe')
481 lib = dll.replace('.dll', '.lib')
482 u32 = ['user32.lib']
483 printf('Linking', bname)
484 mf = dest + '.manifest'
485 with open(mf, 'wb') as f:
486 f.write(EXE_MANIFEST.encode('utf-8'))
487 cmd = [LINK] + [
488 '/MACHINE:' + machine, '/NODEFAULTLIB', '/ENTRY:start_here',
489 '/LIBPATH:' + env.obj_dir, '/SUBSYSTEM:' + subsys,
490 '/LIBPATH:%s/libs' % env.python_base, '/RELEASE',
491 '/MANIFEST:EMBED', '/MANIFESTINPUT:' + mf,
492 '/STACK:2097152', # Set stack size to 2MB which is what python expects. Default on windows is 1MB
493 'user32.lib', 'kernel32.lib',
494 '/OUT:' + exe] + u32 + dlflags + [embed_resources(env, exe), dest, lib]
495 run(*cmd)
496
497
498 def copy_crt_and_d3d(env):
499 printf('Copying CRT and D3D...')
500 plat = 'x64'
501 for key, val in worker_env.items():
502 if 'COMNTOOLS' in key.upper():
503 redist_dir = os.path.dirname(os.path.dirname(val.rstrip(os.sep)))
504 redist_dir = os.path.join(redist_dir, 'VC', 'Redist', 'MSVC')
505 vc_path = glob.glob(os.path.join(redist_dir, '*', plat, '*.CRT'))[0]
506 break
507 else:
508 raise SystemExit('Could not find Visual Studio redistributable CRT')
509
510 sdk_path = os.path.join(
511 worker_env['UNIVERSALCRTSDKDIR'], 'Redist', worker_env['WINDOWSSDKVERSION'],
512 'ucrt', 'DLLs', plat)
513 if not os.path.exists(sdk_path):
514 raise SystemExit('Windows 10 Universal CRT redistributable not found at: %r' % sdk_path)
515 d3d_path = os.path.join(
516 worker_env['WINDOWSSDKDIR'], 'Redist', 'D3D', plat)
517 if not os.path.exists(d3d_path):
518 raise SystemExit('Windows 10 D3D redistributable not found at: %r' % d3d_path)
519 mesa_path = os.path.join(os.environ['MESA'], '64', 'opengl32sw.dll')
520 if not os.path.exists(mesa_path):
521 raise SystemExit('Mesa DLLs (opengl32sw.dll) not found at: %r' % mesa_path)
522
523 def copy_dll(dll):
524 shutil.copy2(dll, env.dll_dir)
525 dest = os.path.join(env.dll_dir, b(dll))
526 os.chmod(dest, stat.S_IRWXU)
527
528 for dll in glob.glob(os.path.join(d3d_path, '*.dll')):
529 if os.path.basename(dll).lower().startswith('d3dcompiler_'):
530 copy_dll(dll)
531 copy_dll(mesa_path)
532 for dll in glob.glob(os.path.join(sdk_path, '*.dll')):
533 copy_dll(dll)
534 for dll in glob.glob(os.path.join(vc_path, '*.dll')):
535 bname = os.path.basename(dll)
536 if not bname.startswith('vccorlib') and not bname.startswith('concrt'):
537 # Those two DLLs are not required vccorlib is for the CORE CLR
538 # I think concrt is the concurrency runtime for C++ which I believe
539 # nothing in calibre currently uses
540 copy_dll(dll)
541
542
543 def sign_all_pe_files(env):
544 for path in walk(env.base):
545 if path.rpartition('.')[-1].lower() in ('dll', 'pyd', 'exe') and not has_signature(path):
546 sign_file(path)
547
548
549 def main():
550 ext_dir = globals()['ext_dir']
551 args = globals()['args']
552 run_tests = iv['run_tests']
553 env = Env(build_dir())
554 incdir = mkdtemp('include')
555 initbase(env)
556 freeze(env, ext_dir, incdir)
557 build_launchers(env, incdir)
558 build_utils(env)
559 embed_manifests(env)
560 copy_crt_and_d3d(env)
561 if args.sign_installers:
562 sign_all_pe_files(env)
563 if not args.skip_tests:
564 run_tests(os.path.join(env.base, 'calibre-debug.exe'), env.base)
565 create_installer(env, args.compression_level)
566 build_portable(env)
567 build_portable_installer(env)
568 if args.sign_installers:
569 print(f'Signed {len(signatures)} files')
570
571
572 def develop_launcher():
573 import subprocess
574
575 def r(*a):
576 subprocess.check_call(list(a))
577
578 r(
579 'cl.EXE', '/c', '/EHsc', '/MT', '/W3', '/O1', '/nologo', '/D_UNICODE', '/DUNICODE', '/GS-',
580 '/DMODULE="calibre.debug"', '/DBASENAME="calibre-debug"', '/DFUNCTION="main"',
581 r'/TcC:\r\src\bypy\windows\main.c', r'/Fo..\launcher\calibre-debug.obj'
582 )
583 r(
584 'link.EXE', '/MACHINE:X86', '/NODEFAULTLIB', '/ENTRY:start_here',
585 r'/LIBPATH:..\launcher', '/SUBSYSTEM:CONSOLE',
586 r'/LIBPATH:C:\r\sw32\sw\private\python/libs', '/RELEASE',
587 '/MANIFEST:EMBED', r'/MANIFESTINPUT:..\launcher\calibre-debug.obj.manifest',
588 'user32.lib', 'kernel32.lib', r'/OUT:calibre-debug.exe',
589 'user32.lib', '/INCREMENTAL:NO', r'..\launcher\calibre-debug.exe.res',
590 r'..\launcher\calibre-debug.obj', r'..\launcher\calibre-launcher.lib'
591 )
592
593
594 if __name__ == '__main__':
595 main()