| 1 |
"""Shared OS X support functions.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
|
| 7 |
__all__ = [ |
| 8 |
'compiler_fixup', |
| 9 |
'customize_config_vars', |
| 10 |
'customize_compiler', |
| 11 |
'get_platform_osx', |
| 12 |
] |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
_UNIVERSAL_CONFIG_VARS = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS', 'BASECFLAGS', |
| 18 |
'BLDSHARED', 'LDSHARED', 'CC', 'CXX', |
| 19 |
'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS', |
| 20 |
'PY_CORE_CFLAGS') |
| 21 |
|
| 22 |
|
| 23 |
_COMPILER_CONFIG_VARS = ('BLDSHARED', 'LDSHARED', 'CC', 'CXX') |
| 24 |
|
| 25 |
|
| 26 |
_INITPRE = '_OSX_SUPPORT_INITIAL_' |
| 27 |
|
| 28 |
|
| 29 |
def _find_executable(executable, path=None): |
| 30 |
"""Tries to find 'executable' in the directories listed in 'path'. |
| 31 |
|
| 32 |
A string listing directories separated by 'os.pathsep'; defaults to |
| 33 |
os.environ['PATH']. Returns the complete filename or None if not found. |
| 34 |
""" |
| 35 |
if path is None: |
| 36 |
path = os.environ['PATH'] |
| 37 |
|
| 38 |
paths = path.split(os.pathsep) |
| 39 |
base, ext = os.path.splitext(executable) |
| 40 |
|
| 41 |
if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'): |
| 42 |
executable = executable + '.exe' |
| 43 |
|
| 44 |
if not os.path.isfile(executable): |
| 45 |
for p in paths: |
| 46 |
f = os.path.join(p, executable) |
| 47 |
if os.path.isfile(f): |
| 48 |
|
| 49 |
return f |
| 50 |
return None |
| 51 |
else: |
| 52 |
return executable |
| 53 |
|
| 54 |
|
| 55 |
def _read_output(commandstring): |
| 56 |
"""Output from successful command execution or None""" |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
import contextlib |
| 62 |
try: |
| 63 |
import tempfile |
| 64 |
fp = tempfile.NamedTemporaryFile() |
| 65 |
except ImportError: |
| 66 |
fp = open("/tmp/_osx_support.%s"%( |
| 67 |
os.getpid(),), "w+b") |
| 68 |
|
| 69 |
with contextlib.closing(fp) as fp: |
| 70 |
cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name) |
| 71 |
return fp.read().strip() if not os.system(cmd) else None |
| 72 |
|
| 73 |
|
| 74 |
def _find_build_tool(toolname): |
| 75 |
"""Find a build tool on current path or using xcrun""" |
| 76 |
return (_find_executable(toolname) |
| 77 |
or _read_output("/usr/bin/xcrun -find %s" % (toolname,)) |
| 78 |
or '' |
| 79 |
) |
| 80 |
|
| 81 |
_SYSTEM_VERSION = None |
| 82 |
|
| 83 |
def _get_system_version(): |
| 84 |
"""Return the OS X system version as a string""" |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
global _SYSTEM_VERSION |
| 92 |
|
| 93 |
if _SYSTEM_VERSION is None: |
| 94 |
_SYSTEM_VERSION = '' |
| 95 |
try: |
| 96 |
f = open('/System/Library/CoreServices/SystemVersion.plist') |
| 97 |
except IOError: |
| 98 |
|
| 99 |
|
| 100 |
pass |
| 101 |
else: |
| 102 |
try: |
| 103 |
m = re.search(r'<key>ProductUserVisibleVersion</key>\s*' |
| 104 |
r'<string>(.*?)</string>', f.read()) |
| 105 |
finally: |
| 106 |
f.close() |
| 107 |
if m is not None: |
| 108 |
_SYSTEM_VERSION = '.'.join(m.group(1).split('.')[:2]) |
| 109 |
|
| 110 |
|
| 111 |
return _SYSTEM_VERSION |
| 112 |
|
| 113 |
def _remove_original_values(_config_vars): |
| 114 |
"""Remove original unmodified values for testing""" |
| 115 |
|
| 116 |
for k in list(_config_vars): |
| 117 |
if k.startswith(_INITPRE): |
| 118 |
del _config_vars[k] |
| 119 |
|
| 120 |
def _save_modified_value(_config_vars, cv, newvalue): |
| 121 |
"""Save modified and original unmodified value of configuration var""" |
| 122 |
|
| 123 |
oldvalue = _config_vars.get(cv, '') |
| 124 |
if (oldvalue != newvalue) and (_INITPRE + cv not in _config_vars): |
| 125 |
_config_vars[_INITPRE + cv] = oldvalue |
| 126 |
_config_vars[cv] = newvalue |
| 127 |
|
| 128 |
def _supports_universal_builds(): |
| 129 |
"""Returns True if universal builds are supported on this system""" |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
osx_version = _get_system_version() |
| 136 |
if osx_version: |
| 137 |
try: |
| 138 |
osx_version = tuple(int(i) for i in osx_version.split('.')) |
| 139 |
except ValueError: |
| 140 |
osx_version = '' |
| 141 |
return bool(osx_version >= (10, 4)) if osx_version else False |
| 142 |
|
| 143 |
|
| 144 |
def _find_appropriate_compiler(_config_vars): |
| 145 |
"""Find appropriate C compiler for extension module builds""" |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if 'CC' in os.environ: |
| 162 |
return _config_vars |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
cc = oldcc = _config_vars['CC'].split()[0] |
| 167 |
if not _find_executable(cc): |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
cc = _find_build_tool('clang') |
| 180 |
|
| 181 |
elif os.path.basename(cc).startswith('gcc'): |
| 182 |
|
| 183 |
data = _read_output("'%s' --version" |
| 184 |
% (cc.replace("'", "'\"'\"'"),)) |
| 185 |
if data and 'llvm-gcc' in data: |
| 186 |
|
| 187 |
cc = _find_build_tool('clang') |
| 188 |
|
| 189 |
if not cc: |
| 190 |
raise SystemError( |
| 191 |
"Cannot locate working compiler") |
| 192 |
|
| 193 |
if cc != oldcc: |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
for cv in _COMPILER_CONFIG_VARS: |
| 198 |
if cv in _config_vars and cv not in os.environ: |
| 199 |
cv_split = _config_vars[cv].split() |
| 200 |
cv_split[0] = cc if cv != 'CXX' else cc + '++' |
| 201 |
_save_modified_value(_config_vars, cv, ' '.join(cv_split)) |
| 202 |
|
| 203 |
return _config_vars |
| 204 |
|
| 205 |
|
| 206 |
def _remove_universal_flags(_config_vars): |
| 207 |
"""Remove all universal build arguments from config vars""" |
| 208 |
|
| 209 |
for cv in _UNIVERSAL_CONFIG_VARS: |
| 210 |
|
| 211 |
if cv in _config_vars and cv not in os.environ: |
| 212 |
flags = _config_vars[cv] |
| 213 |
flags = re.sub('-arch\s+\w+\s', ' ', flags) |
| 214 |
flags = re.sub('-isysroot [^ \t]*', ' ', flags) |
| 215 |
_save_modified_value(_config_vars, cv, flags) |
| 216 |
|
| 217 |
return _config_vars |
| 218 |
|
| 219 |
|
| 220 |
def _remove_unsupported_archs(_config_vars): |
| 221 |
"""Remove any unsupported archs from config vars""" |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if 'CC' in os.environ: |
| 233 |
return _config_vars |
| 234 |
|
| 235 |
if re.search('-arch\s+ppc', _config_vars['CFLAGS']) is not None: |
| 236 |
|
| 237 |
|
| 238 |
status = os.system( |
| 239 |
"""echo 'int main{};' | """ |
| 240 |
"""'%s' -c -arch ppc -x c -o /dev/null /dev/null 2>/dev/null""" |
| 241 |
%(_config_vars['CC'].replace("'", "'\"'\"'"),)) |
| 242 |
if status: |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
for cv in _UNIVERSAL_CONFIG_VARS: |
| 252 |
if cv in _config_vars and cv not in os.environ: |
| 253 |
flags = _config_vars[cv] |
| 254 |
flags = re.sub('-arch\s+ppc\w*\s', ' ', flags) |
| 255 |
_save_modified_value(_config_vars, cv, flags) |
| 256 |
|
| 257 |
return _config_vars |
| 258 |
|
| 259 |
|
| 260 |
def _override_all_archs(_config_vars): |
| 261 |
"""Allow override of all archs with ARCHFLAGS env var""" |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if 'ARCHFLAGS' in os.environ: |
| 266 |
arch = os.environ['ARCHFLAGS'] |
| 267 |
for cv in _UNIVERSAL_CONFIG_VARS: |
| 268 |
if cv in _config_vars and '-arch' in _config_vars[cv]: |
| 269 |
flags = _config_vars[cv] |
| 270 |
flags = re.sub('-arch\s+\w+\s', ' ', flags) |
| 271 |
flags = flags + ' ' + arch |
| 272 |
_save_modified_value(_config_vars, cv, flags) |
| 273 |
|
| 274 |
return _config_vars |
| 275 |
|
| 276 |
|
| 277 |
def _check_for_unavailable_sdk(_config_vars): |
| 278 |
"""Remove references to any SDKs not available""" |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
cflags = _config_vars.get('CFLAGS', '') |
| 290 |
m = re.search(r'-isysroot\s+(\S+)', cflags) |
| 291 |
if m is not None: |
| 292 |
sdk = m.group(1) |
| 293 |
if not os.path.exists(sdk): |
| 294 |
for cv in _UNIVERSAL_CONFIG_VARS: |
| 295 |
|
| 296 |
if cv in _config_vars and cv not in os.environ: |
| 297 |
flags = _config_vars[cv] |
| 298 |
flags = re.sub(r'-isysroot\s+\S+(?:\s|$)', ' ', flags) |
| 299 |
_save_modified_value(_config_vars, cv, flags) |
| 300 |
|
| 301 |
return _config_vars |
| 302 |
|
| 303 |
|
| 304 |
def compiler_fixup(compiler_so, cc_args): |
| 305 |
""" |
| 306 |
This function will strip '-isysroot PATH' and '-arch ARCH' from the |
| 307 |
compile flags if the user has specified one them in extra_compile_flags. |
| 308 |
|
| 309 |
This is needed because '-arch ARCH' adds another architecture to the |
| 310 |
build, without a way to remove an architecture. Furthermore GCC will |
| 311 |
barf if multiple '-isysroot' arguments are present. |
| 312 |
""" |
| 313 |
stripArch = stripSysroot = False |
| 314 |
|
| 315 |
compiler_so = list(compiler_so) |
| 316 |
|
| 317 |
if not _supports_universal_builds(): |
| 318 |
|
| 319 |
|
| 320 |
stripArch = stripSysroot = True |
| 321 |
else: |
| 322 |
stripArch = '-arch' in cc_args |
| 323 |
stripSysroot = '-isysroot' in cc_args |
| 324 |
|
| 325 |
if stripArch or 'ARCHFLAGS' in os.environ: |
| 326 |
while True: |
| 327 |
try: |
| 328 |
index = compiler_so.index('-arch') |
| 329 |
|
| 330 |
del compiler_so[index:index+2] |
| 331 |
except ValueError: |
| 332 |
break |
| 333 |
|
| 334 |
if 'ARCHFLAGS' in os.environ and not stripArch: |
| 335 |
|
| 336 |
|
| 337 |
compiler_so = compiler_so + os.environ['ARCHFLAGS'].split() |
| 338 |
|
| 339 |
if stripSysroot: |
| 340 |
while True: |
| 341 |
try: |
| 342 |
index = compiler_so.index('-isysroot') |
| 343 |
|
| 344 |
del compiler_so[index:index+2] |
| 345 |
except ValueError: |
| 346 |
break |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
sysroot = None |
| 352 |
if '-isysroot' in cc_args: |
| 353 |
idx = cc_args.index('-isysroot') |
| 354 |
sysroot = cc_args[idx+1] |
| 355 |
elif '-isysroot' in compiler_so: |
| 356 |
idx = compiler_so.index('-isysroot') |
| 357 |
sysroot = compiler_so[idx+1] |
| 358 |
|
| 359 |
if sysroot and not os.path.isdir(sysroot): |
| 360 |
from distutils import log |
| 361 |
log.warn("Compiling with an SDK that doesn't seem to exist: %s", |
| 362 |
sysroot) |
| 363 |
log.warn("Please check your Xcode installation") |
| 364 |
|
| 365 |
return compiler_so |
| 366 |
|
| 367 |
|
| 368 |
def customize_config_vars(_config_vars): |
| 369 |
"""Customize Python build configuration variables. |
| 370 |
|
| 371 |
Called internally from sysconfig with a mutable mapping |
| 372 |
containing name/value pairs parsed from the configured |
| 373 |
makefile used to build this interpreter. Returns |
| 374 |
the mapping updated as needed to reflect the environment |
| 375 |
in which the interpreter is running; in the case of |
| 376 |
a Python from a binary installer, the installed |
| 377 |
environment may be very different from the build |
| 378 |
environment, i.e. different OS levels, different |
| 379 |
built tools, different available CPU architectures. |
| 380 |
|
| 381 |
This customization is performed whenever |
| 382 |
distutils.sysconfig.get_config_vars() is first |
| 383 |
called. It may be used in environments where no |
| 384 |
compilers are present, i.e. when installing pure |
| 385 |
Python dists. Customization of compiler paths |
| 386 |
and detection of unavailable archs is deferred |
| 387 |
until the first extension module build is |
| 388 |
requested (in distutils.sysconfig.customize_compiler). |
| 389 |
|
| 390 |
Currently called from distutils.sysconfig |
| 391 |
""" |
| 392 |
|
| 393 |
if not _supports_universal_builds(): |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
_remove_universal_flags(_config_vars) |
| 399 |
|
| 400 |
|
| 401 |
_override_all_archs(_config_vars) |
| 402 |
|
| 403 |
|
| 404 |
_check_for_unavailable_sdk(_config_vars) |
| 405 |
|
| 406 |
return _config_vars |
| 407 |
|
| 408 |
|
| 409 |
def customize_compiler(_config_vars): |
| 410 |
"""Customize compiler path and configuration variables. |
| 411 |
|
| 412 |
This customization is performed when the first |
| 413 |
extension module build is requested |
| 414 |
in distutils.sysconfig.customize_compiler). |
| 415 |
""" |
| 416 |
|
| 417 |
|
| 418 |
_find_appropriate_compiler(_config_vars) |
| 419 |
|
| 420 |
|
| 421 |
_remove_unsupported_archs(_config_vars) |
| 422 |
|
| 423 |
|
| 424 |
_override_all_archs(_config_vars) |
| 425 |
|
| 426 |
return _config_vars |
| 427 |
|
| 428 |
|
| 429 |
def get_platform_osx(_config_vars, osname, release, machine): |
| 430 |
"""Filter values for get_platform()""" |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
macver = _config_vars.get('MACOSX_DEPLOYMENT_TARGET', '') |
| 440 |
macrelease = _get_system_version() or macver |
| 441 |
macver = macver or macrelease |
| 442 |
|
| 443 |
if macver: |
| 444 |
release = macver |
| 445 |
osname = "macosx" |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
cflags = _config_vars.get(_INITPRE+'CFLAGS', |
| 452 |
_config_vars.get('CFLAGS', '')) |
| 453 |
if macrelease: |
| 454 |
try: |
| 455 |
macrelease = tuple(int(i) for i in macrelease.split('.')[0:2]) |
| 456 |
except ValueError: |
| 457 |
macrelease = (10, 0) |
| 458 |
else: |
| 459 |
|
| 460 |
macrelease = (10, 0) |
| 461 |
|
| 462 |
if (macrelease >= (10, 4)) and '-arch' in cflags.strip(): |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
machine = 'fat' |
| 467 |
|
| 468 |
archs = re.findall('-arch\s+(\S+)', cflags) |
| 469 |
archs = tuple(sorted(set(archs))) |
| 470 |
|
| 471 |
if len(archs) == 1: |
| 472 |
machine = archs[0] |
| 473 |
elif archs == ('i386', 'ppc'): |
| 474 |
machine = 'fat' |
| 475 |
elif archs == ('i386', 'x86_64'): |
| 476 |
machine = 'intel' |
| 477 |
elif archs == ('i386', 'ppc', 'x86_64'): |
| 478 |
machine = 'fat3' |
| 479 |
elif archs == ('ppc64', 'x86_64'): |
| 480 |
machine = 'fat64' |
| 481 |
elif archs == ('i386', 'ppc', 'ppc64', 'x86_64'): |
| 482 |
machine = 'universal' |
| 483 |
else: |
| 484 |
raise ValueError( |
| 485 |
"Don't know machine value for archs=%r" % (archs,)) |
| 486 |
|
| 487 |
elif machine == 'i386': |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
if sys.maxint >= 2**32: |
| 492 |
machine = 'x86_64' |
| 493 |
|
| 494 |
elif machine in ('PowerPC', 'Power_Macintosh'): |
| 495 |
|
| 496 |
|
| 497 |
if sys.maxint >= 2**32: |
| 498 |
machine = 'ppc64' |
| 499 |
else: |
| 500 |
machine = 'ppc' |
| 501 |
|
| 502 |
return (osname, release, machine) |