U
    ee&                     @   s   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mZ G dd dZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZdS )    )find_executable)environ)join)	cpu_count)Recipe)BuildInterruptingExceptionbuild_platformc                   @   s   e Zd ZdZdZg ZddgZdddgZdgZd	gZ	d
dddgZ
dd Zdd Zedd Zedd Zedd Zedd Zedd Zedd Zd$dd Zd%d"d#ZdS )&ArchN z-target {target}z-fomit-frame-pointerz	-DANDROIDz-I{ctx.ndk.sysroot_include_dir}z-I{python_includes}z-L{ctx_libs_dir}z-lmz-pthreadz-sharedz-Wl,-O1z-Wl,-Bsymbolic-functionsc                 C   s   || _ g | _d S N)ctxextra_global_link_paths)selfr    r   :/tmp/pip-unpacked-wheel-h4dze4ss/pythonforandroid/archs.py__init__+   s    zArch.__init__c                 C   s   | j S r   archr   r   r   r   __str__4   s    zArch.__str__c                 C   s   t | jjj| jS r   )r   r   ndkZsysroot_lib_dircommand_prefixr   r   r   r   ndk_lib_dir7   s    zArch.ndk_lib_dirc                 C   s   t | jt| jjS r   )r   r   strr   ndk_apir   r   r   r   ndk_lib_dir_versioned;   s    zArch.ndk_lib_dir_versionedc                    s    fdd j jD S )Nc                    s$   g | ]}d   jj|j  dqS )z{}/{}r   )formatr   Zinclude_dir).0dr   r   r   
<listcomp>A   s
   
z%Arch.include_dirs.<locals>.<listcomp>)r   include_dirsr   r   r   r   r    ?   s    
zArch.include_dirsc                 C   s   dj | j| jjdS )N{triplet}{ndk_api}Ztripletr   )r   r   r   r   r   r   r   r   targetG   s     zArch.targetc                 C   s   |   S )zTFull path of the clang compiler depending on the android's ndk
        version used.get_clang_exer   r   r   r   	clang_exeQ   s    zArch.clang_exec                 C   s   | j ddS )zVFull path of the clang++ compiler depending on the android's ndk
        version used.T)	plus_plusr$   r   r   r   r   clang_exe_cxxW   s    zArch.clang_exe_cxxFc                 C   s4   d}|rdj | j|d}|r$|d7 }t| jjj|S )zReturns the full path of the clang/clang++ compiler, supports two
        kwargs:

          - `with_target`: prepend `target` to clang
          - `plus_plus`: will return the clang++ compiler (defaults to `False`)
        Zclangz{target}-{compiler})r#   compilerz++)r   r#   r   r   r   Zllvm_bin_dir)r   Zwith_targetr'   r)   r   r   r   r%   ]   s     zArch.get_clang_exeTc              
   C   s  i }dt krt d |d< d| jj| jd|d< | jrT|d  dd| j 7  < |d |d< d| jj| j| jt| j	| j
d| jjjdd d	|d
< dddd | jD  d d| jj| j| j
d |d< d| j|d< d}| jjrPttt ddrP| jjd }d|d< | jj|d< |dd t  D  | jjd |d< t| j|d d}|d krtd|d  td| j|rdj| j||d d|d< dj| j||d d|d< n(d j| j|d!|d< d"j| j|d#|d< | jjj|d$< | jjj|d%< | jjj  d&|d'< | jjj!|d(< | jjj"|d)< d*t#t$ |d+< | j
|d,< d-t#| jj%|d.< |d d d| j& |d/< t'(d0| jjj) | j}t|*| j
d1d2d3t+| jjj,|d4< d5t krd6- D ]}|t krt | ||< q|S )7NHOME )r#   CFLAGSZCXXFLAGSzinclude/python{}r      )r   r   Zpython_includesCPPFLAGSc                 S   s    g | ]}d | dd d qS )z-L''z'"'"')replace)r   	link_pathr   r   r   r      s   
z Arch.get_env.<locals>.<listcomp>)Zctx_libs_dirLDFLAGSZLDLIBSr
   Z
USE_CCACHE1Z
NDK_CCACHEc                 S   s    i | ]\}}| d r||qS )ZCCACHE_)
startswith)r   kvr   r   r   
<dictcomp>   s     
  z Arch.get_env.<locals>.<dictcomp>PATH)pathzSearching path are: {!r}zCouldn't find executable for CC. This indicates a problem locating the {} executable in the Android NDK, not that you don't have a normal compiler installed. Exiting.z{ccache}{exe} {cflags})execcachecflagsCCz{ccache}{execxx} {cxxflags})execxxr;   ZcxxflagsCXXz{ccache}{exe})r:   r;   z{ccache}{execxx})r>   r;   ARZRANLIBz --strip-unneededZSTRIPREADELFZOBJCOPYz	make -j{}ZMAKEZARCHz
android-{}ZNDK_APILDSHAREDhostznative-buildbuildz	lib.{}-{}ZBUILDLIB_PATHSOURCE_DATE_EPOCHz@LC_ALL TZ SOURCE_DATE_EPOCH PYTHONHASHSEED BUILD_DATE BUILD_TIME).r   r   common_cflagsr   r#   arch_cflagscommon_cppflagsr   r   Zget_python_install_dirr   Zpython_recipeversionr   common_ldflagsZget_libs_dircommon_ldlibsr;   boolintgetupdateitemsenvr   r&   printr   r(   r   Zllvm_arZllvm_ranlibZ
llvm_stripZllvm_readelfZllvm_objcopyr   r   r   common_ldsharedr   Z
get_recipenameZget_build_dirr   Zmajor_minor_version_stringsplit)r   Zwith_flags_in_ccrQ   r;   ccZhostpython_reciper5   r   r   r   get_envm   s    
		 




 


zArch.get_env)FF)T)__name__
__module____qualname__r   r   rG   rF   rH   rJ   rK   rS   r   r   propertyr   r   r    r#   r&   r(   r%   rW   r   r   r   r   r	   
   sB   	



	


r	   c                   @   s    e Zd ZdZdZedd ZdS )ArchARMZarmeabizarm-linux-androideabic                 C   s2   | j d}djdd|d |d g| jjdS )N-r!   Zarmv7a      r"   )r   rU   r   r   r   r   )r   Ztarget_datar   r   r   r#      s
    zArchARM.targetN)rX   rY   rZ   r   r   r[   r#   r   r   r   r   r\      s   r\   c                   @   s   e Zd ZdZdddddgZdS )ArchARMv7_azarmeabi-v7az-march=armv7-az-mfloat-abi=softfpz	-mfpu=vfpz-mthumb-fPICN)rX   rY   rZ   r   rG   r   r   r   r   r`      s   r`   c                   @   s"   e Zd ZdZdZdddddgZdS )	Archx86x86zi686-linux-androidz-march=i686z-mssse3z-mfpmath=ssez-m32ra   NrX   rY   rZ   r   r   rG   r   r   r   r   rb     s   rb   c                   @   s"   e Zd ZdZdZdddddgZdS )	
Archx86_64x86_64zx86_64-linux-androidz-march=x86-64z-msse4.2z-mpopcntz-m64ra   Nrd   r   r   r   r   re     s   re   c                   @   s   e Zd ZdZdZddgZdS )ArchAarch_64z	arm64-v8azaarch64-linux-androidz-march=armv8-ara   Nrd   r   r   r   r   rg     s
   rg   N)distutils.spawnr   osr   os.pathr   multiprocessingr   Zpythonforandroid.reciper   Zpythonforandroid.utilr   r   r	   r\   r`   rb   re   rg   r   r   r   r   <module>   s    f