o
    bb<                     @   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   home/ych/rk3568/buildroot/output/rockchip_rk3568_recovery/host/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   )_fix_compile_argsr   r!   extendappendforcer   mkpathr   r   dirnamespawnr   r   )selfsourceoutput_filemacros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#   r3   r   r   )	r4   objsrcextcc_argsr:   r=   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_linkr1   r   r   r2   r3   r'   objectsr(   r   r	   r   debug)r4   rJ   output_libnamerE   rK   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rt|\}}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++rF   )rG   _fix_lib_argsr   
isinstancestrtype	TypeErrorr   r   joinrI   rJ   r.   r1   r2   r   
EXECUTABLEr&   r%   r$   r   r   r   r   r3   r   r
   r   rK   )r4   target_descrJ   rN   rE   	librarieslibrary_dirsruntime_library_dirsexport_symbolsrK   r9   r:   
build_temprM   r;   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   )r4   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   Zgcczg++)r   get_config_varr   r   r   shlexsplit)r4   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,rc      Zfreebsdz-Wl,-rpath=zhp-uxz-Wl,+sz+sGNULDyesz-Wl,--enable-new-dtags,-Rz-Wl,-R)sysplatformdistutils.utilrl   rm   rk   r   rh   )r4   rd   rl   rm   Zmacosx_target_verr   r   r   runtime_library_dir_option  s   z(UnixCCompiler.runtime_library_dir_optionc                 C   rb   )Nz-lr   )r4   libr   r   r   library_option=  rf   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st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)lib_typedylib
xcode_stubstaticr*   CFLAGSz-isysroot\s*(\S+)/r   z/System/z/usr/z/usr/local/)rH   rs   rt   r   rh   researchgroupr   r   rU   
startswithexists)r4   dirsrw   rK   Zshared_fZdylib_fZxcode_stub_fZstatic_fcflagsmZsysrootrd   ry   r{   r}   r|   r   r   r   find_library_file@  sH   



zUnixCCompiler.find_library_file)NNNNN)Nr   N)
NNNNNr   NNNN)r   )__name__
__module____qualname__compiler_typeexecutablesrs   rt   src_extensionsobj_extensionstatic_lib_extensionshared_lib_extensionZdylib_lib_extensionZxcode_stub_lib_extensionstatic_lib_formatshared_lib_formatZdylib_lib_formatZxcode_stub_lib_formatexe_extensionr?   rD   rO   ra   re   rk   rv   rx   r   r   r   r   r   r   e   s`    	


!

?(r   )__doc__r   rs   r   ri   	distutilsr   distutils.dep_utilr   distutils.ccompilerr   r   r   distutils.errorsr   r   r	   r
   r   Z_macos_compatr   r   r   r   r   r   r   r   r   <module>   s     