U
    úá)feJ  ã                   @   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   ú;/home/yuhw/wc/larsoft925/src/wire-cell-dev/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   sv   || _ t|ƒ| _t|jj d¡ ¡ d�| jd< | j j	j
sNtd| j jj ƒ dS tD ]}|  |¡ ||j	jkrR qrqRdS )zC
        Uses must include list of dependencies in "uses".
        r\   )ÚBATS_LIB_PATHr[   zsmplpkgs: tests suppressed for N)rV   r   Úusesr   rH   ÚparentÚfind_dirÚabspathÚscript_environrD   ÚTESTSr   r/   rX   Údo_groupZ
TEST_GROUP)r   rV   rb   Úgroupr   r   r   r   œ   s    


zValidationContext.__init__c                 C   sâ   | j  d| ¡ |dkrdnd}|g}|| j |g ¡7 }|D ] }| jD ]@}|  ||¡}| j j |¡D ] }td||f ƒ |  	||¡ qdqF|dkr’q<| j
D ]B}|  ||¡}| j j |¡D ]"}td|› d|› �ƒ |  |¡ q¶q˜q<d S )NZtesting_Úcheckr   r\   ztests: (%s) %sztests: (z
) script: )rV   Úcycle_groupÚextra_prefixesÚgetÚcompiled_extensionsr`   rH   Úant_globr   ÚprogramÚscript_templatesÚscript)r   ri   ÚfeaturesÚprefixesr]   r^   ÚpatÚoner   r   r   rh   ¯   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 )rV   rD   rg   r   rH   r/   r   r   r   r   Ú	__enter__É   s    
zValidationContext.__enter__c                 C   s   | j  d¡ d S )NÚ	libraries)rV   rk   )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   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   Úincr\   Útests)ÚsourceÚtargetrs   Úinstall_pathÚincludesÚuse)rV   rD   rg   r   Ú	get_rpathrb   r‹   Úsuffixrp   r/   Úreplace)r   r�   rs   Úrpathr^   r   r   r   rp   ö   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Ú_)rs   r/   Zut_cwdr”   Ztest_scripts_envZtest_scripts_sourceZtest_scripts_templater‘   )rV   rD   rg   r‹   r–   r/   r—   rH   rŒ   rq   rm   r	   r   rK   Ústripr   r   rG   Úenvironr.   rf   Úbasenamerb   )	r   r�   r^   r/   ZoutnodeZtemplÚcliZinterpZtsenvr   r   r   rr   
  s6    


ùzValidationContext.scriptc                 K   s*   | j jjsdS | j f |||dœ|—Ž dS )z!Simple wrapper for arbitrary ruleN©Úruler�   r‘   )rV   rD   rg   )r   r    r�   r‘   r*   r   r   r   r    3  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_getF  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>\  ó    z,ValidationContext.get_file.<locals>.<lambda>zscp:c                    s
   ˆ   | ¡S r   )r¯   r±   r   r   r   r²   ^  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_fileW  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_fileh  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   rv   ZtwoZdnoder   r   r   rB   m  s    


  ÿzValidationContext.diff)N)N)r   )r   r   )r°   r°   )r°   r°   )r0   r1   r2   rn   rq   rf   r   rl   r`   r   rh   rw   r|   r„   r‹   r�   rp   rr   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_ZWireCellé   )ÚsetrD   ÚaddrU   ÚlowerrH   rŒ   Úout_dirre   r   Úsort)
rV   Zuselstr®   Úretrv   ZlibpathÚlÚsdZblddirZpkgdirr   r   r   r•   x  s    
r•   r   c           $   
   C   sx  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¡}| j	 
d¡}|  d	¡ |�r&|| |d
 ¡7 }|dg7 }| ¡ g| jd| < |�r<|  d| |¡ |	�rl||	 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 }|	�rd}|�r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-d.¡}| ¡ | j|< �qt | ||g ƒ}| j! 
| j"¡}| j! 
| j¡}td/|› d0|› d1|› �ƒ | j	 d2¡g}|�rÈ|| d3¡7 }d4d$„ |D ƒ}|�rt|  d5¡ |D ]ˆ} d6D ]|}!d7|! }"|"| jj#k�rò| j | j d8d9|! ¡¡}#| | jjd- |#j d9d-¡ |"| |#d:� |!d;k�rò| j| jj$|#|d<d=� �qò�qê|S )>NÚsmplpkg_graphr   rŽ   Úsrcr   ZappsrJ   r\   rx   z/*.hÚ	INCLUDES_z${PREFIX}/include/%sz*.cxxz*.cuz*.kokkosz(No header files for ROOT dictionary "%s"Ú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)rs   r/   r�   r‘   r“   Zexport_includesr”   Zapplicationsc                 S   s*   | j d }td|› �ƒ d}| |¡ d S )Nr   zgenerating doctest main: ah  
#define DOCTEST_CONFIG_IMPLEMENT
#include "WireCellUtil/doctest.h"
#include "WireCellUtil/Logging.h"
#include "spdlog/spdlog.h"
#include "spdlog/cfg/env.h"
int main(int argc, char** argv) {
    WireCell::Log::add_stderr();
    spdlog::cfg::load_env_levels();
    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)rs   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/   rs   r�   r‘   rÒ   T)ÚcwdÚrelative_trick)%r   r»   r   r¿   Úhasattrr
   rÃ   r-   r¾   rH   rd   rk   ro   re   rD   Úinstall_filesr   r‰   Zgen_rootcling_dictr   rŒ   r	   rc   r/   Úinsertr   r   rp   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$   ) w