U
    ®8dmB  ã                   @   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/pr-199/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   s2   |   d¡ |   d¡ |   d¡ | jdddd� d S )NÚcompiler_cxxÚdatarepoÚwcb_unit_testz--docsr   zTcomma separated list of docs to generate.  eg: 'org2hml,org2pdf,doxy'.  default:none)ÚdefaultÚhelp)Ú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¡ddgd� | jdddd� | jddd
d� | jj 	d¡| j_
d S )Nr3   r4   r5   ZrpathifyÚorgÚCXXFLAGSz
-std=c++17ZpythonZPYTHONT)ÚvarÚ	mandatoryZbashZBASHZbatsZBATSFztest/bats/binz/usr/binz/usr/local/bin)r>   r?   Ú	path_listÚjsonnetZJSONNETÚdiffZDIFFú,)r8   ÚenvÚappend_uniqueÚfind_programÚosÚpathÚrealpathr;   ÚdocsÚsplitÚDOCS)Úcfgr   r   r   Ú	configureN   s    





ÿrN   c                 C   s:   |   d¡ |   d¡ |   d¡ | j d| jj d¡¡ d S )Nr5   r4   r<   rL   rC   )r8   rD   rE   r;   rJ   rK   )Úbldr   r   r   Úbuildf   s    


rP   )Ú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d„Zd-dd„Zd.dd„Zdd„ Zd/dd „Zd!d"„ Zd#d$„ Zd0d&d'„Zd1d(d)„Zd*d+„ ZdS )2Ú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„   || _ t|ƒ| _t|jj d¡ ¡ d�| jd< | j j	j
sNtd| j jj ƒ dS tdd�}d}tD ]}|  |¡ ||j	jkr` q€q`dS )	zC
        Uses must include list of dependencies in "uses".
        rV   )ZBATS_LIB_PATHrT   zsmplpkgs: tests suppressed for NrU   rW   ú
test/%s*%s)rO   r   Úusesr   rH   ÚparentÚfind_dirÚabspathÚscript_environrD   ÚTESTSr   r/   rQ   Údo_groupZ
TEST_GROUP)r   rO   rY   Úextra_prefixesÚ	match_patÚgroupr   r   r   r   †   s    



zValidationContext.__init__c           	      C   sæ   | j  d| ¡ |dkrdnd}|g}|| j |g ¡7 }d}|D ] }| jD ]@}|||f }| j j |¡D ] }td||f ƒ |  ||¡ qhqJ|dkr–q@| j	D ]B}|||f }| j j |¡D ]"}td|› d|› �ƒ |  
|¡ qºqœq@d S )	NZtesting_Úcheckr   rV   rX   ztests: (%s) %sztests: (z
) script: )rO   Úcycle_groupr`   ÚgetÚcompiled_extensionsrH   Úant_globr   ÚprogramÚscript_templatesÚscript)	r   rb   ÚfeaturesÚprefixesra   ÚprefixÚextÚpatÚoner   r   r   r_   ¡   s$    

zValidationContext.do_groupc                 C   s    | j jjstd| j jj ƒ | S )Nz7smplpkgs: variant checks will not be built nor run for )rO   rD   r^   r   rH   r/   r   r   r   r   Ú	__enter__¼   s    
zValidationContext.__enter__c                 C   s   | j  d¡ d S )NÚ	libraries)rO   rd   )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   ÚupperrO   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)rO   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   )rO   rH   r€   r�   r‚   Úfind_or_declarer„   r   r   r   Únodify_declareà   s
    z ValidationContext.nodify_declarer   c                 C   sn   | j jjsdS ddgt|ƒ }| j  | j¡}|  |¡}| ¡ }| j j|g|j	 
|d¡|ddddg| jd� dS )	zrCompile a C++ program to use in validation.

        Add "test" as a feature to also run as a unit test.

        NZcxxZ
cxxprogramr   ÚincrV   Ztests)ÚsourceÚtargetrk   Úinstall_pathÚincludesÚuse)rO   rD   r^   r   Ú	get_rpathrY   r…   Úsuffixrh   r/   Úreplace)r   r‰   rk   Zrpathrn   r   r   r   rh   é   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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test_scriptsÚ_)rk   r/   Zut_cwdr�   Ztest_scripts_envZtest_scripts_sourceZtest_scripts_templaterŠ   )rO   rD   r^   r…   r�   r/   r�   rH   r†   ri   re   r	   r   rK   Ústripr   rG   Úenvironr.   r]   ÚbasenamerY   )	r   r‰   rn   r/   ZoutnodeZtemplÚcliZinterpZtsenvr   r   r   rj   ý   s4    


ùzValidationContext.scriptc                 K   s*   | j jjsdS | j f |||dœ|—Ž dS )z!Simple wrapper for arbitrary ruleN©Úruler‰   rŠ   )rO   rD   r^   )r   r˜   r‰   rŠ   r*   r   r   r   r˜   %  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)
rO   rD   r^   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‘   )rO   rD   r^   rš   r›   r�   r\   Úexec_command)r   rŸ   r    r¡   Úlocalr   r   r   Úrule_scp_get8  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>N  ó    z,ValidationContext.get_file.<locals>.<lambda>zscp:c                    s
   ˆ   | ¡S r   )r§   r©   r   r   r   rª   P  r«   z'get file from absolute path not allowedz.urlr—   )rO   rD   r^   Ú
startswithÚ
ValueErrorrH   r†   rZ   Ú	make_noder/   rž   )r   r¡   r¦   Zrule_getZlnoder    r   r   r   Úget_fileI  s    



zValidationContext.get_filec                 C   s   | j jjsd S tƒ ‚d S r   )rO   rD   r^   ZUnimplemented)r   r¦   r¡   r   r   r   Úput_fileZ  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)rO   rD   r^   r‡   rZ   r†   r/   )r   rp   ZtwoZdnoder   r   r   rB   _  s    


  ÿzValidationContext.diff)N)N)r   )r   r   )r¨   r¨   )r¨   r¨   )r0   r1   r2   rf   ri   r]   r   r`   r   r_   rq   rv   r~   r…   r‡   rh   rj   r˜   r£   r§   r¯   r°   rB   r   r   r   r   rR   t   s0   ú

	
	
