
    ZQd{                         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T 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  ej        d          Zd Z G 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                  &    ddl m}   |              d S )Nr   show_compilers)distutils.ccompilerr   r   s    2/usr/lib/python3.11/distutils/command/build_ext.pyr   r      s(    222222N    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dS )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'   r(   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 | _        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selfs    r   initialize_optionszbuild_ext.initialize_optionsj   s     
 
 

		r   c           
      $   ddl m} |                     ddddddd	d
           | j        | j        j        | _        | j        j        | _        |                                }|                    d          }| j	        | j        j	        pg | _	        t          | j	        t                    r)| j	                            t          j                  | _	        t          j        t          j        k    rB| j	                            t          j                            t          j        d                     | j	                            |                    t          j        j                             ||k    r<| j	                            |                    t          j        j                             |                     d           |                     d           | j        g | _        | j        g | _        nCt          | j        t                    r)| j                            t          j                  | _        | j        g | _        nCt          | j        t                    r)| j                            t          j                  | _        t          j        dk    r| j                            t          j                            t          j        d                     t          j        t          j        k    rB| j                            t          j                            t          j        d                     | j        r+t          j                            | j        d          | _        n*t          j                            | j        d          | _        | j	                            t          j                            t?                                           tA          t          dd           }|r| j                            |           | j!        dk    rd}n| j!        dd          }t          j                            t          j        d          }|r t          j                            ||          }| j                            |           t          j"        d d         dk    rt          j#        $                    t          j                            t          j        d                    rT| j                            t          j                            t          j        ddtK                      z   d                     n| j                            d           	 | j(        r+| j(                            d"          }d# |D             | _(        | j)        r| j)                            d"          | _)        | j*        g | _*        n| j*                            d$          | _*        | j+        rt          j                            tX          d          }t          j                            tX          d          }	t          j        -                    |          r| j	                            |           t          j        -                    |	          r4| j                            |	           | j                            |	           t          | j.        t                    r9	 t_          | j.                  | _.        d S # t`          $ r tc          d%          w xY wd S )&Nr   )	sysconfigbuild)r+   r+   )r-   r-   )r6   r6   )r!   r!   )r#   r#   )r:   r:   )r,   r,      )plat_specificincluder2   r5   ntlibsDebugRelease_homewin32   PCbuild   cygwinbinlibpythonconfig.FPy_ENABLE_SHAREDLIBDIR,c                     g | ]}|d fS )1 ).0symbols     r   
<listcomp>z.build_ext.finalize_options.<locals>.<listcomp>   s    ???VFC=???r    zparallel should be an integer)2	distutilsr?   set_undefined_optionsr.   distributionext_packageext_modulesr*   get_python_incr/   
isinstancestrsplitospathsepsysexec_prefixbase_exec_prefixappendpathjoinextendensure_string_listr2   r3   r4   nameprefixr!   r-   dirnamer   getattrr,   platform
executable
startswithr   get_config_varpython_buildr0   r1   r9   r(   r   isdirr:   int
ValueErrorDistutilsOptionError)
r<   r?   
py_includeplat_py_include	_sys_homesuffixnew_libdefinesuser_includeuser_libs
             r   finalize_optionszbuild_ext.finalize_options   s    ''''''""7#=#?#;#5#5#;#=	% 	% 	% <,8DL+7 --//
#222CC$ $ 1 > D"Dd'-- 	D $ 1 7 7
 C CD ?c222$$RW\\#/9%M%MNNN 	  !1!1"'/!B!BCCCj(($$%%bgo668 8 8 	,,,/// >!DN$ "D)3// 	D $ 1 7 7
 C CD:DJJ
C(( 	6))"*55DJ
 7d?? $$RW\\#/6%J%JKKK#sz11!((c6JF)S)STTTz K"$',,t"H"H"$',,t	"J"J $$RW__5J5L5L%M%MNNNWd33I 4!((333 ~((  +gll3?I>>G 8',,w77$$W--- <x''~((cou)M)MNN .!((cj%6>ASAUAU6U6>*@ *@ A A A A
 !((---
	. ; 	@k'',,G??w???DK
 : 	/))#..DJ>!DNN!^11#66DN 9 	,7<<	9==Lw||Iu55Hw}}\** 7!((666w}}X&& ,!((222
