This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git
The following commit(s) were added to refs/heads/dev by this push:
new 4383b1a [Bugfix] Run ninja build command in MSVC dev env (#11)
4383b1a is described below
commit 4383b1a6d81f5403879e9266f3d0924a289c227a
Author: Yaoyao Ding <[email protected]>
AuthorDate: Sun Sep 14 17:36:31 2025 -0400
[Bugfix] Run ninja build command in MSVC dev env (#11)
* run the ninja build in msvc dev env on windows
* format & lint
* remove one debug print
---
python/tvm_ffi/cpp/load_inline.py | 62 ++++++++++++++++++++++++++++++++++++++-
tests/python/test_load_inline.py | 5 ----
2 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/python/tvm_ffi/cpp/load_inline.py
b/python/tvm_ffi/cpp/load_inline.py
index 3c3c8d3..ced9705 100644
--- a/python/tvm_ffi/cpp/load_inline.py
+++ b/python/tvm_ffi/cpp/load_inline.py
@@ -128,6 +128,63 @@ def _get_cuda_target() -> str:
return "-gencode=arch=compute_70,code=sm_70"
+def _run_command_in_dev_prompt(args, cwd, capture_output):
+ """Locates the Developer Command Prompt and runs a command within its
environment."""
+ try:
+ # Path to vswhere.exe
+ vswhere_path = os.path.join(
+ os.environ.get("ProgramFiles(x86)", "C:\\Program Files (x86)"),
+ "Microsoft Visual Studio",
+ "Installer",
+ "vswhere.exe",
+ )
+
+ if not os.path.exists(vswhere_path):
+ raise FileNotFoundError("vswhere.exe not found.")
+
+ # Find the Visual Studio installation path
+ vs_install_path = subprocess.run(
+ [
+ vswhere_path,
+ "-latest",
+ "-prerelease",
+ "-products",
+ "*",
+ "-property",
+ "installationPath",
+ ],
+ capture_output=True,
+ text=True,
+ check=True,
+ ).stdout.strip()
+
+ if not vs_install_path:
+ raise FileNotFoundError("No Visual Studio installation found.")
+
+ # Construct the path to the VsDevCmd.bat file
+ vsdevcmd_path = os.path.join(vs_install_path, "Common7", "Tools",
"VsDevCmd.bat")
+
+ if not os.path.exists(vsdevcmd_path):
+ raise FileNotFoundError(f"VsDevCmd.bat not found at:
{vsdevcmd_path}")
+
+ # Use cmd.exe to run the batch file and then your command.
+ # The /k flag keeps the command prompt open after the batch file runs.
+ # The "&" symbol chains the commands.
+ cmd_command = '"{vsdevcmd_path}" -arch=x64 & {command}'.format(
+ vsdevcmd_path=vsdevcmd_path, command=" ".join(args)
+ )
+
+ # Execute the command in a new shell
+ return subprocess.run(cmd_command, cwd=cwd,
capture_output=capture_output, shell=True)
+
+ except (FileNotFoundError, subprocess.CalledProcessError) as e:
+ raise RuntimeError(
+ "Failed to run the following command in MSVC developer
environment: {}".format(
+ " ".join(args)
+ )
+ ) from e
+
+
def _generate_ninja_build(
name: str,
build_dir: str,
@@ -248,7 +305,10 @@ def _build_ninja(build_dir: str) -> None:
num_workers = os.environ.get("MAX_JOBS", None)
if num_workers is not None:
command += ["-j", num_workers]
- status = subprocess.run(args=command, cwd=build_dir, capture_output=True)
+ if IS_WINDOWS:
+ status = _run_command_in_dev_prompt(args=command, cwd=build_dir,
capture_output=True)
+ else:
+ status = subprocess.run(args=command, cwd=build_dir,
capture_output=True)
if status.returncode != 0:
msg = ["ninja exited with status {}".format(status.returncode)]
encoding = "oem" if IS_WINDOWS else "utf-8"
diff --git a/tests/python/test_load_inline.py b/tests/python/test_load_inline.py
index d72bfa7..25dcee4 100644
--- a/tests/python/test_load_inline.py
+++ b/tests/python/test_load_inline.py
@@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.
-import sys
import numpy
import pytest
@@ -29,7 +28,6 @@ import tvm_ffi.cpp
from tvm_ffi.module import Module
[email protected](sys.platform.startswith("win"), reason="needs to robustify
windows support")
def test_load_inline_cpp():
mod: Module = tvm_ffi.cpp.load_inline(
name="hello",
@@ -56,7 +54,6 @@ def test_load_inline_cpp():
numpy.testing.assert_equal(x + 1, y)
[email protected](sys.platform.startswith("win"), reason="needs to robustify
windows support")
def test_load_inline_cpp_with_docstrings():
mod: Module = tvm_ffi.cpp.load_inline(
name="hello",
@@ -83,7 +80,6 @@ def test_load_inline_cpp_with_docstrings():
numpy.testing.assert_equal(x + 1, y)
[email protected](sys.platform.startswith("win"), reason="needs to robustify
windows support")
def test_load_inline_cpp_multiple_sources():
mod: Module = tvm_ffi.cpp.load_inline(
name="hello",
@@ -126,7 +122,6 @@ def test_load_inline_cpp_multiple_sources():
numpy.testing.assert_equal(x + 1, y)
[email protected](sys.platform.startswith("win"), reason="needs to robustify
windows support")
def test_load_inline_cpp_build_dir():
mod: Module = tvm_ffi.cpp.load_inline(
name="hello",