U
    {h/                     @   s  d dl Z d dlZd dlZd dlmZ ddlmZmZ ddlm	Z	 ddl
mZ zd dlZee d ZW n ek
r   dZd ZY nX G d	d
 d
Zdd ZedddZedddZdd Zdd Zd!ddZdd Zdd Zdd Zd"ddZed kre  dS )#    N)defaultdict   )	cythonizeextended_iglob   )is_package_dir)Optionsg      ?c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
	_FakePoolc                 C   s   t ||D ]}q
d S N)map)selffuncargs_ r   :/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Build/Cythonize.py	map_async   s    z_FakePool.map_asyncc                 C   s   d S r
   r   r   r   r   r   close   s    z_FakePool.closec                 C   s   d S r
   r   r   r   r   r   	terminate   s    z_FakePool.terminatec                 C   s   d S r
   r   r   r   r   r   join   s    z_FakePool.joinN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r	      s   r	   c                 C   s>   t j| \}}t|r6t j|\}}d||f }q||fS )Nz%s/%s)ospathsplitr   )r   base_dirpackage_pathparentr   r   r   find_package_base!   s
    r    )returnc                 C   s4   t tjjt| }t||}tt| |j	 d S r
   )
r   r   r   abspathr   _cython_compile_files_buildlistitemsparallel)path_patternoptions	all_pathsext_modules_by_basedirr   r   r   cython_compile)   s    
r,   c                 C   s   t t}| D ]}|jr>|}tj|r.t|rBtj|}qnd }tj|rbtj|ddg}n|g}t	|f|j
|j|j|j|j|j|j|j|jd	|j}|r|jr|| | qt|S )Nz**z
*.{py,pyx})	ZnthreadsZexclude_failuresexcludeZcompiler_directivescompile_time_envforcequietdepfilelanguage)r   r%   build_inplacer   r   isdirr   dirnamer   r   r'   
keep_goingexcludes
directivesr.   r/   r0   r1   r2   r)   buildextenddict)r*   r)   Zext_modules_to_buildr   r   pathsext_modulesr   r   r   r#   /   s8    
r#   c                 C   s   t dd | D }|sd S |dks*|dk r:t| d  d S zt|}W n tk
rb   t }Y nX z|tdd | D  W n   |   Y nX |  |	  d S )Nc                 s   s   | ]\}}t |V  qd S r
   )len).0r   modulesr   r   r   	<genexpr>U   s     z_build.<locals>.<genexpr>r   r   r   c                 S   s$   g | ]\}}|D ]}||gfqqS r   r   )r?   r   r@   extr   r   r   
<listcomp>a   s      z_build.<locals>.<listcomp>)
sumrun_distutilsmultiprocessingZPoolOSErrorr	   r   r   r   r   )r=   r'   Zmodcountpoolr   r   r   r$   T   s&    r$   c                 C   s   zddl m} W nB tk
rR   zddlm} W n tk
rL   tdY nX Y nX | \}}ddg}t }d }z:|rt| tj|d}|d|g |d||d	 W 5 |rt| |rtj|rt	
| X d S )
Nr   )setupzL'distutils' is not available. Please install 'setuptools' for binary builds.	build_ext-i)dirz--build-tempzsetup.py)script_namescript_argsr=   )distutils.corerI   ImportError
setuptoolsr   getcwdchdirr   r4   shutilrmtreetempfilemkdtempr:   )r   rI   r   r=   rN   cwdtemp_dirr   r   r   rE   k   s2    


rE   c              	      s   ddl m} || |||dd\}}ddddd	}d
d t| D   fdd}|  t|d@ dksnt|d |t|d  |d   }	}
}t| dt| d||	 d||
 d ||	d krtd|| d||	 d d S )Nr   )cymeit	   )repeatg&.>gư>gMbP?g      ?)ZnsecZusecZmsecsecc                 S   s   g | ]\}}||fqS r   r   )r?   unitscaler   r   r   rC      s     zbenchmark.<locals>.<listcomp>c                    s6    D ]\}}| |kr q"qt d| | dd| S )Nz"Timing is below nanoseconds: {t:f}z.3f )RuntimeError)tr_   r^   Zscalesr   r   format_time   s
    zbenchmark.<locals>.format_timer   r   z loops, best of z: z per loop (median: )   z3The timings are likely unreliable. The worst time (z6) was more than four times slower than the best time (z).)ZCython.Build.InlinerZ   reversedr&   sortr>   AssertionErrorprint)codeZ
setup_codeimport_moduler8   rZ   ZtimingsnumberZunitsrd   ZfastestZmedianZslowestr   rc   r   	benchmark   s    $,ro   c               
   C   s"  ddl m} m} ddlm}m}m} | |dd}|jddd	d
