B
    XM\JÇ  ã            )   @   sR  d dl mZmZmZ d dlmZ d dlZd dlmZ d dl	Z	d dl	m
Z
 d dlZd dlZd dlZd dlZd dlZd dlZd dlmZmZmZ d dlZyd dlmZ W n ek
r¼   dZY nX 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  d dlm!Z! d dl"m#Z# d dl$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3 ej4dkršd dlm5Z5 er¨ej6Z6nd dl6m6Z6 ddgdgdœZ7d“dd „Z8d!d"„ Z9d”d#d$„Z:d%d&„ Z;d'd(„ Z<d)d*„ Z=d+d,„ Z>d•d.d/„Z?d0d1„ Z@d–d2d3„ZAd—d4d5„ZBd6d7„ ZCd˜d9d:„ZDd™d;d<„ZEd=d>„ ZFd?d@„ ZGdAdB„ ZHdCdD„ ZIG dEdF„ dFƒZJdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZdXd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdng)ZKdodpdqdrdsdtgZLdudv„ ZMdwdx„ ZNdydz„ ZOd{d|„ ZPd}d~„ ZQdd€„ ZRdd‚„ ZSdƒd„„ ZTd…d†„ ZUd‡dˆ„ ZVd‰dŠ„ ZWd‹dŒ„ ZXddŽ„ ZYdd„ ZZd‘d’„ Z[dS )šé    )Úabsolute_importÚdivisionÚprint_function)ÚdefaultdictN)Úpartial)Úglob)ÚcallÚcheck_outputÚCalledProcessError)ÚreadlinkF)Úexternal)ÚPY3)Úlchmod)Úlinked_data)Úwalk_prefix)Ú	pkgs_dirs)ÚTemporaryDirectory)Úmd5_file)Úutils)Úget_exports_memoizedÚget_linkages_memoizedÚget_runpaths)Úcodefile_type)Úget_package_obj_files)Úget_run_exportsÚget_build_index)Úwhich_package)ÚOverLinkingErrorÚOverDependingErrorÚdarwin)Úmacho)Úscandir)ZDLLfileZEXEfileÚ	machofileÚelffile)ÚwinZosxÚlinuxc          	   C   sV  t j || ¡}t|ƒrd S t j |¡r*d S t j |¡s:d S t  |¡jdkrNd S d}t  |d¡ t	j
|dt ¡ d}y| d¡}| d¡ W n tk
r¢   d S X t dtj¡}ytj| ¡ dd tjd}	W n tk
rê   | ¡ }	Y nX y| |	¡}
W n2 tk
r,   t d	tj¡}d
}| |	¡}
Y nX |
rl|rHt dtj¡nt dtj¡}| |
 ¡ ¡spd S nd S |	d d … }W d Q R X dtjdkr¦|r¦d| d n|d t j |¡  }|rÖt|dƒrÖ|  ¡ }|j!||dd}||krôd S t"d| ƒ t	j
|dt ¡ d:}y| #|¡ W n$ tk
rF   | #| $¡ ¡ Y nX W d Q R X d S )Nr   Fiý  zr+)ÚmodeÚencodingéd   z^#!.+$)ZtagnameÚflagss   ^#!.+$Ts   \/python[w]?(?:$|\s|\Z)z\/python[w]?(:$|\s|\z)z#!r   z
/bin/bash z/bin/pythonwz/bin/Úencodeé   )Úcountzupdating shebang:Úw)r'   )%ÚosÚpathÚjoinr   ÚislinkÚisfileÚstatÚst_sizeÚchmodÚioÚopenÚlocaleZgetpreferredencodingÚreadÚseekÚUnicodeDecodeErrorÚreÚcompileÚMr   Z	mmap_mmapÚfilenoZmmap_MAP_PRIVATEÚOSErrorÚmatchÚ	TypeErrorÚsearchÚgroupÚsysÚplatformÚbasenameÚhasattrr*   ÚsubÚprintÚwriteÚdecode)ÚfÚprefixÚbuild_pythonÚ
osx_is_appr/   Zbytes_ZfiÚdataZSHEBANG_PATZmmÚmZpython_patternZpy_execZnew_dataÚfo© rT   ú/lib/python3.7/site-packages/conda_build/post.pyÚfix_shebang6   sb    


rV   c          	   C   st   t j | ¡}d |jd  d¡d d… ¡}tt j t |j	|¡d| d¡d  ¡dƒ}| 
d| ¡ W d Q R X d S )	NÚ.Úpythoné   z%s.pthú-r   r-   z./%s
)r.   r/   rG   r0   ÚvariantÚsplitr7   r   Úget_site_packagesÚhost_prefixrK   )Úegg_pathÚconfigÚfnÚpy_verrS   rT   rT   rU   Ú	write_pthu   s
    rc   c                s<  ‡fdd„| D ƒ‰ d  |jd  d¡dd… ¡}t ˆ|¡}xättj  |d¡ƒD ]Ì‰tj ˆ¡rò|sŒt	‡ ‡fdd	„t
ˆd
d
dD ƒƒsštˆ|d qRtdˆƒ yt tj  ˆd¡ˆd ¡ W n tk
rÖ   Y nX t tj  ˆd¡¡ x0t ˆ¡D ]ò}|dkrt tj  ˆ|¡¡ qútj tj  ||¡¡rÌyBtjtj  ˆ|¡tj  ||¡|j|jd t tj  ˆ|¡¡ W nP tk
rÈ } z0tj t|ƒ ¡ d ¡}tdjˆ||dƒ‚W dd}~X Y nX qút tj  ˆ|¡tj  ||¡¡ qúW qRtj ˆ¡rRˆˆ kr
qRtdˆƒ tˆ|d qRW t tj  |d¡¡ dS )z]
    remove the need for easy-install.pth and finally remove easy-install.pth
    itself
    c                s   g | ]}t j ˆ |¡‘qS rT   )r.   r/   r0   )Ú.0rM   )rN   rT   rU   ú
