U
    !šbgL  ã                   @   sÊ   d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	Z
ddlmZmZmZmZ G dd„ deƒZej ej e¡¡Zd	d
„ Zdd„ Zdd„ ZddlmZ G dd„ dƒZeddd„ƒZeddd„ƒZdS )aj  This tool implements a source package following a few contentions.

Your source package may build any combination of the following:

 - shared libraries (src/*.cxx)
 - headers exposing an API to libraries (inc/NAME/*.h)
 - a ROOT dictionary for this API
 - main programs (apps/*.cxx)
 - test programs (test/test_*, test/check_*, wscript_build)

This tool will produce various methods on the build context.  You can
avoid passing <name> to them if you set APPNAME in your wscript file.

This file is part of the wire-cell-toolkit but we keep it free of
direct concepts specific to building WCT.  See wcb.py for those.

é    N)Úcontextmanager)Úto_listÚ
subst_vars)Úconf)ÚdebugÚinfoÚerrorÚwarnc                   @   sB   e Zd Zedddd�Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚSimpleGraphZblackZblueZgray©ÚlibÚappÚtstc                 C   s   t ƒ | _t ƒ | _d S ©N)ÚdictÚ_nodesÚ_edges©Úself© r   ú4/home/yuhw/wc/larsoft925/src/0.29.2/waft/smplpkgs.pyÚ__init__   s    zSimpleGraph.__init__c              	   C   sŽ   dg}| j  ¡ D ]\}}| d| ¡ q| j ¡ D ]F\}}|D ]8}d}|dkrRd}| d|d |d | j| |f ¡ q>q2| d	¡ d
 |¡S )Nzdigraph deps {z	"%s";Ú r   z,constraint=falsez	"%s" -> "%s"[color="%s"%s];r   é   ú}Ú
)r   ÚitemsÚappendr   ÚcolorsÚjoin)r   ÚlinesÚnodeÚattrsÚedgeÚcatÚextrar   r   r   Ú__str__!   s    ÿ

zSimpleGraph.__str__c                 K   sD   |   |¡ | ¡ D ],\}}|di}|D ]}| j||ff|Ž q&qd S )NT)Úadd_noder   Úadd_edge)r   ÚpkgÚkwdsr$   ÚdepsÚotherr   r   r   Úregister0   s
    
zSimpleGraph.registerc                 K   s*   || j kr| j |  |¡ n
|| j |< d S r   )r   Úupdate)r   Únamer*   r   r   r   r'   7   s    
zSimpleGraph.add_nodec                 K   s*   || j kr| j |  |¡ n
|| j |< d S r   )r   r.   )r   r#   r*   r   r   r   r(   <   s    
zSimpleGraph.add_edgeN)
Ú__name__Ú
__module__Ú__qualname__r   r   r   r&   r-   r'   r(   r   r   r   r   r
      s   r
   c                 C   sL   |   d¡ |   d¡ |   d¡ |   d¡ | jdddd� | jd	d
dd� d S )NÚcompiler_cxxÚwcb_unit_testÚdatarepoÚorgz--docsr   zTcomma separated list of docs to generate.  eg: 'org2hml,org2pdf,doxy'.  default:none)ÚdefaultÚhelpz--docs-prefixzshare/doc/wirecellz:Installation root for docs, appended to PREFIX if relative)ÚloadÚ
add_option)Úoptr   r   r   ÚoptionsF   s    



ÿÿr<   c                 C   sÀ   |   d¡ |   d¡ |   d¡ |   d¡ |   d¡ | j ddg¡ | jdd	d
d� | jddd
d� | jdddtj d¡gd� | jdddd� | jddd
d� | jj 	d¡| j_
| jj| j_d S )Nr3   r4   r5   Zrpathifyr6   ÚCXXFLAGSz
-std=c++17ZpythonZPYTHONT)ÚvarÚ	mandatoryZbashZBASHZbatsZBATSFztest/bats/bin)r>   r?   Ú	path_listÚjsonnetZJSONNETÚdiffZDIFFú,)r9   ÚenvÚappend_uniqueÚfind_programÚosÚpathÚrealpathr<   ÚdocsÚsplitÚDOCSÚdocs_prefixÚDOCS_PREFIX)Úcfgr   r   r   Ú	configureQ   s    





ÿrP   c                 C   s²   |   d¡ |   d¡ |   d¡ | j d| jj d¡¡ t| jdd ƒ}|rP|| j_| jjd | j_	| jjrœ| jj 
d¡r„| jj| j_	n| jjd | jj | j_	td	| jj	› �ƒ d S )
Nr4   r5   r6   rL   rC   rM   z/share/doc/wirecellú/zsmplpkgs: docs install path: )r9   rD   rE   r<   rJ   rK   ÚgetattrrN   ÚPREFIXÚDOCS_INSTALL_PATHÚ
startswithr   )ÚbldrM   r   r   r   Úbuildi   s    


rW   )Útest_group_sequencec                   @   s¾   e Zd ZddgZddddddœZi Zed	d
�Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd.dd„Zd/dd„Zd0dd„Zdd „ Zd1d!d"„Zd#d$„ Zd%d&„ Zd2d(d)„Zd3d*d+„Zd,d-„ ZdS )4ÚValidationContextú.cxxz.kokkosz${PYTHON} ${SCRIPT}z${PYTEST} ${SCRIPT}z${BASH} ${SCRIPT}z${BATS} --jobs 1 ${SCRIPT}z${JSONNET} ${SCRIPT})z.pyz.pytz.shú.batsz.jsonnet)Útest©Zatomicc                 C   s   dj ||d�S )Nztest/{prefix}*{ext})ÚprefixÚext)Úformat)r   r^   r_   r   r   r   Úsource_glob˜   s    zValidationContext.source_globc                 C   sr   || _ t|ƒ| _|jj d¡}|r6t| ¡ d�| jd< t	d| j j
j› �ƒ tD ]}|  |¡ ||j
jkrN qnqNdS )zC
        Uses must include list of dependencies in "uses".
        r\   )ZBATS_LIB_PATHr[   zsmplpkgs: test status: N)rV   r   ÚusesrH   ÚparentÚfind_dirr   ÚabspathÚscript_environr   rD   ÚTESTSrX   Údo_groupZ
TEST_GROUP)r   rV   rb   ZtdirÚgroupr   r   r   r   œ   s    

zValidationContext.__init__c              	   C   s  | j  d| ¡ d}|dkr*| j jjr*d}|g}|| j |g ¡7 }td|› d|› d|› d�ƒ |D ]¦}| jD ]F}|  ||¡}| j j	 
|¡D ]&}td	|› d
|› d�ƒ |  ||¡ qŠql|dkr¾qb| jD ]B}|  ||¡}| j j	 
|¡D ]"}td	|› d|› �ƒ |  |¡ qâqÄqbd S )NZtesting_r   r]   r\   ztests: group "z" with features "z" and prefixes "ú"ztests: (z) source: "Úcheckz
) script: )rV   Úcycle_grouprD   rg   Úextra_prefixesÚgetr   Úcompiled_extensionsra   rH   Úant_globÚprogramÚscript_templatesÚscript)r   ri   ÚfeaturesÚprefixesr^   r_   ÚpatÚoner   r   r   rh   ³   s*    

zValidationContext.do_groupc                 C   s   | S r   r   r   r   r   r   Ú	__enter__Ñ   s    zValidationContext.__enter__c                 C   s   | j  d¡ d S )NÚ	libraries)rV   rl   )r   Úexc_typeÚ	exc_valueÚexc_tracebackr   r   r   Ú__exit__Ö   s    zValidationContext.__exit__c                 G   sR   |st S tƒ }|D ]}|t|ƒ7 }qd}|D ] }d| ¡  }|| jjkr,d}q,|S )z<
        Return true if we are built with all pkgs 
        TÚHAVE_F)ÚtrueÚlistr   ÚupperrV   rD   )r   ÚargsZpkgsÚaZhave_allr)   Úmayber   r   r   ÚhasÚ   s    zValidationContext.hasNc                 C   s,   |dkr| j j}t|tjjƒr"|S | |¡S ©zReturn a resource nodeN)rV   rH   Ú
isinstanceÚwaflibÚNodeÚfind_resource©r   Zname_or_noderH   r   r   r   Únodify_resourceì   s
    z!ValidationContext.nodify_resourcec                 C   s,   |dkr| j j}t|tjjƒr"|S | |¡S r†   )rV   rH   r‡   rˆ   r‰   Úfind_or_declarer‹   r   r   r   Únodify_declareõ   s
    z ValidationContext.nodify_declarer   c                 C   s`   ddgt |ƒ }| j | j¡}|  |¡}| ¡ }| jj|g|j |d¡|ddddg| jd� dS )	z5Compile a C++ program to use in validation.

        ZcxxZ
cxxprogramr   NÚincr\   Ztests)ÚsourceÚtargetrt   Úinstall_pathÚincludesÚuse)	r   rV   Ú	get_rpathrb   rŒ   Úsuffixrq   r/   Úreplace)r   r�   rt   Úrpathr_   r   r   r   rq   þ   s    

úzValidationContext.programc           	   
   C   s  | j jjsdS |  |¡}| ¡ }|j |d¡}| j j |d ¡}| j	 
|d¡}|dkrhtd|› �ƒ dS t|| j jƒ}| d¡d  ¡ }|sžtd|› �ƒ dS td|› d	|› �ƒ ttjƒ}| | j 
|i ¡¡ | j d
|d tj |¡ | j j| j||||gd� dS )z=Create a task for atomic unit test with an interpreted scriptNr   z.outz*skipping script of unsupported extention: ú r   z+skipping script with no found interpreter: zsmplpkgs: script z -> Ztest_scriptsÚ_)rt   r/   Zut_cwdr”   Ztest_scripts_envZtest_scripts_sourceZtest_scripts_templater‘   )rV   rD   rg   rŒ   r–   r/   r—   rH   r�   rr   rn   r	   r   rK   Ústripr   r   rG   Úenvironr.   rf   Úbasenamerb   )	r   r�   r_   r/   ZoutnodeZtemplÚcliZinterpZtsenvr   r   r   rs     s6    


ùzValidationContext.scriptc                 K   s   | j f |||dœ|—Ž dS )z!Simple wrapper for arbitrary rule©Úruler�   r‘   N)rV   )r   r    r�   r‘   r*   r   r   r   r    9  s    zValidationContext.rulec                 C   sT   | j jjsdS ddlm} |jd }| ¡ }||ƒ ¡  ¡ }|jd }| 	|¡ dS )z@A rule function transform a URL file into its target via HTTP(s)Nr   )Úurlopen)
rV   rD   rg   Zurllib.requestr¡   ÚinputsÚreadÚdecodeÚoutputsÚwrite)r   Útaskr¡   ÚunodeÚremoteÚtextZonoder   r   r   Úrule_http_get@  s    


zValidationContext.rule_http_getc                 C   sL   | j jjsdS |jd }| ¡ dd… }|jd  ¡ }| d|› d|› �¡S )z:A rule function to transform a URL file its target via scpNr   é   zscp r™   )rV   rD   rg   r¢   r£   r¥   re   Úexec_command)r   r§   r¨   r©   Úlocalr   r   r   Úrule_scp_getL  s    

zValidationContext.rule_scp_getú...c                    s„   ˆ j jjsdS | d¡r&‡ fdd„}n | d¡r>‡ fdd„}ntdƒ‚ˆ j j |¡}|j |j	d ¡}| 
|¡ ˆ j |||d	� dS )
z,Make task to bring remote file to local fileN)zhttp://zhttps://c                    s
   ˆ   | ¡S r   )r«   ©r§   r   r   r   Ú<lambda>b  ó    z,ValidationContext.get_file.<locals>.<lambda>zscp:c                    s
   ˆ   | ¡S r   )r¯   r±   r   r   r   r²   d  r³   z'get file from absolute path not allowedz.urlrŸ   )rV   rD   rg   rU   Ú
ValueErrorrH   r�   rc   Ú	make_noder/   r¦   )r   r©   r®   Zrule_getZlnoder¨   r   r   r   Úget_file]  s    



zValidationContext.get_filec                 C   s   | j jjsd S tƒ ‚d S r   )rV   rD   rg   ZUnimplemented)r   r®   r©   r   r   r   Úput_filen  s    
zValidationContext.put_filec                 C   sZ   | j jjsdS |  |¡}|  |¡}|j |jd |j d ¡}| j d||g|gdd� dS )z.Make a task to output a diff between two filesNrš   z.diffz${DIFF} ${SRC} > ${TGT}T)r    r�   r‘   Úshell)rV   rD   rg   rŽ   rc   r�   r/   )r   rw   ZtwoZdnoder   r   r   rB   s  s    


  ÿzValidationContext.diff)N)N)r   )r   r   )r°   r°   )r°   r°   )r0   r1   r2   ro   rr   rf   r   rm   ra   r   rh   rx   r}   r…   rŒ   rŽ   rq   rs   r    r«   r¯   r¶   r·   rB   r   r   r   r   rY   ‚   s2   ú

	
	
)


rY   Tc           
      C   s˜   t | jd d gƒ}|D ]j}| jd|  }|D ]}| |¡ q.|r| d¡r|dd …  ¡ }| j | j¡}| |¡ ¡ }	| |	¡ qt	|ƒ}| 
¡  |S )NrS   z/libÚLIBPATH_ÚWireCellé   )ÚsetrD   ÚaddrU   ÚlowerrH   r�   Úout_dirre   r€   Úsort)
rV   Zuselstr®   Úretrw   ZlibpathÚlÚsdZblddirZpkgdirr   r   r   r•   ~  s    
r•   r   c           $   
   C   s¤  t tt|ƒƒƒ}| ¡  t t|t|ƒ ƒƒ}| ¡  t t|t|ƒ ƒƒ}| ¡  t| dƒsbt ƒ | _| j |¡ t| dƒs€tƒ | _| jj	|tt|ƒƒtt|ƒƒtt|ƒƒd� | j
g}g }g }| j d¡}| j d¡}	| j d¡}
| j d¡}| j d¡}| j d	¡}|  d
¡ |�rD|| |d ¡7 }|dg7 }| ¡ g| jd| < |�rZ|  d| |¡ |	�rŠ||	 d¡7 }||	 d¡7 }||	 d¡7 }|
�rî|�s¢td| ƒ d|k�râ|
 d¡}| j|||||d� | | j |d ¡¡ ntd| ƒ t| jdƒ�r| j d¡}||7 }|	�rHd}|�r$d}td|› �ƒ | d||||||d� |  d¡ dd„ }|�r| d ¡}|�r|jj}| j d!|› �¡}| j d!|› d"�¡}| d#|¡ d$ d%d&„ |D ƒ¡}d'|› d(�}| |||d)� td*|› d+|› �ƒ | jd,d!|› �|||  ||g ¡||||g d-� tƒ }|�rŒ| d¡D ]V}| j |j d"d¡¡}| j|g||||g d.� |j  ¡  d/d0¡}| ¡ | j|< �q4t!| ||g ƒ}| j" | j#¡}| j" | j
¡}td1|› d2|› d3|› �ƒ | j d4¡g}|�rô|| d5¡7 }d6d&„ |D ƒ}|�r |  d7¡ |D ]ˆ} d8D ]|}!d9|! }"|"| jj$k�r| j | j d:d;|! ¡¡}#| | jjd/ |#j d;d/¡ |"| |#d<� |!d=k�r| j| jj%|#|d>d?� �q�q|S )@NÚsmplpkg_namesÚsmplpkg_graphr   r�   Úsrcr   ZappsrJ   r\   ry   z/*.hÚ	INCLUDES_z${PREFIX}/include/%sz*.cxxz*.cuz*.kokkosz(No header files for ROOT dictionary "%s"ZROOTSYSz	LinkDef.h)Úheadersr“   r”   zDict.cxxzMNo ROOT dictionary will be generated for "%s" unless "ROOTSYS" added to "use"ZPROTOCzsrc/**/*.protor   zsmplpkgs: library: zcxx cxxshlib)rt   r/   r�   r‘   r“   Zexport_includesr”   Zapplicationsc                 S   s*   | j d }td|› �ƒ d}| |¡ d S )Nr   zgenerating doctest main: a&  
#define DOCTEST_CONFIG_IMPLEMENT
#include "WireCellUtil/doctest.h"
#include "WireCellUtil/Logging.h"
int main(int argc, char** argv) {
    WireCell::Log::default_logging("stderr","info",true);
    doctest::Context context;
    context.applyCommandLine(argc, argv);
    return context.run();

})r¥   r   r¦   )ÚtskÚoutrª   r   r   r   Úwrite_doctest_mainã  s
    

