
    `i7                    
   d 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m	Z	 ddl
mZ ddl
mZ ddlmZmZ dd	lmZmZmZmZ dd
lmZmZmZmZ d"dZd#dZd$dZd%dZd&d Z e!d!k    r ej"         e                       dS dS )'z-TVM-FFI Stub Generator (``tvm-ffi-stubgen``).    )annotationsNPath   )codegen)consts)FileInfocollect_files)collect_global_funcscollect_type_keysobject_info_from_type_keytoposort_objects)FuncInfo
ImportItem
InitConfigOptionsreturnintc            
     &   t                      } | j        pg D ]}t          j        |           d | j        D             }t          d | j        D                       }t                      }d}| j        rGt          | j        d                   	                                }|
                                r|j        }t          j                                        }|D ]f}	 t          ||           # t           $ rF t#          t          j         d|j         dt)          j                     t          j                    Y cw xY w| j        r%|
J d            t1          ||| j        ||           |D ]}| j        r/t#          t          j         d	|j         t          j                    	 t7          || ||           L# t           $ rF t#          t          j         d|j         dt)          j                     t          j                    Y w xY w~dS )
zCommand line entry point for ``tvm-ffi-stubgen``.

    This generates in-place type stubs inside special ``tvm-ffi-stubgen`` blocks
    in the given files or directories. See the module docstring for an
    overview and examples of the block syntax.
    c                6    g | ]}t          j        |          S  )ctypesCDLL).0libs     d/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/stub/cli.py
<listcomp>z__main__.<locals>.<listcomp>2   s"    111FK111    c                ,    g | ]}t          |          S r   r   )r   fs     r   r   z__main__.<locals>.<listcomp>3   s    *F*F*Fq477*F*F*Fr   Nr   z[Failed] File "z": z!init-path could not be determined)init_cfg	init_pathglobal_funcsz[File] )_parse_argsimports	importlibimport_moduledllsr
   filesr   r   resolveis_fileparentCTY_MAP_DEFAULTScopy_stage_1	ExceptionprintTERM_REDpath	traceback
format_exc
TERM_RESETinit_stage_2verbose	TERM_CYAN_stage_3)optimpr(   r)   r#   r"   ty_mapfiles           r   __main__rA   (   sz    --C{ b % %$$$$11111D)*F*FCI*F*F*FGGE.B.D.DL!I
y )1&&..00	 	)!(I .3355F  	T6"""" 	 	 	:bbdibbI<P<R<RbTUT`bb    	 x 
$$&I$$$X%	
 	
 	
 	
   ; 	DQ[BBBALBBCCC	T35555 	 	 	:bbdibbI<P<R<RbTUT`bb    	 	1s&   C00AE ?E *F==AHHr@   r	   r?   dict[str, str]Nonec                N   | j         D ]}|j        dk    r	 t          |j        t                    sJ |j                            d          \  }}n+# t          $ r}t          d|j         d          |d }~ww xY w|                                ||                                <   d S )Nzty-mapz->zInvalid ty_map format at line z. Example: `A.B -> C.D`)	code_blockskind
isinstanceparamstrsplit
ValueErrorlineno_startstrip)r@   r?   codelhsrhses         r   r0   r0   c   s       	. 	.9  !$*c22222:++D11SS    _T5F___  #&))++F399;;	. 	.s   9A
A8A33A8r)   list[FileInfo]r!   r   r"   r   r#   dict[str, list[FuncInfo]]c                    d fd}d  D             }d  D             t           j        z  }|j                                        }|r|                    d          s|dz  }|                    d          }	t                      }
|D ]}|
                    |g            |
                                D ]\  }}||	k    s|	                    |          s"t          ||v rg n|                    |g           d	 
          }t          t          |          |z
            }t          |          }|s|s||                    dd          z  }|                    dd           |dz  } ||          }|                    dd          5 }|                    t%          j        |j        ||||||	k                         d d d            n# 1 swxY w Y   |                                 |dz  } ||          }|                    dd          5 }|                    t%          j        |j        |d                     d d d            n# 1 swxY w Y   |                                 d S )Nr4   r   r   r	   c                   |                                  st          | dg           }nHD ] }|                     |j                  r|c S !t          j        | d          }|J d|                                  |           |S )Nr   )r4   linesrE   T)r@   include_emptyzFailed to read file: )existsr	   samefiler4   	from_fileappend)r4   retr@   r)   s      r   _find_or_insert_filez&_stage_2.<locals>._find_or_insert_filez   s    {{}} 	CBB???CC    ==++  KKK $$dCCCC??$BD$B$B???S
r   c                R    h | ]$}|j         D ]}|j        d k    |j        d         %S )globalr   rE   rF   rH   r   r@   rN   s      r   	<setcomp>z_stage_2.<locals>.<setcomp>   sG     ' ' 'D4D' ',0	U]H]H]
1H]H]H]H]r   c                F    h | ]}|j         D ]}|j        d k    |j        S )objectr`   ra   s      r   rb   z_stage_2.<locals>.<setcomp>   sC       1A )-TYRZEZEZ
EZEZEZEZr   .c                    | j         j        S )N)schemaname)r    s    r   <lambda>z_stage_2.<locals>.<lambda>   s
    !(- r   )key/T)parentsexist_okz_ffi_api.pyazutf-8)encoding)is_rootz__init__.py_ffi_api)	submodule)r4   r   r   r	   )r-   BUILTIN_TYPE_KEYSprefixrM   endswithrstripr   
