a
    Âè“g-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   ú:/home/yuhw/wc/larsoft925/src/wct-porting2/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   sH   |   |¡ | ¡ D ]0\}}|di}|D ]}| j||ffi |¤Ž q&qd S )NT)Úadd_noder   Úadd_edge)r   ÚpkgÚkwdsr$   ÚdepsÚotherr   r   r   Úregister0   s
    
zSimpleGraph.registerc                 K   s*   || j v r| j |  |¡ n
|| j |< d S r   )r   Úupdate)r   Únamer*   r   r   r   r'   7   s    
zSimpleGraph.add_nodec                 K   s*   || j v 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\   )Ú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   rc   ZtdirÚgroupr   r   r   r   œ   s    

zValidationContext.__init__c              	   C   s  | j  d| ¡ d}|dv 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   rh   Úextra_prefixesÚgetr   Úcompiled_extensionsra   rH   Úant_globÚprogramÚscript_templatesÚscript)r   rj   ÚfeaturesÚprefixesr^   r_   ÚpatÚoner   r   r   ri   ³   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   rm   )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v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u 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u 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g d¢| jd� dS )z5Compile a C++ program to use in validation.

        ZcxxZ
cxxprogramr   N)Úincr\   Útests)ÚsourceÚtargetru   Úinstall_pathÚincludesÚuse)	r   rV   Ú	get_rpathrc   r�   Úsuffixrr   r/   Úreplace)r   r’   ru   Úrpathr_   r   r   r   rr   þ   s    

úzValidationContext.programc           	   
   C   s  | j jjsdS |  |¡}| ¡ }|j |d¡}| j j |d ¡}| j	 
|d¡}|du 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Ú_)ru   r/   Zut_cwdr–   Ztest_scripts_envZtest_scripts_sourceZtest_scripts_templater“   )rV   rD   rh   r�   r˜   r/   r™   rH   rŽ   rs   ro   r	   r   rK   Ústripr   r   rG   Úenvironr.   rg   Úbasenamerc   )	r   r’   r_   r/   ZoutnodeZtemplÚcliZinterpZtsenvr   r   r   rt     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   rh   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   rh   r¥   r¦   r¨   rf   Ú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   rh   rU   Ú
ValueErrorrH   rŽ   rd   Ú	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   rh   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   rh   r�   rd   rŽ   r/   )r   rx   ZtwoZdnoder   r   r   rB   s  s    


ÿzValidationContext.diff)N)N)r   )r   r   )r³   r³   )r³   r³   )r0   r1   r2   rp   rs   rg   r   rn   ra   r   ri   ry   r~   r†   r�   r�   rr   rt   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_dirrf   r�   Úsort)
rV   Zuselstr±   Úretrx   ZlibpathÚlÚsdZblddirZpkgdirr   r   r   r—   ~  s    
r—   r   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|v �rÞ|
 d¡}ˆ j|||d� | ˆ j |d ¡¡ ntd| ƒ tˆ jdƒ�rˆ j d¡}||7 }|	�rDd}|�r d}td|› �ƒ ˆ d||||||d� ˆ  d¡ ‡ f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$v �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\   rz   z/*.hÚ	INCLUDES_z${PREFIX}/include/%sz*.cxxz*.cuz*.kokkosz(No header files for ROOT dictionary "%s"ÚROOTSYSz	LinkDef.h)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)ru   r/   r’   r“   r•   Zexport_includesr–   Zapplicationsc                    s2   | j d }td|› �ƒ dˆ jj }| |¡ 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","%s",true);
    doctest::Context context;
    context.applyCommandLine(argc, argv);
    return context.run();

})r¨   r   r<   Úwith_spdlog_active_levelr©   )Ú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>û  r¶   zsmplpkg.<locals>.<listcomp>Zmake_wcdoctest_Ú_source)r/   r¢   r“   z
smplpkgs: z --> zcxx cxxprogram test)ru   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Ó   (  r¶   Údocumentation)ÚhtmlÚpdfÚorg2z.orgÚ.)r/   ru   r’   r“   rØ   T)ÚcwdÚrelative_trick)&r�   r¿   r   rÃ   ÚhasattrrÇ   r   r
   rÈ   r-   rÂ   rH   re   rm   rq   rf   rD   Úinstall_filesr   r‹   Zgen_rootcling_dictrŽ   r	   r   rd   r/   Úinsertr   rr   r—   r   r™   r‚   rY   ÚrootÚtop_dirrL   rT   )$rV   r/   r–   Zapp_useZtest_user•   Zheadersr’   Ú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   Údirnamerf   Ú__file__Z_tooldirr<   rP   rW   r4   rX   rY   r—   rè   r   r   r   r   Ú<module>   s$   ) }