Compile and build a C/C++/CUDA module from source files. This function compiles the given C, C++, and/or CUDA source files into a shared library or object file. The compiler is selected automatically based on file extension: - ``.c`` — compiled with the C compiler (``$CC``) - ``.cc
( # noqa: PLR0913
name: str,
*,
sources: Sequence[str] | str | None = None,
cpp_files: Sequence[str] | str | None = None,
cuda_files: Sequence[str] | str | None = None,
extra_cflags: Sequence[str] | None = None,
extra_cuda_cflags: Sequence[str] | None = None,
extra_ldflags: Sequence[str] | None = None,
extra_include_paths: Sequence[str] | None = None,
build_directory: str | None = None,
backend: str | None = None,
output: str | None = None,
)
| 1147 | |
| 1148 | |
| 1149 | def build( # noqa: PLR0913 |
| 1150 | name: str, |
| 1151 | *, |
| 1152 | sources: Sequence[str] | str | None = None, |
| 1153 | cpp_files: Sequence[str] | str | None = None, |
| 1154 | cuda_files: Sequence[str] | str | None = None, |
| 1155 | extra_cflags: Sequence[str] | None = None, |
| 1156 | extra_cuda_cflags: Sequence[str] | None = None, |
| 1157 | extra_ldflags: Sequence[str] | None = None, |
| 1158 | extra_include_paths: Sequence[str] | None = None, |
| 1159 | build_directory: str | None = None, |
| 1160 | backend: str | None = None, |
| 1161 | output: str | None = None, |
| 1162 | ) -> str: |
| 1163 | """Compile and build a C/C++/CUDA module from source files. |
| 1164 | |
| 1165 | This function compiles the given C, C++, and/or CUDA source files into a shared library or |
| 1166 | object file. The compiler is selected automatically based on file extension: |
| 1167 | |
| 1168 | - ``.c`` — compiled with the C compiler (``$CC``) |
| 1169 | - ``.cc``, ``.cpp``, ``.cxx`` — compiled with the C++ compiler (``$CXX``) |
| 1170 | - ``.o``, ``.obj`` — pre-compiled objects, passed directly to the linker |
| 1171 | |
| 1172 | When ``output`` is ``None`` (the default) or has a shared-library extension, object files are |
| 1173 | linked into a shared library. When ``output`` has an object-file extension (``.o``, ``.obj``), |
| 1174 | linking is skipped and the path to the object file is returned. |
| 1175 | |
| 1176 | Note that this function does not automatically export functions to the tvm ffi module. You need to |
| 1177 | manually use the TVM FFI export macros (e.g., ``TVM_FFI_DLL_EXPORT_TYPED_FUNC``) in your source files to export |
| 1178 | functions. This gives you more control over which functions are exported and how they are exported. |
| 1179 | |
| 1180 | Extra compiler and linker flags can be provided via the ``extra_cflags``, ``extra_cuda_cflags``, and ``extra_ldflags`` |
| 1181 | parameters. The default flags are generally sufficient for most use cases, but you may need to provide additional |
| 1182 | flags for your specific use case. |
| 1183 | |
| 1184 | The include dir of tvm ffi and dlpack are used by default for the compiler to find the headers. Thus, you can |
| 1185 | include any header from tvm ffi in your source files. You can also provide additional include paths via the |
| 1186 | ``extra_include_paths`` parameter and include custom headers in your source code. |
| 1187 | |
| 1188 | The compiled shared library is cached in a cache directory to avoid recompilation. The `build_directory` parameter |
| 1189 | is provided to specify the build directory. If not specified, a default tvm ffi cache directory will be used. |
| 1190 | The default cache directory can be specified via the `TVM_FFI_CACHE_DIR` environment variable. If not specified, |
| 1191 | the default cache directory is ``~/.cache/tvm-ffi``. |
| 1192 | |
| 1193 | The C compiler is controlled by the ``$CC`` environment variable (default: ``cc`` on Unix, ``cl`` on Windows). |
| 1194 | The C++ compiler is controlled by the ``$CXX`` environment variable (default: ``c++`` on Unix, ``cl`` on Windows). |
| 1195 | |
| 1196 | Parameters |
| 1197 | ---------- |
| 1198 | name |
| 1199 | The name of the tvm ffi module. |
| 1200 | sources |
| 1201 | Source files to compile. The compiler is auto-detected from the file extension: |
| 1202 | |
| 1203 | - ``.c`` → C compiler (``$CC``) |
| 1204 | - ``.cc``, ``.cpp``, ``.cxx`` → C++ compiler (``$CXX``) |
| 1205 | - ``.cu`` → CUDA/HIP compiler (``nvcc`` or ``hipcc``) |
| 1206 | - ``.o``, ``.obj`` → pre-compiled objects, passed directly to the linker |
no test coverage detected