-
Notifications
You must be signed in to change notification settings - Fork 69
/
__init__.py
1261 lines (1061 loc) · 47.9 KB
/
__init__.py
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Quansight, LLC
# SPDX-FileCopyrightText: 2021 Filipe Laíns <lains@riseup.net>
"""Meson Python build backend
Implements PEP 517 hooks.
"""
from __future__ import annotations
import collections
import contextlib
import functools
import importlib.machinery
import io
import itertools
import json
import os
import pathlib
import platform
import re
import shutil
import subprocess
import sys
import sysconfig
import tarfile
import tempfile
import textwrap
import typing
import warnings
from typing import (
Any, Callable, ClassVar, DefaultDict, Dict, List, Optional, Sequence, Set,
TextIO, Tuple, Type, TypeVar, Union
)
if sys.version_info < (3, 11):
import tomli as tomllib
else:
import tomllib
import mesonpy._compat
import mesonpy._elf
import mesonpy._introspection
import mesonpy._tags
import mesonpy._util
import mesonpy._wheelfile
from mesonpy._compat import (
Collection, Iterable, Iterator, Literal, Mapping, ParamSpec, Path,
cached_property, read_binary, typing_get_args
)
if typing.TYPE_CHECKING: # pragma: no cover
import pyproject_metadata # noqa: F401
__version__ = '0.13.0.dev0'
_COLORS = {
'red': '\33[31m',
'cyan': '\33[36m',
'yellow': '\33[93m',
'light_blue': '\33[94m',
'bold': '\33[1m',
'dim': '\33[2m',
'underline': '\33[4m',
'reset': '\33[0m',
}
_NO_COLORS = {color: '' for color in _COLORS}
_NINJA_REQUIRED_VERSION = '1.8.2'
class _depstr:
"""Namespace that holds the requirement strings for dependencies we *might*
need at runtime. Having them in one place makes it easier to update.
"""
patchelf = 'patchelf >= 0.11.0'
ninja = f'ninja >= {_NINJA_REQUIRED_VERSION}'
def _init_colors() -> Dict[str, str]:
"""Detect if we should be using colors in the output. We will enable colors
if running in a TTY, and no environment variable overrides it. Setting the
NO_COLOR (https://no-color.org/) environment variable force-disables colors,
and FORCE_COLOR forces color to be used, which is useful for thing like
Github actions.
"""
if 'NO_COLOR' in os.environ:
if 'FORCE_COLOR' in os.environ:
warnings.warn('Both NO_COLOR and FORCE_COLOR environment variables are set, disabling color')
return _NO_COLORS
elif 'FORCE_COLOR' in os.environ or sys.stdout.isatty():
return _COLORS
return _NO_COLORS
_STYLES = _init_colors() # holds the color values, should be _COLORS or _NO_COLORS
_EXTENSION_SUFFIXES = importlib.machinery.EXTENSION_SUFFIXES.copy()
_EXTENSION_SUFFIX_REGEX = re.compile(r'^\.(?:(?P<abi>[^.]+)\.)?(?:so|pyd|dll)$')
assert all(re.match(_EXTENSION_SUFFIX_REGEX, x) for x in _EXTENSION_SUFFIXES)
def _showwarning(
message: Union[Warning, str],
category: Type[Warning],
filename: str,
lineno: int,
file: Optional[TextIO] = None,
line: Optional[str] = None,
) -> None: # pragma: no cover
"""Callable to override the default warning handler, to have colored output."""
print('{yellow}WARNING{reset} {}'.format(message, **_STYLES))
def _setup_cli() -> None:
"""Setup CLI stuff (eg. handlers, hooks, etc.). Should only be called when
actually we are in control of the CLI, not on a normal import.
"""
warnings.showwarning = _showwarning
try: # pragma: no cover
import colorama
except ModuleNotFoundError: # pragma: no cover
pass
else: # pragma: no cover
colorama.init() # fix colors on windows
def _as_python_declaration(value: Any) -> str:
if isinstance(value, str):
return f"r'{value}'"
elif isinstance(value, os.PathLike):
return _as_python_declaration(os.fspath(value))
elif isinstance(value, Iterable):
return '[' + ', '.join(map(_as_python_declaration, value)) + ']'
raise NotImplementedError(f'Unsupported type: {type(value)}')
class Error(RuntimeError):
def __str__(self) -> str:
return str(self.args[0])
class ConfigError(Error):
"""Error in the backend configuration."""
class MesonBuilderError(Error):
"""Error when building the Meson package."""
class _WheelBuilder():
"""Helper class to build wheels from projects."""
# Maps wheel scheme names to Meson placeholder directories
_SCHEME_MAP: ClassVar[Dict[str, Tuple[str, ...]]] = {
'scripts': ('{bindir}',),
'purelib': ('{py_purelib}',),
'platlib': ('{py_platlib}', '{moduledir_shared}'),
'headers': ('{includedir}',),
'data': ('{datadir}',),
# our custom location
'mesonpy-libs': ('{libdir}', '{libdir_shared}')
}
def __init__(
self,
project: Project,
metadata: Optional[pyproject_metadata.StandardMetadata],
source_dir: pathlib.Path,
install_dir: pathlib.Path,
build_dir: pathlib.Path,
sources: Dict[str, Dict[str, Any]],
copy_files: Dict[str, str],
) -> None:
self._project = project
self._metadata = metadata
self._source_dir = source_dir
self._install_dir = install_dir
self._build_dir = build_dir
self._sources = sources
self._copy_files = copy_files
self._libs_build_dir = self._build_dir / 'mesonpy-wheel-libs'
@cached_property
def _wheel_files(self) -> DefaultDict[str, List[Tuple[pathlib.Path, str]]]:
return self._map_to_wheel(self._sources, self._copy_files)
@property
def _has_internal_libs(self) -> bool:
return bool(self._wheel_files['mesonpy-libs'])
@property
def _has_extension_modules(self) -> bool:
# Assume that all code installed in {platlib} is Python ABI dependent.
return bool(self._wheel_files['platlib'])
@property
def normalized_name(self) -> str:
return self._project.name.replace('-', '_')
@property
def basename(self) -> str:
"""Normalized wheel name and version (eg. meson_python-1.0.0)."""
return '{distribution}-{version}'.format(
distribution=self.normalized_name,
version=self._project.version,
)
@property
def tag(self) -> mesonpy._tags.Tag:
"""Wheel tags."""
if self.is_pure:
return mesonpy._tags.Tag('py3', 'none', 'any')
if not self._has_extension_modules:
# The wheel has platform dependent code (is not pure) but
# does not contain any extension module (does not
# distribute any file in {platlib}) thus use generic
# implementation and ABI tags.
return mesonpy._tags.Tag('py3', 'none', None)
return mesonpy._tags.Tag(None, self._stable_abi, None)
@property
def name(self) -> str:
"""Wheel name, this includes the basename and tag."""
return '{basename}-{tag}'.format(
basename=self.basename,
tag=self.tag,
)
@property
def distinfo_dir(self) -> str:
return f'{self.basename}.dist-info'
@property
def data_dir(self) -> str:
return f'{self.basename}.data'
@cached_property
def is_pure(self) -> bool:
"""Is the wheel "pure" (architecture independent)?"""
# XXX: I imagine some users might want to force the package to be
# non-pure, but I think it's better that we evaluate use-cases as they
# arise and make sure allowing the user to override this is indeed the
# best option for the use-case.
if self._wheel_files['platlib']:
return False
for _, file in self._wheel_files['scripts']:
if self._is_native(file):
return False
return True
@property
def wheel(self) -> bytes: # noqa: F811
"""Return WHEEL file for dist-info."""
return textwrap.dedent('''
Wheel-Version: 1.0
Generator: meson
Root-Is-Purelib: {is_purelib}
Tag: {tag}
''').strip().format(
is_purelib='true' if self.is_pure else 'false',
tag=self.tag,
).encode()
@property
def entrypoints_txt(self) -> bytes:
"""dist-info entry_points.txt."""
if not self._metadata:
return b''
data = self._metadata.entrypoints.copy()
data.update({
'console_scripts': self._metadata.scripts,
'gui_scripts': self._metadata.gui_scripts,
})
text = ''
for entrypoint in data:
if data[entrypoint]:
text += f'[{entrypoint}]\n'
for name, target in data[entrypoint].items():
text += f'{name} = {target}\n'
text += '\n'
return text.encode()
@cached_property
def _stable_abi(self) -> Optional[str]:
"""Determine stabe ABI compatibility.
Examine all files installed in {platlib} that look like
extension modules (extension .pyd on Windows, .dll on Cygwin,
and .so on other platforms) and, if they all share the same
PEP 3149 filename stable ABI tag, return it.
All files that look like extension modules are verified to
have a file name compatibel with what is expected by the
Python interpreter. An exception is raised otherwise.
Other files are ignored.
"""
soext = sorted(_EXTENSION_SUFFIXES, key=len)[0]
abis = []
for path, src in self._wheel_files['platlib']:
if path.suffix == soext:
match = re.match(r'^[^.]+(.*)$', path.name)
assert match is not None
suffix = match.group(1)
if suffix not in _EXTENSION_SUFFIXES:
raise ValueError(
f'Extension module {str(path)!r} not compatible with Python interpreter. '
f'Filename suffix {suffix!r} not in {set(_EXTENSION_SUFFIXES)}.')
match = _EXTENSION_SUFFIX_REGEX.match(suffix)
assert match is not None
abis.append(match.group('abi'))
stable = [x for x in abis if x and re.match(r'abi\d+', x)]
if len(stable) > 0 and len(stable) == len(abis) and all(x == stable[0] for x in stable[1:]):
return stable[0]
return None
@property
def top_level_modules(self) -> Collection[str]:
modules = set()
for type_ in self._wheel_files:
for path, _ in self._wheel_files[type_]:
top_part = path.parts[0]
# file module
if top_part.endswith('.py'):
modules.add(top_part[:-3])
else:
# native module
for extension in _EXTENSION_SUFFIXES:
if top_part.endswith(extension):
modules.add(top_part[:-len(extension)])
# XXX: We assume the order in _EXTENSION_SUFFIXES
# goes from more specific to last, so we go
# with the first match we find.
break
else: # nobreak
# skip Windows import libraries
if top_part.endswith('.a'):
continue
# package module
modules.add(top_part)
return modules
def _is_native(self, file: Union[str, pathlib.Path]) -> bool:
"""Check if file is a native file."""
self._project.build() # the project needs to be built for this :/
with open(file, 'rb') as f:
if platform.system() == 'Linux':
return f.read(4) == b'\x7fELF' # ELF
elif platform.system() == 'Darwin':
return f.read(4) in (
b'\xfe\xed\xfa\xce', # 32-bit
b'\xfe\xed\xfa\xcf', # 64-bit
b'\xcf\xfa\xed\xfe', # arm64
b'\xca\xfe\xba\xbe', # universal / fat (same as java class so beware!)
)
elif platform.system() == 'Windows':
return f.read(2) == b'MZ'
# For unknown platforms, check for file extensions.
_, ext = os.path.splitext(file)
if ext in ('.so', '.a', '.out', '.exe', '.dll', '.dylib', '.pyd'):
return True
return False
def _warn_unsure_platlib(self, origin: pathlib.Path, destination: pathlib.Path) -> None:
"""Warn if we are unsure if the file should be mapped to purelib or platlib.
This happens when we use heuristics to try to map a file purelib or
platlib but can't differentiate between the two. In which case, we place
the file in platlib to be safe and warn the user.
If we can detect the file is architecture dependent and indeed does not
belong in purelib, we will skip the warning.
"""
# {moduledir_shared} is currently handled in heuristics due to a Meson bug,
# but we know that files that go there are supposed to go to platlib.
if self._is_native(origin):
# The file is architecture dependent and does not belong in puredir,
# so the warning is skipped.
return
warnings.warn(
'Could not tell if file was meant for purelib or platlib, '
f'so it was mapped to platlib: {origin} ({destination})',
stacklevel=2,
)
def _map_from_heuristics(self, origin: pathlib.Path, destination: pathlib.Path) -> Optional[Tuple[str, pathlib.Path]]:
"""Extracts scheme and relative destination with heuristics based on the
origin file and the Meson destination path.
"""
warnings.warn('Using heuristics to map files to wheel, this may result in incorrect locations')
sys_paths = mesonpy._introspection.SYSCONFIG_PATHS
# Try to map to Debian dist-packages
if mesonpy._introspection.DEBIAN_PYTHON:
search_path = origin
while search_path != search_path.parent:
search_path = search_path.parent
if search_path.name == 'dist-packages' and search_path.parent.parent.name == 'lib':
calculated_path = origin.relative_to(search_path)
warnings.warn(f'File matched Debian heuristic ({calculated_path}): {origin} ({destination})')
self._warn_unsure_platlib(origin, destination)
return 'platlib', calculated_path
# Try to map to the interpreter purelib or platlib
for scheme in ('purelib', 'platlib'):
# try to match the install path on the system to one of the known schemes
scheme_path = pathlib.Path(sys_paths[scheme]).absolute()
destdir_scheme_path = self._install_dir / scheme_path.relative_to(scheme_path.anchor)
try:
wheel_path = pathlib.Path(origin).relative_to(destdir_scheme_path)
except ValueError:
continue
if sys_paths['purelib'] == sys_paths['platlib']:
self._warn_unsure_platlib(origin, destination)
return 'platlib', wheel_path
return None # no match was found
def _map_from_scheme_map(self, destination: str) -> Optional[Tuple[str, pathlib.Path]]:
"""Extracts scheme and relative destination from Meson paths.
Meson destination path -> (wheel scheme, subpath inside the scheme)
Eg. {bindir}/foo/bar -> (scripts, foo/bar)
"""
for scheme, placeholder in [
(scheme, placeholder)
for scheme, placeholders in self._SCHEME_MAP.items()
for placeholder in placeholders
]: # scheme name, scheme path (see self._SCHEME_MAP)
if destination.startswith(placeholder):
relative_destination = pathlib.Path(destination).relative_to(placeholder)
return scheme, relative_destination
return None # no match was found
def _map_to_wheel(
self,
sources: Dict[str, Dict[str, Any]],
copy_files: Dict[str, str],
) -> DefaultDict[str, List[Tuple[pathlib.Path, str]]]:
"""Map files to the wheel, organized by scheme."""
wheel_files = collections.defaultdict(list)
for files in sources.values(): # entries in intro-install_plan.json
for file, details in files.items(): # install path -> {destination, tag}
# try mapping to wheel location
meson_destination = details['destination']
install_details = (
# using scheme map
self._map_from_scheme_map(meson_destination)
# using heuristics
or self._map_from_heuristics(
pathlib.Path(copy_files[file]),
pathlib.Path(meson_destination),
)
)
if install_details:
scheme, destination = install_details
wheel_files[scheme].append((destination, file))
continue
# not found
warnings.warn(
'File could not be mapped to an equivalent wheel directory: '
'{} ({})'.format(copy_files[file], meson_destination)
)
return wheel_files
def _install_path(
self,
wheel_file: mesonpy._wheelfile.WheelFile,
counter: mesonpy._util.CLICounter,
origin: Path,
destination: pathlib.Path,
) -> None:
""""Install" file or directory into the wheel
and do the necessary processing before doing so.
Some files might need to be fixed up to set the RPATH to the internal
library directory on Linux wheels for eg.
"""
location = destination.as_posix()
counter.update(location)
# fix file
if os.path.isdir(origin):
for root, dirnames, filenames in os.walk(str(origin)):
# Sort the directory names so that `os.walk` will walk them in a
# defined order on the next iteration.
dirnames.sort()
for name in sorted(filenames):
path = os.path.normpath(os.path.join(root, name))
if os.path.isfile(path):
arcname = os.path.join(destination, os.path.relpath(path, origin).replace(os.path.sep, '/'))
wheel_file.write(path, arcname)
else:
if self._has_internal_libs and platform.system() == 'Linux':
# add .mesonpy.libs to the RPATH of ELF files
if self._is_native(os.fspath(origin)):
# copy ELF to our working directory to avoid Meson having to regenerate the file
new_origin = self._libs_build_dir / pathlib.Path(origin).relative_to(self._build_dir)
os.makedirs(new_origin.parent, exist_ok=True)
shutil.copy2(origin, new_origin)
origin = new_origin
# add our in-wheel libs folder to the RPATH
elf = mesonpy._elf.ELF(origin)
libdir_path = f'$ORIGIN/{os.path.relpath(f".{self._project.name}.mesonpy.libs", destination.parent)}'
if libdir_path not in elf.rpath:
elf.rpath = [*elf.rpath, libdir_path]
wheel_file.write(origin, location)
def _wheel_write_metadata(self, whl: mesonpy._wheelfile.WheelFile) -> None:
# add metadata
whl.writestr(f'{self.distinfo_dir}/METADATA', self._project.metadata)
whl.writestr(f'{self.distinfo_dir}/WHEEL', self.wheel)
if self.entrypoints_txt:
whl.writestr(f'{self.distinfo_dir}/entry_points.txt', self.entrypoints_txt)
# add license (see https://github.com/mesonbuild/meson-python/issues/88)
if self._project.license_file:
whl.write(
self._source_dir / self._project.license_file,
f'{self.distinfo_dir}/{os.path.basename(self._project.license_file)}',
)
def build(self, directory: Path) -> pathlib.Path:
self._project.build() # ensure project is built
wheel_file = pathlib.Path(directory, f'{self.name}.whl')
with mesonpy._wheelfile.WheelFile(wheel_file, 'w') as whl:
self._wheel_write_metadata(whl)
print('{light_blue}{bold}Copying files to wheel...{reset}'.format(**_STYLES))
with mesonpy._util.cli_counter(
len(list(itertools.chain.from_iterable(self._wheel_files.values()))),
) as counter:
# install root scheme files
root_scheme = 'purelib' if self.is_pure else 'platlib'
for destination, origin in self._wheel_files[root_scheme]:
self._install_path(whl, counter, origin, destination)
# install bundled libraries
for destination, origin in self._wheel_files['mesonpy-libs']:
assert platform.system() == 'Linux', 'Bundling libraries in wheel is currently only supported in POSIX!'
destination = pathlib.Path(f'.{self._project.name}.mesonpy.libs', destination)
self._install_path(whl, counter, origin, destination)
# install the other schemes
for scheme in self._SCHEME_MAP:
if scheme in (root_scheme, 'mesonpy-libs'):
continue
for destination, origin in self._wheel_files[scheme]:
destination = pathlib.Path(self.data_dir, scheme, destination)
self._install_path(whl, counter, origin, destination)
return wheel_file
def build_editable(self, directory: Path, verbose: bool = False) -> pathlib.Path:
self._project.build() # ensure project is built
wheel_file = pathlib.Path(directory, f'{self.name}.whl')
install_path = self._source_dir / '.mesonpy' / 'editable' / 'install'
rebuild_commands = self._project.build_commands(install_path)
import_paths = set()
for name, raw_path in mesonpy._introspection.SYSCONFIG_PATHS.items():
if name not in ('purelib', 'platlib'):
continue
path = pathlib.Path(raw_path)
import_paths.add(install_path / path.relative_to(path.anchor))
install_path.mkdir(parents=True, exist_ok=True)
with mesonpy._wheelfile.WheelFile(wheel_file, 'w') as whl:
self._wheel_write_metadata(whl)
whl.writestr(
f'{self.distinfo_dir}/direct_url.json',
self._source_dir.as_uri().encode(),
)
# install hook module
hook_module_name = f'_mesonpy_hook_{self.normalized_name.replace(".", "_")}'
hook_install_code = textwrap.dedent(f'''
MesonpyFinder.install(
project_name={_as_python_declaration(self._project.name)},
hook_name={_as_python_declaration(hook_module_name)},
project_path={_as_python_declaration(self._source_dir)},
build_path={_as_python_declaration(self._build_dir)},
import_paths={_as_python_declaration(import_paths)},
top_level_modules={_as_python_declaration(self.top_level_modules)},
rebuild_commands={_as_python_declaration(rebuild_commands)},
verbose={verbose},
)
''').strip().encode()
whl.writestr(
f'{hook_module_name}.py',
read_binary('mesonpy', '_editable.py') + hook_install_code,
)
# install .pth file
whl.writestr(
f'{self.normalized_name}-editable-hook.pth',
f'import {hook_module_name}'.encode(),
)
# install non-code schemes
for scheme in self._SCHEME_MAP:
if scheme in ('purelib', 'platlib', 'mesonpy-libs'):
continue
for destination, origin in self._wheel_files[scheme]:
destination = pathlib.Path(self.data_dir, scheme, destination)
whl.write(origin, destination.as_posix())
return wheel_file
MesonArgsKeys = Literal['dist', 'setup', 'compile', 'install']
MesonArgs = Mapping[MesonArgsKeys, List[str]]
class Project():
"""Meson project wrapper to generate Python artifacts."""
_ALLOWED_DYNAMIC_FIELDS: ClassVar[List[str]] = [
'version',
]
_metadata: Optional[pyproject_metadata.StandardMetadata]
def __init__( # noqa: C901
self,
source_dir: Path,
working_dir: Path,
build_dir: Optional[Path] = None,
meson_args: Optional[MesonArgs] = None,
editable_verbose: bool = False,
) -> None:
self._source_dir = pathlib.Path(source_dir).absolute()
self._working_dir = pathlib.Path(working_dir).absolute()
self._build_dir = pathlib.Path(build_dir).absolute() if build_dir else (self._working_dir / 'build')
self._editable_verbose = editable_verbose
self._install_dir = self._working_dir / 'install'
self._meson_native_file = self._source_dir / '.mesonpy-native-file.ini'
self._meson_cross_file = self._source_dir / '.mesonpy-cross-file.ini'
self._meson_args: MesonArgs = collections.defaultdict(list)
self._env = os.environ.copy()
# prepare environment
ninja_path = _env_ninja_command()
if ninja_path is not None:
self._env.setdefault('NINJA', str(ninja_path))
# setuptools-like ARCHFLAGS environment variable support
if sysconfig.get_platform().startswith('macosx-'):
archflags = self._env.get('ARCHFLAGS')
if archflags is not None:
arch, *other = filter(None, (x.strip() for x in archflags.split('-arch')))
if other:
raise ConfigError(f'Multi-architecture builds are not supported but $ARCHFLAGS={archflags!r}')
macver, _, nativearch = platform.mac_ver()
if arch != nativearch:
x = self._env.setdefault('_PYTHON_HOST_PLATFORM', f'macosx-{macver}-{arch}')
if not x.endswith(arch):
raise ConfigError(f'$ARCHFLAGS={archflags!r} and $_PYTHON_HOST_PLATFORM={x!r} do not agree')
family = 'aarch64' if arch == 'arm64' else arch
cross_file_data = textwrap.dedent(f'''
[binaries]
c = ['cc', '-arch', {arch!r}]
cpp = ['cpp', '-arch', {arch!r}]
[host_machine]
system = 'Darwin'
cpu = {arch!r}
cpu_family = {family!r}
endian = 'little'
''')
self._meson_cross_file.write_text(cross_file_data)
self._meson_args['setup'].extend(('--cross-file', os.fspath(self._meson_cross_file)))
# load config -- PEP 621 support is optional
self._config = tomllib.loads(self._source_dir.joinpath('pyproject.toml').read_text())
self._pep621 = 'project' in self._config
if self.pep621:
try:
import pyproject_metadata # noqa: F811
except ModuleNotFoundError: # pragma: no cover
self._metadata = None
else:
self._metadata = pyproject_metadata.StandardMetadata.from_pyproject(self._config, self._source_dir)
else:
print(
'{yellow}{bold}! Using Meson to generate the project metadata '
'(no `project` section in pyproject.toml){reset}'.format(**_STYLES)
)
self._metadata = None
if self._metadata:
self._validate_metadata()
# load meson args
for key in self._get_config_key('args'):
self._meson_args[key].extend(self._get_config_key(f'args.{key}'))
# XXX: We should validate the user args to make sure they don't conflict with ours.
self._check_for_unknown_config_keys({
'args': typing_get_args(MesonArgsKeys),
})
# meson arguments from the command line take precedence over
# arguments from the configuration file thus are added later
if meson_args:
for key, value in meson_args.items():
self._meson_args[key].extend(value)
# make sure the build dir exists
self._build_dir.mkdir(exist_ok=True, parents=True)
self._install_dir.mkdir(exist_ok=True, parents=True)
# write the native file
native_file_data = textwrap.dedent(f'''
[binaries]
python = '{sys.executable}'
''')
native_file_mismatch = (
not self._meson_native_file.exists()
or self._meson_native_file.read_text() != native_file_data
)
if native_file_mismatch:
try:
self._meson_native_file.write_text(native_file_data)
except OSError:
# if there are permission errors or something else in the source
# directory, put the native file in the working directory instead
# (this won't survive multiple calls -- Meson will have to be reconfigured)
self._meson_native_file = self._working_dir / '.mesonpy-native-file.ini'
self._meson_native_file.write_text(native_file_data)
# configure the meson project; reconfigure if the user provided a build directory
self._configure(reconfigure=bool(build_dir) and not native_file_mismatch)
# set version if dynamic (this fetches it from Meson)
if self._metadata and 'version' in self._metadata.dynamic:
self._metadata.version = self.version
def _get_config_key(self, key: str) -> Any:
value: Any = self._config
for part in f'tool.meson-python.{key}'.split('.'):
if not isinstance(value, Mapping):
raise ConfigError(f'Configuration entry "tool.meson-python.{key}" should be a TOML table not {type(value)}')
value = value.get(part, {})
return value
def _proc(self, *args: str) -> None:
"""Invoke a subprocess."""
print('{cyan}{bold}+ {}{reset}'.format(' '.join(args), **_STYLES))
r = subprocess.run(list(args), env=self._env, cwd=self._build_dir)
if r.returncode != 0:
raise SystemExit(r.returncode)
def _meson(self, *args: str) -> None:
"""Invoke Meson."""
return self._proc('meson', *args)
def _configure(self, reconfigure: bool = False) -> None:
"""Configure Meson project.
We will try to reconfigure the build directory if possible to avoid
expensive rebuilds.
"""
sys_paths = mesonpy._introspection.SYSCONFIG_PATHS
setup_args = [
f'--prefix={sys.base_prefix}',
os.fspath(self._source_dir),
os.fspath(self._build_dir),
f'--native-file={os.fspath(self._meson_native_file)}',
# TODO: Allow configuring these arguments
'-Ddebug=false',
'-Doptimization=2',
# XXX: This should not be needed, but Meson is using the wrong paths
# in some scenarios, like on macOS.
# https://github.com/mesonbuild/meson-python/pull/87#discussion_r1047041306
'--python.purelibdir',
sys_paths['purelib'],
'--python.platlibdir',
sys_paths['platlib'],
# user args
*self._meson_args['setup'],
]
if reconfigure:
setup_args.insert(0, '--reconfigure')
try:
self._meson('setup', *setup_args)
except subprocess.CalledProcessError:
if reconfigure: # if failed reconfiguring, try a normal configure
self._configure()
else:
raise
def _validate_metadata(self) -> None:
"""Check the pyproject.toml metadata and see if there are any issues."""
assert self._metadata
# check for unsupported dynamic fields
unsupported_dynamic = {
key for key in self._metadata.dynamic
if key not in self._ALLOWED_DYNAMIC_FIELDS
}
if unsupported_dynamic:
s = ', '.join(f'"{x}"' for x in unsupported_dynamic)
raise MesonBuilderError(f'Unsupported dynamic fields: {s}')
# check if we are running on an unsupported interpreter
if self._metadata.requires_python:
self._metadata.requires_python.prereleases = True
if platform.python_version().rstrip('+') not in self._metadata.requires_python:
raise MesonBuilderError(
f'Unsupported Python version {platform.python_version()}, '
f'expected {self._metadata.requires_python}'
)
def _check_for_unknown_config_keys(self, valid_args: Mapping[str, Collection[str]]) -> None:
config = self._config.get('tool', {}).get('meson-python', {})
for key, valid_subkeys in config.items():
if key not in valid_args:
raise ConfigError(f'Unknown configuration key "tool.meson-python.{key}"')
for subkey in valid_args[key]:
if subkey not in valid_subkeys:
raise ConfigError(f'Unknown configuration key "tool.meson-python.{key}.{subkey}"')
@cached_property
def _wheel_builder(self) -> _WheelBuilder:
return _WheelBuilder(
self,
self._metadata,
self._source_dir,
self._install_dir,
self._build_dir,
self._install_plan,
self._copy_files,
)
def build_commands(self, install_dir: Optional[pathlib.Path] = None) -> Sequence[Sequence[str]]:
return (
('meson', 'compile', *self._meson_args['compile'],),
(
'meson',
'install',
'--only-changed',
'--destdir',
os.fspath(install_dir or self._install_dir),
*self._meson_args['install'],
),
)
@functools.lru_cache(maxsize=None)
def build(self) -> None:
"""Trigger the Meson build."""
for cmd in self.build_commands():
self._meson(*cmd[1:])
@classmethod
@contextlib.contextmanager
def with_temp_working_dir(
cls,
source_dir: Path = os.path.curdir,
build_dir: Optional[Path] = None,
meson_args: Optional[MesonArgs] = None,
editable_verbose: bool = False,
) -> Iterator[Project]:
"""Creates a project instance pointing to a temporary working directory."""
with tempfile.TemporaryDirectory(prefix='.mesonpy-', dir=os.fspath(source_dir)) as tmpdir:
yield cls(source_dir, tmpdir, build_dir, meson_args, editable_verbose)
@functools.lru_cache()
def _info(self, name: str) -> Dict[str, Any]:
"""Read info from meson-info directory."""
file = self._build_dir.joinpath('meson-info', f'{name}.json')
return typing.cast(
Dict[str, str],
json.loads(file.read_text())
)
@property
def _install_plan(self) -> Dict[str, Dict[str, Dict[str, str]]]:
"""Meson install_plan metadata."""
return self._info('intro-install_plan').copy()
@property
def _copy_files(self) -> Dict[str, str]:
"""Files that Meson will copy on install and the target location."""
copy_files = {}
for origin, destination in self._info('intro-installed').items():
destination_path = pathlib.Path(destination).absolute()
copy_files[origin] = os.fspath(
self._install_dir / destination_path.relative_to(destination_path.anchor)
)
return copy_files
@property
def _lib_paths(self) -> Set[str]:
copy_files = self._copy_files
return {
os.path.dirname(copy_files[file])
for files in self._install_plan.values()
for file, details in files.items()
if details['destination'].startswith('{libdir_')
}
@property
def _meson_name(self) -> str:
"""Name in meson.build."""
name = self._info('intro-projectinfo')['descriptive_name']
assert isinstance(name, str)
return name
@property
def _meson_version(self) -> str:
"""Version in meson.build."""
name = self._info('intro-projectinfo')['version']
assert isinstance(name, str)
return name
@property
def name(self) -> str:
"""Project name. Specified in pyproject.toml."""
name = self._metadata.name if self._metadata else self._meson_name
assert isinstance(name, str)
return name.replace('-', '_')
@property
def version(self) -> str:
"""Project version. Either specified in pyproject.toml or meson.build."""
if self._metadata and 'version' not in self._metadata.dynamic:
version = str(self._metadata.version)
else:
version = self._meson_version
assert isinstance(version, str)
return version
@cached_property
def metadata(self) -> bytes:
"""Project metadata."""
# the rest of the keys are only available when using PEP 621 metadata
if not self.pep621:
data = textwrap.dedent(f'''
Metadata-Version: 2.1
Name: {self.name}
Version: {self.version}
''').strip()
return data.encode()
# re-import pyproject_metadata to raise ModuleNotFoundError if it is really missing
import pyproject_metadata # noqa: F401, F811
assert self._metadata
core_metadata = self._metadata.as_rfc822()
# use self.version as the version may be dynamic -- fetched from Meson
#
# we need to overwrite this field in the RFC822 field as
# pyproject_metadata removes 'version' from the dynamic fields when
# giving it a value via the dataclass
core_metadata.headers['Version'] = [self.version]
return bytes(core_metadata)
@property
def license_file(self) -> Optional[pathlib.Path]:
if self._metadata:
license_ = self._metadata.license
if license_ and license_.file:
return pathlib.Path(license_.file)
return None
@property
def is_pure(self) -> bool:
"""Is the wheel "pure" (architecture independent)?"""
return bool(self._wheel_builder.is_pure)
@property
def pep621(self) -> bool: