
(h{                 @   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 Td d l m	 Z	 m
 Z
 d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z e j d  Z d d   Z Gd d   d e  Z d S)zdistutils.command.build_ext

Implements the Distutils 'build_ext' command, for building extension
modules (currently limited to C extensions, should accommodate C++
extensions ASAP).    N)Command)*)customize_compilerget_python_version)get_config_h_filename)newer_group)	Extension)get_platform)log)	USER_BASEz3^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$c              C   s   d d l  m }  |    d  S)Nr   )show_compilers)distutils.ccompilerr   )r    r   M/tmp/pip-build-kmztzv_d/setuptools/setuptools/_distutils/command/build_ext.pyr      s    r   c               @   s  e  Z d  Z d Z d e j Z d` da d	 d
 d e   f d d d d f d d d e f db dc dd d d d e f de df dg dh di dj dk dl dm dn g Z d d% d( d1 d8 g Z	 d: d2 d; e
 f g Z d< d=   Z d> d?   Z d@ dA   Z dB dC   Z dD dE   Z dF dG   Z dH dI   Z dJ dK   Z dL dM   Z e j dN dO    Z dP dQ   Z dR dS   Z dT dU   Z dV dW   Z dX dY   Z dZ d[   Z d\ d]   Z d^ d_   Z d2 S)o	build_extz8build C/C++ extensions (compile/link to build directory)z (separated by '%s')
build-lib=b(directory for compiled extension modulesbuild-temp=t1directory for temporary files (build by-products)z
plat-name=pz>platform name to cross-compile for, if supported (default: %s)inplaceiz=ignore build-lib and put compiled extensions into the source z,directory alongside your pure Python moduleszinclude-dirs=Iz.list of directories to search for header filesdefine=DC preprocessor macros to defineundef=U!C preprocessor macros to undefine
libraries=l!external C libraries to link withzlibrary-dirs=Lz.directories to search for external C librariesrpath=R7directories to search for shared C libraries at runtimelink-objects=O2extra explicit link objects to include in the linkdebugg'compile/link with debugging informationforcef2forcibly build everything (ignore file timestamps)	compiler=cspecify the compiler type	parallel=jnumber of parallel build jobsswig-cppN)make SWIG create C++ files (default is C)
swig-opts=!list of SWIG command line optionsswig=path to the SWIG executableuser#add user include, library and rpathzhelp-compilerzlist available compilersc             C   s   d  |  _  d  |  _ d  |  _ d  |  _ d |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _	 d  |  _
 d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  S)Nr   )
extensions	build_lib	plat_name
build_tempr   packageinclude_dirsdefineundef	librarieslibrary_dirsrpathlink_objectsr+   r.   compilerswigswig_cpp	swig_optsr=   parallel)selfr   r   r   initialize_optionsj   s*    																				zbuild_ext.initialize_optionsc       
      C   sw  d d l  m } |  j d d& d' d( d) d* d+ d,  |  j d  k rP |  j j |  _ |  j j |  _ | j   } | j d d  } |  j	 d  k r |  j j	 p g  |  _	 t
 |  j	 t  r |  j	 j t j  |  _	 t j t j k r |  j	 j t j j t j d   |  j	 j | j t j j   | | k rI|  j	 j | j t j j   |  j d  |  j d  |  j d  k r{g  |  _ |  j d  k rg  |  _ n* t
 |  j t  r|  j j t j  |  _ |  j d  k rg  |  _ n* t
 |  j t  r|  j j t j  |  _ t j d k ra|  j j t j j t j d   t j t j k rj|  j j t j j t j d   |  j rt j j |  j d  |  _ n t j j |  j d  |  _ |  j	 j t j j t     t  t d d   } | r|  j j |  |  j! d k rd } n |  j! d d   } t j j t j d  } | rQt j j | |  } |  j j |  t j" d  d  d k rt j# j$ t j j t j d   r|  j j t j j t j d d t%   d   n |  j j d  | j& d  r$| j' s|  j j | j& d    n |  j j d  |  j( rU|  j( j d!  } d" d#   | D |  _( |  j) rs|  j) j d!  |  _) |  j* d  k rg  |  _* n |  j* j d$  |  _* |  j+ r*t j j t, d  } t j j t, d  }	 t j j- |  r|  j	 j |  t j j- |	  r*|  j j |	  |  j j |	  t
 |  j. t  rsy t/ |  j.  |  _. Wn t0 k
 rrt1 d%   Yn Xd  S)-Nr   )	sysconfigbuildr@   rB   rK   r+   r.   rO   rA   plat_specific   includerG   rJ   ntZlibsZDebugZRelease_homewin32   ZPCbuild   cygwinbinlibpythonconfig.Py_ENABLE_SHAREDLIBDIR,c             S   s   g  |  ] } | d  f  q S)1r   ).0symbolr   r   r   
<listcomp>   s   	 z.build_ext.finalize_options.<locals>.<listcomp> zparallel should be an integer)	build_librj   )
build_temprk   )compilerrl   )debugrm   )forcern   )parallelro   )	plat_namerp   )2	distutilsrR   set_undefined_optionsrC   distributionext_packageext_modulesr?   get_python_incrD   
isinstancestrsplitospathsepsysexec_prefixbase_exec_prefixappendpathjoinextendensure_string_listrG   rH   rI   nameprefixr+   rB   dirnamer   getattrrA   platform
executable
startswithr   get_config_varpython_buildrE   rF   rN   r=   r   isdirrO   int
ValueErrorDistutilsOptionError)
rP   rR   Z
py_includeZplat_py_include	_sys_homesuffixZnew_libZdefinesZuser_includeZuser_libr   r   r   finalize_options   s    	"		""		$
				zbuild_ext.finalize_optionsc          	   C   s  d d l  m } |  j s d  S|  j j   rj |  j d  } |  j j | j   pS g   |  j	 j
 | j  | d |  j d |  j d |  j d |  j  |  _ t |  j  t j d k r |  j t   k r |  j j |  j  |  j d  k	 r |  j j |  j  |  j d  k	 r9x* |  j D] \ } } |  j j | |  qW|  j d  k	 rlx! |  j D] } |  j j |  qRW|  j d  k	 r|  j j |  j  |  j	 d  k	 r|  j j |  j	  |  j d  k	 r|  j j |  j  |  j  d  k	 r|  j j! |  j   |  j"   d  S)	Nr   )new_compiler
build_clibrK   verbosedry_runr.   rW   )#r   r   r?   rs   has_c_librariesget_finalized_commandrG   r   Zget_library_namesrH   r   r   rK   r   r   r.   r   rz   r   rA   r	   Z
initializerD   Zset_include_dirsrE   Zdefine_macrorF   Zundefine_macroZset_librariesZset_library_dirsrI   Zset_runtime_library_dirsrJ   Zset_link_objectsbuild_extensions)rP   r   r   r   valuemacror   r   r   run  s>    			!zbuild_ext.runc       
      C   s  t  | t  s t d   xt |  D]\ } } t  | t  rF q( t  | t  sh t |  d k rt t d   | \ } } t j d |  t  | t	  o t
 j |  s t d   t  | t  s t d   t | | d  } x9 d D]1 } | j |  } | d k	 r t | | |  q W| j d  | _ d | k rOt j d  | j d  } | rg  | _ g  | _ x | D]~ }	 t  |	 t  ot |	  d k st d   t |	  d k r| j j |	 d  q}t |	  d k r}| j j |	  q}W| | | <q( Wd S)a  Ensure that the list of extensions (presumably provided as a
        command option 'extensions') is valid, i.e. it is a list of
        Extension objects.  We also support the old-style list of 2-tuples,
        where the tuples are (ext_name, build_info), which are converted to
        Extension instances here.

        Raise DistutilsSetupError if the structure is invalid anywhere;
        just returns otherwise.
        z:'ext_modules' option must be a list of Extension instances   zMeach element of 'ext_modules' option must be an Extension instance or 2-tuplezvold-style (ext_name, build_info) tuple found in ext_modules for extension '%s' -- please convert to Extension instancezRfirst element of each tuple in 'ext_modules' must be the extension name (a string)zOsecond element of each tuple in 'ext_modules' must be a dictionary (build info)sourcesrD   rH   rG   extra_objectsextra_compile_argsextra_link_argsNrI   Zdef_filez9'def_file' element of build info dict no longer supportedmacrosrU   z9'macros' element of build info dict must be 1- or 2-tupler   )zinclude_dirszlibrary_dirsz	librarieszextra_objectszextra_compile_argszextra_link_args)rU   r   )rw   listDistutilsSetupError	enumerater   tuplelenr
   warnrx   extension_name_rematchdictgetsetattrruntime_library_dirsdefine_macrosundef_macrosr   )
rP   r?   r   extext_nameZ
build_infokeyvalr   r   r   r   r   check_extensions_listV  sT    
	"				 
		!	zbuild_ext.check_extensions_listc             C   s>   |  j  |  j  g  } x! |  j D] } | j | j  q  W| S)N)r   r?   r   r   )rP   	filenamesr   r   r   r   get_source_files  s
    zbuild_ext.get_source_filesc             C   sG   |  j  |  j  g  } x* |  j D] } | j |  j | j   q  W| S)N)r   r?   r   get_ext_fullpathr   )rP   outputsr   r   r   r   get_outputs  s
    zbuild_ext.get_outputsc             C   s4   |  j  |  j  |  j r& |  j   n
 |  j   d  S)N)r   r?   rO   _build_extensions_parallel_build_extensions_serial)rP   r   r   r   r     s    	zbuild_ext.build_extensionsc                s    j  }  j  d k r$ t j   } y d d l m } Wn t k
 rR d  } Yn X| d  k rm  j   d  S| d |  j      f d d    j D } xA t  j |  D]- \ } }  j	 |   | j
   Wd  QRXq WWd  QRXd  S)NTr   )ThreadPoolExecutormax_workersc                s%   g  |  ] }   j   j |   q Sr   )submitbuild_extension)rf   r   )executorrP   r   r   rh     s   	z8build_ext._build_extensions_parallel.<locals>.<listcomp>)rO   rz   	cpu_countconcurrent.futuresr   ImportErrorr   r?   zip_filter_build_errorsresult)rP   workersr   futuresr   Zfutr   )r   rP   r   r     s     	
z$build_ext._build_extensions_parallelc          
   C   s<   x5 |  j  D]* } |  j |   |  j |  Wd  QRXq
 Wd  S)N)r?   r   r   )rP   r   r   r   r   r     s    z"build_ext._build_extensions_serialc             c   sd   y	 d  VWnT t  t t f k
 r_ } z+ | j s3   |  j d | j | f  WYd  d  } ~ Xn Xd  S)Nz"building extension "%s" failed: %s)CCompilerErrorDistutilsErrorCompileErroroptionalr   r   )rP   r   er   r   r   r     s    			zbuild_ext._filter_build_errorsc       
      C   s  | j  } | d  k s+ t | t t f  r> t d | j   t |  } |  j | j  } | | j } |  j	 p t
 | | d  s t j d | j  d  St j d | j  |  j | |  } | j p g  } | j d  d   } x! | j D] } | j | f  q W|  j j | d |  j d | d | j d |  j d	 | d
 | j } | d  d   |  _ | j rt| j | j  | j pg  } | j p|  j j |  }	 |  j j | | d |  j |  d | j d | j  d	 | d |  j! |  d |  j d |  j d |	 d  S)Nzjin 'ext_modules' option (extension '%s'), 'sources' must be present and must be a list of source filenamesnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extension
output_dirr   rD   r+   Zextra_postargsdependsrG   rH   r   export_symbolsrB   Ztarget_lang)"r   rw   r   r   r   r   sortedr   r   r.   r   r
   r+   infoswig_sourcesr   r   r   r   rK   compilerB   rD   Z_built_objectsr   r   r   languageZdetect_languageZlink_shared_objectget_librariesrH   r   get_export_symbols)
rP   r   r   ext_pathr   
extra_argsr   rF   Zobjectsr   r   r   r   r     sN    	"										zbuild_ext.build_extensionc             C   s  g  } g  } i  } |  j  r( t j d  |  j  sO d |  j k sO d | j k rX d } n d } xr | D]j } t j j |  \ } }	 |	 d k r | j | d |  | j |  | d | | <qe | j |  qe W| s | S|  j p |  j	   }
 |
 d g } | j
 |  j  |  j  r$| j d  |  j sNx | j D] } | j |  q7WxE | D]= } | | } t j d	 | |  |  j | d
 | | g  qUW| S)zWalk the list of source files in 'sources', looking for SWIG
        interface (.i) files.  Run SWIG on all that are found, and
        return a modified 'sources' list with SWIG source files replaced
        by the generated C (or C++) files.
        z/--swig-cpp is deprecated - use --swig-opts=-c++z-c++z.cppz.cz.i_wraprU   z-pythonzswigging %s to %sz-o)rM   r
   r   rN   rz   r   splitextr   rL   	find_swigr   r   spawn)rP   r   	extensionZnew_sourcesr   Zswig_targetsZ
target_extsourcebaser   rL   Zswig_cmdotargetr   r   r   r   2  s>    				
zbuild_ext.swig_sourcesc             C   s   t  j d k r d St  j d k ri xW d D]5 } t  j j d | d  } t  j j |  r) | Sq) Wd Sn t d	 t  j   d
 S)zReturn the name of the SWIG executable.  On Unix, this is
        just "swig" -- it should be in the PATH.  Tries a bit harder on
        Windows.
        posixrL   rW   1.31.21.1z	c:\swig%szswig.exez>I don't know how to find (much less run) SWIG on platform '%s'N)r   r   r   )rz   r   r   r   isfileDistutilsPlatformError)rP   versfnr   r   r   r   h  s    zbuild_ext.find_swigc             C   s   |  j  |  } | j d  } |  j | d  } |  j ss t j j | d d  | g   } t j j |  j |  Sd j | d d   } |  j d  } t j j	 | j
 |   } t j j | |  S)	zReturns the path of the filename for a given extension.

        The file is located in `build_lib` or directly in the package
        (inplace option).
        ra   rU   Nr   build_pyr   r   r   )get_ext_fullnamery   get_ext_filenamer   rz   r   r   r@   r   abspathZget_package_dir)rP   r   fullnameZmodpathfilenamerC   r   package_dirr   r   r   r     s    	#zbuild_ext.get_ext_fullpathc             C   s&   |  j  d k r | S|  j  d | Sd S)zSReturns the fullname of a given extension name.

        Adds the `package.` prefixNra   )rC   )rP   r   r   r   r   r     s    zbuild_ext.get_ext_fullnamec             C   s?   d d l  m } | j d  } | d  } t j j |   | S)zConvert the name of an extension (eg. "foo.bar") into the name
        of the file from which it will be loaded (eg. "foo/bar.so", or
        "foo\bar.pyd").
        r   )r   ra   
EXT_SUFFIX)distutils.sysconfigr   ry   rz   r   r   )rP   r   r   r   
ext_suffixr   r   r   r     s    zbuild_ext.get_ext_filenamec             C   s   d | j  j d  d
 } y | j d  Wn: t k
 rg d | j d  j d d  j d  } Yn Xd	 | } | | j k r | j j |  | j S)a  Return the list of symbols that a shared extension has to
        export.  This either uses 'ext.export_symbols' or, if it's not
        provided, "PyInit_" + module_name.  Only relevant on Windows, where
        the .pyd file (DLL) must export the module "PyInit_" function.
        _ra   rU   asciir   punycode   -   _ZPyInitr   )r   ry   encodeUnicodeEncodeErrorreplacedecoder   r   )rP   r   r   Zinitfunc_namer   r   r   r     s    -
zbuild_ext.get_export_symbolsc             C   s<  t  j d k r} d d l m } t |  j |  s5d } |  j rJ | d } | t  j d ?t  j d ?d @f } | j | g Sn d d	 l	 m
 } d
 } | d  rt t  d  r d } nZ t  j d k r d } nB d t j k r| d  d k r d } n | d  d k rd } | r5| d  } | j d | g S| j S)zReturn the list of libraries to link against when building a
        shared extension.  On most platforms, this is just 'ext.libraries';
        on Windows, we add the Python library (eg. python20.dll).
        rY   r   )MSVCCompilerz
python%d%dZ_d         )r   Frb   ZgetandroidapilevelTr\   _PYTHON_HOST_PLATFORMZANDROID_API_LEVELMACHDEP	LDVERSIONr_   )r|   r   Zdistutils._msvccompilerr  rw   rK   r+   
hexversionrG   r   r   hasattrrz   environ)rP   r   r  templateZ	pythonlibr   Zlink_libpythonZ	ldversionr   r   r   r     s2    
	

			zbuild_ext.get_libraries)r   r   r   )r   r   r   )r   r   r   )r   r   r    )r!   r"   r#   )r%   r&   r'   )r(   r)   r*   )zdebugr,   r-   )zforcer/   r0   )r1   r2   r3   )r4   r5   r6   )r7   Nr8   )r9   Nr:   )r;   Nr<   )zuserNr>   ) __name__
__module____qualname__descriptionrz   r{   Zsep_byr	   user_optionsboolean_optionsr   help_optionsrQ   r   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r   r   r   r   r   r   !   s~     

   
          	@N	
L6	
r   )__doc__r  rz   rer|   distutils.corer   Zdistutils.errorsr   r   r   r   distutils.dep_utilr   distutils.extensionr   distutils.utilr	   rq   r
   siter   r   r   r   r   r   r   r   r   <module>   s"   
	