This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
1628 lines (1401 loc) · 64.7 KB
/
setup.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
# Autodetecting setup.py script for building the Python extensions
import argparse
import importlib._bootstrap
import importlib.machinery
import importlib.util
import logging
import os
import re
import shlex
import sys
import sysconfig
import warnings
from glob import glob, escape
import _osx_support
try:
import subprocess
del subprocess
SUBPROCESS_BOOTSTRAP = False
except ImportError:
# Bootstrap Python: distutils.spawn uses subprocess to build C extensions,
# subprocess requires C extensions built by setup.py like _posixsubprocess.
#
# Use _bootsubprocess which only uses the os module.
#
# It is dropped from sys.modules as soon as all C extension modules
# are built.
import _bootsubprocess
sys.modules['subprocess'] = _bootsubprocess
del _bootsubprocess
SUBPROCESS_BOOTSTRAP = True
with warnings.catch_warnings():
# bpo-41282 (PEP 632) deprecated distutils but setup.py still uses it
warnings.filterwarnings(
"ignore",
"The distutils package is deprecated",
DeprecationWarning
)
warnings.filterwarnings(
"ignore",
"The distutils.sysconfig module is deprecated, use sysconfig instead",
DeprecationWarning
)
from distutils.command.build_ext import build_ext
from distutils.command.build_scripts import build_scripts
from distutils.command.install import install
from distutils.command.install_lib import install_lib
from distutils.core import Extension, setup
from distutils.errors import CCompilerError, DistutilsError
from distutils.spawn import find_executable
# This global variable is used to hold the list of modules to be disabled.
DISABLED_MODULE_LIST = []
# --list-module-names option used by Tools/scripts/generate_module_names.py
LIST_MODULE_NAMES = False
logging.basicConfig(format='%(message)s', level=logging.INFO)
log = logging.getLogger('setup')
def get_platform():
# Cross compiling
if "_PYTHON_HOST_PLATFORM" in os.environ:
return os.environ["_PYTHON_HOST_PLATFORM"]
# Get value of sys.platform
if sys.platform.startswith('osf1'):
return 'osf1'
return sys.platform
CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
HOST_PLATFORM = get_platform()
MS_WINDOWS = (HOST_PLATFORM == 'win32')
CYGWIN = (HOST_PLATFORM == 'cygwin')
MACOS = (HOST_PLATFORM == 'darwin')
AIX = (HOST_PLATFORM.startswith('aix'))
VXWORKS = ('vxworks' in HOST_PLATFORM)
EMSCRIPTEN = HOST_PLATFORM == 'emscripten-wasm32'
CC = os.environ.get("CC")
if not CC:
CC = sysconfig.get_config_var("CC")
if EMSCRIPTEN:
# emcc is a Python script from a different Python interpreter.
os.environ.pop("PYTHONPATH", None)
SUMMARY = """
Python is an interpreted, interactive, object-oriented programming
language. It is often compared to Tcl, Perl, Scheme or Java.
Python combines remarkable power with very clear syntax. It has
modules, classes, exceptions, very high level dynamic data types, and
dynamic typing. There are interfaces to many system calls and
libraries, as well as to various windowing systems (X11, Motif, Tk,
Mac, MFC). New built-in modules are easily written in C or C++. Python
is also usable as an extension language for applications that need a
programmable interface.
The Python implementation is portable: it runs on many brands of UNIX,
on Windows, DOS, Mac, Amiga... If your favorite system isn't
listed here, it may still be supported, if there's a C compiler for
it. Ask around on comp.lang.python -- or just try compiling Python
yourself.
"""
CLASSIFIERS = """
Development Status :: 6 - Mature
License :: OSI Approved :: Python Software Foundation License
Natural Language :: English
Programming Language :: C
Programming Language :: Python
Topic :: Software Development
"""
def run_command(cmd):
status = os.system(cmd)
return os.waitstatus_to_exitcode(status)
# Set common compiler and linker flags derived from the Makefile,
# reserved for building the interpreter and the stdlib modules.
# See bpo-21121 and bpo-35257
def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
flags = sysconfig.get_config_var(compiler_flags)
py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
def add_dir_to_list(dirlist, dir):
"""Add the directory 'dir' to the list 'dirlist' (after any relative
directories) if:
1) 'dir' is not already in 'dirlist'
2) 'dir' actually exists, and is a directory.
"""
if dir is None or not os.path.isdir(dir) or dir in dirlist:
return
for i, path in enumerate(dirlist):
if not os.path.isabs(path):
dirlist.insert(i + 1, dir)
return
dirlist.insert(0, dir)
def sysroot_paths(make_vars, subdirs):
"""Get the paths of sysroot sub-directories.
* make_vars: a sequence of names of variables of the Makefile where
sysroot may be set.
* subdirs: a sequence of names of subdirectories used as the location for
headers or libraries.
"""
dirs = []
for var_name in make_vars:
var = sysconfig.get_config_var(var_name)
if var is not None:
m = re.search(r'--sysroot=([^"]\S*|"[^"]+")', var)
if m is not None:
sysroot = m.group(1).strip('"')
for subdir in subdirs:
if os.path.isabs(subdir):
subdir = subdir[1:]
path = os.path.join(sysroot, subdir)
if os.path.isdir(path):
dirs.append(path)
break
return dirs
MACOS_SDK_ROOT = None
MACOS_SDK_SPECIFIED = None
def macosx_sdk_root():
"""Return the directory of the current macOS SDK.
If no SDK was explicitly configured, call the compiler to find which
include files paths are being searched by default. Use '/' if the
compiler is searching /usr/include (meaning system header files are
installed) or use the root of an SDK if that is being searched.
(The SDK may be supplied via Xcode or via the Command Line Tools).
The SDK paths used by Apple-supplied tool chains depend on the
setting of various variables; see the xcrun man page for more info.
Also sets MACOS_SDK_SPECIFIED for use by macosx_sdk_specified().
"""
global MACOS_SDK_ROOT, MACOS_SDK_SPECIFIED
# If already called, return cached result.
if MACOS_SDK_ROOT:
return MACOS_SDK_ROOT
cflags = sysconfig.get_config_var('CFLAGS')
m = re.search(r'-isysroot\s*(\S+)', cflags)
if m is not None:
MACOS_SDK_ROOT = m.group(1)
MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
else:
MACOS_SDK_ROOT = _osx_support._default_sysroot(
sysconfig.get_config_var('CC'))
MACOS_SDK_SPECIFIED = False
return MACOS_SDK_ROOT
def is_macosx_sdk_path(path):
"""
Returns True if 'path' can be located in a macOS SDK
"""
return ( (path.startswith('/usr/') and not path.startswith('/usr/local'))
or path.startswith('/System/Library')
or path.startswith('/System/iOSSupport') )
def grep_headers_for(function, headers):
for header in headers:
with open(header, 'r', errors='surrogateescape') as f:
if function in f.read():
return True
return False
def find_file(filename, std_dirs, paths):
"""Searches for the directory where a given file is located,
and returns a possibly-empty list of additional directories, or None
if the file couldn't be found at all.
'filename' is the name of a file, such as readline.h or libcrypto.a.
'std_dirs' is the list of standard system directories; if the
file is found in one of them, no additional directives are needed.
'paths' is a list of additional locations to check; if the file is
found in one of them, the resulting list will contain the directory.
"""
if MACOS:
# Honor the MacOSX SDK setting when one was specified.
# An SDK is a directory with the same structure as a real
# system, but with only header files and libraries.
sysroot = macosx_sdk_root()
# Check the standard locations
for dir_ in std_dirs:
f = os.path.join(dir_, filename)
if MACOS and is_macosx_sdk_path(dir_):
f = os.path.join(sysroot, dir_[1:], filename)
if os.path.exists(f): return []
# Check the additional directories
for dir_ in paths:
f = os.path.join(dir_, filename)
if MACOS and is_macosx_sdk_path(dir_):
f = os.path.join(sysroot, dir_[1:], filename)
if os.path.exists(f):
return [dir_]
# Not found anywhere
return None
def validate_tzpath():
base_tzpath = sysconfig.get_config_var('TZPATH')
if not base_tzpath:
return
tzpaths = base_tzpath.split(os.pathsep)
bad_paths = [tzpath for tzpath in tzpaths if not os.path.isabs(tzpath)]
if bad_paths:
raise ValueError('TZPATH must contain only absolute paths, '
+ f'found:\n{tzpaths!r}\nwith invalid paths:\n'
+ f'{bad_paths!r}')
def find_module_file(module, dirlist):
"""Find a module in a set of possible folders. If it is not found
return the unadorned filename"""
dirs = find_file(module, [], dirlist)
if not dirs:
return module
if len(dirs) > 1:
log.info(f"WARNING: multiple copies of {module} found")
return os.path.abspath(os.path.join(dirs[0], module))
class PyBuildExt(build_ext):
def __init__(self, dist):
build_ext.__init__(self, dist)
self.srcdir = None
self.lib_dirs = None
self.inc_dirs = None
self.config_h_vars = None
self.failed = []
self.failed_on_import = []
self.missing = []
self.disabled_configure = []
if '-j' in os.environ.get('MAKEFLAGS', ''):
self.parallel = True
def add(self, ext):
self.extensions.append(ext)
def addext(self, ext, *, update_flags=True):
"""Add extension with Makefile MODULE_{name} support
"""
if update_flags:
self.update_extension_flags(ext)
state = sysconfig.get_config_var(f"MODULE_{ext.name.upper()}_STATE")
if state == "yes":
self.extensions.append(ext)
elif state == "disabled":
self.disabled_configure.append(ext.name)
elif state == "missing":
self.missing.append(ext.name)
elif state == "n/a":
# not available on current platform
pass
else:
# not migrated to MODULE_{name}_STATE yet.
self.announce(
f'WARNING: Makefile is missing module variable for "{ext.name}"',
level=2
)
self.extensions.append(ext)
def update_extension_flags(self, ext):
"""Update extension flags with module CFLAGS and LDFLAGS
Reads MODULE_{name}_CFLAGS and _LDFLAGS
Distutils appends extra args to the compiler arguments. Some flags like
-I must appear earlier, otherwise the pre-processor picks up files
from system include directories.
"""
upper_name = ext.name.upper()
# Parse compiler flags (-I, -D, -U, extra args)
cflags = sysconfig.get_config_var(f"MODULE_{upper_name}_CFLAGS")
if cflags:
for token in shlex.split(cflags):
switch = token[0:2]
value = token[2:]
if switch == '-I':
ext.include_dirs.append(value)
elif switch == '-D':
key, _, val = value.partition("=")
if not val:
val = None
ext.define_macros.append((key, val))
elif switch == '-U':
ext.undef_macros.append(value)
else:
ext.extra_compile_args.append(token)
# Parse linker flags (-L, -l, extra objects, extra args)
ldflags = sysconfig.get_config_var(f"MODULE_{upper_name}_LDFLAGS")
if ldflags:
for token in shlex.split(ldflags):
switch = token[0:2]
value = token[2:]
if switch == '-L':
ext.library_dirs.append(value)
elif switch == '-l':
ext.libraries.append(value)
elif (
token[0] != '-' and
token.endswith(('.a', '.o', '.so', '.sl', '.dylib'))
):
ext.extra_objects.append(token)
else:
ext.extra_link_args.append(token)
return ext
def set_srcdir(self):
self.srcdir = sysconfig.get_config_var('srcdir')
if not self.srcdir:
# Maybe running on Windows but not using CYGWIN?
raise ValueError("No source directory; cannot proceed.")
self.srcdir = os.path.abspath(self.srcdir)
def remove_disabled(self):
# Remove modules that are present on the disabled list
extensions = [ext for ext in self.extensions
if ext.name not in DISABLED_MODULE_LIST]
# move ctypes to the end, it depends on other modules
ext_map = dict((ext.name, i) for i, ext in enumerate(extensions))
if "_ctypes" in ext_map:
ctypes = extensions.pop(ext_map["_ctypes"])
extensions.append(ctypes)
self.extensions = extensions
def update_sources_depends(self):
# Fix up the autodetected modules, prefixing all the source files
# with Modules/.
# Add dependencies from MODULE_{name}_DEPS variable
moddirlist = [
# files in Modules/ directory
os.path.join(self.srcdir, 'Modules'),
# files relative to build base, e.g. libmpdec.a, libexpat.a
os.getcwd()
]
# Fix up the paths for scripts, too
self.distribution.scripts = [os.path.join(self.srcdir, filename)
for filename in self.distribution.scripts]
# Python header files
include_dir = escape(sysconfig.get_path('include'))
headers = [sysconfig.get_config_h_filename()]
headers.extend(glob(os.path.join(include_dir, "*.h")))
headers.extend(glob(os.path.join(include_dir, "cpython", "*.h")))
headers.extend(glob(os.path.join(include_dir, "internal", "*.h")))
for ext in self.extensions:
ext.sources = [ find_module_file(filename, moddirlist)
for filename in ext.sources ]
# Update dependencies from Makefile
makedeps = sysconfig.get_config_var(f"MODULE_{ext.name.upper()}_DEPS")
if makedeps:
# remove backslashes from line break continuations
ext.depends.extend(
dep for dep in makedeps.split() if dep != "\\"
)
ext.depends = [
find_module_file(filename, moddirlist) for filename in ext.depends
]
# re-compile extensions if a header file has been changed
ext.depends.extend(headers)
def handle_configured_extensions(self):
# The sysconfig variables built by makesetup that list the already
# built modules and the disabled modules as configured by the Setup
# files.
sysconf_built = set(sysconfig.get_config_var('MODBUILT_NAMES').split())
sysconf_shared = set(sysconfig.get_config_var('MODSHARED_NAMES').split())
sysconf_dis = set(sysconfig.get_config_var('MODDISABLED_NAMES').split())
mods_built = []
mods_disabled = []
for ext in self.extensions:
# If a module has already been built or has been disabled in the
# Setup files, don't build it here.
if ext.name in sysconf_built:
mods_built.append(ext)
if ext.name in sysconf_dis:
mods_disabled.append(ext)
mods_configured = mods_built + mods_disabled
if mods_configured:
self.extensions = [x for x in self.extensions if x not in
mods_configured]
# Remove the shared libraries built by a previous build.
for ext in mods_configured:
# Don't remove shared extensions which have been built
# by Modules/Setup
if ext.name in sysconf_shared:
continue
fullpath = self.get_ext_fullpath(ext.name)
if os.path.lexists(fullpath):
os.unlink(fullpath)
return mods_built, mods_disabled
def set_compiler_executables(self):
# When you run "make CC=altcc" or something similar, you really want
# those environment variables passed into the setup.py phase. Here's
# a small set of useful ones.
compiler = os.environ.get('CC')
args = {}
# unfortunately, distutils doesn't let us provide separate C and C++
# compilers
if compiler is not None:
(ccshared,cflags) = sysconfig.get_config_vars('CCSHARED','CFLAGS')
args['compiler_so'] = compiler + ' ' + ccshared + ' ' + cflags
self.compiler.set_executables(**args)
def build_extensions(self):
self.set_srcdir()
self.set_compiler_executables()
self.configure_compiler()
self.init_inc_lib_dirs()
# Detect which modules should be compiled
self.detect_modules()
if not LIST_MODULE_NAMES:
self.remove_disabled()
self.update_sources_depends()
mods_built, mods_disabled = self.handle_configured_extensions()
if LIST_MODULE_NAMES:
for ext in self.extensions:
print(ext.name)
for name in self.missing:
print(name)
return
build_ext.build_extensions(self)
if SUBPROCESS_BOOTSTRAP:
# Drop our custom subprocess module:
# use the newly built subprocess module
del sys.modules['subprocess']
for ext in self.extensions:
self.check_extension_import(ext)
self.summary(mods_built, mods_disabled)
def summary(self, mods_built, mods_disabled):
longest = max([len(e.name) for e in self.extensions], default=0)
if self.failed or self.failed_on_import:
all_failed = self.failed + self.failed_on_import
longest = max(longest, max([len(name) for name in all_failed]))
def print_three_column(lst):
lst.sort(key=str.lower)
# guarantee zip() doesn't drop anything
while len(lst) % 3:
lst.append("")
for e, f, g in zip(lst[::3], lst[1::3], lst[2::3]):
print("%-*s %-*s %-*s" % (longest, e, longest, f,
longest, g))
if self.missing:
print()
print("The necessary bits to build these optional modules were not "
"found:")
print_three_column(self.missing)
print("To find the necessary bits, look in setup.py in"
" detect_modules() for the module's name.")
print()
if mods_built:
print()
print("The following modules found by detect_modules() in"
" setup.py, have been")
print("built by the Makefile instead, as configured by the"
" Setup files:")
print_three_column([ext.name for ext in mods_built])
print()
if mods_disabled:
print()
print("The following modules found by detect_modules() in"
" setup.py have not")
print("been built, they are *disabled* in the Setup files:")
print_three_column([ext.name for ext in mods_disabled])
print()
if self.disabled_configure:
print()
print("The following modules found by detect_modules() in"
" setup.py have not")
print("been built, they are *disabled* by configure:")
print_three_column(self.disabled_configure)
print()
if self.failed:
failed = self.failed[:]
print()
print("Failed to build these modules:")
print_three_column(failed)
print()
if self.failed_on_import:
failed = self.failed_on_import[:]
print()
print("Following modules built successfully"
" but were removed because they could not be imported:")
print_three_column(failed)
print()
if any('_ssl' in l
for l in (self.missing, self.failed, self.failed_on_import)):
print()
print("Could not build the ssl module!")
print("Python requires a OpenSSL 1.1.1 or newer")
if sysconfig.get_config_var("OPENSSL_LDFLAGS"):
print("Custom linker flags may require --with-openssl-rpath=auto")
print()
if os.environ.get("PYTHONSTRICTEXTENSIONBUILD") and (
self.failed or self.failed_on_import or self.missing
):
raise RuntimeError("Failed to build some stdlib modules")
def build_extension(self, ext):
if ext.name == '_ctypes':
if not self.configure_ctypes(ext):
self.failed.append(ext.name)
return
try:
build_ext.build_extension(self, ext)
except (CCompilerError, DistutilsError) as why:
self.announce('WARNING: building of extension "%s" failed: %s' %
(ext.name, why))
self.failed.append(ext.name)
return
def check_extension_import(self, ext):
# Don't try to import an extension that has failed to compile
if ext.name in self.failed:
self.announce(
'WARNING: skipping import check for failed build "%s"' %
ext.name, level=1)
return
# Workaround for Mac OS X: The Carbon-based modules cannot be
# reliably imported into a command-line Python
if 'Carbon' in ext.extra_link_args:
self.announce(
'WARNING: skipping import check for Carbon-based "%s"' %
ext.name)
return
if MACOS and (
sys.maxsize > 2**32 and '-arch' in ext.extra_link_args):
# Don't bother doing an import check when an extension was
# build with an explicit '-arch' flag on OSX. That's currently
# only used to build 32-bit only extensions in a 4-way
# universal build and loading 32-bit code into a 64-bit
# process will fail.
self.announce(
'WARNING: skipping import check for "%s"' %
ext.name)
return
# Workaround for Cygwin: Cygwin currently has fork issues when many
# modules have been imported
if CYGWIN:
self.announce('WARNING: skipping import check for Cygwin-based "%s"'
% ext.name)
return
ext_filename = os.path.join(
self.build_lib,
self.get_ext_filename(self.get_ext_fullname(ext.name)))
# If the build directory didn't exist when setup.py was
# started, sys.path_importer_cache has a negative result
# cached. Clear that cache before trying to import.
sys.path_importer_cache.clear()
# Don't try to load extensions for cross builds
if CROSS_COMPILING:
return
loader = importlib.machinery.ExtensionFileLoader(ext.name, ext_filename)
spec = importlib.util.spec_from_file_location(ext.name, ext_filename,
loader=loader)
try:
importlib._bootstrap._load(spec)
except ImportError as why:
self.failed_on_import.append(ext.name)
self.announce('*** WARNING: renaming "%s" since importing it'
' failed: %s' % (ext.name, why), level=3)
assert not self.inplace
basename, tail = os.path.splitext(ext_filename)
newname = basename + "_failed" + tail
if os.path.exists(newname):
os.remove(newname)
os.rename(ext_filename, newname)
except:
exc_type, why, tb = sys.exc_info()
self.announce('*** WARNING: importing extension "%s" '
'failed with %s: %s' % (ext.name, exc_type, why),
level=3)
self.failed.append(ext.name)
def add_multiarch_paths(self):
# Debian/Ubuntu multiarch support.
# https://wiki.ubuntu.com/MultiarchSpec
tmpfile = os.path.join(self.build_temp, 'multiarch')
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
ret = run_command(
'%s -print-multiarch > %s 2> /dev/null' % (CC, tmpfile))
multiarch_path_component = ''
try:
if ret == 0:
with open(tmpfile) as fp:
multiarch_path_component = fp.readline().strip()
finally:
os.unlink(tmpfile)
if multiarch_path_component != '':
add_dir_to_list(self.compiler.library_dirs,
'/usr/lib/' + multiarch_path_component)
add_dir_to_list(self.compiler.include_dirs,
'/usr/include/' + multiarch_path_component)
return
if not find_executable('dpkg-architecture'):
return
opt = ''
if CROSS_COMPILING:
opt = '-t' + sysconfig.get_config_var('HOST_GNU_TYPE')
tmpfile = os.path.join(self.build_temp, 'multiarch')
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
ret = run_command(
'dpkg-architecture %s -qDEB_HOST_MULTIARCH > %s 2> /dev/null' %
(opt, tmpfile))
try:
if ret == 0:
with open(tmpfile) as fp:
multiarch_path_component = fp.readline().strip()
add_dir_to_list(self.compiler.library_dirs,
'/usr/lib/' + multiarch_path_component)
add_dir_to_list(self.compiler.include_dirs,
'/usr/include/' + multiarch_path_component)
finally:
os.unlink(tmpfile)
def add_wrcc_search_dirs(self):
# add library search path by wr-cc, the compiler wrapper
def convert_mixed_path(path):
# convert path like C:\folder1\folder2/folder3/folder4
# to msys style /c/folder1/folder2/folder3/folder4
drive = path[0].lower()
left = path[2:].replace("\\", "/")
return "/" + drive + left
def add_search_path(line):
# On Windows building machine, VxWorks does
# cross builds under msys2 environment.
pathsep = (";" if sys.platform == "msys" else ":")
for d in line.strip().split("=")[1].split(pathsep):
d = d.strip()
if sys.platform == "msys":
# On Windows building machine, compiler
# returns mixed style path like:
# C:\folder1\folder2/folder3/folder4
d = convert_mixed_path(d)
d = os.path.normpath(d)
add_dir_to_list(self.compiler.library_dirs, d)
tmpfile = os.path.join(self.build_temp, 'wrccpaths')
os.makedirs(self.build_temp, exist_ok=True)
try:
ret = run_command('%s --print-search-dirs >%s' % (CC, tmpfile))
if ret:
return
with open(tmpfile) as fp:
# Parse paths in libraries line. The line is like:
# On Linux, "libraries: = path1:path2:path3"
# On Windows, "libraries: = path1;path2;path3"
for line in fp:
if not line.startswith("libraries"):
continue
add_search_path(line)
finally:
try:
os.unlink(tmpfile)
except OSError:
pass
def add_cross_compiling_paths(self):
tmpfile = os.path.join(self.build_temp, 'ccpaths')
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# bpo-38472: With a German locale, GCC returns "gcc-Version 9.1.0
# (GCC)", whereas it returns "gcc version 9.1.0" with the C locale.
ret = run_command('LC_ALL=C %s -E -v - </dev/null 2>%s 1>/dev/null' % (CC, tmpfile))
is_gcc = False
is_clang = False
in_incdirs = False
try:
if ret == 0:
with open(tmpfile) as fp:
for line in fp.readlines():
if line.startswith("gcc version"):
is_gcc = True
elif line.startswith("clang version"):
is_clang = True
elif line.startswith("#include <...>"):
in_incdirs = True
elif line.startswith("End of search list"):
in_incdirs = False
elif (is_gcc or is_clang) and line.startswith("LIBRARY_PATH"):
for d in line.strip().split("=")[1].split(":"):
d = os.path.normpath(d)
if '/gcc/' not in d:
add_dir_to_list(self.compiler.library_dirs,
d)
elif (is_gcc or is_clang) and in_incdirs and '/gcc/' not in line and '/clang/' not in line:
add_dir_to_list(self.compiler.include_dirs,
line.strip())
finally:
os.unlink(tmpfile)
if VXWORKS:
self.add_wrcc_search_dirs()
def add_ldflags_cppflags(self):
# Add paths specified in the environment variables LDFLAGS and
# CPPFLAGS for header and library files.
# We must get the values from the Makefile and not the environment
# directly since an inconsistently reproducible issue comes up where
# the environment variable is not set even though the value were passed
# into configure and stored in the Makefile (issue found on OS X 10.3).
for env_var, arg_name, dir_list in (
('LDFLAGS', '-R', self.compiler.runtime_library_dirs),
('LDFLAGS', '-L', self.compiler.library_dirs),
('CPPFLAGS', '-I', self.compiler.include_dirs)):
env_val = sysconfig.get_config_var(env_var)
if env_val:
parser = argparse.ArgumentParser()
parser.add_argument(arg_name, dest="dirs", action="append")
# To prevent argparse from raising an exception about any
# options in env_val that it mistakes for known option, we
# strip out all double dashes and any dashes followed by a
# character that is not for the option we are dealing with.
#
# Please note that order of the regex is important! We must
# strip out double-dashes first so that we don't end up with
# substituting "--Long" to "-Long" and thus lead to "ong" being
# used for a library directory.
env_val = re.sub(r'(^|\s+)-(-|(?!%s))' % arg_name[1],
' ', env_val)
options, _ = parser.parse_known_args(env_val.split())
if options.dirs:
for directory in reversed(options.dirs):
add_dir_to_list(dir_list, directory)
def configure_compiler(self):
# Ensure that /usr/local is always used, but the local build
# directories (i.e. '.' and 'Include') must be first. See issue
# 10520.
if not CROSS_COMPILING:
add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
# only change this for cross builds for 3.3, issues on Mageia
if CROSS_COMPILING:
self.add_cross_compiling_paths()
self.add_multiarch_paths()
self.add_ldflags_cppflags()
def init_inc_lib_dirs(self):
if (not CROSS_COMPILING and
os.path.normpath(sys.base_prefix) != '/usr' and
not sysconfig.get_config_var('PYTHONFRAMEWORK')):
# OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
# (PYTHONFRAMEWORK is set) to avoid # linking problems when
# building a framework with different architectures than
# the one that is currently installed (issue #7473)
add_dir_to_list(self.compiler.library_dirs,
sysconfig.get_config_var("LIBDIR"))
add_dir_to_list(self.compiler.include_dirs,
sysconfig.get_config_var("INCLUDEDIR"))
system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
system_include_dirs = ['/usr/include']
# lib_dirs and inc_dirs are used to search for files;
# if a file is found in one of those directories, it can
# be assumed that no additional -I,-L directives are needed.
if not CROSS_COMPILING:
self.lib_dirs = self.compiler.library_dirs + system_lib_dirs
self.inc_dirs = self.compiler.include_dirs + system_include_dirs
else:
# Add the sysroot paths. 'sysroot' is a compiler option used to
# set the logical path of the standard system headers and
# libraries.
self.lib_dirs = (self.compiler.library_dirs +
sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
self.inc_dirs = (self.compiler.include_dirs +
sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
system_include_dirs))
config_h = sysconfig.get_config_h_filename()
with open(config_h) as file:
self.config_h_vars = sysconfig.parse_config_h(file)
# OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
if HOST_PLATFORM in ['osf1', 'unixware7', 'openunix8']:
self.lib_dirs += ['/usr/ccs/lib']
# HP-UX11iv3 keeps files in lib/hpux folders.
if HOST_PLATFORM == 'hp-ux11':
self.lib_dirs += ['/usr/lib/hpux64', '/usr/lib/hpux32']
if MACOS:
# This should work on any unixy platform ;-)
# If the user has bothered specifying additional -I and -L flags
# in OPT and LDFLAGS we might as well use them here.
#
# NOTE: using shlex.split would technically be more correct, but
# also gives a bootstrap problem. Let's hope nobody uses
# directories with whitespace in the name to store libraries.
cflags, ldflags = sysconfig.get_config_vars(
'CFLAGS', 'LDFLAGS')
for item in cflags.split():
if item.startswith('-I'):
self.inc_dirs.append(item[2:])
for item in ldflags.split():
if item.startswith('-L'):
self.lib_dirs.append(item[2:])
def detect_simple_extensions(self):
#
# The following modules are all pretty straightforward, and compile
# on pretty much any POSIXish platform.
#
# array objects
self.addext(Extension('array', ['arraymodule.c']))
# Context Variables
self.addext(Extension('_contextvars', ['_contextvarsmodule.c']))
# math library functions, e.g. sin()
self.addext(Extension('math', ['mathmodule.c']))
# complex math library functions
self.addext(Extension('cmath', ['cmathmodule.c']))
# libm is needed by delta_new() that uses round() and by accum() that
# uses modf().
self.addext(Extension('_datetime', ['_datetimemodule.c']))
self.addext(Extension('_zoneinfo', ['_zoneinfo.c']))
# random number generator implemented in C
self.addext(Extension("_random", ["_randommodule.c"]))
self.addext(Extension("_bisect", ["_bisectmodule.c"]))
self.addext(Extension("_heapq", ["_heapqmodule.c"]))
# C-optimized pickle replacement
self.addext(Extension("_pickle", ["_pickle.c"]))
# _json speedups
self.addext(Extension("_json", ["_json.c"]))
# profiler (_lsprof is for cProfile.py)
self.addext(Extension('_lsprof', ['_lsprof.c', 'rotatingtree.c']))
# static Unicode character database
self.addext(Extension('unicodedata', ['unicodedata.c']))
self.addext(Extension('_opcode', ['_opcode.c']))
# asyncio speedups
self.addext(Extension("_asyncio", ["_asynciomodule.c"]))
self.addext(Extension("_queue", ["_queuemodule.c"]))
self.addext(Extension("_statistics", ["_statisticsmodule.c"]))
self.addext(Extension("_struct", ["_struct.c"]))
self.addext(Extension("_typing", ["_typingmodule.c"]))
# Modules with some UNIX dependencies -- on by default:
# (If you have a really backward UNIX, select and socket may not be
# supported...)
# fcntl(2) and ioctl(2)
self.addext(Extension('fcntl', ['fcntlmodule.c']))
# grp(3)
self.addext(Extension('grp', ['grpmodule.c']))
self.addext(Extension('_socket', ['socketmodule.c']))
self.addext(Extension('spwd', ['spwdmodule.c']))
# select(2); not on ancient System V
self.addext(Extension('select', ['selectmodule.c']))
# Memory-mapped files (also works on Win32).
self.addext(Extension('mmap', ['mmapmodule.c']))
# Lance Ellinghaus's syslog module
# syslog daemon interface
self.addext(Extension('syslog', ['syslogmodule.c']))
# Python interface to subinterpreter C-API.
self.addext(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
#
# Here ends the simple stuff. From here on, modules need certain
# libraries, are platform-specific, or present other surprises.
#
# Multimedia modules
# These don't work for 64-bit platforms!!!
# These represent audio samples or images as strings:
#
# Operations on audio samples
# According to #993173, this one should actually work fine on
# 64-bit platforms.