<listcomp>‚   s    z+remove_easy_install_pth.<locals>.<listcomp>rW   rX   NrY   z	*-py*.eggc             3   s    | ]}t j ˆ|¡ˆ kV  qd S )N)r.   r/   r0   )rd   Úi)Úabsfilesr_   rT   rU   ú	<genexpr>‡   s    z*remove_easy_install_pth.<locals>.<genexpr>F)Zwindows_forward_slashes)r`   zfound egg dir:zEGG-INFOz-infoÚ__pycache__)ÚlockingéÿÿÿÿzàTried to merge folder {egg_path} into {sp_dir}, but {fn} exists in both locations.  Please either add build/preserve_egg_dir: True to meta.yaml, or manually remove the file during your install process to avoid this conflict.)r_   Úsp_dirra   z
found egg:zeasy-install.pth)r0   r[   r\   r   r]   r   r.   r/   ÚisdirÚanyr   rc   rJ   ÚshutilZmover@   Úrm_rfÚlistdirÚexistsÚ	copy_intoZtimeoutrj   ÚIOErrorrG   ÚstrÚformatr2   )ÚfilesrN   r`   Úpreserve_egg_dirrb   rl   ra   ÚerT   )rg   r_   rN   rU   Úremove_easy_install_pth}   sH    


"&

rz   c             C   sl   t t| ƒƒ}xZ|D ]R}| ¡ r|j d¡rtj |j¡\}}x&dD ]}|| |krBt || ¡ qBW qW dS )z3remove .py (.pyc) files alongside .so or .pyd files)z.soz.pyd)z.pyz.pycz.pyoN)	Úlistr!   Úis_fileÚnameÚendswithr.   r/   ÚsplitextÚunlink)rN   rw   ra   r}   Ú_ÚextrT   rT   rU   Úrm_py_along_so´   s    

rƒ   c             C   s:   t  d¡}x*| D ]"}| |¡rt tj ||¡¡ qW dS )aB  pyo considered harmful: https://www.python.org/dev/peps/pep-0488/

    The build may have proceeded with:
        [install]
        optimize = 1
    .. in setup.cfg in which case we can end up with some stdlib __pycache__
    files ending in .opt-N.pyc on Python 3, as well as .pyo files for the
    package's own python. z.*(?:\.pyo$|\.opt-[0-9]\.pyc)N)r<   r=   rA   r.   r€   r/   r0   )rw   rN   Zre_pyora   rT   rT   rU   Úrm_pyoÀ   s    	


r„   c             C   s:   t  d¡}x*| D ]"}| |¡rt tj ||¡¡ qW d S )Nz.*(?:\.pyc$))r<   r=   rA   r.   r€   r/   r0   )rw   rN   Zre_pycra   rT   rT   rU   Úrm_pycÏ   s    


r…   c             C   s4   d| kr0t j |ddd¡}t j |¡r0t  |¡ d S )Nzshare/info/dirZshareÚinfoÚdir)r.   r/   r0   r2   r€   )rw   rN   ra   rT   rT   rU   Úrm_share_info_dirÖ   s    rˆ   rT   c                sð  t j |¡sd S g }dd„ |D ƒ}tƒ }x"|D ]}| tt | |¡ƒ¡ q.W t| ƒ| }x†|D ]~‰ tjdkr†t	‡ fdd„dD ƒƒr’q^nˆ  
d¡r’q^tr dt j nd}	ˆ  d	¡r^t j ˆ ¡|	 t j ˆ ¡ d
 | kr^| ˆ ¡ q^W |rìt j |¡sütdƒ nðtdƒ tjdkrd}
nd}
|
t|ƒd 8 }
tt|tdƒd }|
|k rL|}
g g}|dddg}td |¡ƒd  }}xV|D ]N}t|ƒd }|| |
kr¨| g ¡ |}n||7 }|t|ƒd   |¡ qzW x|D ]}t|| |d qÒW d S )Nc             S   s   g | ]}t j |¡‘qS rT   )r.   r/   Únormpath)rd   ÚskiprT   rT   rU   re   á   s    z'compile_missing_pyc.<locals>.<listcomp>Zwin32c                s   g | ]}ˆ   ¡  |¡‘qS rT   )ÚlowerÚ
startswith)rd   Ústart)ra   rT   rU   re   é   s    )zlibrary/binzlibrary\binZscriptsÚbinri   Ú z.pyÚczAcompiling .pyc files... failed as no python interpreter was foundzcompiling .pyc files...iþ  iø  rY   )Úkeyr+   z-Wiz-mZ
py_compileú )Úcwd)r.   r/   r2   ÚsetÚupdateÚfnmatchÚfilterrE   rF   rn   rŒ   r   Úsepr~   ÚdirnamerG   ÚappendrJ   ÚlenÚmaxr0   r   )rw   r“   Ú
python_exeÚskip_compile_pycZcompile_filesZskip_compile_pyc_nZskipped_filesrŠ   Zunskipped_filesZcache_prefixÚlimitZlower_limitÚgroupsÚargsZargs_lenÚlengthrM   Zlength_thisrD   rT   )ra   rU   Úcompile_missing_pycÝ   sR    




