o
    @aF                     @   s   d Z ddlmZ ddlmZ zddlmZ W n ey%   ddlmZ Y nw ddl	Z
ddl
Z
ddlmZ ddlmZ G dd deZG d	d
 d
eZG dd deeedZG dd deZdS )z|
This modules contains a distutils extension mechanism for Pythran
    * PythranExtension: is used as distutils's Extension
    N)defaultdict)Iterable)	build_ext)	Extensionc                       s    e Zd ZdZ fddZ  ZS )PythranBuildExtMixIna  Subclass of `distutils.command.build_ext.build_ext` which is required to
    build `PythranExtension` with the configured C++ compiler. It may also be
    subclassed if you want to combine with another build_ext class (NumPy,
    Cython implementations).

    c                    sN  t f  fdd} fdd}d d d d d d d d}t| D ]}t| j|r0|| j|||< q||= qtdd d urO|D ]}t| j|rN|| j|j q>d tdd d urxzdd lm} |j	fd	d
}||_	W n	 t
yw   Y nw tjdd D ]}	dD ]}
z	 t| j|
|	 q ttfy   Y qw qt| jdrtt}t| jjdd  D ]\}}	| jj| dkr||	 |d  qd|v rd|v r|d D ]}d| jj|< qz(tt| W | D ]}|| j|||  qd urdd lm} |_	S S | D ]}|| j|||  qd ur&dd lm} |_	w )Nc                    s*   t | |}t|trt| s|d S |S Nr   )getattr
isinstancer   )objkeyvarStringTypes ./usr/lib/python3/dist-packages/pythran/dist.py	get_value    s   
z7PythranBuildExtMixIn.build_extension.<locals>.get_valuec                    s:   t | |}t|trt| s||d< d S t| || d S r   )r   r	   r   setattr)r
   r   valuer   r   r   r   	set_value'   s   
z7PythranBuildExtMixIn.build_extension.<locals>.set_value)ZpreprocessorZcompiler_cxxcompiler_socompilerZ
linker_exe	linker_socccxxr   r   c                    s$   | dkr j } | g|R i |S )Nzcl.exe)r   )exeargskwargs)extfind_exer   r   	_find_exeM   s   z7PythranBuildExtMixIn.build_extension.<locals>._find_exer   Zignoreflags)r   r   Tr      z-archZx86_64Zi386)strlistkeyshasattrr   r   r   Zdistutils._msvccompilerZ_msvccompilerr   ImportErrorcfggetsplitremoveAttributeError
ValueErrorr   	enumerater   appendsuperr   build_extension)selfr   r   r   prevr   compZmsvcr   flagtargetZarchsi	__class__)r   r   r   r   r/      s|   	
	

z$PythranBuildExtMixIn.build_extension)__name__
__module____qualname____doc__r/   __classcell__r   r   r6   r   r      s    r   c                   @   s   e Zd Zdd ZdS )PythranBuildExtMetac                 C   s   G dd dt |}|S )Nc                   @      e Zd ZdS )z8PythranBuildExtMeta.__getitem__.<locals>.PythranBuildExtNr8   r9   r:   r   r   r   r   PythranBuildExt|       r@   )r   )r0   baser@   r   r   r   __getitem__{   s   zPythranBuildExtMeta.__getitem__N)r8   r9   r:   rC   r   r   r   r   r=   y   s    r=   c                   @   r>   )r@   Nr?   r   r   r   r   r@      rA   r@   )	metaclassc                   @   s2   e Zd ZdZdd Zedd Zejdd ZdS )PythranExtensionz
    Description of a Pythran extension

    Similar to distutils.core.Extension except that the sources are .py files
    They must be processable by pythran, of course.

    The compilation process ends up in a native Python module.
    c                 O   sd   t jdddi|}|dd | _|dd | _|| _tj| ||g|R i | | jdd  d S )NZpythonTr   r   sourcesr   )	r&   Zmake_extensionpopr   r   _sourcesr   __init____dict__)r0   namerF   r   r   Zcfg_extr   r   r   rI      s   zPythranExtension.__init__c                 C   s   dd l m} g }| jD ]T}tj|\}}|dkr|| q|d }tj|rZtj|r;tj|tj|k rZd| j	v rNtj| j	d dd  }n| j	}|j
|||dd || q|S )	Nr   z.pyz.cpp.r    T)Zcpponly)Zpythran.toolchainZ	toolchainrH   ospathsplitextr-   existsgetmtimerK   Zcompile_pythranfile)r0   ZtcZcxx_sourcessourcerB   r   Zoutput_fileZmodule_namer   r   r   rF      s&   


zPythranExtension.sourcesc                 C   s
   || _ d S )N)rH   )r0   rF   r   r   r   rF      s   
N)r8   r9   r:   r;   rI   propertyrF   setterr   r   r   r   rE      s    	
rE   )r;   Zpythran.configZconfigr&   collectionsr   Zcollections.abcr   r%   os.pathrN   Zdistutils.command.build_extr   ZLegacyBuildExtZnumpy.distutils.extensionr   objectr   typer=   r@   rE   r   r   r   r   <module>   s     d	