
    
_d{                         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mZm	Z	m
Z
mZmZmZ d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 ddlmZ  ej<                  d      Zd Z  G d de      Z!y)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)DistutilsOptionErrorDistutilsSetupErrorCCompilerErrorDistutilsErrorCompileErrorDistutilsPlatformError)customize_compilerget_python_version)get_config_h_filename)newer_group)	Extension)get_platform)log   )
py37compat)	USER_BASEz3^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$c                      ddl m}   |         y )Nr   show_compilers)	ccompilerr   r   s    I/usr/lib/python3/dist-packages/setuptools/_distutils/command/build_ext.pyr   r   #   s    *    c                   &   e Zd ZdZdej
                  z  Zddddd e       z  fdd	d
dez   fddddddez   fddddddddddgZg dZ	ddde
fgZd  Zd! Zd" Zd# Zd$ Zd% Zd& Zd' Zd( Zej,                  d)        Zd* Zd+ Zd, Zd- Zd. Zd/ Zd0 Zd1 Zy)2	build_extz8build C/C++ extensions (compile/link to build directory)z (separated by '%s'))z
build-lib=bz(directory for compiled extension modules)zbuild-temp=tz1directory for temporary files (build by-products)z
plat-name=pz>platform name to cross-compile for, if supported (default: %s))inplaceiziignore build-lib and put compiled extensions into the source directory alongside your pure Python moduleszinclude-dirs=Iz.list of directories to search for header files)zdefine=DzC preprocessor macros to define)zundef=Uz!C preprocessor macros to undefine)z
libraries=lz!external C libraries to link withzlibrary-dirs=Lz.directories to search for external C libraries)zrpath=Rz7directories to search for shared C libraries at runtime)zlink-objects=Oz2extra explicit link objects to include in the link)debuggz'compile/link with debugging information)forcefz2forcibly build everything (ignore file timestamps))z	compiler=czspecify the compiler type)z	parallel=jznumber of parallel build jobs)swig-cppNz)make SWIG create C++ files (default is C))z
swig-opts=Nz!list of SWIG command line options)zswig=Nzpath to the SWIG executable)userNz#add user include, library and rpath)r    r)   r+   r/   r0   zhelp-compilerNzlist available compilersc                 *   d | _         d | _        d | _        d | _        d| _        d | _        d | _        d | _        d | _        d | _	        d | _
        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        y )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_optsr0   parallelselfs    r   initialize_optionszbuild_ext.initialize_optionsm   s     
 
 

		r   c           
         ddl m} | j                  ddddddd	d
       | j                  | j                  j
                  | _        | j                  j                  | _        |j                         }|j                  d      }| j                  | j                  j                  xs g | _	        t        | j                  t              r.| j                  j                  t        j                        | _	        t        j                   t        j"                  k7  rG| j                  j%                  t        j&                  j)                  t        j                   d             | j                  j+                  |j                  t        j&                  j                               ||k7  rB| j                  j+                  |j                  t        j&                  j                               | j-                  d       | j-                  d       | j.                  g | _        | j0                  g | _        nHt        | j0                  t              r.| j0                  j                  t        j                        | _        | j2                  g | _        nHt        | j2                  t              r.| j2                  j                  t        j                        | _        t        j4                  dk(  r| j0                  j%                  t        j&                  j)                  t        j                   d             t        j"                  t        j6                  k7  rG| j0                  j%                  t        j&                  j)                  t        j"                  d             | j8                  r0t        j&                  j)                  | j:                  d      | _        n/t        j&                  j)                  | j:                  d      | _        | j                  j%                  t        j&                  j=                  t?                            | j0                  j%                  t        j"                         | j@                  dk(  rd}n| j@                  dd  }t        j&                  j)                  t        j                   d      }|r t        j&                  j)                  ||      }| j0                  j%                  |       t        jB                  d d dk(  r||jD                  sU| j0                  j%                  t        j&                  j)                  t        j6                  ddtG               z   d             n| j0                  j%                  d       |jI                  d      rR|jD                  s+| j0                  j%                  |jI                  d             n| j0                  j%                  d       | jJ                  r2| jJ                  j                  d      }|D cg c]  }|d f c}| _%        | jL                  r | jL                  j                  d      | _&        | jN                  g | _'        n | jN                  j                  d!      | _'        | jP                  rt        j&                  j)                  tR        d      }t        j&                  j)                  tR        d      }	t        j&                  jU                  |      r| j                  j%                  |       t        j&                  jU                  |	      r6| j0                  j%                  |	       | j2                  j%                  |	       t        | jV                  t              r	 tY        | jV                        | _+        y y c c}w # tZ        $ r t]        d"      w xY w)#Nr   )	sysconfigbuild)r3   r3   )r5   r5   )r>   r>   )r)   r)   )r+   r+   )rB   rB   )r4   r4   r   )plat_specificincluder:   r=   ntlibsDebugReleasewin32   PCbuild   cygwinlibpythonconfig.Py_ENABLE_SHAREDLIBDIR,1 zparallel should be an integer)/	distutilsrG   set_undefined_optionsr6   distributionext_packageext_modulesr2   get_python_incr7   
