a
    ӗaU                     @   s   d Z 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 )0zShared OS X support functions.    N)compiler_fixupcustomize_config_varscustomize_compilerget_platform_osx)CFLAGSLDFLAGS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 )zTries 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PATHwin32z.exe)
osenvironsplitpathseppathsplitextsysplatformisfilejoin)
executabler   pathsbaseextpf r   //home/manager/Python-3.9.10/Lib/_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 s0    Y  dS )z0Output from successful command execution or Noner   Nz/tmp/_osx_support.%szw+bz%s >'%s' 2>&1z%s 2>/dev/null >'%s'zutf-8)
contextlibtempfileZ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 )z0Find a build tool on current path or using xcrunz/usr/bin/xcrun -find %s )r!   r/   )Ztoolnamer   r   r    _find_build_toolM   s
    r1   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 )z*Return the OS X system version as a stringNr0   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 )z}
    Return the macOS system version as a tuple

    The return value is safe to use to compare
    two version numbers.
    Nc                 s   s   | ]}t |V  qd S Nint.0ir   r   r    	<genexpr>       z,_get_system_version_tuple.<locals>.<genexpr>r2   r   )_SYSTEM_VERSION_TUPLEr<   tupler   
ValueErrorZosx_versionr   r   r    _get_system_version_tupleu   s    
rI   c                 C   s"   t | D ]}|tr| |= qdS )z-Remove original unmodified values for testingN)list
startswith_INITPRE)_config_varskr   r   r    _remove_original_values   s    
rO   c                 C   s8   |  |d}||kr,t| | vr,|| t| < || |< dS )z@Save modified and original unmodified value of configuration varr0   N)getrL   )rM   cvZnewvalueZoldvaluer   r   r    _save_modified_value   s    rR   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 rda t S )z= Returns the root of the default SDK for this system, or '/' Nz%s -c -E -v - </dev/nullTFz#include <...>zEnd of search listz/usr/include/z.sdk/usr/includei)_cache_default_sysrootr/   
splitlinesrK   r,   endswith)cccontents
in_incdirsliner   r   r    _default_sysroot   s$    


r[   c                  C   s   t  } | rt| dkS dS )z=Returns True if universal builds are supported on this system
      F)rI   boolrH   r   r   r    _supports_universal_builds   s    r`   c                  C   s   t  } | r| dkS dS )z9Returns True if arm64 builds are supported on this system)   r   F)rI   rH   r   r   r    _supports_arm64_builds   s    rb   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rtD ]L}|| v r|t jvr| |  }|d
kr|n|d |d< t| |d| q| S )z7Find appropriate C compiler for extension module buildsr   r   clanggccz'%s' --version''"'"'zllvm-gcczCannot locate working compilerr   z++ )r   r   r   r!   r1   r   basenamerK   r/   replaceSystemError_COMPILER_CONFIG_VARSrR   r   )rM   rW   ZoldccdatarQ   Zcv_splitr   r   r    _find_appropriate_compiler   s,    

rm   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 )z5Remove all universal build arguments from config vars-arch\s+\w+\srg   )flagsz-isysroot\s*\S+)_UNIVERSAL_CONFIG_VARSr   r   r7   subASCIIrR   )rM   rQ   ro   r   r   r    _remove_universal_flags  s    rs   c                 C   s   dt jv r| S td| d durt d| d ddf }|rtD ]8}|| v rF|t jvrF| | }tdd	|}t| || qF| 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/nullre   rf   z-arch\s+ppc\w*\srg   )	r   r   r7   r8   r)   ri   rp   rq   rR   )rM   statusrQ   ro   r   r   r    _remove_unsupported_archs  s    
	ru   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 )z2Allow override of all archs with ARCHFLAGS env var	ARCHFLAGS-archrn   rg   )r   r   rp   r7   rq   rR   )rM   ZarchrQ   ro   r   r   r    _override_all_archs:  s    

rx   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 )z+Remove references to any SDKs not availabler   r0   z-isysroot\s*(\S+)Nr3   z-isysroot\s*\S+(?:\s|$)rg   )rP   r7   r8   r:   r   r   existsrp   r   rq   rR   )rM   cflagsr;   ZsdkrQ   ro   r   r   r    _check_for_unavailable_sdkK  s    
r{   c           
      C   s  d }}t | } t s d }}nd|v }tdd |D }|sHdtjv rz| d}| ||d = W qH tyz   Y qY qH0 qHnFt stt	t
| D ].}| | dkr| |d  d	kr| ||d = qdtjv r|s| tjd   } |r>d
d t| D }|sq>|d }| | dkr.| ||d = q| ||d = qd}|}dd t|D }|st| }dd t| D }|D ]B}|| dkr||d  } qn|| t
dd } qqx|rtj|sd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.
    FTrw   c                 s   s   | ]}| d r|V  qdS )	-isysrootNrK   )rA   argr   r   r    rC   y  rD   z!compiler_fixup.<locals>.<genexpr>rv   r4   r3   arm64c                 S   s   g | ]\}}| d r|qS r|   r}   rA   rB   xr   r   r    
<listcomp>  rD   z"compiler_fixup.<locals>.<listcomp>r   r|   Nc                 S   s   g | ]\}}| d r|qS r   r}   r   r   r   r    r     rD   c                 S   s   g | ]\}}| d r|qS r   r}   r   r   r   r    r     rD   )logz4Compiling with an SDK that doesn't seem to exist: %sz$Please check your Xcode installation)rJ   r`   anyr   r   indexrG   rb   reversedrangelenr   	enumerater   isdir	distutilsr   warn)
compiler_soZcc_argsZ	stripArchZstripSysrootr   idxindicessysrootZargvarr   r   r   r    r   f  sZ    	



r   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
    )r`   rs   rx   r{   rM   r   r   r    r     s
    r   c                 C   s   t |  t|  t|  | S )zCustomize compiler path and configuration variables.

    This customization is performed when the first
    extension module build is requested
    in distutils.sysconfig.customize_compiler.
    )rm   ru   rx   r   r   r   r    r     s    	r   c                 C   s  |  dd}t p|}|p|}|r|}d}|  td |  dd}|rz$tdd |ddd	 D }W q ty   d
}Y q0 nd
}|dkrDd| v rDd}td|}tt	t
|}t|dkr|d }nj|dkrd}n\|dkrd}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rd}n |dv rtjdkr|d }nd!}|||fS )"z Filter values for get_platform()MACOSX_DEPLOYMENT_TARGETr0   Zmacosxr   c                 s   s   | ]}t |V  qd S r=   r>   r@   r   r   r    rC     rD   z#get_platform_osx.<locals>.<genexpr>r2   r   r4   )r]   r   r\   rw   Zfatz-arch\s+(\S+)r3   )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   )rP   r<   rL   rF   r   rG   r,   r7   findallsortedsetr   r   maxsize)rM   osnamereleasemachineZmacverZ
macreleaserz   archsr   r   r    r     sX    



$






r   )N)F)__doc__r   r7   r   __all__rp   rk   rL   r!   r/   r1   r5   r<   rE   rI   rO   rR   rT   r[   r`   rb   rm   rs   ru   rx   r{   r   r   r   r   r   r   r   r    <module>   s:   


	

>(Q)