$




r£   c             C   s    xš|D ]’}|  dtj d ¡rtj tj |¡ ¡ ¡}| | d ¡r| d¡\}}}| | d ¡\}}}|| kr”||kr”t	d 
||¡ƒ t d¡ qd S qW d S )Nz
.dist-infoZMETADATArZ   zBERROR: Top level dist-info version incorrect (is {}, should be {})r+   )r~   r.   r˜   r/   rG   r™   r‹   rŒ   Ú
rpartitionrJ   rv   rE   Úexit)r}   Úversionrw   rM   Zf_lowerr   ZdistnamerT   rT   rU   Úcheck_dist_info_version  s    
r§   c       	      C   sv   t ||ƒ |rt||ƒ n*tj |j¡r.|jn|j}t||||d t||||d t	|ƒ t
||ƒ t| ||ƒ d S )N)r“   r   rž   )rx   )r„   r…   r.   r/   r2   rO   Zhost_pythonr£   rz   rƒ   rˆ   r§   )	r}   r¦   rw   rN   r`   rx   Znoarchrž   r   rT   rT   rU   Úpost_process  s    

r¨   c             C   sz  |   |¡r@tj | t|ƒd d … ¡} | |kr<t d|  ¡ | S |   d¡rNd S |   d¡r\d S d| ksp|   d¡rjtj | ¡} tt	ƒ}x"|D ]}|tj |¡  
|¡ qŠW | |kr¾t d|  ¡ t||  ƒdkr^|rètj |¡| krè|S tƒ }x(||  D ]}| ttj ||¡ƒ¡ qøW t|ƒdkr>t d| ||  f ¡ n ||   ¡  td| ||  f ƒ ||  d S td	|  ƒ d S )
Nr+   zError: Could not find %sú/z@rpath/z@executable_path/z)Error: Found multiple instances of %s: %sz=Found multiple instances of %s (%s).  Choosing the first one.r   z#Don't know how to find %s, skipping)rŒ   r.   r/   r‰   r›   rE   r¥   rG   r   r{   rš   r”   Úaddr   r0   ÚsortrJ   )ÚlinkrN   rw   r/   Z
file_namesrM   Zmd5srT   rT   rU   Úfind_lib.  s:    



r­   c       	      C   sÂ   |d }t d|| f ƒ ||krf| |¡rf| ||¡}t dƒ t|ƒsVt d| ¡ nt d|| f ƒ t|||| ƒ}t d| ƒ |sˆd S tj 	tj 
|¡d¡}d|tj |¡f }| d	d
¡}|S )Nr}   zFixing linking of %s in %szY.. seems to be linking to a compiler runtime, replacing build prefix with host prefix andzKError: Compiler runtime library in build prefix not found in host prefix %sz%.. fixing linking of %s in %s insteadzNew link location is %sÚlibz@rpath/%s/%sz/./r©   )rJ   rŒ   Úreplacer   rE   r¥   r­   r.   r/   Úrelpathr™   rG   )	r/   Z	link_dictr^   Úbuild_prefixrw   r¬   Zlink_locZlib_to_linkZretrT   rT   rU   Úosx_ch_linkT  s"    
r²   ©r®   c       	   
   C   s¦   t jdkst‚t | ¡}tj| tt|||d|d}|r”xZ|D ]R}|dkrLq>tj	 
dtj	 tj	 
||¡tj	 | ¡¡d¡ dd¡}tj| |dd	 q>W |r¢t| |ƒ d S )
Nr   )r^   r±   rw   )Zdylibsr   z@loader_pathz/./r©   T)Úverbose)rE   rF   ÚAssertionErrorr    ZotoolZinstall_name_changer   r²   r.   r/   r0   r°   r™   r¯   Z	add_rpathÚassert_relative_osx)	r/   r^   r±   rw   ÚrpathsÚnamesÚsÚrpathZ	rpath_newrT   rT   rU   Úmk_relative_osx‡  s$    

r»   c          	   C   s¼  t j || ¡}t j |¡}t d|¡}y t|d|gƒ d¡ ¡ d }W n  t	k
rf   t
d| ƒ dS X | t j¡}g }x€|D ]x}| d¡r˜| |¡ q~| d¡r~t j ||¡}	|	 d	t j ¡rÒt
d
 ||¡ƒ q~dt j ||¡ }	|	|kr~| |	¡ q~W x|D ]ˆ}
|
dkr |
 d¡srt j t | |
¡¡}t j t j |
t j | ¡¡¡}||ksjtd ||| |
¡ƒ‚d| }
|
|kr | |
¡ q W d |¡}
t
d||
f ƒ t|dd|
|gƒ dS )zARespects the original values and converts abs to $ORIGIN-relativeÚpatchelfz--print-rpathzutf-8r   z&patchelf: --print-rpath failed for %s
Nz$ORIGINr©   z..z6Warning: rpath {0} is outside prefix {1} (removing it)z$ORIGIN/r   z8utils.relative {0} and relpath {1} disagree for {2}, {3}ú:z+patchelf: file: %s
    setting rpath to: %sz--force-rpathz--set-rpath)r.   r/   r0   r™   r   Zfind_executabler	   rL   Ú
splitlinesr
   rJ   r\   ÚpathseprŒ   rš   r°   r˜   rv   r‰   r   ZrelativeÚ
ValueErrorr   )rM   rN   r·   ZelfÚoriginr¼   ÚexistingÚnewÚoldr°   rº   Zrel_oursZ
rel_stdlibrT   rT   rU   Úmk_relative_linux£  sD     







rÅ   c             C   s.   x(t  | ¡D ]}| |¡rtd|  ƒ‚qW d S )Nz7library at %s appears to have an absolute path embedded)r    Z
get_dylibsrŒ   ÚRuntimeError)r/   rN   r}   rT   rT   rU   r¶   Ñ  s    
r¶   c          	   C   sè   g }d }| j  d¡}t| |ƒ}	dd„ |	D ƒ}t||||dddd\}
}
}| j}| |¡}|r‚| j |d kr‚|d | j   di ¡}n\xZtD ]R}tj 	|| j
¡}tj 	|| j¡}tj |¡rÄt|ƒ}P qˆtj |¡rˆt|ƒ}P qˆW |||fS )Nr®   c             S   s"   g | ]}d D ]}||kr|‘qqS ))z.dylibz.soz.dllrT   )rd   rM   r‚   rT   rT   rU   re   Ü  s    z,determine_package_nature.<locals>.<listcomp>F)ÚsubdirÚbldpkgs_dirÚoutput_folderÚchannel_urlsÚdebugr´   Zclear_cacheÚpackagesÚrun_exports)r}   rŒ   r   r   ZchannelÚgetr   r.   r/   r0   Z	dist_namera   rr   r   r2   )ÚpkgrN   rÇ   rÈ   rÉ   rÊ   ÚdsosrÍ   Z
lib_prefixZ	codefilesr   ZchanneldataZchannel_usedZpkgs_dirZtest_folderZtest_filenamerT   rT   rU   Údetermine_package_nature×  s4    


rÑ   c       
      C   sJ   t | |||||ƒ\}}}|r dS t|ƒrFdd„ |D ƒ}	t|	ƒrBdS dS dS )z³
    Result :: "non-library", "plugin library", "dso library", "run-exports library"
    .. in that order, i.e. if have both dsos and run_exports, it's a run_exports_library.
    zrun-exports libraryc                s&   g | ]‰ t ‡ fd d„dD ƒƒsˆ ‘qS )c             3   s   | ]}|ˆ kr|V  qd S )NrT   )rd   Úpart)ÚdsorT   rU   rh     s    z,library_nature.<locals>.<listcomp>.<genexpr>)zlib/R/libraryzsite-packages)rn   )rd   rT   )rÓ   rU   re     s    z"library_nature.<locals>.<listcomp>zdso libraryzplugin libraryznon-library)rÑ   r›   )
rÏ   rN   rÇ   Úbldpkgs_dirsrÉ   rÊ   rÐ   rÍ   r   Zdsos_without_pluginsrT   rT   rU   Úlibrary_natureø  s    rÕ   c             C   sD   g }t |ƒ}x2| D ]*}x$|D ]}|jd |kr| |¡ qW qW |S )Nr   )r   Úquadrš   )r¸   rN   ZresultsÚpkgsr}   rÏ   rT   rT   rU   Údists_from_names  s    

rØ   c               @   s   e Zd Zdd„ ZdS )ÚFakeDistc             C   s$   || _ |g| _|| _|| _|| _d S )N)r}   rÖ   r¦   Úbuild_numberZbuild_string)Úselfr}   r¦   rÚ   Ú	build_strrT   rT   rU   Ú__init__  s
    zFakeDist.__init__N)Ú__name__Ú
__module__Ú__qualname__rÝ   rT   rT   rT   rU   rÙ     s   rÙ   z	/opt/X11/z/usr/lib/libSystem.B.dylibz/usr/lib/libcrypto.0.9.8.dylibz/usr/lib/libobjc.A.dylibz1/System/Library/Frameworks/Accelerate.framework/*z*/System/Library/Frameworks/AGL.framework/*z-/System/Library/Frameworks/AppKit.framework/*z:/System/Library/Frameworks/ApplicationServices.framework/*z3/System/Library/Frameworks/AudioToolbox.framework/*z0/System/Library/Frameworks/AudioUnit.framework/*z3/System/Library/Frameworks/AVFoundation.framework/*z0/System/Library/Frameworks/CFNetwork.framework/*z-/System/Library/Frameworks/Carbon.framework/*z,/System/Library/Frameworks/Cocoa.framework/*z0/System/Library/Frameworks/CoreAudio.framework/*z5/System/Library/Frameworks/CoreFoundation.framework/*z3/System/Library/Frameworks/CoreGraphics.framework/*z0/System/Library/Frameworks/CoreMedia.framework/*z4/System/Library/Frameworks/CoreBluetooth.framework/*z//System/Library/Frameworks/CoreMIDI.framework/*z3/System/Library/Frameworks/CoreServices.framework/*z//System/Library/Frameworks/CoreText.framework/*z0/System/Library/Frameworks/CoreVideo.framework/*z//System/Library/Frameworks/CoreWLAN.framework/*z6/System/Library/Frameworks/DiskArbitration.framework/*z1/System/Library/Frameworks/Foundation.framework/*z5/System/Library/Frameworks/GameController.framework/*z,/System/Library/Frameworks/GLKit.framework/*z./System/Library/Frameworks/ImageIO.framework/*z2/System/Library/Frameworks/IOBluetooth.framework/*z,/System/Library/Frameworks/IOKit.framework/*z0/System/Library/Frameworks/IOSurface.framework/*z-/System/Library/Frameworks/OpenAL.framework/*z-/System/Library/Frameworks/OpenGL.framework/*z-/System/Library/Frameworks/Quartz.framework/*z1/System/Library/Frameworks/QuartzCore.framework/*z//System/Library/Frameworks/Security.framework/*z//System/Library/Frameworks/StoreKit.framework/*z:/System/Library/Frameworks/SystemConfiguration.framework/*z-/System/Library/Frameworks/WebKit.framework/*z**/KERNEL32.dllz**/ADVAPI32.dllz**/RPCRT4.dllz**/ntdll.dllz**/msvcrt.dllz**/api-ms-win*.dllc                s¶   t ƒ }tƒ }| r| d nd‰x|D ]ˆ}tj ˆ|¡}	t|	ƒs>q"t|	ddˆˆd}
ˆrh‡‡fdd„|
D ƒ}
‡ ‡fdd„|
D ƒ}
‡fd	d„|
D ƒ}
|
||< | |
¡}| |¡ q"W ||fS )
Nr   r   TF)Zresolve_filenamesZrecurseÚsysrootZenvrootc                s&   g | ]}|  ˆ ¡r| ˆ ˆ¡n|‘qS rT   )rŒ   r¯   )rd   Ún)rá   Úsysroot_substitutionrT   rU   re   ]  s   z(_collect_needed_dsos.<locals>.<listcomp>c                s&   g | ]}|  ˆ ¡r| ˆ ˆ¡n|‘qS rT   )rŒ   r¯   )rd   râ   )r±   Úbuild_prefix_substitutionrT   rU   re   _  s   c                s(   g | ] }|  ˆ ¡r tj |ˆ ¡n|‘qS rT   )rŒ   r.   r/   r°   )rd   râ   )Ú
run_prefixrT   rU   re   a  s   )	r”   Údictr.   r/   r0   r   r   Úunionrª   )Úsysrootsrw   rå   rã   r±   rä   Úall_needed_dsosÚneeded_dsos_for_filerM   r/   ÚneededrT   )r±   rä   rå   rá   rã   rU   Ú_collect_needed_dsosQ  s*    


rì   c          	      sê  i }i }i }	i }
xÊ||fD ]¼}x´t  |¡D ]¤\}}}x–|D ]Œ}t j ||¡‰ t‡ fdd„dD ƒƒo|tˆ ddd k	}t‡ fdd„dD ƒƒ}|sž|sžqDt j ˆ |¡}|rº||krºqD||
krÄqD||krÔ|| ng }t|ƒsò|| krò| |¡ t	t
||ƒƒ}x"|D ]}||kr| |¡ qW |||< t|| ƒrDt‡fdd„tˆ ƒD ƒƒ}||
|< |rrd	||| d
 < qD|rD|ˆ krÂ|| d
 j d¡sD|| d
 jdkrªqDtd ˆ || d
 ¡ƒ d	|	|| d
 < qDW q0W qW |||	|
fS )Nc             3   s   | ]}t j ˆ |¡V  qd S )N)Úglob2r–   )rd   r‚   )ÚfprT   rU   rh   t  s    z'_map_file_to_package.<locals>.<genexpr>)z*.so*z*.dylib*z*.dllF)Zskip_symlinksc             3   s   | ]}t j ˆ |¡V  qd S )N)rí   r–   )rd   r‚   )rî   rT   rU   rh   v  s    )z*.az*.libc             3   s(   | ] ‰ t ‡ fd d„ˆD ƒƒsˆ V  qdS )c             3   s   | ]}t j ˆ |¡V  qd S )N)rí   r–   )rd   Úpattern)ry   rT   rU   rh   Ž  s    z1_map_file_to_package.<locals>.<genexpr>.<genexpr>N)rn   )rd   )Úignore_list_syms)ry   rU   rh     s    Tr   Zgcc_impl_linuxZllvmzsysroot in {}, owner is {})r.   Úwalkr/   r0   rn   r   r°   r›   rš   r{   r   r”   r   r}   rŒ   rJ   rv   )rw   rå   r±   ré   Úpkg_vendored_distrð   rã   Úprefix_ownersZcontains_dsosZcontains_static_libsÚall_lib_exportsrN   Zsubdir2r   ZfilezÚfileZdynamic_libZ
static_libZrpZownersZnew_pkgsZnew_pkgZexportsrT   )rî   rð   rU   Ú_map_file_to_packagei  sP    



rö   c             C   s   | }|}|}|}t ||||ƒS )N)rÙ   )Úpkg_nameÚpkg_versionrÜ   rÚ   Zpkg_vendoring_nameZpkg_vendoring_versionZpkg_vendoring_build_strZpkg_vendoring_build_numberrT   rT   rU   Ú_get_fake_pkg_dist  s    rù   c             C   s$   |  d¡r|  |¡ |r t|ƒ d S )Nz  ERROR)rŒ   rš   rJ   )ÚerrorsÚtextr´   rT   rT   rU   Ú
_print_msg©  s    

rü   c	                sô  |  |¡r|g| }	n|g}	d}
xT|	D ]L}| ||¡‰ t‡ fdd„|D ƒƒ}
|
r&d ˆ ¡}t| d ||¡|d P q&W |
sÔt|ƒrÔtj |¡}g }g }xN|D ]F}| dtj	¡}|  |¡rÐ| 
| ||¡¡ q | tj |d|¡¡ q W x|D ]}| t|ƒ¡ qðW t|ƒr¼|d d d	… ‰‡fd
d„|D ƒ}ttt|ƒƒ|jd}tj ||  |tj	 d¡¡}d |¡}tt||ƒƒ}t|ƒr¤t| d |||d ¡|d nt| d ||¡|d nt| d ||¡|d n|
sðt| d ||¡|d d S )NFc                s   g | ]}t j ˆ |¡‘qS rT   )rí   r–   )rd   r-   )Úneeded_dso_wrT   rU   re   ¼  s    z0_lookup_in_system_whitelists.<locals>.<listcomp>zNeeded DSO {}z{}: {} found in the whitelist)r´   r©   z**rk   c          	      s*   g | ]"}t tj |d d d… ˆ g¡ƒ‘qS )Nrk   )r›   r.   r/   Úcommonprefix)rd   r¹   )Úrev_needed_dsorT   rU   re   Ö  s   )r‘   r   z'{}: {} found in CDT/compiler package {}r   zA{}: {} not found in any CDT/compiler package, nor the whitelist?!zj{}: {} not found in sysroot, is this binary repackaging? .. do you need to use install_name_tool/patchelf?z5{}: did not find - or even know where to look for: {})rŒ   r¯   rn   rv   rü   r›   r.   r/   rG   r˜   rš   Úextendr0   r   rœ   ÚrangeÚ__getitem__r‰   r{   r   )rú   Ú	whitelistÚ
needed_dsorè   Úmsg_preludeÚinfo_preludeÚsysroot_prefixrã   r´   ZreplacementsÚin_whitelistZreplacementÚn_dso_pZ	dso_fnameZsysroot_filesZdirs_to_globrá   Z
sysroot_osZdir_to_globZ
match_lensÚidxÚin_prefix_dsor×   rT   )rý   rÿ   rU   Ú_lookup_in_system_whitelists°  sT    









r  c                sš  |‰ ˆ dkrt dƒ d ˆ ¡}ˆ |kr*dnd}ttˆ |ƒƒ}‡fdd„|D ƒ}x|D ]}||
krT| |¡ qTW t‡ fdd„|D ƒƒ}t|ƒd	kr°t| d
 |||d |¡|d næ|rÌt| d ||¡|d nÊt|ƒdkr.t|ƒdkr.t| d
 ||dd„ |D ƒ|¡|d t| d |dd„ |D ƒ¡|d nht|ƒd	krXt| d |ˆ ||¡|d n>ˆ |krzt| d |ˆ ¡|d n|r–t| d |ˆ ¡|d d S )Nr©   rË   zNeeded DSO {}z (and also in this package)r   c                s   g | ]}|j d  ˆ kr|‘qS )r   )rÖ   )rd   rÏ   )Úrequirements_runrT   rU   re   ö  s    z._lookup_in_prefix_packages.<locals>.<listcomp>c                s   g | ]}t j ˆ |¡‘qS rT   )rí   r–   )rd   r-   )r  rT   rU   re   û  s    r+   z{}: {} found in {}{}r   )r´   z{}: {} found in the whitelistc             S   s   g | ]}|j d  ‘qS )r   )rÖ   )rd   ÚprT   rT   rU   re     s    zf{}: .. but {} not in reqs/run, (i.e. it is overlinking) (likely) or a missing dependency (less likely)c             S   s   g | ]}|j d  ‘qS )r   )rÖ   )rd   r  rT   rT   rU   re     s    z3{}: {} found in multiple packages in run/reqs: {}{}z {}: {} not found in any packagesz{}: {} found in this package)rJ   rv   r{   r   rª   rn   r›   rü   )rú   r  rw   rå   r  r  r  Úwarn_preluder´   r  Úlib_packagesÚlib_packages_usedr	  Zand_alsor×   Zin_pkgs_in_run_reqsrÏ   r  rT   )r  r  rU   Ú_lookup_in_prefix_packagesí  sL    






r  c                st  xl| D ]b‰ t j |ˆ ¡}t|ƒ}|r|t| d¡d  kr@qd |ˆ ¡}d |ˆ ¡}d |ˆ ¡}|rl|n|}yt|ƒ}W n   t|d |¡ƒ wY nX |rÒ|sÒt	‡ fdd„|D ƒƒsÒt|d	 |||¡|d
 |ˆ  }x|D ]ˆ}| 
dt j¡}| t j¡s,| d¡s,t||| |||||||	|
|ƒ qà| |¡rPt|d ||¡|d
 qàt|||||||||ƒ	 qàW qW d S )NrZ   r   zWARNING ({},{})z  ERROR ({},{})z   INFO ({},{})z{}: pyldd.py failed to processc             3   s   | ]}t   ˆ |¡V  qd S )N)r–   )rd   r-   )rM   rT   rU   rh   .  s    z)_show_linking_messages.<locals>.<genexpr>z{}: runpaths {} found in {})r´   r©   ú$z1{}: {} found in build prefix; should never happen)r.   r/   r0   r   Úfiletypes_for_platformr\   rv   r   rü   rn   r¯   r˜   rŒ   r  r  )rw   rú   rê   r±   rå   r÷   Úerror_overlinkingÚrunpath_whitelistr´   r  r  r  r  rè   r  rã   rÇ   r/   Úfiletyper  Úerr_preluder  r  Zrunpathsrë   r  rT   )rM   rU   Ú_show_linking_messages  s<    

r  c       1         s  d}g }d}d}dd„ |D ƒ}t |ˆƒ}t |¡‰ˆ d¡rFˆ d¡ ‡ ‡‡‡‡fdd	„|D ƒ‰t‡‡fd
d„|D ƒƒ}t|ƒrœ|d |krœt|d fƒ}ntƒ }t| |||ƒ}dddddg}|
}dd„ tt	j
 |dd¡ƒD ƒ}g }t|ƒsˆdkrdddg}t}nˆ d¡rdg}t}t||ˆ||
|ƒ\} }!t|ˆ|
| |||ƒ\}"}#}#}$xª|D ]¢}%t	j
 ˆ|%¡}&t|&ƒ}'|'rV|'tˆ d¡d  kr’qV|!|% }(xZ|(D ]R})|r |) d¡s |) |¡s |) |¡s |)|"kr td |)¡ƒ t d¡ q W qVW ||7 }t|||!|
ˆ| ||||||||||ˆƒ ||krºd | ¡}*d  | ¡}+d! | ¡},xd|| D ]X}-ˆ|- d"kr€|rz|,n|+}.nˆ|- d#kr”|*}.n|+}.t|d$ |.ˆ|- |-¡|d% q^W t|ƒr|rd&d„ |D ƒ}/t|/ƒrêt|/ƒ‚d'd„ |D ƒ}0t|0ƒrt|0ƒ‚n
t d¡ d S )(NTz	$SYSROOT/z$PATH/c             S   s   g | ]}|  d ¡d ‘qS )r’   r   )r\   )rd   ÚreqrT   rT   rU   re   N  s    z*check_overlinking_impl.<locals>.<listcomp>r%   z	libgcc-ngc          	      s    i | ]}t |ˆˆˆ ˆˆƒ|“qS rT   )rÕ   )rd   Úpackage)rÔ   rÊ   rÉ   rå   rÇ   rT   rU   ú
<dictcomp>S  s   z*check_overlinking_impl.<locals>.<dictcomp>c                s*   g | ]"}|j d  ˆ krˆ| dkr|‘qS )r   znon-library)rÖ   )rd   r  )Úignore_listÚpackage_naturerT   rU   re   U  s    rk   ÚmainÚ_mainz*get_pc_thunk*Z___clang_call_terminateZ_timeoutc             S   s   g | ]}|t j ‘qS rT   )r.   r˜   )rd   rá   rT   rT   rU   re   e  s    z**rá   zosx-64z	/usr/lib/z	/opt/X11/z/System/Library/Frameworks/r$   z
C:/WindowsrZ   r   r©   z"  ERROR :: {} not in prefix_ownersr+   z   INFO ({})zWARNING ({})z  ERROR ({})zrun-exports libraryzplugin libraryz´{}: {} package {} in requirements/run but it is not used (i.e. it is overdepending or perhaps statically linked? If that is what you want then add it to `build/ignore_run_exports`))r´   c             S   s   g | ]}d |kr|‘qS )ZoverlinkingrT   )rd   ÚerrorrT   rT   rU   re   œ  s    c             S   s   g | ]}d |kr|‘qS )ZoverdependingrT   )rd   r!  rT   rT   rU   re   Ÿ  s    )rØ   r   Zensure_listrŒ   rš   r”   r›   rù   r   r.   r/   r0   ÚDEFAULT_MAC_WHITELISTÚDEFAULT_WIN_WHITELISTrì   rö   r   r  r\   rJ   rv   rE   r¥   r  rü   r   r   )1r÷   rø   rÜ   rÚ   rÇ   Zignore_run_exportsr  Zrequirements_buildZrequirements_hostrå   r±   Úmissing_dso_whitelistr  r  Úerror_overdependingr´   Zexception_on_errorrw   rÔ   rÉ   rÊ   rú   rã   rä   rÌ   r  r  rò   rð   r  rè   r  ré   rê   ró   r   rô   rM   r/   r  rë   r  r  r  r  r®   r  Zoverlinking_errorsZoverdepending_errorsrT   )rÔ   rÊ   r  rÉ   r  rå   rÇ   rU   Úcheck_overlinking_implA  sŒ    



















r&  c             C   sê   t |  d¡|  d¡|  d¡|  d¡| jj|  d¡dd„ | j di ¡ d	g ¡D ƒd
d„ | j di ¡ dg ¡D ƒdd„ | j di ¡ dg ¡D ƒ| jj| jj| j di ¡ dg ¡| j di ¡ dg ¡| jj| jj	| jj
d|| jj| jj| jjƒS )Nzpackage/namezpackage/versionzbuild/stringzbuild/numberzbuild/ignore_run_exportsc             S   s   g | ]}|  d ¡d ‘qS )r’   r   )r\   )rd   r  rT   rT   rU   re   ­  s    z%check_overlinking.<locals>.<listcomp>ZrequirementsÚrunc             S   s   g | ]}|  d ¡d ‘qS )r’   r   )r\   )rd   r  rT   rT   rU   re   ®  s    Zbuildc             S   s   g | ]}|  d ¡d ‘qS )r’   r   )r\   )rd   r  rT   rT   rU   re   ¯  s    Zhostr$  r  T)r&  Ú	get_valuer`   Útarget_subdirÚmetarÎ   r^   r±   r  r%  r´   rÈ   rÉ   rÊ   )rR   rw   rT   rT   rU   Úcheck_overlinking¦  s*    
r+  c             C   sˆ   t j | jj|¡}t|ƒ}|s"d S |  ddg¡}tj 	d¡rX|dkrXt
|| jj|d n,tjdkr„|dkr„t|| jj| jj||d d S )	Nzbuild/rpathsr®   r%   r#   )r·   r   r"   )rw   r·   )r.   r/   r0   r`   r^   r   r(  rE   rF   rŒ   rÅ   r»   r±   )rR   rM   rw   r/   Z
codefile_tr·   rT   rT   rU   Úpost_process_shared_lib¾  s    r,  c       	      C   sü   t dƒ x$t|ƒD ]}| ¡ rt|jdƒ qW xÈ| D ]À}tj ||¡}t |¡}t 	|j
¡}|}|tj@ rz|tjB tjB }|tjB tjB tjB tjB tjB }||kr4yt||ƒ W q4 ttjfk
rò } zt t¡}| t|ƒ¡ W d d }~X Y q4X q4W d S )NzFixing permissionsiý  )rJ   r!   Úis_dirr   r/   r.   r0   Úlstatr3   ÚS_IMODEÚst_modeÚS_IXUSRÚS_IXGRPÚS_IXOTHÚS_IWUSRÚS_IWGRPÚS_IRUSRÚS_IRGRPÚS_IROTHr@   r   ÚPermissionErrorZ
get_loggerrÞ   Úwarnru   )	rw   rN   r/   rM   ÚstZold_modeZnew_modery   ÚlogrT   rT   rU   Úfix_permissionsÊ  s$    