i t|dd |jddd	di t|dd |jddddi t|dd |jddddd dd |jdddddd |jddddd d |jd!d"d#dd$d d%d |jd&d'dd(d)d*d+ |jd,dd-d)d.d+ |jd/d0d1d2d3g d4d5 |jd6d7d8d9d d:d; |jd<d=d>d9d d?d; |jd@dAdBtd dCdD |jdEdFdBtd dGdD |jdHdIdJdKt	t
dLt
pdM dNdD |jdOdPdQd9d dRd; |jdSdTdUd9d dVd; |jdWdXd9d dYd; |jdZd[d\d9d d]d; |jd^d_d9d d`d; |jdadbd9dcdd |jdedfdg |S )hNr   )ArgumentParserRawDescriptionHelpFormatterr   )ParseDirectivesActionParseOptionsActionParseCompileTimeEnvActiona  Environment variables:
  CYTHON_FORCE_REGEN: if set to 1, forces cythonize to regenerate the output files regardless
        of modification times and changes.
  CYTHON_CACHE_DIR: the base directory containing Cython's caches.
  Environment variables accepted by setuptools are supported to configure the C compiler and build:
  https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#compiler-and-linker-options)formatter_classepilogz-Xz--directivezNAME=VALUE,...r8   zset a compiler directive)metavardestdefaulttypeactionhelpz-Ez--compile-time-envr.   z'set a compile time environment variablez-sz--optionz
NAME=VALUEr)   zset a cythonize optionz-2language_levelstore_constz#use Python 2 syntax mode by default)rx   r{   constry   r|   z-3   z#use Python 3 syntax mode by default)rx   r{   r   r|   z--3strz=use Python 3 syntax mode by default (deprecated alias for -3)z-+z--cplusr2   zc++zCompile as C++ rather than Cz-az
--annotatery   annotatez/Produce a colorized HTML version of the source.)r{   r   rx   r|   z--annotate-fullcZfullczZProduce a colorized HTML version of the source which includes entire generated C/C++-code.z-xz	--excludeZPATTERNr7   appendz2exclude certain file patterns from the compilation)rw   rx   r{   ry   r|   z-bz--buildr9   
store_truez2build extension modules using distutils/setuptools)rx   r{   ry   r|   rK   z	--inplacer3   zHbuild extension modules in place using distutils/setuptools (implies -b)z--timeitro   Z
CODESTRINGzabuild in place, then compile+run CODESTRING as benchmark in first module's namespace (implies -i))rx   rw   rz   ry   r|   z--setupbenchmark_setupz6use CODESTRING as pre-benchmark setup code for --benchz-jz
--parallelr'   Nz(run builds in N parallel jobs (default: r   rf   z-fz--forcer/   zforce recompilationz-qz--quietr0   z"be less verbose during compilationz	--lenientlenientzBincrease Python compatibility by ignoring some compile time errorsz-kz--keep-goingr6   z8compile as much as possible, ignore compilation failuresz--no-docstringsno_docstringszstrip docstringsz-Mz	--depfilez produce depfiles for the sources)r{   r|   sources*)nargs)argparserp   rq   ZCompiler.CmdLinerr   rs   rt   add_argumentstrintparallel_compiles)rp   rq   rr   rs   rt   parserr   r   r   create_args_parser   s    
  
  
    r   c                 C   sN   |  |\}}|j}|D ](}|dr6| d|  q|| q|`||fS )N-zunknown option )parse_known_argsr   
startswitherrorr   )r   r   r)   unknownr   optionr   r   r   parse_args_raw   s    
r   c                 C   s   t  }t|| \}} |jd k	r&d|_n| s4|d |jr@d|_td krNd|_|jrn|jdksbt	|j|j
d< |jrdt_dt_|jr|jt_|jrdt_|| fS )NTzno source files providedr   )r   r   Z3strr}   F)r   r   ro   r3   r   r9   rF   r'   r}   rj   r)   r   r   Zerror_on_unknown_namesZerror_on_uninitializedr   r   Z
docstrings)r   r   r)   r   r   r   
parse_args   s*    

r   c                 C   s  t | \}}g }|D ]R}dd t|D }|s\dd l}td|jd ||jd |d || qt	||}|r|j
rtt| |j |jd k	rd  }}	|r| \}}
|
r|
d j}	dd l}|d k	r|jd| t|j|j|	d |d k	r|j| d S )Nc                 S   s   g | ]}t j|qS r   )r   r   r"   )r?   pr   r   r   rC   "  s     zmain.<locals>.<listcomp>r   z#{}: No such file or directory: '{}')filer   )rm   )r   r   sysrk   formatargvstderrexitr:   r#   r9   r$   r%   r&   r'   ro   popitemnamer   insertr   remove)r   r)   r<   r*   r   Zexpanded_pathr   r+   r   rm   Zfirst_extensionsr   r   r   main  s8    



 
r   __main__)NNN)N)r   rT   rV   collectionsr   ZDependenciesr   r   ZUtilsr   ZCompilerr   rF   r   	cpu_countr   rP   r	   r    r;   r,   r#   r$   rE   ro   r   r   r   r   r   r   r   r   r   <module>   s2   
%
I
%
