o
    JAfä<  ã                   @   s¬   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZmZmZmZ ddlmZ dd	lmZ d
d„ Zdd„ Zdd„ ZG dd„ de
ƒZdS )a9  distutils.unixccompiler

Contains the UnixCCompiler class, a subclass of CCompiler that handles
the "typical" Unix-style command-line C compiler:
  * macros defined with -Dname[=value]
  * macros undefined with -Uname
  * include search directories specified with -Idir
  * libraries specified with -lllib
  * library search directories specified with -Ldir
  * compile handled by 'cc' (or similar) executable with -c option:
    compiles .c to .o
  * link static library handled by 'ar' command (possibly with 'ranlib')
  * link shared library handled by 'cc -shared'
é    N)Ú	sysconfig)Únewer)Ú	CCompilerÚgen_preprocess_optionsÚgen_lib_options)ÚDistutilsExecErrorÚCompileErrorÚLibErrorÚ	LinkError)Úlogé   )Úcompiler_fixupc                 C   sT   d}t j | d ¡dkrd}d| | v r|d7 }d| | v s| d|… | |d… fS )zÿ
    For macOS, split command into 'env' portion (if any)
    and the rest of the linker command.

    >>> _split_env(['a', 'b', 'c'])
    ([], ['a', 'b', 'c'])
    >>> _split_env(['/usr/bin/env', 'A=3', 'gcc'])
    (['/usr/bin/env', 'A=3'], ['gcc'])
    r   Úenvr   ú=N©ÚosÚpathÚbasename©ÚcmdÚpivot© r   úH/usr/lib/python3.10/site-packages/setuptools/_distutils/unixccompiler.pyÚ
_split_env)   s   
ÿr   c                 C   s,   t j | d ¡dk}| d|… | |d… fS )a  
    AIX platforms prefix the compiler with the ld_so_aix
    script, so split that from the linker command.

    >>> _split_aix(['a', 'b', 'c'])
    ([], ['a', 'b', 'c'])
    >>> _split_aix(['/bin/foo/ld_so_aix', 'gcc'])
    (['/bin/foo/ld_so_aix'], ['gcc'])
    r   Z	ld_so_aixNr   r   r   r   r   Ú
_split_aix;   s   
r   c                 C   s,   t |ƒ}| d|… |kr|nd}| |d… S )a  
    The linker command usually begins with the compiler
    command (possibly multiple elements), followed by zero or more
    params for shared library building.

    If the LDSHARED env variable overrides the linker command,
    however, the commands may not match.

    Return the best guess of the linker parameters by stripping
    the linker command. If the compiler command does not
    match the linker command, assume the linker command is
    just the first element.

    >>> _linker_params('gcc foo bar'.split(), ['gcc'])
    ['foo', 'bar']
    >>> _linker_params('gcc foo bar'.split(), ['other'])
    ['foo', 'bar']
    >>> _linker_params('ccache gcc foo bar'.split(), 'ccache gcc'.split())
    ['foo', 'bar']
    >>> _linker_params(['gcc'], ['gcc'])
    []
    Nr   )Úlen)Z
linker_cmdZcompiler_cmdZc_lenr   r   r   r   Ú_linker_paramsI   s   r   c                	   @   sü   e Zd ZdZddgdgdgddgdgddgddœZejdd… d	kr'd
ged
< g d¢ZdZdZ	dZ
dZdZd Z ZZeZejdkrDdZ					d'dd„Zdd„ Z	d(dd„Z										d)dd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd*d%d&„ZdS )+ÚUnixCCompilerÚunixNÚccz-sharedÚarz-cr)ÚpreprocessorÚcompilerÚcompiler_soÚcompiler_cxxÚ	linker_soÚ
linker_exeÚarchiverÚranlibé   Údarwinr(   )z.cz.Cz.ccz.cxxz.cppz.mz.oz.az.soz.dylibz.tbdzlib%s%sÚcygwinz.exec              
   C   sÊ   |   d ||¡}|\}}}t||ƒ}	| j|	 }
|r|
 d|g¡ |r'||
d d…< |r.|
 |¡ |
 |¡ | js?|d u s?t||ƒrc|rJ|  tj	 
|¡¡ z|  |
¡ W d S  tyb } zt|ƒ‚d }~ww d S )Nú-or   )Z_fix_compile_argsr   r!   ÚextendÚappendÚforcer   Úmkpathr   r   ÚdirnameÚspawnr   r   )ÚselfÚsourceZoutput_fileZmacrosZinclude_dirsÚextra_preargsÚextra_postargsÚ
fixed_argsÚignoreÚpp_optsZpp_argsÚmsgr   r   r   Ú
preprocessŽ   s*   	




€ÿûzUnixCCompiler.preprocessc           	   
   C   sT   t | j|| ƒ}z|  || |d|g | ¡ W d S  ty) } zt|ƒ‚d }~ww )Nr,   )r   r#   r2   r   r   )	r3   ÚobjÚsrcÚextZcc_argsr6   r9   r#   r:   r   r   r   Ú_compile¯   s   "€ÿzUnixCCompiler._compiler   c              
   C   s¬   |   ||¡\}}| j||d}|  ||¡rN|  tj |¡¡ |  | j|g | | j	 ¡ | j