isinstancestrsplitospathsepsysexec_prefixbase_exec_prefixappendpathjoinextendensure_string_listr:   r;   r<   nameprefixr)   r5   dirnamer   r4   platformpython_buildr   get_config_varr8   r9   rA   r0   r   isdirrB   int
ValueErrorr   )
rD   rG   
py_includeplat_py_includesuffixnew_libdefinessymboluser_includeuser_libs
             r   finalize_optionszbuild_ext.finalize_options   sr   '""&($$&		
 <<,,88DL++77 --/
#222C$ $ 1 1 > > D"Dd''- $ 1 1 7 7

 CD ??c222$$RWW\\#//9%MN 	  !1!1"''//!BCj($$_%:%:277??%KL,/ >>!DN$ "D))3/ $ 1 1 7 7

 CD::DJ

C())"**5DJ
 77d? $$RWW\\#//6%JK##szz1!!((c6J6JF)STzz"$'',,t"H"$'',,t	"J $$RWW__5J5L%MN$$S%9%9: ~~(  +ggll3??I>G'',,w7$$W- <<x'))!!((GGLL

E86H6J+JH !!((-
 ##$67))!!(()A)A()KL !!((- ;;kk'',G7>?VFC=?DK
 ::))#.DJ>>!DN!^^11#6DN 9977<<	9=Lww||Iu5Hww}}\*!!((6ww}}X&!!((2

!!(+dmmS)L #DMM 2 */ @4  L*+JKKLs   ^$^) )^>c                 P   ddl m} | j                  sy | j                  j	                         rc| j                  d      }| j                  j                  |j                         xs g        | j                  j                  |j                          || j                  | j                  | j                  | j                        | _        t!        | j                         t"        j$                  dk(  r<| j&                  t)               k7  r%| j                  j+                  | j&                         | j,                  %| j                  j/                  | j,                         | j0                  0| j0                  D ]!  \  }}| j                  j3                  ||       # | j4                  ,| j4                  D ]  }| j                  j7                  |        | j                  %| j                  j9                  | j                         | j                  %| j                  j;                  | j                         | j<                  %| j                  j?                  | j<                         | j@                  %| j                  jC                  | j@                         | jE                          y )Nr   )new_compiler
build_clib)r>   verbosedry_runr+   rK   )#r   r   r2   r_   has_c_librariesget_finalized_commandr:   rn   get_library_namesr;   rk   r   r>   r   r   r+   r   rf   rp   r4   r   
initializer7   set_include_dirsr8   define_macror9   undefine_macroset_librariesset_library_dirsr<   set_runtime_library_dirsr=   set_link_objectsbuild_extensions)rD   r   r   rp   valuemacros         r   runzbuild_ext.run  s   , 
 ,,.33LAJNN!!*">">"@"FBG$$Z%:%:; %]]LLLL**	
 	4==) 77d?t~~?MM$$T^^4 (MM**4+<+<=;;"#{{ 8e**478::! 4,,U34>>%MM''7(MM**4+<+<=::!MM224::>(MM**4+<+<= 	r   c                    t        |t              st        d      t        |      D ]  \  }}t        |t              rt        |t
              rt        |      dk7  rt        d      |\  }}t        j                  d|       t        |t              rt        j                  |      st        d      t        |t              st        d      t	        ||d         }dD ]#  }|j                  |      }|t        |||       % |j                  d
      |_        d|v rt        j                  d       |j                  d      }|rg |_        g |_        |D ]  }	t        |	t
              rt        |	      dv st        d      t        |	      dk(  r|j"                  j%                  |	d          Xt        |	      dk(  sg|j                   j%                  |	        |||<    y	)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 instancesr   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)sources)r7   r;   r:   extra_objectsextra_compile_argsextra_link_argsNr<   def_filez9'def_file' element of build info dict no longer supportedmacros)r   r   z9'macros' element of build info dict must be 1- or 2-tupler   r   )rc   listr   	enumerater   tuplelenr   warningrd   extension_name_rematchdictgetsetattrruntime_library_dirsdefine_macrosundef_macrosrk   )
rD   r2   r!   extext_name
build_infokeyvalr   r   s
             r   check_extensions_listzbuild_ext.check_extensions_list[  s    *d+%L   
+ J	 FAs#y) c5)SX])4 
 $' HjKK: 	 x-2C2I2I(2S)< 
 j$/)8  Hj&;<C 
+ !nnS)?Cc*
+ (2~~g'>C$Z'R  ^^H-F$&!#% # 	8E&ue4Uv9M14  5zQ((//a9Uq))007	8  JqMUJ	 r   c                     | j                  | j                         g }| j                  D ]  }|j                  |j                          |S N)r   r2   rn   r   )rD   	filenamesr   s      r   get_source_fileszbuild_ext.get_source_files  sF    ""4??3	 ?? 	*CS[[)	*r   c                     | j                  | j                         g }| j                  D ],  }|j                  | j                  |j                               . |S r   )r   r2   rk   get_ext_fullpathrp   )rD   outputsr   s      r   get_outputszbuild_ext.get_outputs  sP     	""4??3
 ?? 	<CNN400:;	<r   c                     | j                  | j                         | j                  r| j                          y | j	                          y r   )r   r2   rB   _build_extensions_parallel_build_extensions_serialrC   s    r   r   zbuild_ext.build_extensions  s3    ""4??3==++-))+r   c                    | j                   }| j                   du rt        j                         }	 ddlm} || j                          y  |      5 }| j                  D cg c]  }|j                  | j                  |        }}t        | j                  |      D ]/  \  }}| j                  |      5  |j                          d d d        1 	 d d d        y # t
        $ r d }Y w xY wc c}w # 1 sw Y   [xY w# 1 sw Y   y xY w)NTr   )ThreadPoolExecutor)max_workers)rB   rf   	cpu_countconcurrent.futuresr   ImportErrorr   r2   submitbuild_extensionzip_filter_build_errorsresult)rD   workersr   executorr   futuresfuts          r   r   z$build_ext._build_extensions_parallel  s    --==D llnG	= ?))+G4 	!FJoo?B 4 4c:G   9 !S..s3 !JJL! !!		! 	!  	G	! !	! 	!sG   C C:!#C)/C:3C.
