
][\z                 @   s   d  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 k r d d l m Z e e    Z n  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_BASEnt)get_build_versionz3^[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   1/usr/lib/python3.4/distutils/command/build_ext.pyr      s    r   c               @   st  e  Z d  Z d Z d e j Z dW dX d	 d
 d e   f d d d d f d d d e f dY dZ d[ d d d e f d\ d] d^ d_ d` da db dc dd g Z d d% d( d. d5 g Z	 d7 d/ d8 e
 f g Z d9 d:   Z d; d<   Z d= d>   Z d? d@   Z dA dB   Z dC dD   Z dE dF   Z dG dH   Z dI dJ   Z dK dL   Z dM dN   Z dO dP   Z dQ dR   Z dS dT   Z dU dV   Z d/ S)e	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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  S)Nr   )
extensions	build_lib	plat_name
build_tempr   packageinclude_dirsdefineundef	librarieslibrary_dirsrpathlink_objectsr-   r0   compilerswigswig_cpp	swig_optsr<   )selfr   r   r   initialize_optionsi   s(    																			zbuild_ext.initialize_optionsc       
   	   C   s  d d l  m } |  j d d- d. d/ d0 d1 d2  |  j d  k rP |  j j |  _ n  |  j j |  _ | j   } | j d
 d  } |  j	 d  k r |  j j	 p g  |  _	 n  t
 |  j	 t  r |  j	 j t j  |  _	 n  t j t j k r|  j	 j t j j t j d   n  |  j	 j |  | | k r7|  j	 j |  n  |  j d  |  j d  k r_g  |  _ n  |  j d  k rzg  |  _ n- t
 |  j t  r|  j j t j  |  _ n  |  j d  k rg  |  _ n- t
 |  j t  r|  j j t j  |  _ n  t j d k r|  j j t j j t j d   t j t j k rW|  j j t j j t j d   n  |  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 |  n  t  d k rc|  j! d k rd } n |  j! d d   } t j j t j d  } | rPt j j | |  } n  |  j j |  qt  d k r|  j j t j j t j d d   qt  d k r|  j j t j j t j d d   q|  j j t j j t j d d   n  t j" d  d  d k s%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$   q|  j j d%  n  d& r| j& d'  r| j' s|  j j | j& d(   q|  j j d%  n  |  j( r|  j( j d)  } d* d+   | D |  _( n  |  j) r0|  j) j d)  |  _) n  |  j* d  k rKg  |  _* n |  j* j d,  |  _* |  j+ rt j j t, d  } t j j t, d"  }	 t j j- |  r|  j	 j |  n  t j j- |	  r|  j j |	  |  j j |	  qn  d  S)3Nr   )	sysconfigZbuildr?   rA   rJ   r-   r0   r@   Zplat_specific   ZincluderF   r   ZlibsZDebugZReleaseZ_home	   win32    ZPCbuild   ZPCzVS8.0   zVS7.1ZVC6   cygwinatheosbinlibZpythonZconfig.FPy_ENABLE_SHAREDZLIBDIR,c             S   s   g  |  ] } | d  f  q S)1r   ).0Zsymbolr   r   r   
<listcomp>   s   	 z.build_ext.finalize_options.<locals>.<listcomp> )	build_librd   )
build_tempre   )compilerrf   )debugrg   )forcerh   )	plat_nameri   ).	distutilsrP   Zset_undefined_optionsrB   distributionZext_packageZext_modulesr>   Zget_python_incrC   
isinstancestrsplitospathsepsysexec_prefixbase_exec_prefixappendpathjoinZensure_string_listrF   rG   rH   nameprefixr-   rA   dirnamer   getattrMSVC_VERSIONr@   platform
executable
startswithr   get_config_varZpython_buildrD   rE   rM   r<   r   isdir)
rN   rP   Z
py_includeZplat_py_includeZ	_sys_homesuffixZnew_libZdefinesZuser_includeZuser_libr   r   r   finalize_options   s    	%"%		2$
				zbuild_ext.finalize_optionsc          	   C   s  d d l  m } |  j s d  S|  j j   rm |  j d  } |  j j | j   pS g   |  j	 j
 | j  n  | d |  j d |  j d |  j d |  j  |  _ t |  j  t j d k r |  j t   k r |  j j |  j  n  |  j d  k	 r|  j j |  j  n  |  j d  k	 rEx- |  j D] \ } } |  j j | |  qWn  |  j d  k	 r{x$ |  j D] } |  j j |  q^Wn  |  j d  k	 r|  j j |  j  n  |  j	 d  k	 r|  j j |  j	  n  |  j d  k	 r|  j j |  j  n  |  j  d  k	 r|  j j! |  j   n  |  j"   d  S)	Nr   )new_compiler
build_clibrJ   verbosedry_runr0   r   )#r   r   r>   rk   Zhas_c_librariesget_finalized_commandrF   extendZget_library_namesrG   rt   r   rJ   r   r   r0   r   ro   rw   r@   r	   Z
initializerC   Zset_include_dirsrD   Zdefine_macrorE   Zundefine_macroZset_librariesZset_library_dirsrH   Zset_runtime_library_dirsrI   Zset_link_objectsbuild_extensions)rN   r   r   rw   valuemacror   r   r   run  s>    			!zbuild_ext.runc       
      C   s0  t  | t  s t d   n  xt |  D]\ } } t  | t  rL q+ n  t  | t  sn t |  d k r} t d   n  | \ } } t j d |  t  | t	  o t
 j |  s t d   n  t  | t  s t d   n  t | | d  } x< d D]4 } | j |  } | d k	 r t | | |  q q W| j d  | _ d | k ret j d  n  | j d  } | rg  | _ g  | _ x | D] }	 t  |	 t  ot |	  d k st d   n  t |	  d k r| j j |	 d  qt |	  d k r| j j |	  qqWn  | | | <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-tuplezuold-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)sourcesrC   rG   rF   extra_objectsextra_compile_argsextra_link_argsNrH   Zdef_filez9'def_file' element of build info dict no longer supportedmacrosrQ   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)rQ   r   )rl   listDistutilsSetupError	enumerater   tuplelenr
   warnrm   extension_name_rematchdictgetsetattrruntime_library_dirsdefine_macrosundef_macrosrt   )
rN   r>   r   extext_nameZ
build_infokeyvalr   r   r   r   r   check_extensions_listU  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   )rN   	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>   rt   get_ext_fullpathrw   )rN   Zoutputsr   r   r   r   get_outputs  s
    zbuild_ext.get_outputsc             C   s   |  j  |  j  x| |  j D]q } y |  j |  Wq t t t f k
 r } z. | j s^   n  |  j d | j | f  WYd  d  } ~ Xq Xq Wd  S)Nz"building extension "%s" failed: %s)	r   r>   build_extensionZCCompilerErrorZDistutilsErrorZCompileErrorZoptionalr   rw   )rN   r   er   r   r   r     s    		zbuild_ext.build_extensionsc       
      C   s	  | j  } | d  k s+ t | t t f  rA t d | j   n  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 rz| j | j  n  | 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 filenamesZnewerz$skipping '%s' extension (up-to-date)zbuilding '%s' extensionZ
output_dirr   rC   r-   Zextra_postargsdependsrF   rG   r   export_symbolsrA   Ztarget_lang)!r   rl   r   r   r   rw   r   r   r0   r   r
   r-   infoswig_sourcesr   r   r   rt   rJ   compilerA   rC   Z_built_objectsr   r   r   languageZdetect_languageZlink_shared_objectget_librariesrG   r   get_export_symbols)
rN   r   r   ext_pathr   Z
extra_argsr   rE   Zobjectsr   r   r   r   r     sN    	"										zbuild_ext.build_extensionc             C   s  g  } g  } i  } |  j  r+ t j d  n  |  j  sR d |  j k sR d | j k r[ d } n d } xr | D]j } t j j |  \ } }	 |	 d k r | j | d |  | j |  | d | | <qh | j |  qh W| s | S|  j p |  j	   }
 |
 d g } | j
 |  j  |  j  r*| j d  n  |  j sWx! | j D] } | j |  q=Wn  xE | D]= } | | } t j d	 | |  |  j | d
 | | g  q^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_wraprQ   z-pythonzswigging %s to %sz-o)rL   r
   r   rM   ro   ru   splitextrt   rK   	find_swigr   r   Zspawn)rN   r   	extensionZnew_sourcesr   Zswig_targetsZ
target_extsourcebaser   rK   Zswig_cmdotargetr   r   r   r     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.
        posixrK   r   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   )ro   rw   ru   rv   isfileZDistutilsPlatformError)rN   Zversfnr   r   r   r   O  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).
        r]   rQ   Nr   build_pyr   r   r   )get_ext_fullnamern   get_ext_filenamer   ro   ru   rv   r?   r   abspathZget_package_dir)rN   r   fullnameZmodpathfilenamerB   r   Zpackage_dirr   r   r   r   g  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.` prefixNr]   )rB   )rN   r   r   r   r   r     s    zbuild_ext.get_ext_fullnamec             C   so   d d l  m } | j d  } | d  } t j d k r[ |  j r[ t j j |   d | St 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   r]   Z
EXT_SUFFIXr   _d)distutils.sysconfigr   rn   ro   rw   r-   ru   rv   )rN   r   r   r   Z
ext_suffixr   r   r   r     s    zbuild_ext.get_ext_filenamec             C   sC   d | j  j d  d } | | j k r< | j j |  n  | 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.
        ZPyInit_r]   rQ   r   )rw   rn   r   rt   )rN   r   Zinitfunc_namer   r   r   r     s    zbuild_ext.get_export_symbolsc             C   s6  t  j d k r d d l m } t |  j |  s} d } |  j rM | d } n  | t  j d ?t  j d ?d @f } | j | g S| j Snt  j d	 d
  d k r d } | t  j d ?t  j d ?d @f } | j | g St  j d	 d
  d k rd d l	 m
 } d } | t  j d ?t  j d ?d @f } g  } xS | j d  j   D]< } | j d  rr| j | d d	   qC| j |  qCW| j | d g | St  j d k r| j St  j d	 d  d k r| j Sd d l	 m
 } d r+| j d  r+d j t  j d ?t  j d ?d @t  j  } | j | g S| j Sd	 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).
        rS   r   )MSVCCompilerz
python%d%dr            NrX   rY   zpython%d.%drZ   )rP   ZSHLIBSz-lr   mdarwin   ZaixFr^   zpython{}.{}{})rq   r|   distutils.msvccompilerr   rl   rJ   r-   
hexversionrF   rj   rP   r   rn   r~   rt   formatabiflags)rN   r   r   templateZ	pythonlibrP   Zextrar\   r   r   r   r     sJ    
	
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forcer1   r2   )r3   r4   r5   )r6   Nr7   )r8   Nr9   )r:   Nr;   )zuserNr=   )__name__
__module____qualname__Zdescriptionro   rp   Zsep_byr	   Zuser_optionsZboolean_optionsr   Zhelp_optionsrO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   "   st     

   
         	@N	R6	r   )__doc__rq   ro   reZdistutils.corer   Zdistutils.errorsr   r   r   r   Zdistutils.dep_utilr   Zdistutils.extensionr   Zdistutils.utilr	   rj   r
   Zsiter   rw   r   r   intr{   r   r   r   r   r   r   r   r   <module>   s    $