rLz|  | j
|g ¡ W d S  tyK } zt|ƒ‚d }~ww d S t d|¡ d S )N)Ú
output_dirúskipping %s (up-to-date))Ú_fix_object_argsÚlibrary_filenameÚ
_need_linkr0   r   r   r1   r2   r'   Úobjectsr(   r   r	   r   Údebug)r3   rE   Zoutput_libnamer@   rF   Útarget_langÚoutput_filenamer:   r   r   r   Úcreate_static_lib¶   s   €ÿýzUnixCCompiler.create_static_libc              
   C   s˜  |   ||¡\}}|  |||¡}|\}}}t| |||ƒ}t|ttd ƒfƒs(tdƒ‚|d ur3tj 	||¡}|  
||¡rÄ|| j | d|g }|	rMdg|d d…< |
rU|
|d d…< |r\| |¡ |  tj |¡¡ zN|tjk}|rp| jn| jd d … }|dkr¥| jr¥t|ƒ\}}t|ƒ\}}t| jƒ\}}t| jƒ\}}t||ƒ}|| | | }t||ƒ}|  || ¡ W d S  tyÃ } zt|ƒ‚d }~ww t d|¡ d S )Nz%'output_dir' must be a string or Noner,   z-gr   zc++rA   )rB   Z_fix_lib_argsr   Ú
isinstanceÚstrÚtypeÚ	TypeErrorr   r   ÚjoinrD   rE   r-   r0   r1   r   Z
EXECUTABLEr&   r%   r$   r   r   r   r   r2   r   r
   r   rF   )r3   Ztarget_descrE   rH   r@   Z	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsrF   r5   r6   Z
build_temprG   r7   Zlib_optsZld_argsZbuilding_exeZlinkerr   Z	linker_neÚaixZ	linker_naÚ_Zcompiler_cxx_neZlinker_exe_neÚparamsr:   r   r   r   ÚlinkÎ   sD   




€ÿzUnixCCompiler.linkc                 C   ó   d| S )Nú-Lr   )r3   Údirr   r   r   Úlibrary_dir_option  ó   z UnixCCompiler.library_dir_optionc                 C   s0   t  d¡}tj t |¡d ¡}d|v pd|v S )NÚCCr   Úgcczg++)r   Úget_config_varr   r   r   ÚshlexÚsplit)r3   Zcc_varr"   r   r   r   Ú_is_gcc  s   
zUnixCCompiler._is_gccc                 C   s®   t jd d… dkr&ddlm}m} |ƒ }|r"||ƒddgkr"d| S d| S t jd d	… d
kr3d| S t jd d… dkrH|  ¡ rBdndd| gS t d¡dkrSd| S d| S )Nr)   r*   r   )Úget_macosx_target_verÚsplit_versioné
   é   z-Wl,-rpath,rT   é   Zfreebsdz-Wl,-rpath=zhp-uxz-Wl,+sz+sÚGNULDÚyesz-Wl,--enable-new-dtags,-Rz-Wl,-R)ÚsysÚplatformZdistutils.utilr^   r_   r]   r   rZ   )r3   rU   r^   r_   Zmacosx_target_verr   r   r   Úruntime_library_dir_option  s   þz(UnixCCompiler.runtime_library_dir_optionc                 C   rS   )Nz-lr   )r3   Úlibr   r   r   Úlibrary_option=  rW   zUnixCCompiler.library_optionc                 C   sŽ  | j |dd}| j |dd}| j |dd}| j |dd}tjdkr8t d¡}t d|¡}	|	d u r3d	}
n|	 d
¡}
|D ]Š}tj	 
||¡}tj	 
||¡}tj	 
||¡}tj	 
||¡}tjdkrœ| d¡sl| d¡rœ| d¡sœtj	 
|
|d
d … |¡}tj	 
|
|d
d … |¡}tj	 
|
|d
d … |¡}tj	 
|
|d
d … |¡}tj	 |¡r¦|  S tj	 |¡r°|  S tj	 |¡rº|  S tj	 |¡rÄ|  S q:d S )NÚshared)Zlib_typeÚdylibÚ
xcode_stubÚstaticr*   ÚCFLAGSz-isysroot\s*(\S+)ú/r   z/System/z/usr/z/usr/local/)rC   re   rf   r   rZ   ÚreÚsearchÚgroupr   r   rN   Ú
startswithÚexists)r3   Údirsrh   rF   Zshared_fZdylib_fZxcode_stub_fZstatic_fZcflagsÚmZsysrootrU   rj   rk   rm   rl   r   r   r   Úfind_library_file@  sH   



ÿþþÿzUnixCCompiler.find_library_file)NNNNN)Nr   N)
NNNNNr   NNNN)r   )Ú__name__Ú
__module__Ú__qualname__Zcompiler_typeZexecutablesre   rf   Zsrc_extensionsZobj_extensionZstatic_lib_extensionZshared_lib_extensionZdylib_lib_extensionZxcode_stub_lib_extensionZstatic_lib_formatZshared_lib_formatZdylib_lib_formatZxcode_stub_lib_formatZexe_extensionr;   r?   rI   rR   rV   r]   rg   ri   rw   r   r   r   r   r   e   s`    	ø


ù!
ÿ
ò?(r   )Ú__doc__r   re   rp   r[   Ú	distutilsr   Zdistutils.dep_utilr   Zdistutils.ccompilerr   r   r   Zdistutils.errorsr   r   r	   r
   r   Z_macos_compatr   r   r   r   r   r   r   r   r   Ú<module>   s     