U
    f.                     @   s  d Z ddlZddlZddl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mZ ddlmZ ddlmZ dd	lmZmZ ed
gdgdgdgdgdgdgdgejd	Zdd ZdZG dd deZG dd deZdZdZ dZ!dd Z"dd  Z#dZ$dS )!a  distutils.cygwinccompiler

Provides the CygwinCCompiler class, a subclass of UnixCCompiler that
handles the Cygwin port of the GNU C compiler to Windows.  It also contains
the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
cygwin in no-cygwin mode).
    N)check_output   )RangeMap)CCompilerErrorCompileErrorDistutilsExecErrorDistutilsPlatformError)
write_file)UnixCCompiler)LooseVersionsuppress_known_deprecationZmsvcr70Zmsvcr71Zmsvcr80Zmsvcr90Zmsvcr100Zmsvcr110Zmsvcr120Zvcruntime140)	i  i  ix  i  i@  i  i  il  i  c                  C   sn   t dtj} zt| d}W n tk
r8   g  Y S X z
t| W S  tk
rh   t	d| dY nX dS )zaInclude the appropriate MSVC runtime library if Python was built
    with MSVC 7.0 or later.
    zMSC v\.(\d{4})r   zUnknown MS Compiler version  N)
researchsysversionintgroupAttributeError_msvcr_lookupKeyError
ValueError)matchZmsc_ver r   Dlib/python3.8/site-packages/setuptools/_distutils/cygwinccompiler.py	get_msvcr4   s    

r   zxUnable to set runtime library search path on Windows, usually indicated by `runtime_library_dirs` parameter to Extensionc                
       s   e Zd ZdZdZdZdZdZdZdZ	dZ
dZd	Zd fdd	Zedd Zdd ZdddZdd Z fddZe fddZ  ZS )CygwinCCompilerz9Handles the Cygwin port of the GNU C compiler to Windows.cygwinz.oz.az.dll.az.dllzlib%s%szcyg%s%sz.exeFc                    s   t  ||| t \}}| d| d| d |tk	rL| d| d tjdd| _	tjdd	| _
| j	| _d
}| j| j	 d| j	 d| j
 d| j	 d| j d| d t | _d S )NzPython's GCC status: z (details: )zCPython's pyconfig.h doesn't seem to support your compiler. Reason: z>. Compiling may fail because of undefined preprocessor macros.ZCCgccZCXXzg++-sharedz -mcygwin -O -Wallz -mcygwin -mdll -O -Wallz	 -mcygwinz
 -mcygwin Zcompilercompiler_soZcompiler_cxxZ
linker_exeZ	linker_so)super__init__check_config_hZdebug_printCONFIG_H_OKwarnosenvirongetcccxx
linker_dllset_executablesr   dll_libraries)selfverbosedry_runforceZstatusZdetailsshared_option	__class__r   r   r$   V   s&    






zCygwinCCompiler.__init__c              
   C   s6   t jdtdd t  tdW  5 Q R  S Q R X d S )Nzgcc_version attribute of CygwinCCompiler is deprecated. Instead of returning actual gcc version a fixed value 11.2.0 is returned.   )
stacklevelz11.2.0)warningsr'   DeprecationWarningr   r   r0   r   r   r   gcc_versiont   s    zCygwinCCompiler.gcc_versionc              
   C   s   |dkrNz|  dd|d|g W q tk