C:C&%C&)C:.C73C::Dc                     | j                   D ]-  }| j                  |      5  | j                  |       d d d        / y # 1 sw Y   :xY wr   )r2   r   r   )rD   r   s     r   r   z"build_ext._build_extensions_serial  sK    ?? 	*C**3/ *$$S)* *	** *s	   >A	c              #      K   	 d  y # t         t        t        f$ rB}|j                  s | j	                  dj                  |j                  |             Y d }~y d }~ww xY ww)Nz"building extension "{}" failed: {})r   r   r	   optionalwarnformatrp   )rD   r   es      r   r   zbuild_ext._filter_build_errors  sR     	P= 	P<<II:AA#((ANOO	Ps*   A"	 A"A8AA"AA"c                    |j                   }|t        |t        t        f      st	        d|j
                  z        t        |      }| j                  |j
                        }||j                  z   }| j                  s.t        ||d      s!t        j                  d|j
                         y t        j                  d|j
                         | j                  ||      }|j                  xs g }|j                   d d  }|j"                  D ]  }|j%                  |f        | j&                  j)                  || j*                  ||j,                  | j                  ||j                        }|d d  | _        |j0                  r|j3                  |j0                         |j4                  xs g }|j6                  xs | j&                  j9                  |      }	| j&                  j;                  ||| j=                  |      |j>                  |j@                  || jC                  |      | j                  | j*                  |	
       y )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   r7   r)   extra_postargsdepends)r:   r;   r   r   export_symbolsr)   r5   target_lang)"r   rc   r   r   r   rp   sortedr   r   r+   r   r   r)   infoswig_sourcesr   r   r   rk   r>   compiler5   r7   _built_objectsr   rn   r   languagedetect_languagelink_shared_objectget_librariesr;   r   get_export_symbols)
rD   r   r   ext_pathr   
extra_argsr   r9   objectsr   s
             r   r   zbuild_ext.build_extension  s   ++?*WtUm"D%-/2xx8  /((2CKK'

k'8WEII<chhGHH.9
 ##GS1  ++1r
