@@ -172,6 +172,21 @@ def customize_compiler(compiler):
172
172
varies across Unices and is stored in Python's Makefile.
173
173
"""
174
174
if compiler .compiler_type == "unix" :
175
+ if sys .platform == "darwin" :
176
+ # Perform first-time customization of compiler-related
177
+ # config vars on OS X now that we know we need a compiler.
178
+ # This is primarily to support Pythons from binary
179
+ # installers. The kind and paths to build tools on
180
+ # the user system may vary significantly from the system
181
+ # that Python itself was built on. Also the user OS
182
+ # version and build tools may not support the same set
183
+ # of CPU architectures for universal builds.
184
+ global _config_vars
185
+ if not _config_vars .get ('CUSTOMIZED_OSX_COMPILER' , '' ):
186
+ import _osx_support
187
+ _osx_support .customize_compiler (_config_vars )
188
+ _config_vars ['CUSTOMIZED_OSX_COMPILER' ] = 'True'
189
+
175
190
(cc , cxx , opt , cflags , ccshared , ldshared , so_ext , ar , ar_flags ) = \
176
191
get_config_vars ('CC' , 'CXX' , 'OPT' , 'CFLAGS' ,
177
192
'CCSHARED' , 'LDSHARED' , 'SO' , 'AR' , 'ARFLAGS' )
@@ -494,35 +509,12 @@ def _init_os2():
494
509
_config_vars = g
495
510
496
511
497
- def _read_output (commandstring ):
498
- """
499
- Returns os.popen(commandstring, "r").read(), but
500
- without actually using os.popen because that
501
- function is not usable during python bootstrap
502
- """
503
- # NOTE: tempfile is also not useable during
504
- # bootstrap
505
- import contextlib
506
- try :
507
- import tempfile
508
- fp = tempfile .NamedTemporaryFile ()
509
- except ImportError :
510
- fp = open ("/tmp/distutils.%s" % (
511
- os .getpid (),), "w+b" )
512
-
513
- with contextlib .closing (fp ) as fp :
514
- cmd = "%s >'%s'" % (commandstring , fp .name )
515
- os .system (cmd )
516
- data = fp .read ()
517
-
518
- return data .decode ('utf-8' )
519
-
520
512
def get_config_vars (* args ):
521
513
"""With no arguments, return a dictionary of all configuration
522
514
variables relevant for the current platform. Generally this includes
523
515
everything needed to build extensions and install both pure modules and
524
516
extensions. On Unix, this means every variable defined in Python's
525
- installed Makefile; on Windows and Mac OS it's a much smaller set.
517
+ installed Makefile; on Windows it's a much smaller set.
526
518
527
519
With arguments, return a list of values that result from looking up
528
520
each argument in the configuration variable dictionary.
@@ -555,153 +547,11 @@ def get_config_vars(*args):
555
547
srcdir = os .path .join (base , _config_vars ['srcdir' ])
556
548
_config_vars ['srcdir' ] = os .path .normpath (srcdir )
557
549
550
+ # OS X platforms require special customization to handle
551
+ # multi-architecture, multi-os-version installers
558
552
if sys .platform == 'darwin' :
559
- from distutils .spawn import find_executable
560
-
561
- kernel_version = os .uname ()[2 ] # Kernel version (8.4.3)
562
- major_version = int (kernel_version .split ('.' )[0 ])
563
-
564
- # Issue #13590:
565
- # The OSX location for the compiler varies between OSX
566
- # (or rather Xcode) releases. With older releases (up-to 10.5)
567
- # the compiler is in /usr/bin, with newer releases the compiler
568
- # can only be found inside Xcode.app if the "Command Line Tools"
569
- # are not installed.
570
- #
571
- # Futhermore, the compiler that can be used varies between
572
- # Xcode releases. Upto Xcode 4 it was possible to use 'gcc-4.2'
573
- # as the compiler, after that 'clang' should be used because
574
- # gcc-4.2 is either not present, or a copy of 'llvm-gcc' that
575
- # miscompiles Python.
576
-
577
- # skip checks if the compiler was overriden with a CC env variable
578
- if 'CC' not in os .environ :
579
- cc = oldcc = _config_vars ['CC' ]
580
- if not find_executable (cc ):
581
- # Compiler is not found on the shell search PATH.
582
- # Now search for clang, first on PATH (if the Command LIne
583
- # Tools have been installed in / or if the user has provided
584
- # another location via CC). If not found, try using xcrun
585
- # to find an uninstalled clang (within a selected Xcode).
586
-
587
- # NOTE: Cannot use subprocess here because of bootstrap
588
- # issues when building Python itself (and os.popen is
589
- # implemented on top of subprocess and is therefore not
590
- # usable as well)
591
-
592
- data = (find_executable ('clang' ) or
593
- _read_output (
594
- "/usr/bin/xcrun -find clang 2>/dev/null" ).strip ())
595
- if not data :
596
- raise DistutilsPlatformError (
597
- "Cannot locate working compiler" )
598
-
599
- _config_vars ['CC' ] = cc = data
600
- _config_vars ['CXX' ] = cc + '++'
601
-
602
- elif os .path .basename (cc ).startswith ('gcc' ):
603
- # Compiler is GCC, check if it is LLVM-GCC
604
- data = _read_output ("'%s' --version 2>/dev/null"
605
- % (cc .replace ("'" , "'\" '\" '" ),))
606
- if 'llvm-gcc' in data :
607
- # Found LLVM-GCC, fall back to clang
608
- data = (find_executable ('clang' ) or
609
- _read_output (
610
- "/usr/bin/xcrun -find clang 2>/dev/null" ).strip ())
611
- if find_executable (data ):
612
- _config_vars ['CC' ] = cc = data
613
- _config_vars ['CXX' ] = cc + '++'
614
-
615
- if (cc != oldcc
616
- and 'LDSHARED' in _config_vars
617
- and 'LDSHARED' not in os .environ ):
618
- # modify LDSHARED if we modified CC
619
- ldshared = _config_vars ['LDSHARED' ]
620
- if ldshared .startswith (oldcc ):
621
- _config_vars ['LDSHARED' ] = cc + ldshared [len (oldcc ):]
622
-
623
- if major_version < 8 :
624
- # On Mac OS X before 10.4, check if -arch and -isysroot
625
- # are in CFLAGS or LDFLAGS and remove them if they are.
626
- # This is needed when building extensions on a 10.3 system
627
- # using a universal build of python.
628
- for key in ('LDFLAGS' , 'BASECFLAGS' , 'LDSHARED' ,
629
- # a number of derived variables. These need to be
630
- # patched up as well.
631
- 'CFLAGS' , 'PY_CFLAGS' , 'BLDSHARED' ):
632
- flags = _config_vars [key ]
633
- flags = re .sub ('-arch\s+\w+\s' , ' ' , flags , re .ASCII )
634
- flags = re .sub ('-isysroot [^ \t ]*' , ' ' , flags )
635
- _config_vars [key ] = flags
636
-
637
- else :
638
- # Different Xcode releases support different sets for '-arch'
639
- # flags. In particular, Xcode 4.x no longer supports the
640
- # PPC architectures.
641
- #
642
- # This code automatically removes '-arch ppc' and '-arch ppc64'
643
- # when these are not supported. That makes it possible to
644
- # build extensions on OSX 10.7 and later with the prebuilt
645
- # 32-bit installer on the python.org website.
646
- flags = _config_vars ['CFLAGS' ]
647
- if re .search ('-arch\s+ppc' , flags ) is not None :
648
- # NOTE: Cannot use subprocess here because of bootstrap
649
- # issues when building Python itself
650
- status = os .system ("'%s' -arch ppc -x c /dev/null 2>/dev/null" % (
651
- _config_vars ['CC' ].replace ("'" , "'\" '\" '" ),))
652
-
653
- if status != 0 :
654
- # Compiler doesn't support PPC, remove the related
655
- # '-arch' flags.
656
- for key in ('LDFLAGS' , 'BASECFLAGS' ,
657
- # a number of derived variables. These need to be
658
- # patched up as well.
659
- 'CFLAGS' , 'PY_CFLAGS' , 'BLDSHARED' , 'LDSHARED' ):
660
-
661
- flags = _config_vars [key ]
662
- flags = re .sub ('-arch\s+ppc\w*\s' , ' ' , flags )
663
- _config_vars [key ] = flags
664
-
665
-
666
- # Allow the user to override the architecture flags using
667
- # an environment variable.
668
- # NOTE: This name was introduced by Apple in OSX 10.5 and
669
- # is used by several scripting languages distributed with
670
- # that OS release.
671
- if 'ARCHFLAGS' in os .environ :
672
- arch = os .environ ['ARCHFLAGS' ]
673
- for key in ('LDFLAGS' , 'BASECFLAGS' , 'LDSHARED' ,
674
- # a number of derived variables. These need to be
675
- # patched up as well.
676
- 'CFLAGS' , 'PY_CFLAGS' , 'BLDSHARED' ):
677
-
678
- flags = _config_vars [key ]
679
- flags = re .sub ('-arch\s+\w+\s' , ' ' , flags )
680
- flags = flags + ' ' + arch
681
- _config_vars [key ] = flags
682
-
683
- # If we're on OSX 10.5 or later and the user tries to
684
- # compiles an extension using an SDK that is not present
685
- # on the current machine it is better to not use an SDK
686
- # than to fail.
687
- #
688
- # The major usecase for this is users using a Python.org
689
- # binary installer on OSX 10.6: that installer uses
690
- # the 10.4u SDK, but that SDK is not installed by default
691
- # when you install Xcode.
692
- #
693
- m = re .search ('-isysroot\s+(\S+)' , _config_vars ['CFLAGS' ])
694
- if m is not None :
695
- sdk = m .group (1 )
696
- if not os .path .exists (sdk ):
697
- for key in ('LDFLAGS' , 'BASECFLAGS' , 'LDSHARED' ,
698
- # a number of derived variables. These need to be
699
- # patched up as well.
700
- 'CFLAGS' , 'PY_CFLAGS' , 'BLDSHARED' ):
701
-
702
- flags = _config_vars [key ]
703
- flags = re .sub ('-isysroot\s+\S+(\s|$)' , ' ' , flags )
704
- _config_vars [key ] = flags
553
+ import _osx_support
554
+ _osx_support .customize_config_vars (_config_vars )
705
555
706
556
if args :
707
557
vals = []
0 commit comments