B
    Á S]ÂJ  ã               @   s¾   d Z ddlZddlZddlZddddgZdZdZd	Zd&d
d„Zdd„ Z	dd„ Z
dadd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d„ Zd#d„ Zd$d„ Zd%d„ ZdS )'zShared OS X support functions.é    NÚcompiler_fixupÚcustomize_config_varsÚcustomize_compilerÚget_platform_osx)ÚCFLAGSZLDFLAGSZCPPFLAGSZ
BASECFLAGSÚ	BLDSHAREDÚLDSHAREDÚCCÚCXXZ	PY_CFLAGSZ
PY_LDFLAGSZPY_CPPFLAGSZPY_CORE_CFLAGSZPY_CORE_LDFLAGS)r   r   r	   r
   Z_OSX_SUPPORT_INITIAL_c             C   sŒ   |dkrt jd }| t j¡}t j | ¡\}}tjdkrH|dkrH| d } t j | ¡s„x*|D ]"}t j 	|| ¡}t j |¡rZ|S qZW dS | S dS )zÚTries to find 'executable' in the directories listed in 'path'.

    A string listing directories separated by 'os.pathsep'; defaults to
    os.environ['PATH'].  Returns the complete filename or None if not found.
    NÚPATHZwin32z.exe)
ÚosÚenvironÚsplitÚpathsepÚpathÚsplitextÚsysÚplatformÚisfileÚjoin)Ú
executabler   ÚpathsÚbaseZextÚpÚf© r   úlib/python3.7/_osx_support.pyÚ_find_executable   s    

r   c          	   C   sŠ   ddl }yddl}| ¡ }W n( tk
rD   tdt ¡ f dƒ}Y nX | |¡0}d| |jf }t 	|¡s|| 
¡  d¡ ¡ S dS Q R X dS )z0Output from successful command execution or Noner   Nz/tmp/_osx_support.%szw+bz%s 2>/dev/null >'%s'zutf-8)Ú
contextlibÚtempfileZNamedTemporaryFileÚImportErrorÚopenr   ÚgetpidÚclosingÚnameÚsystemÚreadÚdecodeÚstrip)Zcommandstringr   r   ÚfpÚcmdr   r   r   Ú_read_output7   s    r+   c             C   s   t | ƒptd| f ƒpdS )z0Find a build tool on current path or using xcrunz/usr/bin/xcrun -find %sÚ )r   r+   )Ztoolnamer   r   r   Ú_find_build_toolJ   s    r-   c              C   sx   t dkrtda ytdƒ} W n tk
r,   Y nHX zt d|  ¡ ¡}W d|  ¡  X |dk	rtd | d¡ 	d¡dd… ¡a t S )z*Return the OS X system version as a stringNr,   z0/System/Library/CoreServices/SystemVersion.plistz=<key>ProductUserVisibleVersion</key>\s*<string>(.*?)</string>Ú.é   é   )
Ú_SYSTEM_VERSIONr!   ÚOSErrorÚreÚsearchr&   Úcloser   Úgroupr   )r   Úmr   r   r   Ú_get_system_versionS   s    

r8   c             C   s&   x t | ƒD ]}| t¡r
| |= q
W dS )z-Remove original unmodified values for testingN)ÚlistÚ
startswithÚ_INITPRE)Ú_config_varsÚkr   r   r   Ú_remove_original_valuesq   s    
r>   c             C   s8   |   |d¡}||kr,t| | kr,|| t| < || |< dS )z@Save modified and original unmodified value of configuration varr,   N)Úgetr;   )r<   ÚcvZnewvalueZoldvaluer   r   r   Ú_save_modified_valuex   s    rA   c              C   sT   t ƒ } | r@ytdd„ |  d¡D ƒƒ} W n tk
r>   d} Y nX | rPt| dkƒS dS )z=Returns True if universal builds are supported on this systemc             s   s   | ]}t |ƒV  qd S )N)Úint)Ú.0Úir   r   r   ú	<genexpr>Š   s    z-_supports_universal_builds.<locals>.<genexpr>r.   r,   )é
   é   F)r8   Útupler   Ú
ValueErrorÚbool)Zosx_versionr   r   r   Ú_supports_universal_builds€   s    
rK   c             C   sÞ   dt jkr| S | d  ¡ d  }}t|ƒs4tdƒ}n<t j |¡ d¡rptd| 	dd¡f ƒ}|rpd|krptdƒ}|s|t
d	ƒ‚||krÚxTtD ]L}|| krŠ|t jkrŠ| |  ¡ }|d
kr¸|n|d |d< t| |d |¡ƒ qŠW | S )z7Find appropriate C compiler for extension module buildsr	   r   ZclangZgccz'%s' --versionú'z'"'"'zllvm-gcczCannot locate working compilerr
   z++ú )r   r   r   r   r-   r   Úbasenamer:   r+   ÚreplaceÚSystemErrorÚ_COMPILER_CONFIG_VARSrA   r   )r<   ZccZoldccÚdatar@   Zcv_splitr   r   r   Ú_find_appropriate_compiler   s(    


rS   c             C   sZ   xTt D ]L}|| kr|tjkr| | }tjdd|tjd}t dd|¡}t| ||ƒ qW | S )z5Remove all universal build arguments from config varsz-arch\s+\w+\srM   )Úflagsz-isysroot [^ 	]*)Ú_UNIVERSAL_CONFIG_VARSr   r   r3   ÚsubÚASCIIrA   )r<   r@   rT   r   r   r   Ú_remove_universal_flagsÎ   s    
rX   c             C   sˆ   dt jkr| S t d| d ¡dk	r„t  d| d  dd¡f ¡}|r„x@tD ]8}|| krH|t jkrH| | }t dd	|¡}t| ||ƒ qHW | S )
z-Remove any unsupported archs from config varsr	   z-arch\s+ppcr   NzNecho 'int main{};' | '%s' -c -arch ppc -x c -o /dev/null /dev/null 2>/dev/nullrL   z'"'"'z-arch\s+ppc\w*\srM   )	r   r   r3   r4   r%   rO   rU   rV   rA   )r<   Zstatusr@   rT   r   r   r   Ú_remove_unsupported_archsÜ   s    
	