!!(+++dmS)) 	LL #DM 2 2 L L L*+JKKKL	L 	Ls   [3 3\c                 v   ddl m} | j        sd S | j                                        rb|                     d          }| j                            |                                pg            | j	        
                    |j                    || j        | j        | j        | j                  | _        t!          | j                   t"          j        dk    r6| j        t)                      k    r| j                            | j                   | j        | j                            | j                   | j        (| j        D ] \  }}| j                            ||           !| j        $| j        D ]}| j                            |           | j        | j                            | j                   | j	        | j                            | j	                   | j        | j                            | j                   | j         | j        !                    | j                    | "                                 d S )Nr   )new_compiler
build_clib)r6   verbosedry_runr#   rD   )#r   r   r*   r_   has_c_librariesget_finalized_commandr2   rn   get_library_namesr3   rk   r   r6   r   r   r#   r   rf   rp   r,   r
   
initializer/   set_include_dirsr0   define_macror1   undefine_macroset_librariesset_library_dirsr4   set_runtime_library_dirsr5   set_link_objectsbuild_extensions)r<   r   r   rp   valuemacros         r   runzbuild_ext.run  s2   444444  	F
 ,,.. 	<33LAAJN!!*">">"@"@"FBGGG$$Z%:;;; %dm-1\-1\+/:7 7 7 	4=))) 7d??t~??M$$T^444 (M**4+<===;"!% 8 8u**47777:! 4 4,,U3333>%M''777(M**4+<===:!M224:>>>(M**4+<=== 	r   c                    t          |t                    st          d          t          |          D ]	\  }}t          |t                    rt          |t
                    rt          |          dk    rt          d          |\  }}t          j        d|           t          |t                    rt                              |          st          d          t          |t                    st          d          t	          ||d                   }dD ]*}|                    |          }|t          |||           +|                    d
          |_        d|v rt          j        d           |                    d          }|rg |_        g |_        |D ]}	t          |	t
                    rt          |	          dv st          d          t          |	          dk    r!|j                            |	d                    kt          |	          dk    r|j                            |	           |||<   d	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)sources)r/   r3   r2   extra_objectsextra_compile_argsextra_link_argsNr4   def_filez9'def_file' element of build info dict no longer supportedmacros)rA   r   z9'macros' element of build info dict must be 1- or 2-tuplerA   r   )rc   listDistutilsSetupError	enumerater	   tuplelenr   warnrd   extension_name_rematchdictgetsetattrruntime_library_dirsdefine_macrosundef_macrosrk   )
r<   r*   r   extext_name
build_infokeyvalr   r   s
             r   check_extensions_listzbuild_ext.check_extensions_listV  s    *d++ 	P%NP P P  
