a
    ãzeU  ã                   @   sØ   d dl Z d dlZd dlZg d¢ZdZdZdZd-dd„Zd.d	d
„Zdd„ Z	da
dd„ Zda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S )/é    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 u rt jd }| t j¡}t j | ¡\}}tjdkrH|dkrH| d } t j | ¡s„|D ]&}t j 	|| ¡}t j |¡rX|  S qXd S | S d S )NÚPATHÚwin32z.exe)
ÚosÚenvironÚsplitÚpathsepÚpathÚsplitextÚsysÚplatformÚisfileÚjoin)Ú
executabler   ÚpathsÚbaseÚextÚpÚf© r   ú"/usr/lib/python3.9/_osx_support.pyÚ_find_executable   s    

r   Fc                 C   s´   dd l }zdd l}| ¡ }W n& tyB   tdt ¡ f dƒ}Y n0 | |¡R}|rdd| |jf }nd| |jf }t 	|¡sŽ| 
¡  d¡ ¡ nd W  d   ƒ S 1 s¦0    Y  d S )Nr   z/tmp/_osx_support.%szw+bz%s >'%s' 2>&1z%s 2>/dev/null >'%s'zutf-8)Ú
contextlibÚtempfileÚNamedTemporaryFileÚImportErrorÚopenr   ÚgetpidÚclosingÚnameÚsystemÚreadÚdecodeÚstrip)ZcommandstringZcapture_stderrr    r!   ÚfpÚcmdr   r   r   Ú_read_output7   s    ÿÿ
r.   c                 C   s   t | ƒptd| f ƒpdS )Nz/usr/bin/xcrun -find %sÚ )r   r.   )Ztoolnamer   r   r   Ú_find_build_toolM   s
    ÿþr0   c                  C   s~   t d u rzda ztdƒ} W n ty*   Y nP0 zt d|  ¡ ¡}W |  ¡  n
|  ¡  0 |d urzd | d¡ 	d¡d d… ¡a t S )Nr/   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_versionV   s    
ÿr;   c                  C   sJ   t d u rFtƒ } | rFztdd„ |  d¡D ƒƒa W n tyD   da Y n0 t S )Nc                 s   s   | ]}t |ƒV  qd S ©N©Úint©Ú.0Úir   r   r   Ú	<genexpr>   ó    z,_get_system_version_tuple.<locals>.<genexpr>r1   r   )Ú_SYSTEM_VERSION_TUPLEr;   Útupler   Ú
ValueError©Zosx_versionr   r   r   Ú_get_system_version_tupleu   s    
rH   c                 C   s"   t | ƒD ]}| t¡r| |= qd S r<   )ÚlistÚ
startswithÚ_INITPRE)Ú_config_varsÚkr   r   r   Ú_remove_original_valuesˆ   s    
rN   c                 C   s8   |   |d¡}||kr,t| | vr,|| t| < || |< d S )Nr/   )ÚgetrK   )rL   ÚcvZnewvalueZoldvaluer   r   r   Ú_save_modified_value   s    rQ   c                 C   sŽ   t d urt S td| f dƒ}d}| ¡ D ]T}| d¡r<d}q(| d¡rLd}q(|r(| ¡ }|dkrfda q(| d¡r(|d d	… a q(t d u rŠda t S )
Nz%s -c -E -v - </dev/nullTFz#include <...>zEnd of search listz/usr/includeú/z.sdk/usr/includeiôÿÿÿ)Ú_cache_default_sysrootr.   Ú
splitlinesrJ   r+   Úendswith)ÚccÚcontentsZ
in_incdirsÚliner   r   r   Ú_default_sysroot™   s$    


rY   c                  C   s   t ƒ } | rt| dkƒS dS )N©é
   é   F)rH   ÚboolrG   r   r   r   Ú_supports_universal_builds²   s    r^   c                  C   s   t ƒ } | r| dkS dS )N)é   r   F)rH   rG   r   r   r   Ú_supports_arm64_builds¼   s    r`   c                 C   sÚ   dt jv r| S | d  ¡ d  }}t|ƒs4tdƒ}n<t j |¡ d¡rptd| 	dd¡f ƒ}|rpd|v rptdƒ}|s|t
d	ƒ‚||krÖtD ]L}|| v rˆ|t jvrˆ| |  ¡ }|d
kr¶|n|d |d< t| |d |¡ƒ qˆ| S )Nr	   r   ZclangZgccz'%s' --versionú'ú'"'"'zllvm-gcczCannot locate working compilerr
   z++ú )r   r   r   r   r0   r   ÚbasenamerJ   r.   ÚreplaceÚSystemErrorÚ_COMPILER_CONFIG_VARSrQ   r   )rL   rV   ZoldccÚdatarP   Zcv_splitr   r   r   Ú_find_appropriate_compilerÆ   s,    

ÿÿri   c                 C   sV   t D ]L}|| v r|tjvr| | }tjdd|tjd}t dd|¡}t| ||ƒ q| S )Nú-arch\s+\w+\src   )Úflagsz-isysroot\s*\S+)Ú_UNIVERSAL_CONFIG_VARSr   r   r6   ÚsubÚASCIIrQ   )rL   rP   rk   r   r   r   Ú_remove_universal_flags  s    ro   c                 C   s„   dt jv r| S t d| d ¡d ur€t  d| d  dd¡f ¡}|r€tD ]8}|| v rF|t jvrF| | }t dd|¡}t| ||ƒ qF| S )	Nr	   z-arch\s+ppcr   zNecho 'int main{};' | '%s' -c -arch ppc -x c -o /dev/null /dev/null 2>/dev/nullra   rb   z-arch\s+ppc\w*\src   )	r   r   r6   r7   r(   re   rl   rm   rQ   )rL   ÚstatusrP   rk   r   r   r   Ú_remove_unsupported_archs  s    
þÿ	rq   c                 C   sd   dt jv r`t jd }tD ]F}|| v rd| | v r| | }t dd|¡}|d | }t| ||ƒ q| S )NÚ	ARCHFLAGSú-archrj   rc   )r   r   rl   r6   rm   rQ   )rL   ZarchrP   rk   r   r   r   Ú_override_all_archs:  s    

