-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathuniversal.py
2438 lines (2055 loc) · 84.5 KB
/
universal.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: Apache-2.0
# Copyright 2012-2020 The Meson development team
"""A library of random helper functionality."""
from __future__ import annotations
from pathlib import Path
import argparse
import ast
import enum
import sys
import stat
import time
import abc
import multiprocessing
import platform, subprocess, operator, os, shlex, shutil, re
import collections
from functools import lru_cache, wraps
from itertools import tee
from tempfile import TemporaryDirectory, NamedTemporaryFile
import typing as T
import textwrap
import pickle
import errno
import json
import dataclasses
from mesonbuild import mlog
from .core import MesonException, HoldableObject
if T.TYPE_CHECKING:
from typing_extensions import Literal, Protocol
from .._typing import ImmutableListProtocol
from ..build import ConfigurationData
from ..coredata import StrOrBytesPath
from ..environment import Environment
from ..compilers.compilers import Compiler
from ..interpreterbase.baseobjects import SubProject
class _EnvPickleLoadable(Protocol):
environment: Environment
class _VerPickleLoadable(Protocol):
version: str
# A generic type for pickle_load. This allows any type that has either a
# .version or a .environment to be passed.
_PL = T.TypeVar('_PL', bound=T.Union[_EnvPickleLoadable, _VerPickleLoadable])
FileOrString = T.Union['File', str]
_T = T.TypeVar('_T')
_U = T.TypeVar('_U')
__all__ = [
'GIT',
'python_command',
'NoProjectVersion',
'project_meson_versions',
'SecondLevelHolder',
'File',
'FileMode',
'GitException',
'LibType',
'MachineChoice',
'EnvironmentException',
'FileOrString',
'GitException',
'dump_conf_header',
'OrderedSet',
'PerMachine',
'PerMachineDefaultable',
'PerThreeMachine',
'PerThreeMachineDefaultable',
'ProgressBar',
'RealPathAction',
'TemporaryDirectoryWinProof',
'Version',
'check_direntry_issues',
'classify_unity_sources',
'current_vs_supports_modules',
'darwin_get_object_archs',
'default_libdir',
'default_libexecdir',
'default_prefix',
'default_datadir',
'default_includedir',
'default_infodir',
'default_localedir',
'default_mandir',
'default_sbindir',
'default_sysconfdir',
'detect_subprojects',
'detect_vcs',
'determine_worker_count',
'do_conf_file',
'do_conf_str',
'do_replacement',
'expand_arguments',
'extract_as_list',
'first',
'generate_list',
'get_compiler_for_source',
'get_filenames_templates_dict',
'get_rsp_threshold',
'get_variable_regex',
'get_wine_shortpath',
'git',
'has_path_sep',
'is_aix',
'is_android',
'is_ascii_string',
'is_cygwin',
'is_debianlike',
'is_dragonflybsd',
'is_freebsd',
'is_haiku',
'is_hurd',
'is_irix',
'is_linux',
'is_netbsd',
'is_openbsd',
'is_osx',
'is_parent_path',
'is_qnx',
'is_sunos',
'is_windows',
'is_wsl',
'iter_regexin_iter',
'join_args',
'lazy_property',
'listify',
'listify_array_value',
'partition',
'path_is_in_root',
'pickle_load',
'Popen_safe',
'Popen_safe_logged',
'quiet_git',
'quote_arg',
'relative_to_if_possible',
'relpath',
'replace_if_different',
'run_once',
'get_meson_command',
'set_meson_command',
'split_args',
'stringlistify',
'substitute_values',
'substring_is_in_list',
'typeslistify',
'verbose_git',
'version_compare',
'version_compare_condition_with_min',
'version_compare_many',
'search_version',
'windows_detect_native_arch',
'windows_proof_rm',
'windows_proof_rmtree',
]
class NoProjectVersion:
pass
# TODO: this is such a hack, this really should be either in coredata or in the
# interpreter
# {subproject: project_meson_version}
project_meson_versions: T.Dict[str, T.Union[str, NoProjectVersion]] = {}
from glob import glob
if getattr(sys, 'frozen', False):
# Using e.g. a PyInstaller bundle, such as the MSI installed executable.
# It is conventional for freeze programs to set this attribute to indicate
# that the program is self hosted, and for example there is no associated
# "python" executable.
python_command = [sys.executable, 'runpython']
else:
python_command = [sys.executable]
_meson_command: T.Optional['ImmutableListProtocol[str]'] = None
class EnvironmentException(MesonException):
'''Exceptions thrown while processing and creating the build environment'''
class GitException(MesonException):
def __init__(self, msg: str, output: T.Optional[str] = None):
super().__init__(msg)
self.output = output.strip() if output else ''
GIT = shutil.which('git')
def git(cmd: T.List[str], workingdir: StrOrBytesPath, check: bool = False, **kwargs: T.Any) -> T.Tuple[subprocess.Popen[str], str, str]:
assert GIT is not None, 'Callers should make sure it exists'
cmd = [GIT, *cmd]
p, o, e = Popen_safe(cmd, cwd=workingdir, **kwargs)
if check and p.returncode != 0:
raise GitException('Git command failed: ' + str(cmd), e)
return p, o, e
def quiet_git(cmd: T.List[str], workingdir: StrOrBytesPath, check: bool = False) -> T.Tuple[bool, str]:
if not GIT:
m = 'Git program not found.'
if check:
raise GitException(m)
return False, m
p, o, e = git(cmd, workingdir, check)
if p.returncode != 0:
return False, e
return True, o
def verbose_git(cmd: T.List[str], workingdir: StrOrBytesPath, check: bool = False) -> bool:
if not GIT:
m = 'Git program not found.'
if check:
raise GitException(m)
return False
p, _, _ = git(cmd, workingdir, check, stdout=None, stderr=None)
return p.returncode == 0
def set_meson_command(mainfile: str) -> None:
global _meson_command # pylint: disable=global-statement
# On UNIX-like systems `meson` is a Python script
# On Windows `meson` and `meson.exe` are wrapper exes
if not mainfile.endswith('.py'):
_meson_command = [mainfile]
elif os.path.isabs(mainfile) and mainfile.endswith('mesonmain.py'):
# Can't actually run meson with an absolute path to mesonmain.py, it must be run as -m mesonbuild.mesonmain
_meson_command = python_command + ['-m', 'mesonbuild.mesonmain']
else:
# Either run uninstalled, or full path to meson-script.py
_meson_command = python_command + [mainfile]
# We print this value for unit tests.
if 'MESON_COMMAND_TESTS' in os.environ:
mlog.log(f'meson_command is {_meson_command!r}')
def get_meson_command() -> T.Optional['ImmutableListProtocol[str]']:
return _meson_command
def is_ascii_string(astring: T.Union[str, bytes]) -> bool:
try:
if isinstance(astring, str):
astring.encode('ascii')
elif isinstance(astring, bytes):
astring.decode('ascii')
except UnicodeDecodeError:
return False
return True
def check_direntry_issues(direntry_array: T.Union[T.Iterable[T.Union[str, bytes]], str, bytes]) -> None:
import locale
# Warn if the locale is not UTF-8. This can cause various unfixable issues
# such as os.stat not being able to decode filenames with unicode in them.
# There is no way to reset both the preferred encoding and the filesystem
# encoding, so we can just warn about it.
e = locale.getpreferredencoding()
if e.upper() != 'UTF-8' and not is_windows():
if isinstance(direntry_array, (str, bytes)):
direntry_array = [direntry_array]
for de in direntry_array:
if is_ascii_string(de):
continue
mlog.warning(textwrap.dedent(f'''
You are using {e!r} which is not a Unicode-compatible
locale but you are trying to access a file system entry called {de!r} which is
not pure ASCII. This may cause problems.
'''))
class SecondLevelHolder(HoldableObject, metaclass=abc.ABCMeta):
''' A second level object holder. The primary purpose
of such objects is to hold multiple objects with one
default option. '''
@abc.abstractmethod
def get_default_object(self) -> HoldableObject: ...
class FileMode:
# The first triad is for owner permissions, the second for group permissions,
# and the third for others (everyone else).
# For the 1st character:
# 'r' means can read
# '-' means not allowed
# For the 2nd character:
# 'w' means can write
# '-' means not allowed
# For the 3rd character:
# 'x' means can execute
# 's' means can execute and setuid/setgid is set (owner/group triads only)
# 'S' means cannot execute and setuid/setgid is set (owner/group triads only)
# 't' means can execute and sticky bit is set ("others" triads only)
# 'T' means cannot execute and sticky bit is set ("others" triads only)
# '-' means none of these are allowed
#
# The meanings of 'rwx' perms is not obvious for directories; see:
# https://www.hackinglinuxexposed.com/articles/20030424.html
#
# For information on this notation such as setuid/setgid/sticky bits, see:
# https://en.wikipedia.org/wiki/File_system_permissions#Symbolic_notation
symbolic_perms_regex = re.compile('[r-][w-][xsS-]' # Owner perms
'[r-][w-][xsS-]' # Group perms
'[r-][w-][xtT-]') # Others perms
def __init__(self, perms: T.Optional[str] = None, owner: T.Union[str, int, None] = None,
group: T.Union[str, int, None] = None):
self.perms_s = perms
self.perms = self.perms_s_to_bits(perms)
self.owner = owner
self.group = group
def __repr__(self) -> str:
ret = '<FileMode: {!r} owner={} group={}'
return ret.format(self.perms_s, self.owner, self.group)
@classmethod
def perms_s_to_bits(cls, perms_s: T.Optional[str]) -> int:
'''
Does the opposite of stat.filemode(), converts strings of the form
'rwxr-xr-x' to st_mode enums which can be passed to os.chmod()
'''
if perms_s is None:
# No perms specified, we will not touch the permissions
return -1
eg = 'rwxr-xr-x'
if not isinstance(perms_s, str):
raise MesonException(f'Install perms must be a string. For example, {eg!r}')
if len(perms_s) != 9 or not cls.symbolic_perms_regex.match(perms_s):
raise MesonException(f'File perms {perms_s!r} must be exactly 9 chars. For example, {eg!r}')
perms = 0
# Owner perms
if perms_s[0] == 'r':
perms |= stat.S_IRUSR
if perms_s[1] == 'w':
perms |= stat.S_IWUSR
if perms_s[2] == 'x':
perms |= stat.S_IXUSR
elif perms_s[2] == 'S':
perms |= stat.S_ISUID
elif perms_s[2] == 's':
perms |= stat.S_IXUSR
perms |= stat.S_ISUID
# Group perms
if perms_s[3] == 'r':
perms |= stat.S_IRGRP
if perms_s[4] == 'w':
perms |= stat.S_IWGRP
if perms_s[5] == 'x':
perms |= stat.S_IXGRP
elif perms_s[5] == 'S':
perms |= stat.S_ISGID
elif perms_s[5] == 's':
perms |= stat.S_IXGRP
perms |= stat.S_ISGID
# Others perms
if perms_s[6] == 'r':
perms |= stat.S_IROTH
if perms_s[7] == 'w':
perms |= stat.S_IWOTH
if perms_s[8] == 'x':
perms |= stat.S_IXOTH
elif perms_s[8] == 'T':
perms |= stat.S_ISVTX
elif perms_s[8] == 't':
perms |= stat.S_IXOTH
perms |= stat.S_ISVTX
return perms
dot_C_dot_H_warning = """You are using .C or .H files in your project. This is deprecated.
Currently, Meson treats this as C++ code, but they
used to be treated as C code.
Note that the situation is a bit more complex if you are using the
Visual Studio compiler, as it treats .C files as C code, unless you add
the /TP compiler flag, but this is unreliable.
See https://github.com/mesonbuild/meson/pull/8747 for the discussions."""
class File(HoldableObject):
def __init__(self, is_built: bool, subdir: str, fname: str):
if fname.endswith(".C") or fname.endswith(".H"):
mlog.warning(dot_C_dot_H_warning, once=True)
self.is_built = is_built
self.subdir = subdir
self.fname = fname
self.hash = hash((is_built, subdir, fname))
def __str__(self) -> str:
return self.relative_name()
def __repr__(self) -> str:
ret = '<File: {0}'
if not self.is_built:
ret += ' (not built)'
ret += '>'
return ret.format(self.relative_name())
@staticmethod
@lru_cache(maxsize=None)
def from_source_file(source_root: str, subdir: str, fname: str) -> File:
if not os.path.isfile(os.path.join(source_root, subdir, fname)):
raise MesonException(f'File {fname} does not exist.')
return File(False, subdir, fname)
@staticmethod
@lru_cache(maxsize=None)
def from_built_file(subdir: str, fname: str) -> 'File':
return File(True, subdir, fname)
@staticmethod
@lru_cache(maxsize=None)
def from_built_relative(relative: str) -> 'File':
dirpart, fnamepart = os.path.split(relative)
return File.from_built_file(dirpart, fnamepart)
@staticmethod
def from_absolute_file(fname: str) -> 'File':
return File(False, '', fname)
@lru_cache(maxsize=None)
def rel_to_builddir(self, build_to_src: str) -> str:
if self.is_built:
return self.relative_name()
else:
return os.path.join(build_to_src, self.subdir, self.fname)
@lru_cache(maxsize=None)
def absolute_path(self, srcdir: str, builddir: str) -> str:
absdir = srcdir
if self.is_built:
absdir = builddir
return os.path.join(absdir, self.relative_name())
@property
def suffix(self) -> str:
return os.path.splitext(self.fname)[1][1:].lower()
def endswith(self, ending: T.Union[str, T.Tuple[str, ...]]) -> bool:
return self.fname.endswith(ending)
def split(self, s: str, maxsplit: int = -1) -> T.List[str]:
return self.fname.split(s, maxsplit=maxsplit)
def rsplit(self, s: str, maxsplit: int = -1) -> T.List[str]:
return self.fname.rsplit(s, maxsplit=maxsplit)
def __eq__(self, other: object) -> bool:
if not isinstance(other, File):
return NotImplemented
if self.hash != other.hash:
return False
return (self.fname, self.subdir, self.is_built) == (other.fname, other.subdir, other.is_built)
def __hash__(self) -> int:
return self.hash
@lru_cache(maxsize=None)
def relative_name(self) -> str:
return os.path.join(self.subdir, self.fname)
def get_compiler_for_source(compilers: T.Iterable['Compiler'], src: 'FileOrString') -> 'Compiler':
"""Given a set of compilers and a source, find the compiler for that source type."""
for comp in compilers:
if comp.can_compile(src):
return comp
raise MesonException(f'No specified compiler can handle file {src!s}')
def classify_unity_sources(compilers: T.Iterable['Compiler'], sources: T.Sequence['FileOrString']) -> T.Dict['Compiler', T.List['FileOrString']]:
compsrclist: T.Dict['Compiler', T.List['FileOrString']] = {}
for src in sources:
comp = get_compiler_for_source(compilers, src)
if comp not in compsrclist:
compsrclist[comp] = [src]
else:
compsrclist[comp].append(src)
return compsrclist
MACHINE_NAMES = ['build', 'host']
MACHINE_PREFIXES = ['build.', '']
class MachineChoice(enum.IntEnum):
"""Enum class representing one of the two abstract machine names used in
most places: the build, and host, machines.
"""
BUILD = 0
HOST = 1
def __str__(self) -> str:
return f'{self.get_lower_case_name()} machine'
def get_lower_case_name(self) -> str:
return MACHINE_NAMES[self.value]
def get_prefix(self) -> str:
return MACHINE_PREFIXES[self.value]
@dataclasses.dataclass(eq=False, order=False)
class PerMachine(T.Generic[_T]):
build: _T
host: _T
def __getitem__(self, machine: MachineChoice) -> _T:
return [self.build, self.host][machine.value]
def __setitem__(self, machine: MachineChoice, val: _T) -> None:
setattr(self, machine.get_lower_case_name(), val)
def miss_defaulting(self) -> PerMachineDefaultable[T.Optional[_T]]:
"""Unset definition duplicated from their previous to None
This is the inverse of ''default_missing''. By removing defaulted
machines, we can elaborate the original and then redefault them and thus
avoid repeating the elaboration explicitly.
"""
unfreeze: PerMachineDefaultable[T.Optional[_T]] = PerMachineDefaultable()
unfreeze.build = self.build
unfreeze.host = self.host
if unfreeze.host == unfreeze.build:
unfreeze.host = None
return unfreeze
def assign(self, build: _T, host: _T) -> None:
self.build = build
self.host = host
@dataclasses.dataclass(eq=False, order=False)
class PerThreeMachine(PerMachine[_T]):
"""Like `PerMachine` but includes `target` too.
It turns out just one thing do we need track the target machine. There's no
need to computer the `target` field so we don't bother overriding the
`__getitem__`/`__setitem__` methods.
"""
target: _T
def miss_defaulting(self) -> "PerThreeMachineDefaultable[T.Optional[_T]]":
"""Unset definition duplicated from their previous to None
This is the inverse of ''default_missing''. By removing defaulted
machines, we can elaborate the original and then redefault them and thus
avoid repeating the elaboration explicitly.
"""
unfreeze: PerThreeMachineDefaultable[T.Optional[_T]] = PerThreeMachineDefaultable()
unfreeze.build = self.build
unfreeze.host = self.host
unfreeze.target = self.target
if unfreeze.target == unfreeze.host:
unfreeze.target = None
if unfreeze.host == unfreeze.build:
unfreeze.host = None
return unfreeze
def matches_build_machine(self, machine: MachineChoice) -> bool:
return self.build == self[machine]
@dataclasses.dataclass(eq=False, order=False)
class PerMachineDefaultable(PerMachine[T.Optional[_T]]):
"""Extends `PerMachine` with the ability to default from `None`s.
"""
build: T.Optional[_T] = None
host: T.Optional[_T] = None
def default_missing(self) -> PerMachine[_T]:
"""Default host to build
This allows just specifying nothing in the native case, and just host in the
cross non-compiler case.
"""
assert self.build is not None, 'Cannot fill in missing when all fields are empty'
return PerMachine(self.build, self.host if self.host is not None else self.build)
@classmethod
def default(cls, is_cross: bool, build: _T, host: _T) -> PerMachine[_T]:
"""Easy way to get a defaulted value
This allows simplifying the case where you can control whether host and
build are separate or not with a boolean. If the is_cross value is set
to true then the optional host value will be used, otherwise the host
will be set to the build value.
"""
m = cls(build)
if is_cross:
m.host = host
return m.default_missing()
@dataclasses.dataclass(eq=False, order=False)
class PerThreeMachineDefaultable(PerMachineDefaultable[T.Optional[_T]], PerThreeMachine[T.Optional[_T]]):
"""Extends `PerThreeMachine` with the ability to default from `None`s.
"""
target: T.Optional[_T] = None
def default_missing(self) -> PerThreeMachine[_T]:
"""Default host to build and target to host.
This allows just specifying nothing in the native case, just host in the
cross non-compiler case, and just target in the native-built
cross-compiler case.
"""
assert self.build is not None, 'Cannot default a PerMachine when all values are None'
host = self.host if self.host is not None else self.build
target = self.target if self.target is not None else host
return PerThreeMachine(self.build, host, target)
def is_sunos() -> bool:
return platform.system().lower() == 'sunos'
def is_osx() -> bool:
return platform.system().lower() == 'darwin'
def is_linux() -> bool:
return platform.system().lower() == 'linux'
def is_android() -> bool:
return platform.system().lower() == 'android'
def is_haiku() -> bool:
return platform.system().lower() == 'haiku'
def is_openbsd() -> bool:
return platform.system().lower() == 'openbsd'
def is_windows() -> bool:
platname = platform.system().lower()
return platname == 'windows'
def is_wsl() -> bool:
return is_linux() and 'microsoft' in platform.release().lower()
def is_cygwin() -> bool:
return sys.platform == 'cygwin'
def is_debianlike() -> bool:
return os.path.isfile('/etc/debian_version')
def is_dragonflybsd() -> bool:
return platform.system().lower() == 'dragonfly'
def is_netbsd() -> bool:
return platform.system().lower() == 'netbsd'
def is_freebsd() -> bool:
return platform.system().lower() == 'freebsd'
def is_irix() -> bool:
return platform.system().startswith('irix')
def is_hurd() -> bool:
return platform.system().lower() == 'gnu'
def is_qnx() -> bool:
return platform.system().lower() == 'qnx'
def is_aix() -> bool:
return platform.system().lower() == 'aix'
@lru_cache(maxsize=None)
def darwin_get_object_archs(objpath: str) -> 'ImmutableListProtocol[str]':
'''
For a specific object (executable, static library, dylib, etc), run `lipo`
to fetch the list of archs supported by it. Supports both thin objects and
'fat' objects.
'''
_, stdo, stderr = Popen_safe(['lipo', '-info', objpath])
if not stdo:
mlog.debug(f'lipo {objpath}: {stderr}')
return None
stdo = stdo.rsplit(': ', 1)[1]
# Convert from lipo-style archs to meson-style CPUs
map_arch = {
'i386': 'x86',
'arm64': 'aarch64',
'arm64e': 'aarch64',
'ppc7400': 'ppc',
'ppc970': 'ppc',
}
lipo_archs = stdo.split()
meson_archs = [map_arch.get(lipo_arch, lipo_arch) for lipo_arch in lipo_archs]
# Add generic name for armv7 and armv7s
if 'armv7' in stdo:
meson_archs.append('arm')
return meson_archs
def windows_detect_native_arch() -> str:
"""
The architecture of Windows itself: x86, amd64 or arm64
"""
if sys.platform != 'win32':
return ''
try:
import ctypes
process_arch = ctypes.c_ushort()
native_arch = ctypes.c_ushort()
kernel32 = ctypes.windll.kernel32
process = ctypes.c_void_p(kernel32.GetCurrentProcess())
# This is the only reliable way to detect an arm system if we are an x86/x64 process being emulated
if kernel32.IsWow64Process2(process, ctypes.byref(process_arch), ctypes.byref(native_arch)):
# https://docs.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants
if native_arch.value == 0x8664:
return 'amd64'
elif native_arch.value == 0x014C:
return 'x86'
elif native_arch.value == 0xAA64:
return 'arm64'
elif native_arch.value == 0x01C4:
return 'arm'
except (OSError, AttributeError):
pass
# These env variables are always available. See:
# https://msdn.microsoft.com/en-us/library/aa384274(VS.85).aspx
# https://blogs.msdn.microsoft.com/david.wang/2006/03/27/howto-detect-process-bitness/
arch = os.environ.get('PROCESSOR_ARCHITEW6432', '').lower()
if not arch:
try:
# If this doesn't exist, something is messing with the environment
arch = os.environ['PROCESSOR_ARCHITECTURE'].lower()
except KeyError:
raise EnvironmentException('Unable to detect native OS architecture')
return arch
@dataclasses.dataclass
class VcsData:
name: str
cmd: str
repo_dir: str
get_rev: T.List[str]
rev_regex: str
dep: str
wc_dir: T.Optional[str] = None
def detect_vcs(source_dir: T.Union[str, Path]) -> T.Optional[VcsData]:
vcs_systems = [
VcsData(
name = 'git',
cmd = 'git',
repo_dir = '.git',
get_rev = ['git', 'describe', '--dirty=+', '--always'],
rev_regex = '(.*)',
dep = '.git/logs/HEAD',
),
VcsData(
name = 'mercurial',
cmd = 'hg',
repo_dir = '.hg',
get_rev = ['hg', 'id', '-i'],
rev_regex = '(.*)',
dep= '.hg/dirstate',
),
VcsData(
name = 'subversion',
cmd = 'svn',
repo_dir = '.svn',
get_rev = ['svn', 'info'],
rev_regex = 'Revision: (.*)',
dep = '.svn/wc.db',
),
VcsData(
name = 'bazaar',
cmd = 'bzr',
repo_dir = '.bzr',
get_rev = ['bzr', 'revno'],
rev_regex = '(.*)',
dep = '.bzr',
),
]
if isinstance(source_dir, str):
source_dir = Path(source_dir)
parent_paths_and_self = collections.deque(source_dir.parents)
# Prepend the source directory to the front so we can check it;
# source_dir.parents doesn't include source_dir
parent_paths_and_self.appendleft(source_dir)
for curdir in parent_paths_and_self:
for vcs in vcs_systems:
repodir = vcs.repo_dir
cmd = vcs.cmd
if curdir.joinpath(repodir).is_dir() and shutil.which(cmd):
vcs.wc_dir = str(curdir)
return vcs
return None
def current_vs_supports_modules() -> bool:
vsver = os.environ.get('VSCMD_VER', '')
nums = vsver.split('.', 2)
major = int(nums[0])
if major >= 17:
return True
if major == 16 and int(nums[1]) >= 10:
return True
return vsver.startswith('16.9.0') and '-pre.' in vsver
_VERSION_TOK_RE = re.compile(r'(\d+)|([a-zA-Z]+)')
# a helper class which implements the same version ordering as RPM
class Version:
def __init__(self, s: str) -> None:
self._s = s
# extract numeric and alphabetic sequences
# numeric sequences are converted from strings to ints
self._v = [
int(m.group(1)) if m.group(1) else m.group(2)
for m in _VERSION_TOK_RE.finditer(s)]
def __str__(self) -> str:
return '{} (V={})'.format(self._s, str(self._v))
def __repr__(self) -> str:
return f'<Version: {self._s}>'
def __lt__(self, other: object) -> bool:
if isinstance(other, Version):
return self.__cmp(other, operator.lt)
return NotImplemented
def __gt__(self, other: object) -> bool:
if isinstance(other, Version):
return self.__cmp(other, operator.gt)
return NotImplemented
def __le__(self, other: object) -> bool:
if isinstance(other, Version):
return self.__cmp(other, operator.le)
return NotImplemented
def __ge__(self, other: object) -> bool:
if isinstance(other, Version):
return self.__cmp(other, operator.ge)
return NotImplemented
def __eq__(self, other: object) -> bool:
if isinstance(other, Version):
return self._v == other._v
return NotImplemented
def __ne__(self, other: object) -> bool:
if isinstance(other, Version):
return self._v != other._v
return NotImplemented
def __cmp(self, other: 'Version', comparator: T.Callable[[T.Any, T.Any], bool]) -> bool:
# compare each sequence in order
for ours, theirs in zip(self._v, other._v):
# sort a non-digit sequence before a digit sequence
ours_is_int = isinstance(ours, int)
theirs_is_int = isinstance(theirs, int)
if ours_is_int != theirs_is_int:
return comparator(ours_is_int, theirs_is_int)
if ours != theirs:
return comparator(ours, theirs)
# if equal length, all components have matched, so equal
# otherwise, the version with a suffix remaining is greater
return comparator(len(self._v), len(other._v))
def _version_extract_cmpop(vstr2: str) -> T.Tuple[T.Callable[[T.Any, T.Any], bool], str]:
if vstr2.startswith('>='):
cmpop = operator.ge
vstr2 = vstr2[2:]
elif vstr2.startswith('<='):
cmpop = operator.le
vstr2 = vstr2[2:]
elif vstr2.startswith('!='):
cmpop = operator.ne
vstr2 = vstr2[2:]
elif vstr2.startswith('=='):
cmpop = operator.eq
vstr2 = vstr2[2:]
elif vstr2.startswith('='):
cmpop = operator.eq
vstr2 = vstr2[1:]
elif vstr2.startswith('>'):
cmpop = operator.gt
vstr2 = vstr2[1:]
elif vstr2.startswith('<'):
cmpop = operator.lt
vstr2 = vstr2[1:]
else:
cmpop = operator.eq
return (cmpop, vstr2)
def version_compare(vstr1: str, vstr2: str) -> bool:
(cmpop, vstr2) = _version_extract_cmpop(vstr2)
return cmpop(Version(vstr1), Version(vstr2))
def version_compare_many(vstr1: str, conditions: T.Union[str, T.Iterable[str]]) -> T.Tuple[bool, T.List[str], T.List[str]]:
if isinstance(conditions, str):
conditions = [conditions]
found: T.List[str] = []
not_found: T.List[str] = []
for req in conditions:
if not version_compare(vstr1, req):
not_found.append(req)
else:
found.append(req)
return not not_found, not_found, found
# determine if the minimum version satisfying the condition |condition| exceeds
# the minimum version for a feature |minimum|
def version_compare_condition_with_min(condition: str, minimum: str) -> bool:
if condition.startswith('>='):
cmpop = operator.le
condition = condition[2:]
elif condition.startswith('<='):
return False
elif condition.startswith('!='):
return False
elif condition.startswith('=='):
cmpop = operator.le
condition = condition[2:]
elif condition.startswith('='):
cmpop = operator.le
condition = condition[1:]
elif condition.startswith('>'):
cmpop = operator.lt
condition = condition[1:]
elif condition.startswith('<'):
return False
else:
cmpop = operator.le
# Declaring a project(meson_version: '>=0.46') and then using features in
# 0.46.0 is valid, because (knowing the meson versioning scheme) '0.46.0' is
# the lowest version which satisfies the constraint '>=0.46'.
#
# But this will fail here, because the minimum version required by the
# version constraint ('0.46') is strictly less (in our version comparison)
# than the minimum version needed for the feature ('0.46.0').
#
# Map versions in the constraint of the form '0.46' to '0.46.0', to embed
# this knowledge of the meson versioning scheme.
condition = condition.strip()
if re.match(r'^\d+.\d+$', condition):
condition += '.0'
return T.cast('bool', cmpop(Version(minimum), Version(condition)))
def search_version(text: str) -> str:
# Usually of the type 4.1.4 but compiler output may contain
# stuff like this:
# (Sourcery CodeBench Lite 2014.05-29) 4.8.3 20140320 (prerelease)
# Limiting major version number to two digits seems to work
# thus far. When we get to GCC 100, this will break, but
# if we are still relevant when that happens, it can be
# considered an achievement in itself.
#
# This regex is reaching magic levels. If it ever needs
# to be updated, do not complexify but convert to something
# saner instead.
# We'll demystify it a bit with a verbose definition.
version_regex = re.compile(r"""
(?<! # Zero-width negative lookbehind assertion
(
\d # One digit
| \. # Or one period
) # One occurrence
)
# Following pattern must not follow a digit or period
(
\d{1,2} # One or two digits
(
\.\d+ # Period and one or more digits
)+ # One or more occurrences
(
-[a-zA-Z0-9]+ # Hyphen and one or more alphanumeric