(


rR   Tc           
      C   s˜   t | jd d gƒ}|D ]j}| jd|  }|D ]}| |¡ q.|r| d¡r|dd …  ¡ }| j | j¡}| |¡ ¡ }	| |	¡ qt	|ƒ}| 
¡  |S )NÚPREFIXz/libÚLIBPATH_ÚWireCellé   )ÚsetrD   Úaddr¬   ÚlowerrH   r†   Úout_dirr\   ry   Úsort)
rO   Zuselstr¦   Úretrp   ZlibpathÚlÚsdZblddirZpkgdirr   r   r   rŽ   j  s    
rŽ   r   c              	   C   s<  t tt|ƒƒƒ}| ¡  t t|t|ƒ ƒƒ}| ¡  t t|t|ƒ ƒƒ}| ¡  t| dƒsbtƒ | _| jj|tt|ƒƒtt|ƒƒtt|ƒƒd� | jg}g }g }| j	 
d¡}| j	 
d¡}	| j	 
d¡}
| j	 
d¡}| j	 
d¡}|  d¡ |�r|| |d	 ¡7 }|dg7 }| ¡ g| jd
| < |�r0|  d| |¡ |	�r`||	 d¡7 }||	 d¡7 }||	 d¡7 }|
�rÄ|�sxtd| ƒ d|k�r¸|
 d¡}| j|||||d� | | j	 |d ¡¡ ntd| ƒ t| jdƒ�ræ| j	 d¡}||7 }|	�rd}|�rúd}| d||||||d� |  d¡ tƒ }|�rˆ| d¡D ]V}| j	 |j dd¡¡}| j|g||||g d� |j ¡  dd¡}| ¡ | j|< �q0t| ||g ƒ}| j	 d¡}|�r8|  d ¡ d!| jjk�rêtd"|› �ƒ |j d#¡}| d!||d$� d%| jjk�r8td&|› �ƒ | j	 d'| j	j› d(�¡}| d%||d$� |  d)|¡ |S )*NÚsmplpkg_graphr   rˆ   Úsrcr   ZappsrJ   rr   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cxx cxxshlib)rk   r/   r‰   rŠ   rŒ   Zexport_includesr�   ZapplicationsrS   )r‰   rŠ   rŒ   r�   ú-r’   z
README.orgÚdocumentationÚorg2htmlzsmplpkgs: org2html zREADME.html)rk   r‰   rŠ   Úorg2pdfzsmplpkgs: org2pdf zwct-readme-z.pdfz${PREFIX}/share/doc) ry   r¶   r   rº   Úhasattrr
   r¾   r-   r¹   rH   r[   rd   rg   r\   rD   Úinstall_filesr   rƒ   Zgen_rootcling_dictr   r†   r	   r   r/   r�   rh   rz   rR   rL   r   rZ   r®   )rO   r/   r�   Zapp_useZtest_userŒ   rÁ   r‰   ÚincdirZsrcdirZdictdirZappsdirZdocdirZlinkdefZpbsÚeiZvalidation_envsr   ZappbinZkeynameZvcÚreadmeÚhtmlÚpdfr   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   Údirnamer\   Ú__file__Z_tooldirr;   rN   rP   r5   rQ   rR   rŽ   rÍ   r   r   r   r   Ú<module>   s$   ) w