setdefaultitems
startswithsortedgetsetr   replacemkdiropenwriteGgenerate_ffi_apirE   reloadgenerate_init)r)   r?   r!   r"   r#   r]   defined_func_prefixesdefined_objsprefix_filterroot_prefixprefixesrt   	obj_namesfuncsobjsobject_infos	directorytarget_pathtarget_filer    s   `                   r   r9   r9   s   s        ' '"'' ' ' $  	L
 O))++M ]33C88 &&s++K%6%8%8H ( (FB''''%^^-- " "	+%%):):=)I)I%111BB|7G7GPR7S7S''
 
 
 c)nn|344'-- 	\ 	sC 8 88	t444-/**;77cG44 
	GG"+ "k1  	 	 	
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 	-/**;77cG44 	\GGAOK$;VzZZZ[[[	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\E" "s$   7GG	G	0II	I	r=   r   c           
         t                      }t                      g }d}| j        D ]}|j        dk    r|j        \  }}	}
|                    t          |t          |	          o,t          |	t                    o|		                                dk    |
r|
nd                      |
r|
dk    s|
                    d          rd}| j        D ]j}|j        dk    r]|                    |j        d	         g           }|D ]!}|                    |j        j                   "t          j        |||||           k| j        D ]}|j        d
k    r|j        }t          |t                    sJ t#          |          }|                    ||          }t          |          j        }                    |           t          j        |||||           fd|D             }| j        D ]%}|j        dk    rt          j        |||            n&| j        D ]2}|j        dk    r%|z  }|r|dhz  }t          j        |||            n3| j        D ]!}|j        dk    rt          j        |           "|                     |j        |j                   d S )NFzimport-objecttrue)type_checking_onlyalias_FFI_LOAD_LIBzlibinfo.load_lib_moduleTr_   r   rd   c                &    g | ]}|j         v|S r   )	full_name)r   idefined_typess     r   r   z_stage_3.<locals>.<listcomp>   s%    FFFQQ[%E%Eq%E%E%Er   zimport-section__all__LIBexport)r:   dry_run)r|   rE   rF   rH   r[   r   boolrG   rI   lowerru   r{   addrg   rh   r   generate_global_funcsr   r   generate_objectgenerate_import_sectiongenerate_allgenerate_exportupdater:   r   )r@   r=   r?   r#   defined_funcsr%   ffi_load_lib_importedrN   rh   r   r   r   functype_keyobj_infor   export_namesr   s                    @r   r<   r<      s    "eeM!eeM "G!  - -9''.2j+D$eNN/00 A&'93??A.4466&@#(2%%d  
 
 
  -%?22t}}E^7_7_2(,%  G G9   $$TZ]B77E 4 4!!$+"23333#D%#FFF  D D9  zHh,,,,,0::Hzz(H55H"8,,6Ii(((dFGS(CCCFFFF'FFFG   9(((%dGS999E )    9	!!(=8L$ 6+ug5N4s333E "   $ $9  d###KKS[K99999r   c            	         G d dt           j        t           j                  } d.d}t          j        dd	t          j         t          j         t          j         d
|           }|                    dt          ddd           |                    dt          ddd           |                    dt          dd           |                    dt          dd           |                    dt          dd           |                    dt          ddt          j         d           |                    dd d!d"#           |                    d$d%d&'           |                    d(d%d)'           |                                }|j        |j        |j        g}d }t!          |          rEt#          |          s|                    d*           t'          |j        |j        |j        +          }|j        s(|                                 t-          j        d,           t1           ||j                   ||j                  ||j        |j        |j        |j        -          S )/Nc                      e Zd ZdS )"_parse_args.<locals>.HelpFormatterN)__name__
__module____qualname__r   r   r   HelpFormatterr      s        r   r   arg
str | Noner   	list[str]c                H    | sg S d |                      d          D             S )Nc                ^    g | ]*}|                                 |                                 +S r   )rM   )r   items     r   r   z8_parse_args.<locals>._split_list_arg.<locals>.<listcomp>  s-    HHH4::<<H

HHHr   ;)rJ   )r   s    r   _split_list_argz$_parse_args.<locals>._split_list_arg  s-     	IHH3HHHHr   ztvm-ffi-stubgena  Generate type stubs for TVM FFI extensions. It supports two modes
- In `--init-*` mode, it generates missing `_ffi_api.py` and `__init__.py` files, based on the registered global functions and object types in the loaded libraries.
- In normal mode, it processes the given files/directories in-place, generating type stubs inside special `tvm-ffi-stubgen` directive blocks.

Documentation: re   )progdescriptionformatter_classz	--imports IMPORTSz\Additional imports to load before generation, separated by ';' (e.g. 'pkgA;pkgB.submodule').)typedefaultmetavarhelpz--dllsLIBSzShared libraries to preload before generation (e.g. TVM runtime or your extension), separated by ';'. This ensures global function and object metadata is available. Platform-specific suffixes like .so/.dylib/.dll are supported.z--init-pypkgzuPython package name to generate stubs for (e.g. apache-tvm-ffi). Required together with --init-lib and --init-prefix.)r   r   r   z
--init-libzCMake target that produces the shared library to load for stub generation (e.g. tvm_ffi_shared). Required together with --init-pypkg and --init-prefix.z--init-prefixzGlobal function/object prefix to include when generating stubs (e.g. tvm_ffi.). Required together with --init-pypkg and --init-lib.z--indent   zbExtra spaces added inside each generated block, relative to the indentation of the corresponding 'z' line.r)   *PATHzFiles or directories to process. Directories are scanned recursively; only .py and .pyi files are modified. Use tvm-ffi-stubgen directives to select where stubs are generated.)nargsr   r   z	--verbose
store_truezvPrint a unified diff of changes to each file. This is useful for debugging or previewing changes before applying them.)actionr   z	--dry-runz`Don't write changes to files. This is useful for previewing changes without modifying any files.zE--init-pypkg, --init-lib, and --init-prefix must be provided together)pkgshared_targetrt   r   )r%   r(   r8   indentr)   r:   r   )r   r   r   r   )argparseArgumentDefaultsHelpFormatterRawTextHelpFormatterArgumentParserr-   r;   DOC_URLr7   add_argumentrI   r   
STUB_BEGIN
parse_args
init_pypkginit_libinit_prefixanyallerrorr   r)   
print_helpsysexitr   r%   r(   r   r:   r   )r   r   parserargs
init_flagsr!   s         r   r$   r$      sL       >@]   I I I I
 $F
  kF
 ,-9F
 78lF F F &  F ,  	 	 	 -     C       	 	 	 S     G12G G G     0  	 	 	 D	     +	     D/4=$2BCJ"&H
: 
: 	bLL`aaa-#
 
 
 : --_TY''{j   r   rA   )r   r   )r@   r	   r?   rB   r   rC   )r)   rR   r?   rB   r!   r   r"   r   r#   rS   r   rC   )
r@   r	   r=   r   r?   rB   r#   rS   r   rC   )r   r   )#__doc__
__future__r   r   r   r&   r   r5   pathlibr   r   r   r   r   r-   
file_utilsr	   r
   	lib_stater   r   r   r   utilsr   r   r   r   rA   r0   r9   r<   r$   r   r   r   r   r   <module>r      s  " 4 3 " " " " " "       



                       / / / / / / / /            = < < < < < < < < < < <8 8 8 8v. . . . F F F FR?: ?: ?: ?:DC C C CL zCHXXZZ r   