rt   c                 C   sx   |   dd¡}t d|¡}|d urt| d¡}tj |¡sttD ]8}|| v r:|tjvr:| | }t 	dd|¡}t
| ||ƒ q:| S )Nr   r/   z-isysroot\s*(\S+)r2   z-isysroot\s*\S+(?:\s|$)rc   )rO   r6   r7   r9   r   r   Úexistsrl   r   rm   rQ   )rL   Úcflagsr:   ZsdkrP   rk   r   r   r   Ú_check_for_unavailable_sdkK  s    
rw   c           
      C   sö  d }}t | ƒ} tƒ s d }}nd|v }tdd„ |D ƒƒ}|sHdtjv r€z|  d¡}| ||d …= W qH tyz   Y qÆY qH0 qHnFtƒ sÆtt	t
| ƒƒƒD ].}| | dkr–| |d  d	kr–| ||d …= q–dtjv ræ|sæ| tjd  ¡  } |r>d
d„ t| ƒD ƒ}|sq>|d }| | dkr.| ||d …= qì| ||d …= qìd }|}dd„ t|ƒD ƒ}|st| }dd„ t| ƒD ƒ}|D ]B}|| dkrž||d  } q¼n|| t
dƒd … } q¼qx|ròtj |¡sòddlm}	 |	 d|¡ |	 d¡ | S )NFTrs   c                 s   s   | ]}|  d ¡r|V  qdS )ú	-isysrootN©rJ   )r@   Úargr   r   r   rB   y  rC   z!compiler_fixup.<locals>.<genexpr>rr   r3   r2   Úarm64c                 S   s   g | ]\}}|  d ¡r|‘qS ©rx   ry   ©r@   rA   Úxr   r   r   Ú
<listcomp>‘  rC   z"compiler_fixup.<locals>.<listcomp>r   rx   c                 S   s   g | ]\}}|  d ¡r|‘qS r|   ry   r}   r   r   r   r   ¡  rC   c                 S   s   g | ]\}}|  d ¡r|‘qS r|   ry   r}   r   r   r   r   ¤  rC   )Úlogz4Compiling with an SDK that doesn't seem to exist: %sz$Please check your Xcode installation)rI   r^   Úanyr   r   ÚindexrF   r`   ÚreversedÚrangeÚlenr   Ú	enumerater   ÚisdirZ	distutilsr€   Úwarn)
Zcompiler_soZcc_argsZ	stripArchZstripSysrootr‚   ÚidxÚindicesZsysrootZargvarr€   r   r   r   r   f  sZ    	


ÿ
r   c                 C   s"   t ƒ st| ƒ t| ƒ t| ƒ | S r<   )r^   ro   rt   rw   ©rL   r   r   r   r   ·  s
    r   c                 C   s   t | ƒ t| ƒ t| ƒ | S r<   )ri   rq   rt   r‹   r   r   r   r   à  s    	r   c                 C   sŠ  |   dd¡}tƒ p|}|p|}|r€|}d}|   td |   dd¡¡}|r†z$tdd„ | d¡dd	… D ƒƒ}W qŠ ty‚   d
}Y qŠ0 nd
}|dkrDd| ¡ v rDd}t d|¡}tt	t
|ƒƒƒ}t|ƒdkrØ|d }nj|dkræd}n\|dkrôd}nN|dkrd}n>|dkrd}n.|dkr$d}n|dkr4d}ntd|f ƒ‚n<|dkr`tjdkr€d}n |dv r€tjdkr|d }nd!}|||fS )"NZMACOSX_DEPLOYMENT_TARGETr/   Zmacosxr   c                 s   s   | ]}t |ƒV  qd S r<   r=   r?   r   r   r   rB     rC   z#get_platform_osx.<locals>.<genexpr>r1   r   r3   )r[   r   rZ   rs   Zfatz-arch\s+(\S+)r2   )r{   Úx86_64Z
universal2)Úi386Úppc)r   rŒ   Zintel)r   rŽ   rŒ   Zfat3)Úppc64rŒ   Zfat64)r   rŽ   r   rŒ   Ú	universalz%Don't know machine value for archs=%rr   l        rŒ   )ZPowerPCZPower_Macintoshr   rŽ   )rO   r;   rK   rE   r   rF   r+   r6   ÚfindallÚsortedÚsetr…   r   Úmaxsize)rL   ZosnameÚreleaseÚmachineZmacverZ
macreleaserv   Zarchsr   r   r   r   ô  sX    



ÿ$




ÿ

r   )N)F)r   r6   r   Ú__all__rl   rg   rK   r   r.   r0   r4   r;   rD   rH   rN   rQ   rS   rY   r^   r`   ri   ro   rq   rt   rw   r   r   r   r   r   r   r   r   Ú<module>   s8   


	

>(Q)