B
    Í‹dÞ  ã               @   s\  d Z ddlmZ dZddlZddlZddlmZ d,dd„Ze 	¡ Z
ed	ƒZed
ƒZedƒZedƒZeekrpdZnej edd… ¡d Zedej dd¡ƒZedƒd ej dd¡ Zedej de¡ƒZedƒZedƒZedƒZe d¡Zdd„ Zdd„ Zd-dd„Zdd„ Zdd„ Zd.d!d"„Zd/d#d$„Z d0d&d'„Z!d(d)„ Z"e#d*krXe"ej$d+d… ƒ dS )1zÆ
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             C   s   t  | ¡p|S )N)r   Úget_config_var)ÚnameÚdefault© r   úi/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/Cython/Build/BuildExecutable.pyr      s    r   ÚLIBDIRÚLIBPLÚLIBRARYÚ	LDLIBRARYé   ÚCCÚCFLAGSú ÚLINKCCÚLINKFORSHAREDÚLIBSÚSYSLIBSÚEXEc             G   s$   t r |r| | } tj | d ¡ d S )NÚ
)ÚDEBUGÚsysÚstderrÚwrite)ÚmsgÚargsr   r   r	   Ú_debug)   s    r   c               C   s|   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ƒ d S )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_EXTr   r   r   r	   Údump_config/   s    










r%   c             C   sr   |rd  | ¡} t| ƒ ntd  | ¡ƒ ydd l}W n tk
rP   t | ¡}Y nX |j| |d}|rnt |¡ d S )Nr   r   )Úshell)	Újoinr   Ú
subprocessÚImportErrorÚosÚsystemÚcallr   Úexit)Úcmdr&   r(   Ú
returncoder   r   r	   Úruncmd=   s    

r0   c             C   s\   t td| t | d dt dt gtr,dt p8tj tt	¡g t
 ¡  t ¡  t ¡  ƒ d S )Nz-oz.oz-Lz-l)r0   r   r$   r    r!   r#   r*   Úpathr'   r"   r   Úsplitr   r   )Úbasenamer   r   r	   ÚclinkN   s    r4   c             C   s,   t tdd| d | d dt gt ¡  ƒ d S )Nz-cz-oz.oz.cz-I)r0   r   r   r   r2   )r3   r   r   r	   ÚccompileS   s    r5   r   c             C   sd   ddl m}m}m} | t|p dƒd| g ¡\}}td|j| ƒ | ||¡}|j	dkr`t
 d¡ d S )Né   )ÚVersionÚCmdLineÚMainr   z--embedzUsing Cython %s to compile %sr   é   )ZCompilerr7   r8   r9   Úparse_command_lineÚlistr   ÚversionÚcompileZ
num_errorsr   r-   )Ú
input_fileÚoptionsr7   r8   r9   ÚsourcesÚresultr   r   r	   Ú	cycompileV   s    
rC   c             C   s"   t tj | ¡gt|ƒ dd d S )NF)r&   )r0   r*   r1   Úabspathr<   )Úprogram_namer   r   r   r	   Ú	exec_file^   s    rF   Fc             C   sœ   t j | ¡d }|t }|s<t j |¡t j | ¡kr<tdƒ‚|s~t j |¡r~t j | ¡r~t j | ¡t 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)r*   r1   Úsplitextr$   rD   Ú
ValueErrorÚexistsÚgetmtimer   rC   r5   r4   )r?   Zcompiler_argsÚforcer3   Zexe_filer   r   r	   Úbuilda   s    

rL   c             C   s‚   g }d}x`t | ƒD ]L\}}| d¡r0| |¡ n*|dkrD| |¡ n|}| |d d… } P |}qW tdƒ‚t||ƒ}t|| ƒ dS )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--directiver:   zno input file provided)Ú	enumerateÚ
startswithÚappendrH   rL   rF   )r   Zcy_argsZlast_argÚiÚargr?   rE   r   r   r	   Úbuild_and_runt   s    

rS   Ú__main__r:   )r   )T)r   )r   )r   F)%Ú__doc__Ú
__future__r   r   r   r*   Ú	distutilsr   r   Úget_python_incr   r    r!   r"   r#   r1   rG   ÚenvironÚgetr   r   r   r   r   r   r$   r   r%   r0   r4   r5   rC   rF   rL   rS   Ú__name__Úargvr   r   r   r	   Ú<module>   s@   






