junrushao commented on code in PR #259:
URL: https://github.com/apache/tvm-ffi/pull/259#discussion_r2525915544


##########
python/tvm_ffi/stub/stubgen.py:
##########
@@ -434,7 +99,42 @@ def __main__() -> int:
     in the given files or directories. See the module docstring for an
     overview and examples of the block syntax.
     """
-
+    opt = _parse_args()
+    dlls = [ctypes.CDLL(lib) for lib in opt.dlls]
+    global_funcs = _collect_global_funcs()
+    files: list[FileInfo] = collect_files([Path(f) for f in opt.files])
+
+    # Stage 1: Process `tvm-ffi-stubgen(ty-map)`
+    ty_map: dict[str, str] = _collect_ty_maps(files, opt)
+    # Stage 2: Process
+    # - `tvm-ffi-stubgen(begin): global/...`
+    # - `tvm-ffi-stubgen(begin): object/...`
+    for file in files:
+        if opt.verbose:
+            print(f"{C.TERM_CYAN}[File] {file.path}{C.TERM_RESET}")
+        ty_used: set[str] = set()
+        ty_on_file: set[str] = set()
+        fn_ty_map_fn = _fn_ty_map(ty_map, ty_used)
+        # Stage 2.1. Process `tvm-ffi-stubgen(begin): global/...`
+        for code in file.code_blocks:
+            if code.kind == "global":
+                G.generate_global_funcs(code, global_funcs, fn_ty_map_fn, opt)
+        # Stage 2.2. Process `tvm-ffi-stubgen(begin): object/...`
+        for code in file.code_blocks:
+            if code.kind == "object":
+                G.generate_object(code, fn_ty_map_fn, opt)
+                ty_on_file.add(ty_map.get(code.param, code.param))
+        # Stage 2.3. Add imports for used types.
+        for code in file.code_blocks:
+            if code.kind == "import":
+                G.generate_imports(code, ty_used - ty_on_file, opt)
+                break  # Only one import block per file is supported for now.
+        file.update(show_diff=opt.verbose, dry_run=opt.dry_run)
+    del dlls
+    return 0

Review Comment:
   Good catch!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to