Debian Codegraph @

setup/build.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/usr/bin/env python
2
3 __license__ = 'GPL v3'
4 __copyright__ = '2009, Kovid Goyal <[email protected]>'
5 __docformat__ = 'restructuredtext en'
6
7 import errno
8 import glob
9 import json
10 import os
11 import shlex
12 import shutil
13 import subprocess
14 import sys
15 import sysconfig
16 import textwrap
17 from functools import partial
18 from typing import NamedTuple
19
20 from setup import SRC, Command, isbsd, isfreebsd, ishaiku, islinux, ismacos, iswindows
21
22 isunix = islinux or ismacos or isbsd or ishaiku
23
24 py_lib = os.path.join(sys.prefix, 'libs', 'python{}{}.lib'.format(*sys.version_info[:2]))
25
26
27 class CompileCommand(NamedTuple):
28 cmd: list[str]
29 src: str
30 dest: str
31
32
33 class LinkCommand(NamedTuple):
34 cmd: list[str]
35 objects: list[str]
36 dest: str
37
38
39 def walk(path='.'):
40 for dirpath, dirnames, filenames in os.walk(path):
41 for f in filenames:
42 yield os.path.join(dirpath, f)
43
44
45 def init_symbol_name(name):
46 prefix = 'PyInit_'
47 return prefix + name
48
49
50 def absolutize(paths):
51 return list({x if os.path.isabs(x) else os.path.join(SRC, x.replace('/', os.sep)) for x in paths})
52
53
54 class Extension:
55 def __init__(self, name, sources, **kwargs):
56 self.data = d = {}
57 self.name = d['name'] = name
58 self.sources = d['sources'] = absolutize(sources)
59 self.needs_cxx = d['needs_cxx'] = bool([1 for x in self.sources if os.path.splitext(x)[1] in ('.cpp', '.c++', '.cxx')])
60 self.needs_py2 = d['needs_py2'] = kwargs.get('needs_py2', False)
61 self.headers = d['headers'] = absolutize(kwargs.get('headers', []))
62 self.sip_files = d['sip_files'] = absolutize(kwargs.get('sip_files', []))
63 self.needs_exceptions = d['needs_exceptions'] = kwargs.get('needs_exceptions', False)
64 self.qt_modules = d['qt_modules'] = kwargs.get('qt_modules', ['widgets'])
65 self.inc_dirs = d['inc_dirs'] = absolutize(kwargs.get('inc_dirs', []))
66 self.lib_dirs = d['lib_dirs'] = absolutize(kwargs.get('lib_dirs', []))
67 self.extra_objs = d['extra_objs'] = absolutize(kwargs.get('extra_objs', []))
68 self.error = d['error'] = kwargs.get('error', None)
69 self.libraries = d['libraries'] = kwargs.get('libraries', [])
70 self.cflags = d['cflags'] = kwargs.get('cflags', [])
71 self.uses_icu = 'icuuc' in self.libraries
72 self.ldflags = d['ldflags'] = kwargs.get('ldflags', [])
73 self.optional = d['options'] = kwargs.get('optional', False)
74 self.needs_cxx_std = kwargs.get('needs_c++')
75 self.needs_c_std = kwargs.get('needs_c')
76 self.only_build_for = kwargs.get('only', '')
77
78
79 def lazy_load(name):
80 name = name.removeprefix('!')
81 from setup import build_environment
82
83 try:
84 return getattr(build_environment, name)
85 except AttributeError:
86 raise ImportError(f'The setup.build_environment module has no symbol named: {name}')
87
88
89 def expand_file_list(items, is_paths=True, cross_compile_for='native'):
90 if not items:
91 return []
92 ans = []
93 for item in items:
94 if item.startswith('!'):
95 if cross_compile_for == 'native' or not item.endswith('_dirs'):
96 item = lazy_load(item)
97 if hasattr(item, 'rjust'):
98 item = [item]
99 items = expand_file_list(item, is_paths=is_paths, cross_compile_for=cross_compile_for)
100 else:
101 pkg, category = item[1:].split('_')[:2]
102 if category == 'inc':
103 category = 'include'
104 items = [f'bypy/b/windows/64/{pkg}/{category}']
105 items = expand_file_list(item, is_paths=is_paths, cross_compile_for=cross_compile_for)
106 ans.extend(items)
107 elif '*' in item:
108 ans.extend(expand_file_list(sorted(glob.glob(os.path.join(SRC, item))), is_paths=is_paths, cross_compile_for=cross_compile_for))
109 else:
110 item = [item]
111 if is_paths:
112 item = absolutize(item)
113 ans.extend(item)
114 return ans
115
116
117 def is_ext_allowed(cross_compile_for: str, ext: Extension) -> bool:
118 only = ext.only_build_for
119 if only:
120 if islinux and only == cross_compile_for:
121 return True
122 only = set(only.split())
123 q = set(filter(lambda x: globals()['is' + x], ['bsd', 'freebsd', 'haiku', 'linux', 'macos', 'windows']))
124 return len(q.intersection(only)) > 0
125 return True
126
127
128 def parse_extension(ext, compiling_for='native'):
129 ext = ext.copy()
130 only = ext.pop('only', None)
131 kw = {}
132 name = ext.pop('name')
133 get_key = 'linux_'
134 if iswindows:
135 get_key = 'windows_'
136 elif ismacos:
137 get_key = 'macos_'
138 elif isbsd:
139 get_key = 'bsd_'
140 elif isfreebsd:
141 get_key = 'freebsd_'
142 elif ishaiku:
143 get_key = 'haiku_'
144 if compiling_for == 'windows':
145 get_key = 'windows_'
146
147 def get(k, default=''):
148 ans = ext.pop(k, default)
149 ans = ext.pop(get_key + k, ans)
150 return ans
151
152 for k in 'libraries qt_private ldflags cflags error'.split():
153 kw[k] = expand_file_list(get(k).split(), is_paths=False)
154 defines = get('defines')
155 if defines:
156 if 'cflags' not in kw:
157 kw['cflags'] = []
158 cflags = kw['cflags']
159 prefix = '/D' if get_key == 'windows_' else '-D'
160 cflags.extend(prefix + x for x in defines.split())
161 for k in 'inc_dirs lib_dirs sources headers sip_files'.split():
162 v = get(k)
163 if v:
164 kw[k] = expand_file_list(v.split())
165 kw.update(ext)
166 kw['only'] = only
167 return Extension(name, **kw)
168
169
170 def read_extensions():
171 if hasattr(read_extensions, 'extensions'):
172 return read_extensions.extensions
173 with open(os.path.dirname(os.path.abspath(__file__)) + '/extensions.json', 'rb') as f:
174 ans = read_extensions.extensions = json.load(f)
175 return ans
176
177
178 def get_python_include_paths():
179 ans = []
180 for name in sysconfig.get_path_names():
181 if 'include' in name:
182 ans.append(name)
183
184 def gp(x):
185 return sysconfig.get_path(x)
186
187 return sorted(frozenset(filter(None, map(gp, sorted(ans)))))
188
189
190 is_macos_universal_build = ismacos and 'universal2' in sysconfig.get_platform()
191
192
193 def basic_windows_flags(debug=False):
194 cflags = '/c /nologo /W3 /EHsc /O2 /utf-8'.split()
195 cflags.append('/Zi' if debug else '/DNDEBUG')
196 suffix = 'd' if debug else ''
197 cflags.append('/MD' + suffix)
198 ldflags = f'/DLL /nologo /INCREMENTAL:NO /NODEFAULTLIB:libcmt{suffix}.lib'.split()
199 if debug:
200 ldflags.append('/DEBUG')
201 # cflags = '/c /nologo /Ox /MD /W3 /EHsc /Zi'.split()
202 # ldflags = '/DLL /nologo /INCREMENTAL:NO /DEBUG'.split()
203 cflags.append('/GS-')
204 return cflags, ldflags
205
206
207 class Environment(NamedTuple):
208 cc: str
209 cxx: str
210 linker: str
211 base_cflags: list[str]
212 base_cxxflags: list[str]
213 base_ldflags: list[str]
214 cflags: list[str]
215 ldflags: list[str]
216 make: str
217 internal_inc_prefix: str
218 external_inc_prefix: str
219 libdir_prefix: str
220 lib_prefix: str
221 lib_suffix: str
222 obj_suffix: str
223 cc_input_c_flag: str
224 cc_input_cpp_flag: str
225 cc_output_flag: str
226 platform_name: str
227 dest_ext: str
228 std_prefix: str
229
230 def inc_dirs_to_cflags(self, dirs) -> list[str]:
231 return [self.external_inc_prefix + x for x in dirs]
232
233 def lib_dirs_to_ldflags(self, dirs) -> list[str]:
234 return [self.libdir_prefix + x for x in dirs if x]
235
236 def libraries_to_ldflags(self, libs):
237 def map_name(x):
238 if '/' in x:
239 return x
240 return self.lib_prefix + x + self.lib_suffix
241
242 return list(map(map_name, libs))
243
244
245 def init_env(debug=False, sanitize=False, compiling_for='native'):
246 from setup.build_environment import NMAKE, win_cc, win_inc, win_ld, win_lib
247
248 linker = None
249 internal_inc_prefix = external_inc_prefix = '-I'
250 libdir_prefix = '-L'
251 lib_prefix = '-l'
252 lib_suffix = ''
253 std_prefix = '-std='
254 obj_suffix = '.o'
255 cc_input_c_flag = cc_input_cpp_flag = '-c'
256 cc_output_flag = '-o'
257 platform_name = 'linux'
258 dest_ext = '.so'
259 if isunix:
260 cc = os.environ.get('CC', 'gcc')
261 cxx = os.environ.get('CXX', 'g++')
262 debug = '-ggdb' if debug else ''
263 cflags = os.environ.get('OVERRIDE_CFLAGS', f'-Wall -DNDEBUG {debug} -fno-strict-aliasing -pipe -O3')
264 cflags = shlex.split(cflags) + ['-fPIC']
265 ldflags = os.environ.get('OVERRIDE_LDFLAGS', '-Wall')
266 ldflags = shlex.split(ldflags)
267 base_cflags = shlex.split(os.environ.get('CFLAGS', ''))
268 base_cxxflags = shlex.split(os.environ.get('CXXFLAGS', ''))
269 base_ldflags = shlex.split(os.environ.get('LDFLAGS', ''))
270 cflags += base_cflags
271 ldflags += base_ldflags
272 cflags += ['-fvisibility=hidden']
273 if sanitize:
274 cflags.append('-fsanitize=address')
275
276 if islinux:
277 cflags.append('-pthread')
278 if sys.stdout.isatty():
279 base_cflags.append('-fdiagnostics-color=always')
280 cflags.append('-fdiagnostics-color=always')
281 ldflags.append('-shared')
282
283 if isbsd:
284 cflags.append('-pthread')
285 ldflags.append('-shared')
286
287 if ishaiku:
288 cflags.append('-lpthread')
289 ldflags.append('-shared')
290
291 if islinux or isbsd or ishaiku:
292 cflags.extend('-I' + x for x in get_python_include_paths())
293 ldlib = sysconfig.get_config_var('LIBDIR')
294 if ldlib:
295 ldflags += ['-L' + ldlib]
296 ldlib = sysconfig.get_config_var('VERSION')
297 if ldlib:
298 ldflags += ['-lpython' + ldlib + sys.abiflags]
299 ldflags += (sysconfig.get_config_var('LINKFORSHARED') or '').split()
300
301 if ismacos:
302 platform_name = 'macos'
303 if is_macos_universal_build:
304 cflags.extend(['-arch', 'x86_64', '-arch', 'arm64'])
305 ldflags.extend(['-arch', 'x86_64', '-arch', 'arm64'])
306 cflags.append('-D_OSX')
307 ldflags.extend('-bundle -undefined dynamic_lookup'.split())
308 cflags.extend(['-fno-common', '-dynamic'])
309 cflags.extend('-I' + x for x in get_python_include_paths())
310 # Qt headers use __yield() without including <arm_acle.h>, suppress the
311 # resulting error from newer clang versions
312 base_cxxflags.append('-Wno-implicit-function-declaration')
313
314 if iswindows or compiling_for == 'windows':
315 platform_name = 'windows'
316 std_prefix = '/std:'
317 cc = cxx = win_cc
318 linker = win_ld
319 cflags, ldflags = basic_windows_flags(debug)
320 base_cflags, base_cxxflags, base_ldflags = [], [], []
321 if compiling_for == 'windows':
322 cc = cxx = 'clang-cl'
323 linker = 'lld-link'
324 splat = '.build-cache/xwin/root'
325 cflags.append('-fcolor-diagnostics')
326 cflags.append('-fansi-escape-codes')
327 for I in 'sdk/include/um sdk/include/cppwinrt sdk/include/shared sdk/include/ucrt crt/include sdk/include/winrt'.split():
328 cflags.append('/external:I')
329 cflags.append(f'{splat}/{I}')
330 for L in 'sdk/lib/um crt/lib sdk/lib/ucrt'.split():
331 ldflags.append(f'/libpath:{splat}/{L}')
332 for L in os.environ.get('CROSS_LIBDIRS', '').split(os.pathsep):
333 ldflags.append(f'/libpath:{L}')
334 else:
335 for p in win_inc:
336 cflags.append('-I' + p)
337 for p in win_lib:
338 if p:
339 ldflags.append('/LIBPATH:' + p)
340 internal_inc_prefix = external_inc_prefix = '/I'
341 libdir_prefix = '/libpath:'
342 lib_prefix = ''
343 lib_suffix = '.lib'
344 cc_input_c_flag = '/Tc'
345 cc_input_cpp_flag = '/Tp'
346 cc_output_flag = '/Fo'
347 obj_suffix = '.obj'
348 dest_ext = '.pyd'
349 if compiling_for == 'windows':
350 external_inc_prefix = '/external:I'
351 dest_ext = '.cross-windows-x64' + dest_ext
352 obj_suffix = '.cross-windows-x64' + obj_suffix
353 cflags.append('/external:I')
354 cflags.append('bypy/b/windows/64/pkg/python/private/python/include')
355 ldflags.append('/libpath:' + 'bypy/b/windows/64/pkg/python/private/python/libs')
356 else:
357 cflags.extend('-I' + x for x in get_python_include_paths())
358 ldflags.append('/LIBPATH:' + os.path.join(sysconfig.get_config_var('prefix'), 'libs'))
359 return Environment(
360 platform_name=platform_name,
361 dest_ext=dest_ext,
362 std_prefix=std_prefix,
363 base_cflags=base_cflags,
364 base_cxxflags=base_cxxflags,
365 base_ldflags=base_ldflags,
366 cc=cc,
367 cxx=cxx,
368 cflags=cflags,
369 ldflags=ldflags,
370 linker=linker,
371 make=NMAKE if iswindows else 'make',
372 lib_prefix=lib_prefix,
373 obj_suffix=obj_suffix,
374 cc_input_c_flag=cc_input_c_flag,
375 cc_input_cpp_flag=cc_input_cpp_flag,
376 cc_output_flag=cc_output_flag,
377 internal_inc_prefix=internal_inc_prefix,
378 external_inc_prefix=external_inc_prefix,
379 libdir_prefix=libdir_prefix,
380 lib_suffix=lib_suffix,
381 )
382
383
384 class Build(Command):
385 short_description = 'Build calibre C/C++ extension modules'
386 DEFAULT_OUTPUTDIR = os.path.abspath(os.path.join(SRC, 'calibre', 'plugins'))
387 DEFAULT_BUILDDIR = os.path.abspath(os.path.join(os.path.dirname(SRC), 'build'))
388
389 description = textwrap.dedent('''\
390 calibre depends on several python extensions written in C/C++.
391 This command will compile them. You can influence the compile
392 process by several environment variables, listed below:
393
394 CC - C Compiler defaults to gcc
395 CXX - C++ Compiler, defaults to g++
396 CFLAGS - Extra compiler flags
397 LDFLAGS - Extra linker flags
398
399 POPPLER_INC_DIR - poppler header files
400 POPPLER_LIB_DIR - poppler-qt4 library
401
402 PODOFO_INC_DIR - podofo header files
403 PODOFO_LIB_DIR - podofo library files
404
405 QMAKE - Path to qmake
406 ''')
407
408 def add_options(self, parser):
409 choices = [e['name'] for e in read_extensions()] + ['all', 'headless']
410 parser.add_option(
411 '-1', '--only', choices=choices, default='all', help=('Build only the named extension. Available: ' + ', '.join(choices) + '. Default:%default')
412 )
413 parser.add_option('--no-compile', default=False, action='store_true', help='Skip compiling all C/C++ extensions.')
414 parser.add_option('--build-dir', default=None, help='Path to directory in which to place object files during the build process, defaults to "build"')
415 parser.add_option('--output-dir', default=None, help='Path to directory in which to place the built extensions. Defaults to src/calibre/plugins')
416 parser.add_option('--debug', default=False, action='store_true', help='Build in debug mode')
417 parser.add_option(
418 '--sanitize', default=False, action='store_true', help='Build with sanitization support. Run with LD_PRELOAD=$(gcc -print-file-name=libasan.so)'
419 )
420 parser.add_option(
421 '--cross-compile-extensions',
422 choices='windows disabled'.split(),
423 default='disabled',
424 help=(
425 'Cross compile extensions for other platforms. Useful for development.'
426 ' Currently supports of windows extensions on Linux. Remember to run ./setup.py xwin first to install the Windows SDK locally. '
427 ),
428 )
429
430 def dump_db(self, name, db):
431 os.makedirs('build', exist_ok=True)
432 existing = []
433 try:
434 with open(f'build/{name}_commands.json', 'rb') as f:
435 existing = json.load(f)
436 except FileNotFoundError:
437 pass
438 combined = {x['output']: x for x in existing}
439 for x in db:
440 combined[x['output']] = x
441 try:
442 with open(f'build/{name}_commands.json', 'w') as f:
443 json.dump(tuple(combined.values()), f, indent=2)
444 except OSError as err:
445 if err.errno != errno.EROFS:
446 raise
447
448 def run(self, opts):
449 from setup.parallel_build import create_job, parallel_build
450
451 if opts.no_compile:
452 self.info('--no-compile specified, skipping compilation')
453 return
454 self.compiling_for = 'native'
455 if islinux and opts.cross_compile_extensions == 'windows':
456 self.compiling_for = 'windows'
457 if not os.path.exists('.build-cache/xwin/root'):
458 subprocess.check_call([sys.executable, 'setup.py', 'xwin'])
459 self.env = init_env(debug=opts.debug)
460 self.windows_cross_env = init_env(debug=opts.debug, compiling_for='windows')
461 all_extensions = tuple(map(partial(parse_extension, compiling_for=self.compiling_for), read_extensions()))
462 self.build_dir = os.path.abspath(opts.build_dir or self.DEFAULT_BUILDDIR)
463 self.output_dir = os.path.abspath(opts.output_dir or self.DEFAULT_OUTPUTDIR)
464 self.obj_dir = os.path.join(self.build_dir, 'objects')
465 for x in (self.output_dir, self.obj_dir):
466 os.makedirs(x, exist_ok=True)
467 pyqt_extensions, extensions = [], []
468 for ext in all_extensions:
469 if opts.only not in {'all', ext.name}:
470 continue
471 if not is_ext_allowed(self.compiling_for, ext):
472 continue
473 if ext.error:
474 if ext.optional:
475 self.warn(ext.error)
476 continue
477 else:
478 raise Exception(ext.error)
479 (pyqt_extensions if ext.sip_files else extensions).append(ext)
480
481 jobs = []
482 objects_map = {}
483 self.info(f'Building {len(extensions) + len(pyqt_extensions)} extensions')
484 ccdb = []
485 for ext in all_extensions:
486 if ext in pyqt_extensions:
487 continue
488 cmds, objects = self.get_compile_commands(ext, ccdb)
489 objects_map[id(ext)] = objects
490 if ext in extensions:
491 for cmd in cmds:
492 jobs.append(create_job(cmd.cmd))
493 self.dump_db('compile', ccdb)
494 if jobs:
495 self.info(f'Compiling {len(jobs)} files...')
496 if not parallel_build(jobs, self.info):
497 raise SystemExit(1)
498 jobs, link_commands, lddb = [], [], []
499 for ext in all_extensions:
500 if ext in pyqt_extensions:
501 continue
502 objects = objects_map[id(ext)]
503 cmd = self.get_link_command(ext, objects, lddb)
504 if ext in extensions and cmd is not None:
505 link_commands.append(cmd)
506 jobs.append(create_job(cmd.cmd))
507 self.dump_db('link', lddb)
508 if jobs:
509 self.info(f'Linking {len(jobs)} files...')
510 if not parallel_build(jobs, self.info):
511 raise SystemExit(1)
512 for cmd in link_commands:
513 self.post_link_cleanup(cmd)
514
515 jobs = []
516 sbf_map = {}
517 for ext in pyqt_extensions:
518 cmd, sbf, cwd = self.get_sip_commands(ext)
519 sbf_map[id(ext)] = sbf
520 if cmd is not None:
521 jobs.append(create_job(cmd, cwd=cwd))
522 if jobs:
523 self.info(f'SIPing {len(jobs)} files...')
524 if not parallel_build(jobs, self.info):
525 raise SystemExit(1)
526 for ext in pyqt_extensions:
527 sbf = sbf_map[id(ext)]
528 if not os.path.exists(sbf):
529 self.build_pyqt_extension(ext, sbf)
530
531 if opts.only in {'all', 'headless'}:
532 self.build_headless()
533
534 def dest(self, ext, env):
535 return os.path.join(self.output_dir, getattr(ext, 'name', ext)) + env.dest_ext
536
537 def env_for_compilation_db(self, ext):
538 if is_ext_allowed('native', ext):
539 return self.env
540 if ext.only_build_for == 'windows':
541 return self.windows_cross_env
542
543 def get_compile_commands(self, ext, db):
544 obj_dir = self.j(self.obj_dir, ext.name)
545
546 def get(src: str, env: Environment, for_tooling: bool = False) -> CompileCommand:
547 compiler = env.cxx if ext.needs_cxx else env.cc
548 obj = self.j(obj_dir, os.path.splitext(self.b(src))[0] + env.obj_suffix)
549 inf = env.cc_input_cpp_flag if src.endswith(('.cpp', '.cxx')) else env.cc_input_c_flag
550 sinc = [inf, src]
551 if env.cc_output_flag.startswith('/'):
552 if for_tooling: # clangd gets confused by cl.exe style source and output flags
553 oinc = ['-o', obj]
554 else:
555 oinc = [env.cc_output_flag + obj]
556 sinc = [inf + src]
557 else:
558 oinc = [env.cc_output_flag, obj]
559 einc = env.inc_dirs_to_cflags(ext.inc_dirs)
560 if env.cc_output_flag.startswith('/'):
561 cflags = ['/DCALIBRE_MODINIT_FUNC=PyMODINIT_FUNC']
562 else:
563 return_type = 'PyObject*'
564 extern_decl = 'extern "C"' if ext.needs_cxx else ''
565 cflags = [f'-DCALIBRE_MODINIT_FUNC={extern_decl} __attribute__ ((visibility ("default"))) {return_type}']
566 if ext.needs_cxx and ext.needs_cxx_std:
567 if env.cc_output_flag.startswith('/') and ext.needs_cxx == '11':
568 ext.needs_cxx = '14'
569 cflags.append(env.std_prefix + 'c++' + ext.needs_cxx_std)
570
571 if ext.needs_c_std and not env.std_prefix.startswith('/'):
572 cflags.append(env.std_prefix + 'c' + ext.needs_c_std)
573
574 cmd = [compiler] + env.cflags + cflags + ext.cflags + einc + sinc + oinc
575 return CompileCommand(cmd, src, obj)
576
577 objects = []
578 ans = []
579 os.makedirs(obj_dir, exist_ok=True)
580
581 for src in ext.sources:
582 cc = get(src, self.windows_cross_env if self.compiling_for == 'windows' else self.env)
583 objects.append(cc.dest)
584 if self.newer(cc.dest, [src] + ext.headers):
585 ans.append(cc)
586 env = self.env_for_compilation_db(ext)
587 if env is not None:
588 cc = get(src, env, for_tooling=True)
589 db.append({
590 'arguments': cc.cmd,
591 'directory': os.getcwd(),
592 'file': os.path.relpath(src, os.getcwd()),
593 'output': os.path.relpath(cc.dest, os.getcwd()),
594 })
595 return ans, objects
596
597 def get_link_command(self, ext, objects, lddb):
598
599 def get(env: Environment) -> LinkCommand:
600 dest = self.dest(ext, env)
601 compiler = env.cxx if ext.needs_cxx else env.cc
602 linker = env.linker or compiler
603 cmd = [linker]
604 elib = env.lib_dirs_to_ldflags(ext.lib_dirs)
605 xlib = env.libraries_to_ldflags(ext.libraries)
606 all_objects = sorted(objects + ext.extra_objs)
607 if iswindows or env is self.windows_cross_env:
608 pre_ld_flags = []
609 if ext.uses_icu:
610 # windows has its own ICU libs that don't work
611 pre_ld_flags = elib
612 cmd += pre_ld_flags + env.ldflags + ext.ldflags + elib + xlib + ['/EXPORT:' + init_symbol_name(ext.name)] + all_objects + ['/OUT:' + dest]
613 else:
614 cmd += all_objects + ['-o', dest] + env.ldflags + ext.ldflags + elib + xlib
615 return LinkCommand(cmd, objects, dest)
616
617 env = self.env_for_compilation_db(ext)
618 if env is not None:
619 ld = get(env)
620 lddb.append({'arguments': ld.cmd, 'directory': os.getcwd(), 'output': os.path.relpath(ld.dest, os.getcwd())})
621
622 env = self.windows_cross_env if self.compiling_for == 'windows' else self.env
623 lc = get(env)
624 if self.newer(lc.dest, objects + ext.extra_objs):
625 return lc
626
627 def post_link_cleanup(self, link_command):
628 if iswindows:
629 dest = link_command.dest
630 for x in ('.exp', '.lib'):
631 x = os.path.splitext(dest)[0] + x
632 if os.path.exists(x):
633 os.remove(x)
634
635 def check_call(self, *args, **kwargs):
636 """print cmdline if an error occurred
637
638 If something is missing (cmake e.g.) you get a non-informative error
639 self.check_call(qmc + [ext.name+'.pro'])
640 so you would have to look at the source to see the actual command.
641 """
642 try:
643 subprocess.check_call(*args, **kwargs)
644 except Exception:
645 cmdline = ' '.join([f'"{arg}"' if ' ' in arg else arg for arg in args[0]])
646 print(f'Error while executing: {cmdline}\n')
647 raise
648
649 def build_headless(self):
650 from setup.parallel_build import cpu_count
651
652 if iswindows or ishaiku:
653 return # Don't have headless operation on these platforms
654 from setup.build_environment import CMAKE, sw
655
656 self.info('\n####### Building headless QPA plugin', '#' * 7)
657 a = absolutize
658 headers = a([
659 'calibre/headless/headless_backingstore.h',
660 'calibre/headless/headless_integration.h',
661 ])
662 sources = a([
663 'calibre/headless/main.cpp',
664 'calibre/headless/headless_backingstore.cpp',
665 'calibre/headless/headless_integration.cpp',
666 ])
667 others = a(['calibre/headless/headless.json'])
668 target = self.dest('headless', self.env)
669 if not ismacos:
670 target = target.replace('headless', 'libheadless')
671 if not self.newer(target, headers + sources + others):
672 return
673
674 bdir = self.j(self.build_dir, 'headless')
675 if os.path.exists(bdir):
676 shutil.rmtree(bdir)
677 sdir = os.path.join(bdir, 'src')
678 shutil.copytree(os.path.dirname(sources[0]), sdir)
679 with open(os.path.join(sdir, 'CMakeLists.txt'), 'r+') as f:
680 raw = f.read()
681 qt = lazy_load('qt')
682 if qt['version'] >= (6, 10):
683 fp = 'find_package(Qt6 REQUIRED COMPONENTS Gui GuiPrivate Core CorePrivate)'
684 ll = 'target_link_libraries(headless PRIVATE Qt6::Gui Qt6::GuiPrivate Qt6::Core Qt6::CorePrivate)'
685 else:
686 fp = 'find_package(Qt6Gui REQUIRED)'
687 ll = 'target_link_libraries(headless PRIVATE Qt::Gui Qt::GuiPrivate Qt::Core Qt::CorePrivate)'
688 raw = raw.replace('__FIND_GUI__', fp)
689 raw = raw.replace('__LINK_TARGETS__', ll)
690 f.seek(0), f.truncate()
691 f.write(raw)
692 bdir = os.path.join(bdir, 'build')
693 cmd = [CMAKE]
694 if is_macos_universal_build:
695 cmd += ['-DCMAKE_OSX_ARCHITECTURES=x86_64;arm64']
696 if sw and os.path.exists(os.path.join(sw, 'qt')):
697 cmd += ['-DCMAKE_SYSTEM_PREFIX_PATH=' + os.path.join(sw, 'qt').replace(os.sep, '/')]
698 os.makedirs(bdir)
699 cwd = os.getcwd()
700 os.chdir(bdir)
701 try:
702 self.check_call(cmd + ['-S', sdir])
703 self.check_call([self.env.make] + [f'-j{cpu_count or 1}'])
704 finally:
705 os.chdir(cwd)
706 os.rename(self.j(bdir, 'libheadless.so'), target)
707
708 def create_sip_build_skeleton(self, src_dir, ext):
709 from setup.build_environment import pyqt_sip_abi_version
710
711 abi_version = ''
712 if pyqt_sip_abi_version():
713 abi_version = f'abi-version = "{pyqt_sip_abi_version()}"'
714 sipf = ext.sip_files[0]
715 needs_exceptions = 'true' if ext.needs_exceptions else 'false'
716 with open(os.path.join(src_dir, 'pyproject.toml'), 'w') as f:
717 f.write(f'''
718 [build-system]
719 requires = ["sip >=5.3", "PyQt-builder >=1"]
720 build-backend = "sipbuild.api"
721
722 [tool.sip]
723 project-factory = "pyqtbuild:PyQtProject"
724
725 [tool.sip.project]
726 sip-files-dir = "."
727 {abi_version}
728
729 [project]
730 name = "{ext.name}"
731
732 [tool.sip.builder]
733 qmake-settings = [
734 """QMAKE_CC = {self.env.cc}""",
735 """QMAKE_CXX = {self.env.cxx}""",
736 """QMAKE_LINK = {self.env.linker or self.env.cxx}""",
737 """QMAKE_CFLAGS += {shlex.join(self.env.base_cflags)}""",
738 """QMAKE_CXXFLAGS += {shlex.join(self.env.base_cxxflags)}""",
739 """QMAKE_LFLAGS += {shlex.join(self.env.base_ldflags)}""",
740 ]
741
742 [tool.sip.bindings.{ext.name}]
743 headers = {sorted(ext.headers)}
744 sources = {sorted(ext.sources)}
745 exceptions = {needs_exceptions}
746 include-dirs = {ext.inc_dirs}
747 qmake-QT = {ext.qt_modules}
748 sip-file = {os.path.basename(sipf)!r}
749 ''')
750 shutil.copy2(sipf, src_dir)
751
752 def get_sip_commands(self, ext):
753 from setup.build_environment import QMAKE
754
755 pyqt_dir = self.j(self.build_dir, 'pyqt')
756 src_dir = self.j(pyqt_dir, ext.name)
757 # TODO: Handle building extensions with multiple SIP files.
758 sipf = ext.sip_files[0]
759 sbf = self.j(src_dir, self.b(sipf) + '.sbf')
760 cmd = None
761 cwd = None
762 if self.newer(sbf, [sipf] + ext.headers + ext.sources):
763 shutil.rmtree(src_dir, ignore_errors=True)
764 os.makedirs(src_dir)
765 self.create_sip_build_skeleton(src_dir, ext)
766 cwd = src_dir
767 cmd = [sys.executable, '-m', 'sipbuild.tools.build', '--verbose', '--no-make', '--qmake', QMAKE]
768 return cmd, sbf, cwd
769
770 def build_pyqt_extension(self, ext, sbf):
771 self.info(f'\n####### Building {ext.name} extension', '#' * 7)
772 src_dir = os.path.dirname(sbf)
773 cwd = os.getcwd()
774 try:
775 os.chdir(os.path.join(src_dir, 'build'))
776 env = os.environ.copy()
777 if is_macos_universal_build:
778 env['ARCHS'] = 'x86_64 arm64'
779 self.check_call([self.env.make] + ([] if iswindows else [f'-j{os.cpu_count() or 1}']), env=env)
780 e = 'pyd' if iswindows else 'so'
781 m = glob.glob(f'{ext.name}/{ext.name}.*{e}')
782 if not m:
783 raise SystemExit(f'No built PyQt extension file in {os.path.join(os.getcwd(), ext.name)}')
784 if len(m) != 1:
785 raise SystemExit(f'Found extra PyQt extension files: {m}')
786 shutil.copy2(m[0], self.dest(ext, self.env))
787 with open(sbf, 'w') as f:
788 f.write('done')
789 finally:
790 os.chdir(cwd)
791
792 def clean(self):
793 self.output_dir = self.DEFAULT_OUTPUTDIR
794 extensions = map(parse_extension, read_extensions())
795 env = init_env()
796 for ext in extensions:
797 dest = self.dest(ext, env)
798 b, d = os.path.basename(dest), os.path.dirname(dest)
799 b = b.split('.')[0] + '.*'
800 for x in glob.glob(os.path.join(d, b)):
801 if os.path.exists(x):
802 os.remove(x)
803 build_dir = self.DEFAULT_BUILDDIR
804 if os.path.exists(build_dir):
805 shutil.rmtree(build_dir)