++ >	  >	 FAs#y))  c5)) 8SXX]])78 8 8 $' HjH ?@HJ J J x-- @%++H55@)?@ @ @ j$// <);< < < Hj&;<<C+ + + !nnS))?Cc*** (2~~g'>'>C$Z'' / 0 0 0
  ^^H--F 8$&!#% # 8 8E&ue44 7Uv9M9M167 7 7 5zzQ(//a9999Uq)00777JqMM}>	  >	 r   c                     |                      | j                   g }| j        D ]}|                    |j                   |S N)r   r*   rn   r   )r<   	filenamesr   s      r   get_source_fileszbuild_ext.get_source_files  sO    ""4?333	 ? 	* 	*CS[))))r   c                     |                      | j                   g }| j        D ]/}|                    |                     |j                             0|S r   )r   r*   rk   get_ext_fullpathrp   )r<   outputsr   s      r   get_outputszbuild_ext.get_outputs  s\     	""4?333
 ? 	< 	<CNN400::;;;;r   c                     |                      | j                   | j        r|                                  d S |                                  d S r   )r   r*   r:   _build_extensions_parallel_build_extensions_serialr;   s    r   r   zbuild_ext.build_extensions  sR    ""4?333= 	,++-----))+++++r   c                      j         } j         du rt          j                    }	 ddlm} n# t
          $ r d }Y nw xY w|                                  d S  ||          5  fd j        D             }t           j        |          D ]F\  }} 	                    |          5  |
                                 d d d            n# 1 swxY w Y   G	 d d d            d S # 1 swxY w Y   d S )NTr   )ThreadPoolExecutor)max_workersc                 F    g | ]}                     j        |          S rX   )submitbuild_extension)rY   r   executorr<   s     r   r[   z8build_ext._build_extensions_parallel.<locals>.<listcomp>  s:     3 3 3  t';SAA 3 3 3r   )r:   rf   	cpu_countconcurrent.futuresr   ImportErrorr   r*   zip_filter_build_errorsresult)r<   workersr   futuresr   futr   s   `     @r   r   z$build_ext._build_extensions_parallel  s   -=D  lnnG	======= 	 	 	GGG	 ?))+++FG444 	!3 3 3 3 3"&/3 3 3G99 ! !S..s33 ! !JJLLL! ! ! ! ! ! ! ! ! ! ! ! ! ! !!	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!sC   . ==$AC#(C	=C#	CC#CC##C'*C'c                     | j         D ]D}|                     |          5  |                     |           d d d            n# 1 swxY w Y   Ed S r   )r*   r   r   )r<   r   s     r   r   z"build_ext._build_extensions_serial  s    ? 	* 	*C**3// * *$$S)))* * * * * * * * * * * * * * *	* 	*s   AA	A	c              #      K   	 d V  d S # t           t          t          f$ r3}|j        s |                     d|j        d|           Y d }~d S d }~ww xY w)Nzbuilding extension "z
" failed: )CCompilerErrorDistutilsErrorCompileErroroptionalr   rp   )r<   r   es      r   r   zbuild_ext._filter_build_errors  s      	%EEEEE= 	% 	% 	%< IIIxxx$ % % % % % % % % %	%s   
 A(AAc                    |j         }|t          |t          t          f          st	          d|j        z            t          |          }|                     |j                  }||j        z   }| j	        s-t          ||d          st          j        d|j                   d S t          j        d|j                   |                     ||          }|j        pg }|j        d d          }|j        D ]}|                    |f           | j                            || j        ||j        | j        ||j                  }|d d          | _        |j        r|                    |j                   |j        pg }|j        p| j                            |          }	| j                            |||                     |          |j        |j         || !                    |          | j        | j        |	
  
         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   r/   r!   extra_postargsdepends)r2   r3   r   r   export_symbolsr!   r-   target_lang)"r   rc   r   r   r   rp   sortedr   r   r#   r   r   r!   infoswig_sourcesr   r   r   rk   r6   compiler-   r/   _built_objectsr   rn   r   languagedetect_languagelink_shared_objectget_librariesr3   r   get_export_symbols)
r<   r   r   ext_pathr   
extra_argsr   r1   objectsr   s
             r   r   zbuild_ext.build_extension  s   +?*WtUm"D"D?%/14:; ; ;
 //((22CK'
 	:k'8WEE 	:I<chGGGFH.999
 ##GS11  +1r
"111%% 	$ 	$EMM5(####-''48O06696F/3z8B14 ( > > &aaaj
  	.NN3,---(.B
 <I4=#@#@#I#I((X((--)!$!9%2237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 ]{}t          j                            |          \  }}	|	dk    r<|                    |dz   |z              |                    |           |d         ||<   f|                    |           ||s|S | j        p| 	                                }
|
dg}|
                    | j                   | j         r|                    d           | j        s|j        D ]}|                    |           |D ];}||         }t          j        d	||           |                     |d