z#smplpkg.<locals>.write_doctest_mainzdoctest*.cxxz
wcdoctest-rZ   r   z
	c                 S   s   g | ]}t |ƒ‘qS r   )Ústr)Ú.0Úsr   r   r   Ú
<listcomp>û  s     zsmplpkg.<locals>.<listcomp>Zmake_wcdoctest_Z_source)r/   r    r‘   z
smplpkgs: z --> zcxx cxxprogram test)rt   r/   r�   r‘   r˜   r“   r”   )r�   r‘   r“   r”   ú-rš   zsmplpkgs: TOP DIR: z from z or z
README.orgz*.orgc                 S   s   g | ]}|r|‘qS r   r   )rÍ   Úor   r   r   rÏ   (  s      Údocumentation)ÚhtmlÚpdfÚorg2z.orgÚ.)r/   rt   r�   r‘   rÓ   T)ÚcwdÚrelative_trick)&r€   r¼   r   rÀ   ÚhasattrrÄ   r   r
   rÅ   r-   r¿   rH   rd   rl   rp   re   rD   Úinstall_filesr   rŠ   Zgen_rootcling_dictr�   r	   r   rc   r/   Úinsertr   rq   r•   r   r—   r�   rY   ÚrootÚtop_dirrL   rT   )$rV   r/   r”   Zapp_useZtest_user“   rÈ   r�   ÚincdirZsrcdirZdictdirZappsdirZdocdirZtestdirZlinkdefZpbsÚeirË   ZdtsrcsZpkgnameZmainbinZmainsrcÚtmpZtnameZvalidation_envsr   ZappbinZkeynameZvcrÝ   Úbld_dirZorgsr6   r_   ÚfeatrÊ   r   r   r   Úsmplpkg�  sü    




ü



ýù	

 ÿ
ú
ü
  ÿ
 ÿrã   )T)r   r   r   )Ú__doc__rG   Ú
contextlibr   Úwaflib.Utilsr   r   Úwaflib.Configurer   Zwaflib.Contextrˆ   Úwaflib.Logsr   r   r   r	   Úobjectr
   rH   Údirnamere   Ú__file__Z_tooldirr<   rP   rW   r4   rX   rY   r•   rã   r   r   r   r   Ú<module>   s$   ) }