rY   c             C   sh   dt jkrdt jd }xNtD ]F}|| krd| | kr| | }t dd|¡}|d | }t| ||ƒ qW | S )z2Allow override of all archs with ARCHFLAGS env varÚ	ARCHFLAGSz-archz-arch\s+\w+\srM   )r   r   rU   r3   rV   rA   )r<   Zarchr@   rT   r   r   r   Ú_override_all_archs  s    


r[   c             C   s|   |   dd¡}t d|¡}|dk	rx| d¡}tj |¡sxx@tD ]8}|| kr<|tjkr<| | }t 	dd|¡}t
| ||ƒ q<W | S )z+Remove references to any SDKs not availabler   r,   z-isysroot\s+(\S+)Nr/   z-isysroot\s+\S+(?:\s|$)rM   )r?   r3   r4   r6   r   r   ÚexistsrU   r   rV   rA   )r<   Úcflagsr7   Zsdkr@   rT   r   r   r   Ú_check_for_unavailable_sdk  s    

r^   c             C   sT  d }}t | ƒ} tƒ s d }}nd|k}d|k}|s>dtjkrxx8y|  d¡}| ||d …= W q@ tk
rr   P Y q@X q@W dtjkr˜|s˜| tjd  ¡  } |rÖx8y|  d¡}| ||d …= W qž tk
rÐ   P Y qžX qžW d}d|krú| d¡}||d  }n d| kr|  d¡}| |d  }|rPtj |¡sPd	d
l	m
} | d|¡ | d¡ | S )ae  
    This function will strip '-isysroot PATH' and '-arch ARCH' from the
    compile flags if the user has specified one them in extra_compile_flags.

    This is needed because '-arch ARCH' adds another architecture to the
    build, without a way to remove an architecture. Furthermore GCC will
    barf if multiple '-isysroot' arguments are present.
    FTz-archz	-isysrootrZ   r0   Nr/   r   )Úlogz4Compiling with an SDK that doesn't seem to exist: %sz$Please check your Xcode installation)r9   rK   r   r   ÚindexrI   r   r   ÚisdirZ	distutilsr_   Úwarn)Zcompiler_soZcc_argsZ	stripArchZstripSysrootr`   ZsysrootÚidxr_   r   r   r   r   0  sF    	






c             C   s"   t ƒ st| ƒ t| ƒ t| ƒ | S )aö  Customize Python build configuration variables.

    Called internally from sysconfig with a mutable mapping
    containing name/value pairs parsed from the configured
    makefile used to build this interpreter.  Returns
    the mapping updated as needed to reflect the environment
    in which the interpreter is running; in the case of
    a Python from a binary installer, the installed
    environment may be very different from the build
    environment, i.e. different OS levels, different
    built tools, different available CPU architectures.

    This customization is performed whenever
    distutils.sysconfig.get_config_vars() is first
    called.  It may be used in environments where no
    compilers are present, i.e. when installing pure
    Python dists.  Customization of compiler paths
    and detection of unavailable archs is deferred
    until the first extension module build is
    requested (in distutils.sysconfig.customize_compiler).

    Currently called from distutils.sysconfig
    )rK   rX   r[   r^   )r<   r   r   r   r   p  s
    c             C   s   t | ƒ t| ƒ t| ƒ | S )zÅCustomize compiler path and configuration variables.

    This customization is performed when the first
    extension module build is requested
    in distutils.sysconfig.customize_compiler).
    )rS   rY   r[   )r<   r   r   r   r   ™  s    	c             C   s|  |   dd¡}tƒ p|}|p|}|rr|}d}|   td |   dd¡¡}|rˆy$tdd„ | d¡dd	… D ƒƒ}W qŒ tk
r„   d
}Y qŒX nd
}|dkr6d| ¡ kr6d}t d|¡}tt	t
|ƒƒƒ}t|ƒdkrÚ|d }nZ|dkrèd}nL|dkröd}n>|dkrd}n.|dkrd}n|dkr&d}ntd|f ƒ‚n<|dkrRtjdkrrd}n |dkrrtjdkrnd}nd}|||fS ) z Filter values for get_platform()ZMACOSX_DEPLOYMENT_TARGETr,   Zmacosxr   c             s   s   | ]}t |ƒV  qd S )N)rB   )rC   rD   r   r   r   rE   Ç  s    z#get_platform_osx.<locals>.<genexpr>r.   r   r0   )rF   r   )rF   rG   z-archZfatz-arch\s+(\S+)r/   )Úi386Úppc)rd   Úx86_64Zintel)rd   re   rf   Zfat3)Úppc64rf   Zfat64)rd   re   rg   rf   Z	universalz%Don't know machine value for archs=%rrd   l        rf   )ZPowerPCZPower_Macintoshrg   re   )r?   r8   r;   rH   r   rI   r(   r3   ÚfindallÚsortedÚsetÚlenr   Úmaxsize)r<   ZosnameÚreleaseÚmachineZmacverZ
macreleaser]   Zarchsr   r   r   r   ­  sP    


$





)N)Ú__doc__r   r3   r   Ú__all__rU   rQ   r;   r   r+   r-   r1   r8   r>   rA   rK   rS   rX   rY   r[   r^   r   r   r   r   r   r   r   r   Ú<module>   s6   
>(@)