||gz              <|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)r8   r   r   r9   rf   rl   splitextrk   r7   	find_swigrn   r   spawn)r<   r   	extensionnew_sourcesr   swig_targets
target_extsourcebaser   r7   swig_cmdotargets                 r   r   zbuild_ext.swig_sources2  s     = 	HHFGGG= 	Vt~55i)))JJJ 	+ 	+F'**622KT3d{{""4'>J#>???##F+++'22V$$""6**** 	y,DNN,,)$'''= 	$OOF### ~ 	#( # #""""" 	: 	:F!&)FH(&&999JJx4"889999r   c                    t           j        dk    rdS t           j        dk    rMdD ]H}t           j                            d|z  d          }t           j                            |          r|c S IdS 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.
        posixr7   rD   )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   isfileDistutilsPlatformError)r<   versfns      r   r   zbuild_ext.find_swigh  s    
 7g6W__ . " "W\\,"5zBB7>>"%% III "z(%')w/0 0 0r   c                 *   |                      |          }|                    d          }|                     |d                   }| j        sDt	          j        j        |dd         |gz    }t          j                            | j        |          S d                    |dd                   }|                     d          }t          j        	                    |
                    |                    }t          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).
        rR   r   Nr   build_py)get_ext_fullnamere   get_ext_filenamer   rf   rl   rm   r+   r   abspathget_package_dir)r<   r   fullnamemodpathfilenamer.   r  package_dirs           r   r   zbuild_ext.get_ext_fullpath  s     ((22..%%((55| 	: w|WSbS\8*%<=H7<<999 ((71R4=))--j99gooh&>&>w&G&GHH w||K222r   c                 .    | j         |S | j         dz   |z   S )zSReturns the fullname of a given extension name.

        Adds the `package.` prefixNrR   )r.   )r<   r   s     r   r  zbuild_ext.get_ext_fullname  s#     <O<#%00r   c                 z    ddl m} |                    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   rw   rR   
EXT_SUFFIX)distutils.sysconfigrw   re   rf   rl   rm   )r<   r   rw   r   
ext_suffixs        r   r  zbuild_ext.get_ext_filename  sJ    
 	766666>>#&&#^L11
w|X&33r   c                 r   d|j                             d          d         z   }	 |                    d           nO# t          $ rB d|                    d                              dd                              d          z   }Y nw xY wd	|z   }||j        vr|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.
        _rR   r   asciir   punycode   -   _PyInit)rp   re   encodeUnicodeEncodeErrorreplacedecoder   rk   )r<   r   r   initfunc_names       r   r   zbuild_ext.get_export_symbols  s     sx~~c**2..	Y MM'""""! 	Y 	Y 	Y6==44<<T4HHOOPWXXXFFF	Y !6) 222%%m444!!s   ; A	BBc                 *   t           j        dk    rYddlm} t	          | j        |          s=d}| j        r|dz   }|t           j        dz	  t           j        dz	  dz  fz  }|j        |gz   S n$dd	l	m
} d
}	 |r |d          }|j        d|z   gz   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).
        rI   r   )MSVCCompilerz
python%d%d_d         r  FrS   getandroidapilevelTrM   _PYTHON_HOST_PLATFORMANDROID_API_LEVELMACHDEP	LDVERSIONrP   )rh   rt   distutils._msvccompilerr  rc   r6   r!   
hexversionr2   r  rw   hasattrrf   environ)r<   r   r  template	pythonlibrw   link_libpython	ldversions           r   r   zbuild_ext.get_libraries  s     <7""<<<<<<dm\:: 3': /'$H%",s~/Ct.KLM	 }	{223$ ;:::::"N.  >*N;77	}9(<'===}r   ) __name__
__module____qualname__descriptionrf   rg   sep_byr
   user_optionsboolean_optionsr   help_optionsr=   r   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r  r  r   r   rX   r   r   r   r   !   s       LK( $bj0F	5	>	s
'<>>
*	+	9 
#	9F	B	D	,	.	.	#	9F	B	D	D	?	4	?	&	*	6	.	(	0O)
LV HGGO 
$	#^	5
L
  0RL RL RLh>  >  > @L  L  L \    , , ,! ! !(* * *
 % % %J" J" J"X4 4 4l0 0 003 3 361 1 14 4 4" " "&1 1 1 1 1r   r   )__doc__r8  rf   rerh   distutils.corer   distutils.errorsr  r   r   r   distutils.dep_utilr   distutils.extensionr	   distutils.utilr
   r]   r   siter   r   r   r   r   rX   r   r   <module>rB     s@        				 				 



 " " " " " "     F F F F F F F F 5 5 5 5 5 5 * * * * * * ) ) ) ) ) ) ' ' ' ' ' '             BJ;= =   
Q Q Q Q Q Q Q Q Q Qr   