rJ } zt|W 5 d}~X Y qX nNz"|  | j| |d|g |  W n* tk
r } zt|W 5 d}~X Y nX dS )z:Compiles the source by spawning GCC and windres if needed.).rc.resZwindresz-iz-oN)Zspawnr   r   r"   )r0   objsrcextZcc_argsextra_postargsZpp_optsmsgr   r   r   _compile   s    zCygwinCCompiler._compileNc                 C   s  t  |
p
g }
t  |pg }t  |p&g }|r8| t || j |dk	r|| jks`| jdkrtj	|d }tj
tj|\}}tj||d }dtj| dg}|D ]}|| q| t||fd|  || |	s|
d t| |||||||d|	|
||| dS )	zLink the objects.Nr   r   z.defzLIBRARY ZEXPORTSzwriting z-s)copyr'   _runtime_library_dirs_msgextendr/   Z
EXECUTABLEr-   r(   pathdirnamesplitextbasenamejoinappendZexecuter	   r
   link)r0   Ztarget_descZobjectsZoutput_filename
output_dirZ	librariesZlibrary_dirsZruntime_library_dirsZexport_symbolsdebugZextra_preargsrB   Z
build_tempZtarget_langZtemp_dirZdll_nameZdll_extensionZdef_filecontentsZsymr   r   r   rN      sN    



zCygwinCCompiler.linkc                 C   s   |  t g S N)r'   rF   r0   dirr   r   r   runtime_library_dir_option   s    
z*CygwinCCompiler.runtime_library_dir_optionc                    s   t j|}t |||S rR   )r(   rH   normcaser#   _make_out_path)r0   rO   Z	strip_dirZsrc_nameZnorm_src_namer5   r   r   rW      s    zCygwinCCompiler._make_out_pathc                    s   t  j fdddD S )z3
        Add support for rc and res files.
        c                    s   i | ]}|| j  qS r   )obj_extension).0rA   r;   r   r   
<dictcomp>  s      z2CygwinCCompiler.out_extensions.<locals>.<dictcomp>)r>   r=   )r#   out_extensionsr;   r5   r;   r   r[      s    zCygwinCCompiler.out_extensions)FFF)
NNNNNFNNNN)__name__
__module____qualname____doc__compiler_typerX   Zstatic_lib_extensionZshared_lib_extensionZdylib_lib_extensionZstatic_lib_formatZshared_lib_formatZdylib_lib_formatZexe_extensionr$   propertyr<   rD   rN   rU   rW   r[   __classcell__r   r   r5   r   r   I   s:   
          
Z	r   c                       s.   e Zd ZdZdZd fdd	Zdd Z  ZS )	Mingw32CCompilerz:Handles the Mingw32 port of the GNU C compiler to Windows.Zmingw32Fc                    sf   t  ||| d}t| jr&td| j| j d| j d| j d| j | j d| d d S )Nr    z1Cygwin gcc cannot be used with --compiler=mingw32z	 -O -Wallz -shared -O -Wallr   r!   )r#   r$   is_cygwinccr+   r   r.   r,   r-   )r0   r1   r2   r3   r4   r5   r   r   r$     s    



zMingw32CCompiler.__init__c                 C   s   t td S rR   )r   rF   rS   r   r   r   rU     s    z+Mingw32CCompiler.runtime_library_dir_option)FFF)r\   r]   r^   r_   r`   r$   rU   rb   r   r   r5   r   rc     s   rc   okznot okZ	uncertainc               
   C   s   ddl m}  dtjkrtdfS dtjkr0tdfS |  }zJt|jdd}d	}||krbt}d
}nt	}d}||d| d|fW S  t
k
r } ztd| d|j f W Y S d}~X Y nX dS )aw  Check if the current Python installation appears amenable to building
    extensions with GCC.

    Returns a tuple (status, details), where 'status' is one of the following
    constants:

    - CONFIG_H_OK: all is well, go ahead and compile
    - CONFIG_H_NOTOK: doesn't look good
    - CONFIG_H_UNCERTAIN: not sure -- unable to read pyconfig.h

    'details' is a human-readable string explaining the situation.

    Note there are two ways to conclude "OK": either 'sys.version' contains
    the string "GCC" (implying that this Python was built with GCC), or the
    installed "pyconfig.h" contains the string "__GNUC__".
    r   )	sysconfigZGCCzsys.version mentions 'GCC'ZClangzsys.version mentions 'Clang'zutf-8)encodingZ__GNUC__Zmentionszdoes not mentionr   zcouldn't read 'z': N)	distutilsrf   r   r   r&   Zget_config_h_filenamepathlibPathZ	read_textCONFIG_H_NOTOKOSErrorCONFIG_H_UNCERTAINstrerror)rf   fnZconfig_hZ	substringcodeZmention_inflectedexcr   r   r   r%   )  s"    

r%   c                 C   s"   t t| dg }| dS )zCTry to determine if the compiler that would be used is from cygwin.z-dumpmachines   cygwin)r   shlexsplitstripendswith)r+   Z
out_stringr   r   r   rd   Y  s    rd   )%r_   rE   r(   ri   r   rr   r   r9   
subprocessr   _collectionsr   errorsr   r   r   r   Z	file_utilr	   Zunixccompilerr
   r   r   r   leftZundefined_valuer   r   rF   r   rc   r&   rk   rm   r%   rd   Zget_versionsr   r   r   r   <module>   sL    ?0