
    Ae                        d Z ddlmZ dZddlZddlZddlmZ d dZ ej                         Z
 ed      Z ed	      Z ed
      Z ed      Zeek(  rdZn!ej                  j!                  edd       d   Z edej"                  j%                  dd            Z ed      dz   ej"                  j%                  dd      z   Z edej"                  j%                  de            Z ed      Z ed      Z ed      Z ej                  d      Zd Zd Zd!dZd Zd Zd"dZd"dZ d#dZ!d Z"e#dk(  r e"ejH                  dd        yy)$z
Compile a Python script into an executable that embeds CPython and run it.
Requires CPython to be built as a shared library ('libpythonX.Y').

Basic usage:

    python cythonrun somefile.py [ARGS]
    )absolute_importTN)	sysconfig c                 4    t        j                  |       xs |S )N)r   get_config_var)namedefaults     <lib/python3.12/site-packages/Cython/Build/BuildExecutable.pyr   r      s    ##D)4W4    LIBDIRLIBPLLIBRARY	LDLIBRARY   CCCFLAGS LINKCCLINKFORSHAREDLIBSSYSLIBSEXEc                 d    t         r*|r| |z  } t        j                  j                  | dz          y y )N
)DEBUGsysstderrwrite)msgargss     r
   _debugr!   )   s,    *C

t$ r   c                     t        dt               t        dt               t        dt               t        dt               t        dt
               t        dt               t        dt               t        dt               t        d	t               t        d
t               t        dt               t        dt               y )Nz
INCDIR: %szLIBDIR1: %szLIBDIR2: %sz	PYLIB: %szPYLIB_DYN: %szCC: %sz
CFLAGS: %sz
LINKCC: %szLINKFORSHARED: %szLIBS: %szSYSLIBS: %szEXE_EXT: %s)r!   INCDIRLIBDIR1LIBDIR2PYLIB	PYLIB_DYNr   r   r   r   r   r   EXE_EXT r   r
   dump_configr*   /   s{    
< 
='"
='"
;
?I&
8R
< 
< 
.
:t
='"
='"r   c                    |rdj                  |       } t        |        nt        dj                  |              	 dd l}|j                  | |      }|rt        j                  |       y y # t        $ r t        j                  |       }Y 9w xY w)Nr   r   shell)	joinr!   
subprocesscallImportErrorossystemr   exit)cmdr-   r/   
returncodes       r
   runcmdr7   =   sw    hhsmssxx}7  __S_6
   $YYs^
$s   A+ +BBc           	      V   t        t        d| t        z   | dz   dt        z   dt        z   gt
        xr	 dt
        z   xs( t        j                  j                  t        t              gz   t        j                         z   t        j                         z   t        j                         z          y )N-o.oz-Lz-l)r7   r   r(   r$   r%   r'   r2   pathr.   r&   r   splitr   r   basenames    r
   clinkr?   N   s    
FD(W,htmT'\4PW<X,T)^LWe0LMNZZ\#MMO,.;.A.A.CD Er   c           	      r    t        t        dd| dz   | dz   dt        z   gt        j	                         z          y )Nz-cr9   r:   z.cz-I)r7   r   r#   r   r<   r=   s    r
   ccompilerA   S   s.    
BdHTM8D=$-H6<<>YZr   c                     ddl m}m}m} |j	                  t        |xs d      d| gz         \  }}t        d|j                  |        |j                  ||      }|j                  dkD  rt        j                  d       y y )N   )VersionCmdLineMainr)   z--embedzUsing Cython %s to compile %sr      )CompilerrD   rE   rF   parse_command_linelistr!   versioncompile
num_errorsr   r4   )
input_fileoptionsrD   rE   rF   sourcesresults          r
   	cycompilerR   V   sp    1111$w}"2ET^H_2_`GW
*GOOZH\\'7+F1 r   c                 r    t        t        j                  j                  |       gt	        |      z   d       y )NFr,   )r7   r2   r;   abspathrJ   )program_namer    s     r
   	exec_filerV   ^   s%    
BGGOOL)*T$Z7uEr   c                 R   t         j                  j                  |       d   }|t        z   }|sJt         j                  j	                  |      t         j                  j	                  |       k(  rt        d      |st         j                  j                  |      rlt         j                  j                  |       rMt         j                  j                  |       t         j                  j                  |      k  rt        d|       |S t        | |       t        |       t        |       |S )zi
    Build an executable program from a Cython module.

    Returns the name of the executable file.
    r   z?Input and output file names are the same, refusing to overwritez'File is up to date, not regenerating %s)r2   r;   splitextr(   rT   
ValueErrorexistsgetmtimer!   rR   rA   r?   )rN   compiler_argsforcer>   exe_files        r
   buildr_   a   s     ww
+A.H'!HRWW__X."''//*2MMZ[[bggnnX.277>>*3MGGZ(BGG,<,<X,FF8(Cj-(X	(OOr   c                    g }d}t        |       D ]L  \  }}|j                  d      r|j                  |       n"|dv r|j                  |       n|}| |dz   d }  n|}N t        d      t	        ||      }t        ||        y)z
    Build an executable program from a Cython module and runs it.

    Arguments after the module name will be passed verbatimely to the
    program.
    N-)z-Xz--directiverG   zno input file provided)	enumerate
startswithappendrY   r_   rV   )r    cy_argslast_argiargrN   rU   s          r
   build_and_runri   t   s     GHD/ 33>>#NN3..NN3J!:D3 122W-LlD!r   __main__rG   )r   )T)r)   )r)   F)%__doc__
__future__r   r   r   r2   	distutilsr   r   get_python_incr#   r$   r%   r&   r'   r;   rX   environgetr   r   r   r   r   r   r(   r!   r*   r7   r?   rA   rR   rV   r_   ri   __name__argvr)   r   r
   <module>rs      sb   ' 
 	 5 
"	!	!	#

"

!y!;'	I  12/2ID"**..r23		!C	'"**..2*F	F	"**..2">	?/f

#
")
"
"5
)%#"E
[F&"2 z#((12, r   