@@ -27,6 +27,23 @@ internals. We will cover three checkpoints:
2727- Build Python wheel;
2828- Automatic Python package generation tools.
2929
30+ .. note ::
31+
32+ All code used in this guide lives under
33+ `examples/python_packaging <https://github.com/apache/tvm-ffi/tree/main/examples/python_packaging >`_.
34+
35+ .. admonition :: Prerequisite
36+ :class: hint
37+
38+ - Python: 3.9 or newer (for the ``tvm_ffi.config ``/``tvm-ffi-config `` helpers)
39+ - Compiler: C11-capable toolchain (GCC/Clang/MSVC)
40+ - TVM-FFI installed via
41+
42+ .. code-block :: bash
43+
44+ pip install --reinstall --upgrade apache-tvm-ffi
45+
46+
3047 Export C++ to Python
3148--------------------
3249
@@ -53,13 +70,10 @@ C symbols are easier to call into.
5370 Macro :c:macro: `TVM_FFI_DLL_EXPORT_TYPED_FUNC ` exports the function ``AddTwo `` as
5471 a C symbol ``__tvm_ffi_add_two `` inside the shared library.
5572
56- .. code-block :: cpp
57-
58- static int AddTwo(int x) {
59- return x + 2;
60- }
61-
62- TVM_FFI_DLL_EXPORT_TYPED_FUNC(add_two, AddTwo);
73+ .. literalinclude :: ../../examples/python_packaging/src/extension.cc
74+ :language: cpp
75+ :start-after: [tvm_ffi_abi.begin]
76+ :end-before: [tvm_ffi_abi.end]
6377
6478 .. group-tab :: Python (User)
6579
@@ -97,17 +111,10 @@ It registry handles type translation, error handling, and metadata.
97111 C++ function ``AddOne `` is registered with name ``my_ffi_extension.add_one ``
98112 in the global registry using :cpp:class: `tvm::ffi::reflection::GlobalDef `.
99113
100- .. code-block :: cpp
101-
102- static int AddOne(int x) {
103- return x + 1;
104- }
105-
106- TVM_FFI_STATIC_INIT_BLOCK() {
107- namespace refl = tvm::ffi::reflection;
108- refl::GlobalDef()
109- .def("my_ffi_extension.add_one", AddOne);
110- }
114+ .. literalinclude :: ../../examples/python_packaging/src/extension.cc
115+ :language: cpp
116+ :start-after: [global_function.begin]
117+ :end-before: [global_function.end]
111118
112119 .. group-tab :: Python (User)
113120
@@ -166,33 +173,10 @@ makes it easy to expose:
166173 - a constructor, and
167174 - a method ``Sum `` that returns the sum of the two fields.
168175
169- .. code-block :: cpp
170-
171- class IntPairObj : public ffi::Object {
172- public:
173- int64_t a;
174- int64_t b;
175- IntPairObj(int64_t a, int64_t b) : a(a), b(b) {}
176-
177- int64_t Sum() const {
178- return a + b;
179- }
180-
181- TVM_FFI_DECLARE_OBJECT_INFO_FINAL(
182- /*type_key=*/"my_ffi_extension.IntPair",
183- /*class=*/IntPairObj,
184- /*parent_class=*/ffi::Object
185- );
186- };
187-
188- TVM_FFI_STATIC_INIT_BLOCK() {
189- namespace refl = tvm::ffi::reflection;
190- refl::ObjectDef<IntPairObj>()
191- .def(refl::init<int64_t, int64_t>())
192- .def_rw("a", &IntPairObj::a, "the first field")
193- .def_rw("b", &IntPairObj::b, "the second field")
194- .def("sum", &IntPairObj::Sum, "IntPairObj::Sum() method");
195- }
176+ .. literalinclude :: ../../examples/python_packaging/src/extension.cc
177+ :language: cpp
178+ :start-after: [object.begin]
179+ :end-before: [object.end]
196180
197181 .. group-tab :: Python (User)
198182
@@ -238,15 +222,14 @@ CMake Target
238222Assume the source tree contains ``src/extension.cc ``. Create a ``CMakeLists.txt `` that
239223creates a shared target ``my_ffi_extension `` and configures it against TVM-FFI.
240224
241- .. code-block :: cmake
242-
243- add_library(my_ffi_extension SHARED src/extension.cc)
244- tvm_ffi_configure_target(my_ffi_extension STUB_DIR "./python")
245- install(TARGETS my_ffi_extension DESTINATION .)
246- tvm_ffi_install(my_ffi_extension DESTINATION .)
225+ .. literalinclude :: ../../examples/python_packaging/CMakeLists.txt
226+ :language: cmake
227+ :start-after: [example.cmake.begin]
228+ :end-before: [example.cmake.end]
247229
248230Function ``tvm_ffi_configure_target `` sets up TVM-FFI include paths, link against TVM-FFI library,
249- generates stubs under the specified directory, and optionally debug symbols.
231+ generates stubs under ``STUB_DIR ``, and can scaffold stub files when ``STUB_INIT `` is
232+ enabled.
250233
251234Function ``tvm_ffi_install `` places necessary information, e.g. debug symbols in macOS, next to
252235the shared library for proper packaging.
@@ -261,19 +244,10 @@ Define a :pep:`517` build backend in ``pyproject.toml``, with the following step
261244- Specify the source directory of the package via ``wheel.packages ``, and the installation
262245 destination via ``wheel.install-dir ``.
263246
264- .. code-block :: toml
265-
266- [build-system]
267- requires = ["scikit-build-core>=0.10.0", "apache-tvm-ffi"]
268- build-backend = "scikit_build_core.build"
269-
270- [tool.scikit-build]
271- # The wheel is Python ABI-agnostic
272- wheel.py-api = "py3"
273- # The package contains the Python module at `python/my_ffi_extension`
274- wheel.packages = ["python/my_ffi_extension"]
275- # The install dir matches the import name
276- wheel.install-dir = "my_ffi_extension"
247+ .. literalinclude :: ../../examples/python_packaging/pyproject.toml
248+ :language: toml
249+ :start-after: [pyproject.build.begin]
250+ :end-before: [pyproject.build.end]
277251
278252Once fully specified, scikit-build-core will invoke CMake and drive the extension building process.
279253
0 commit comments