"
r=  c             C   sð   t dt|ƒƒ x|D ]}t|| jjƒ qW | jj d¡sÜ|  ¡ }|sLt dƒ | jjdkoft|  	dd¡ƒ}t
|| jj| jjƒ t | jj¡}xP|D ]H}| d¡r²t|| jj||d |d	ksÌt|tƒr||krt| ||ƒ qW tjsìt| |ƒ d S )
Nznumber of files:r$   z Skipping binary relocation logiczosx-64zbuild/osx_is_appFzbin/)rN   rO   rP   T)rJ   r›   Úmake_hardlink_copyr`   r^   r)  rŒ   Úbinary_relocationÚboolr(  Úcheck_symlinksÚcrootr   Úprefix_filesrV   Ú
isinstancer{   r,  Zon_winr+  )rR   rw   rO   rM   r?  rP   rC  rT   rT   rU   Ú
post_buildâ  s(    


rE  c             C   s<  t dkrd S g }tj |¡}xè| D ]à}tj ||¡}tj |¡r"t |ƒ}tj |¡}tj |¡tj |¡ksŒt|ƒrŒt |¡ t	 
||¡ q"| |¡rætj |tj |¡¡}	| d¡sä||	krätd||f ƒ t |¡ t |	|¡ q"| |¡r"| d||f ¡ q"W |r8x |D ]}
td|
 tjd qW t d¡ d S )NFrW   z)Making absolute symlink %s -> %s relativezN%s is a symlink to a path that may not exist after the build is completed (%s)z	Error: %s)rõ   r+   )r   r.   r/   Úrealpathr0   r1   r™   r   Úremover   rs   rŒ   r°   rJ   r€   Úsymlinkrš   rE   Ústderrr¥   )rw   rN   rB  ZmsgsZreal_build_prefixrM   r/   Z	link_pathZreal_link_pathZrelative_pathÚmsgrT   rT   rU   rA  ý  s4    





rA  c          	   C   s€   t j | ¡s"t j t j || ¡¡} t j | ¡}t  | ¡jdkr|tƒ 2}t	 
| |¡ t	 | ¡ t	 
t j ||¡| ¡ W dQ R X dS )zkHardlinks create invalid packages.  Copy files to break the link.
    Symlinks are OK, and unaffected here.r+   N)r.   r/   Úisabsr‰   r0   rG   r.  Úst_nlinkr   r   rs   rp   )r/   rN   ra   ÚdestrT   rT   rU   r>  &  s    
r>  c             C   sf   | j j}tj tj |d¡¡r&tdƒ‚tj tj |d¡¡rDtdƒ‚tj tj |d¡¡rbtdƒ‚d S )Nz__conda_version__.txtz¬support for __conda_version__ has been removed as of Conda-build 3.0.Try Jinja templates instead: http://conda.pydata.org/docs/building/meta-yaml.html#templating-with-jinjaz__conda_buildnum__.txtz­support for __conda_buildnum__ has been removed as of Conda-build 3.0.Try Jinja templates instead: http://conda.pydata.org/docs/building/meta-yaml.html#templating-with-jinjaz__conda_buildstr__.txtz­support for __conda_buildstr__ has been removed as of Conda-build 3.0.Try Jinja templates instead: http://conda.pydata.org/docs/building/meta-yaml.html#templating-with-jinja)r`   Zwork_dirr.   r/   rr   r0   rÀ   )rR   Zsrc_dirrT   rT   rU   Úget_build_metadata8  s    rN  )F)F)rT   )FFrT   )N)r³   )r³   )\Z
__future__r   r   r   Úcollectionsr   r–   Ú	functoolsr   rí   r   r6   r8   r<   r.   ro   r3   Ú
subprocessr   r	   r
   rE   r   ÚImportErrorZconda_build.os_utilsr   Zconda_build.conda_interfacer   r   r   r   r   r   r   Zconda_buildr   Zconda_build.os_utils.lieflddr   r   r   Zconda_build.os_utils.pylddr   Zconda_build.os_utils.lddr   Zconda_build.indexr   r   Zconda_build.inspect_pkgr   Zconda_build.exceptionsr   r   rF   r    r!   r  rV   rc   rz   rƒ   r„   r…   rˆ   r£   r§   r¨   r­   r²   r»   rÅ   r¶   rÑ   rÕ   rØ   rÙ   r"  r#  rì   rö   rù   rü   r  r  r  r&  r+  r,  r=  rE  rA  r>  rN  rT   rT   rT   rU   Ú<module>   sò   


?
7
4

&3

.!
	4=.&e)