""1%%% 	$EMM5(#	$ --''))**%KK ( 
 &aj
 NN3,,-((.B
 <<I4==#@#@#I((((-))!$!9!9%2237**  	) 	
r   c                    g }g }i }| j                   rt        j                  d       | j                   sd| j                  v sd|j                  v rd}nd}|D ]k  }t        j
                  j                  |      \  }}	|	dk(  r1|j                  |dz   |z          |j                  |       |d   ||<   [|j                  |       m |s|S | j                  xs | j                         }
|
dg}|j                  | j                         | j                   r|j                  d       | j                  s"|j                  D ]  }|j                  |        |D ]5  }||   }t        j                  d	||       | j                  |d
||gz          7 |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_wrapz-pythonzswigging %s to %sz-o)r@   r   r   rA   rf   rl   splitextrk   r?   	find_swigrn   r   spawn)rD   r   	extensionnew_sourcesr   swig_targets
target_extsourcebaser   r?   swig_cmdotargets                 r   r   zbuild_ext.swig_sourcesI  s     ==KKIJ MM$..()---JJ 	+F''**62KT3d{""4'>J#>?##F+'22V$""6*	+ yy,DNN,)$'==OOF# ~~(( #"# # 	:F!&)FHH(&&9JJx4"889	:
 r   c                 $   t         j                  dk(  ryt         j                  dk(  rNdD ]H  }t         j                  j                  d|z  d      }t         j                  j	                  |      sF|c S  yt        dt         j                  z        )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.
        posixr?   rK   )z1.3z1.2z1.1z	c:\swig%szswig.exez>I don't know how to find (much less run) SWIG on platform '%s')rf   rp   rl   rm   isfiler
   )rD   versfns      r   r   zbuild_ext.find_swig  s    
 77gWW_ . "WW\\,"5zB77>>"%I"
 "(#%'WW- r   c                    | j                  |      }|j                  d      }| j                  |d         }| j                  sMt	        j
                  j                  |dd |gz    }t        j
                  j                  | j                  |      S d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).
        rW   r   Nr   build_py)get_ext_fullnamere   get_ext_filenamer    rf   rl   rm   r3   r   abspathget_package_dir)rD   r   fullnamemodpathfilenamer6   r   package_dirs           r   r   zbuild_ext.get_ext_fullpath  s     ((2..%((5|| ww||WSb\XJ%>?H77<<99 ((71R=)--j9ggooh&>&>w&GH ww||K22r   c                 B    | j                   |S | j                   dz   |z   S )zSReturns the fullname of a given extension name.

        Adds the `package.` prefixrW   )r6   )rD   r   s     r   r   zbuild_ext.get_ext_fullname  s'     <<O<<#%00r   c                 ~    ddl m} |j                  d      } |d      }t        j                  j
                  | |z   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   ru   rW   
EXT_SUFFIX)rG   ru   re   rf   rl   rm   )rD   r   ru   r   
ext_suffixs        r   r   zbuild_ext.get_ext_filename  s7    
 	/>>#&#L1
ww||X&33r   c                 d   |j                   j                  d      d   }	 |j                  d       d|z   }d	|z   }||j                  vr|j                  j                  |       |j                  S # t        $ r6 d|j                  d      j	                  dd      j                  d      z   }Y xw xY w)
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.
        rW   r   ascii_U_punycode   -   _PyInit)rp   re   encodeUnicodeEncodeErrorreplacedecoder   rk   )rD   r   rp   r{   initfunc_names        r   r   zbuild_ext.get_export_symbols  s     xx~~c"2&	  KK  4ZF 6) 2 22%%m4!!! " 	XDKK
3;;D$GNNwWWF	Xs   A0 0<B/.B/c                 F   t         j                  dk(  rkddlm} t	        | j
                  |      sd}| j                  r|dz   }|t         j                  dz	  t         j                  dz	  dz  fz  }|j                  |gz   S dd	l	m
} d
} |d      rVt        t         d      rd}nCt         j                  dk(  rd}n-dt        j                  v r |d      dk7  rd}n |d      dk(  rd}|r |d      }|j                  d|z   gz   S |j                  t        j                         z   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).
        rO   r   )MSVCCompilerz
python%d%d_d         r  FrX   getandroidapilevelTrS   _PYTHON_HOST_PLATFORMANDROID_API_LEVELr   MACHDEP	LDVERSIONrU   )rh   rs   _msvccompilerr  rc   r>   r)   
hexversionr:   rG   ru   hasattrrf   environr   	pythonlib)rD   r   r  templater#  ru   link_libpython	ldversions           r   r   zbuild_ext.get_libraries  s    <<7"4dmm\:'::'$H$NNb(^^r)T1( 	 }}	{22 3"N013 45%)N\\X-%)N,

:%&9:a?)-'	2h>)-*;7	}}9(<'===}}z33555r   ) __name__
__module____qualname__descriptionrf   rg   sep_byr   user_optionsboolean_optionsr   help_optionsrE   r   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r    r   r   r   r   )   s2   LK( $bjj0FGQ*n-	
	
 <vE	

 	<<@<vE	

 	STAL7;GA6=K&LP HO 
$ :NKL0RLh@ DY v,!**
 P PO
b7r2361	4"*56r   r   )"__doc__r/  rf   rerh   corer   errorsr   r   r   r   r	   r
   rG   r   r   r   dep_utilr   r   r   utilr   distutils._logr    r   siter   r   r   r   r   r1  r   r   <module>r;     sf     	 	 
   ? - " !     BJJUV k6 k6r   