[Mlir-commits] [mlir] [MLIR][Python] add Python wheel build demo/test (PR #160388)
Maksim Levental
llvmlistbot at llvm.org
Wed Sep 24 01:06:36 PDT 2025
https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/160388
>From 0a8c0accb9351ca6d69da3bd19bc94705e18350e Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Tue, 23 Sep 2025 13:16:00 -0700
Subject: [PATCH 1/3] [MLIR][Python] Standalone wheel
---
mlir/examples/standalone/CMakeLists.txt | 8 ++-
.../examples/standalone/python/pyproject.toml | 65 +++++++++++++++++++
mlir/examples/standalone/python/version.py | 43 ++++++++++++
mlir/test/Examples/standalone/lit.local.cfg | 6 ++
mlir/test/Examples/standalone/test.wheel.toy | 14 ++++
5 files changed, 134 insertions(+), 2 deletions(-)
create mode 100644 mlir/examples/standalone/python/pyproject.toml
create mode 100644 mlir/examples/standalone/python/version.py
create mode 100644 mlir/test/Examples/standalone/test.wheel.toy
diff --git a/mlir/examples/standalone/CMakeLists.txt b/mlir/examples/standalone/CMakeLists.txt
index e2bcda7fa6f0b..c6c49fde12d2e 100644
--- a/mlir/examples/standalone/CMakeLists.txt
+++ b/mlir/examples/standalone/CMakeLists.txt
@@ -63,8 +63,12 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
include(MLIRDetectPythonEnv)
mlir_configure_python_dev_packages()
# Note: for EXTERNAL_PROJECT_BUILD this must be set from the command line.
- set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
- set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
+ if(NOT MLIR_PYTHON_PACKAGE_PREFIX)
+ set(MLIR_PYTHON_PACKAGE_PREFIX "mlir_standalone" CACHE STRING "" FORCE)
+ endif()
+ if(NOT MLIR_BINDINGS_PYTHON_INSTALL_PREFIX)
+ set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
+ endif()
add_subdirectory(python)
endif()
add_subdirectory(test)
diff --git a/mlir/examples/standalone/python/pyproject.toml b/mlir/examples/standalone/python/pyproject.toml
new file mode 100644
index 0000000000000..bda15791e9289
--- /dev/null
+++ b/mlir/examples/standalone/python/pyproject.toml
@@ -0,0 +1,65 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+# Copyright (c) 2025.
+
+[project]
+name = "standalone-python-bindings"
+dynamic = ["version"]
+requires-python = ">=3.8,<=3.13"
+dependencies = [
+ "numpy>=1.19.5, <=2.1.2",
+ "PyYAML>=5.4.0, <=6.0.2",
+ # required but no package for it...
+ # "ml_dtypes>=0.5.0, <=0.6.0"
+]
+
+[project.urls]
+Homepage = "https://github.com/llvm/llvm-project"
+Discussions = "https://discourse.llvm.org/"
+"Issue Tracker" = "https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3Amlir%3Apython%20"
+"Source Code" = "https://github.com/llvm/llvm-project/tree/main/mlir/python"
+
+[build-system]
+requires = [
+ "scikit-build-core==0.10.7",
+ # nanobind requires it for stubgen but we don't use stubgen?
+ "typing_extensions==4.12.2",
+ "nanobind>=2.9, <3.0",
+ "pybind11>=2.10.0, <=2.13.6",
+]
+build-backend = "scikit_build_core.build"
+
+[tool.scikit-build]
+experimental = true
+metadata.version.provider = "version"
+metadata.version.provider-path = "."
+
+minimum-version = "0.10"
+# Uncomment to specify the build directory for the wheel (otherwise will be a temporary directory).
+# build-dir = "build"
+cmake.source-dir = "../"
+wheel.exclude = ["bin", "include", "lib", "src", "share"]
+# for installing/distributing only the python stuff
+build.targets = ["StandalonePythonModules"]
+install.components = ["StandalonePythonModules"]
+
+[tool.scikit-build.cmake.define]
+CMAKE_C_COMPILER_LAUNCHER = { env = "CMAKE_C_COMPILER_LAUNCHER", default = "" }
+CMAKE_CXX_COMPILER_LAUNCHER = { env = "CMAKE_CXX_COMPILER_LAUNCHER", default = "" }
+CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
+CMAKE_C_VISIBILITY_PRESET = "hidden"
+CMAKE_CXX_VISIBILITY_PRESET = "hidden"
+CMAKE_VERBOSE_MAKEFILE = "ON"
+
+# for installing/distributing only the python stuff
+LLVM_DISTRIBUTIONS = "StandalonePython"
+LLVM_StandalonePython_DISTRIBUTION_COMPONENTS = "StandalonePythonModules"
+LLVM_ENABLE_PROJECTS = "mlir"
+
+MLIR_PYTHON_PACKAGE_PREFIX = "mlir_standalone"
+MLIR_BINDINGS_PYTHON_INSTALL_PREFIX = "mlir_standalone"
+MLIR_ENABLE_BINDINGS_PYTHON = "ON"
+MLIR_DIR = { env = "MLIR_DIR", default = "" }
+
+LLVM_NATIVE_TOOL_DIR = { env = "LLVM_NATIVE_TOOL_DIR", default = "" }
diff --git a/mlir/examples/standalone/python/version.py b/mlir/examples/standalone/python/version.py
new file mode 100644
index 0000000000000..6eabfc6be85c9
--- /dev/null
+++ b/mlir/examples/standalone/python/version.py
@@ -0,0 +1,43 @@
+from __future__ import annotations
+from pathlib import Path
+from datetime import datetime
+import os
+import re
+
+__all__ = ["dynamic_metadata"]
+
+
+def __dir__() -> list[str]:
+ return __all__
+
+
+def dynamic_metadata(
+ field: str,
+ settings: dict[str, object] | None = None,
+ _project: dict[str, object] = None,
+) -> str:
+ if field != "version":
+ msg = "Only the 'version' field is supported"
+ raise ValueError(msg)
+
+ if settings:
+ msg = "No inline configuration is supported"
+ raise ValueError(msg)
+
+ now = datetime.now()
+ llvm_datetime = os.environ.get(
+ "DATETIME", f"{now.year}{now.month:02}{now.day:02}{now.hour:02}"
+ )
+
+ llvm_src_root = Path(__file__).parent.parent.parent.parent.parent
+ cmake_version_path = llvm_src_root / "cmake/Modules/LLVMVersion.cmake"
+ if not cmake_version_path.exists():
+ cmake_version_path = llvm_src_root / "llvm/CMakeLists.txt"
+ cmake_txt = open(cmake_version_path).read()
+ llvm_version = []
+ for v in ["LLVM_VERSION_MAJOR", "LLVM_VERSION_MINOR", "LLVM_VERSION_PATCH"]:
+ vn = re.findall(rf"set\({v} (\d+)\)", cmake_txt)
+ assert vn, f"couldn't find {v} in cmake txt"
+ llvm_version.append(vn[0])
+
+ return f"{llvm_version[0]}.{llvm_version[1]}.{llvm_version[2]}.{llvm_datetime}"
diff --git a/mlir/test/Examples/standalone/lit.local.cfg b/mlir/test/Examples/standalone/lit.local.cfg
index 3b12dcbd99e83..0027f31c5aae1 100644
--- a/mlir/test/Examples/standalone/lit.local.cfg
+++ b/mlir/test/Examples/standalone/lit.local.cfg
@@ -1,3 +1,5 @@
+import os
+
# Disable with sanitizers for now, this require some more setup apparently.
for san in ["asan", "msan", "ubsan"]:
if san in config.available_features:
@@ -9,5 +11,9 @@ config.substitutions.append(("%host_cxx", config.host_cxx))
config.substitutions.append(("%host_cc", config.host_cc))
config.substitutions.append(("%enable_libcxx", config.enable_libcxx))
config.substitutions.append(("%mlir_cmake_dir", config.mlir_cmake_dir))
+config.substitutions.append(("%mlir_tools_dir", config.mlir_tools_dir))
config.substitutions.append(("%llvm_use_linker", config.llvm_use_linker))
config.substitutions.append(("%cmake_build_type", config.cmake_build_type))
+
+if "PIP_BREAK_SYSTEM_PACKAGES" in os.environ:
+ config.environment["PIP_BREAK_SYSTEM_PACKAGES"] = os.environ["PIP_BREAK_SYSTEM_PACKAGES"]
diff --git a/mlir/test/Examples/standalone/test.wheel.toy b/mlir/test/Examples/standalone/test.wheel.toy
new file mode 100644
index 0000000000000..0dcf547529165
--- /dev/null
+++ b/mlir/test/Examples/standalone/test.wheel.toy
@@ -0,0 +1,14 @@
+# RUN: export LLVM_NATIVE_TOOL_DIR="%mlir_tools_dir"
+# RUN: export MLIR_DIR="%mlir_cmake_dir"
+# RUN: %python -m pip wheel "%mlir_src_root/examples/standalone/python" -v | tee %t
+# RUN: %python -m pip install standalone_python_bindings-*.whl -v | tee -a %t
+# RUN: %python "%mlir_src_root/examples/standalone/test/python/smoketest.py" nanobind | tee -a %t
+
+# RUN: FileCheck --input-file=%t %s
+
+# CHECK: Successfully built standalone-python-bindings
+
+# CHECK: module {
+# CHECK: %[[C2:.*]] = arith.constant 2 : i32
+# CHECK: %[[V0:.*]] = standalone.foo %[[C2]] : i32
+# CHECK: }
>From 742e1794ad92abd819d3f32abcdb467dad9ff6c5 Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Tue, 23 Sep 2025 15:29:30 -0700
Subject: [PATCH 2/3] annotate pyproject.toml
---
mlir/examples/standalone/pyproject.toml | 80 +++++++++++++++++++
.../examples/standalone/python/pyproject.toml | 65 ---------------
.../standalone/{python => }/version.py | 7 ++
mlir/test/Examples/standalone/lit.local.cfg | 4 +-
mlir/test/Examples/standalone/test.wheel.toy | 13 ++-
mlir/test/lit.site.cfg.py.in | 2 +
6 files changed, 102 insertions(+), 69 deletions(-)
create mode 100644 mlir/examples/standalone/pyproject.toml
delete mode 100644 mlir/examples/standalone/python/pyproject.toml
rename mlir/examples/standalone/{python => }/version.py (81%)
diff --git a/mlir/examples/standalone/pyproject.toml b/mlir/examples/standalone/pyproject.toml
new file mode 100644
index 0000000000000..ac2c2a608437e
--- /dev/null
+++ b/mlir/examples/standalone/pyproject.toml
@@ -0,0 +1,80 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+# Copyright (c) 2025.
+
+[project]
+name = "standalone-python-bindings"
+dynamic = ["version"]
+requires-python = ">=3.8,<=3.14"
+dependencies = [
+ "numpy>=1.19.5, <=2.1.2",
+ "PyYAML>=5.4.0, <=6.0.1",
+ "ml_dtypes>=0.1.0, <=0.6.0; python_version<'3.13'",
+ "ml_dtypes>=0.5.0, <=0.6.0; python_version>='3.13'",
+]
+
+[project.urls]
+Homepage = "https://github.com/llvm/llvm-project"
+Discussions = "https://discourse.llvm.org/"
+"Issue Tracker" = "https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3Amlir%3Apython%20"
+"Source Code" = "https://github.com/llvm/llvm-project/tree/main/mlir/python"
+
+[build-system]
+requires = [
+ "scikit-build-core>=0.10.7",
+ "typing_extensions>=4.12.2",
+ "nanobind>=2.9, <3.0",
+ "pybind11>=2.10.0, <=2.13.6",
+]
+build-backend = "scikit_build_core.build"
+
+[tool.scikit-build]
+# The metadata.version.provider stuff below requires a "plugin" and
+# experimental must be enabled currently to use plugins.
+experimental = true
+# This is all optional (i.e., if you want hardcode your own version string).
+metadata.version.provider = "version"
+metadata.version.provider-path = "."
+
+# This is the minimum version of scikit-build-core.
+minimum-version = "0.10"
+cmake.source-dir = "."
+# The conventional LLVM install distribution will have all of these directories; this excludes them
+# so they don't get included in the wheel file.
+wheel.exclude = ["bin", "include", "lib", "src", "share"]
+# This is for installing/distributing the python bindings target and only the python bindings target.
+# See LLVM_DISTRIBUTIONS below.
+build.targets = ["StandalonePythonModules"]
+install.components = ["StandalonePythonModules"]
+
+[tool.scikit-build.cmake.define]
+# Optional
+CMAKE_C_COMPILER = { env = "CMAKE_C_COMPILER", default = "" }
+CMAKE_CXX_COMPILER = { env = "CMAKE_CXX_COMPILER", default = "" }
+CMAKE_C_COMPILER_LAUNCHER = { env = "CMAKE_C_COMPILER_LAUNCHER", default = "" }
+CMAKE_CXX_COMPILER_LAUNCHER = { env = "CMAKE_CXX_COMPILER_LAUNCHER", default = "" }
+CMAKE_GENERATOR = { env = "CMAKE_GENERATOR", default = "" }
+CMAKE_VERBOSE_MAKEFILE = "ON"
+LLVM_USE_LINKER = { env = "LLVM_USE_LINKER", default = "" }
+# Optional but highly recommended.
+CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
+CMAKE_C_VISIBILITY_PRESET = "hidden"
+CMAKE_CXX_VISIBILITY_PRESET = "hidden"
+
+# Non-optinal (alternatively you could have CMAKE_PREFIX_PATH here).
+MLIR_DIR = { env = "MLIR_DIR", default = "" }
+# Non-optinal
+CMAKE_BUILD_TYPE = { env = "CMAKE_BUILD_TYPE", default = "Release" }
+MLIR_ENABLE_BINDINGS_PYTHON = "ON"
+# Effectively non-optional (any downstream project should specify this).
+MLIR_PYTHON_PACKAGE_PREFIX = "mlir_standalone"
+
+# This is for installing/distributing the python bindings target and only the python bindings target.
+LLVM_DISTRIBUTIONS = "StandalonePython"
+LLVM_StandalonePython_DISTRIBUTION_COMPONENTS = "StandalonePythonModules"
+
+# This specifies the directory in the build directory where _mlir_libs, dialects, etc. are installed.
+# Thus, this will be the package (and the name of the package) that pip assumes is the package root.
+# Alternatively, you can use something like wheel.packages = ["python_packages/standalone/mlir_standalone"].
+MLIR_BINDINGS_PYTHON_INSTALL_PREFIX = "mlir_standalone"
diff --git a/mlir/examples/standalone/python/pyproject.toml b/mlir/examples/standalone/python/pyproject.toml
deleted file mode 100644
index bda15791e9289..0000000000000
--- a/mlir/examples/standalone/python/pyproject.toml
+++ /dev/null
@@ -1,65 +0,0 @@
-# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-# Copyright (c) 2025.
-
-[project]
-name = "standalone-python-bindings"
-dynamic = ["version"]
-requires-python = ">=3.8,<=3.13"
-dependencies = [
- "numpy>=1.19.5, <=2.1.2",
- "PyYAML>=5.4.0, <=6.0.2",
- # required but no package for it...
- # "ml_dtypes>=0.5.0, <=0.6.0"
-]
-
-[project.urls]
-Homepage = "https://github.com/llvm/llvm-project"
-Discussions = "https://discourse.llvm.org/"
-"Issue Tracker" = "https://github.com/llvm/llvm-project/issues?q=is%3Aissue%20state%3Aopen%20label%3Amlir%3Apython%20"
-"Source Code" = "https://github.com/llvm/llvm-project/tree/main/mlir/python"
-
-[build-system]
-requires = [
- "scikit-build-core==0.10.7",
- # nanobind requires it for stubgen but we don't use stubgen?
- "typing_extensions==4.12.2",
- "nanobind>=2.9, <3.0",
- "pybind11>=2.10.0, <=2.13.6",
-]
-build-backend = "scikit_build_core.build"
-
-[tool.scikit-build]
-experimental = true
-metadata.version.provider = "version"
-metadata.version.provider-path = "."
-
-minimum-version = "0.10"
-# Uncomment to specify the build directory for the wheel (otherwise will be a temporary directory).
-# build-dir = "build"
-cmake.source-dir = "../"
-wheel.exclude = ["bin", "include", "lib", "src", "share"]
-# for installing/distributing only the python stuff
-build.targets = ["StandalonePythonModules"]
-install.components = ["StandalonePythonModules"]
-
-[tool.scikit-build.cmake.define]
-CMAKE_C_COMPILER_LAUNCHER = { env = "CMAKE_C_COMPILER_LAUNCHER", default = "" }
-CMAKE_CXX_COMPILER_LAUNCHER = { env = "CMAKE_CXX_COMPILER_LAUNCHER", default = "" }
-CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
-CMAKE_C_VISIBILITY_PRESET = "hidden"
-CMAKE_CXX_VISIBILITY_PRESET = "hidden"
-CMAKE_VERBOSE_MAKEFILE = "ON"
-
-# for installing/distributing only the python stuff
-LLVM_DISTRIBUTIONS = "StandalonePython"
-LLVM_StandalonePython_DISTRIBUTION_COMPONENTS = "StandalonePythonModules"
-LLVM_ENABLE_PROJECTS = "mlir"
-
-MLIR_PYTHON_PACKAGE_PREFIX = "mlir_standalone"
-MLIR_BINDINGS_PYTHON_INSTALL_PREFIX = "mlir_standalone"
-MLIR_ENABLE_BINDINGS_PYTHON = "ON"
-MLIR_DIR = { env = "MLIR_DIR", default = "" }
-
-LLVM_NATIVE_TOOL_DIR = { env = "LLVM_NATIVE_TOOL_DIR", default = "" }
diff --git a/mlir/examples/standalone/python/version.py b/mlir/examples/standalone/version.py
similarity index 81%
rename from mlir/examples/standalone/python/version.py
rename to mlir/examples/standalone/version.py
index 6eabfc6be85c9..4d8fd170a84c4 100644
--- a/mlir/examples/standalone/python/version.py
+++ b/mlir/examples/standalone/version.py
@@ -1,3 +1,8 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+# Copyright (c) 2025.
+
from __future__ import annotations
from pathlib import Path
from datetime import datetime
@@ -33,6 +38,8 @@ def dynamic_metadata(
cmake_version_path = llvm_src_root / "cmake/Modules/LLVMVersion.cmake"
if not cmake_version_path.exists():
cmake_version_path = llvm_src_root / "llvm/CMakeLists.txt"
+ if not cmake_version_path.exists():
+ return llvm_datetime
cmake_txt = open(cmake_version_path).read()
llvm_version = []
for v in ["LLVM_VERSION_MAJOR", "LLVM_VERSION_MINOR", "LLVM_VERSION_PATCH"]:
diff --git a/mlir/test/Examples/standalone/lit.local.cfg b/mlir/test/Examples/standalone/lit.local.cfg
index 0027f31c5aae1..e739ec1ba1052 100644
--- a/mlir/test/Examples/standalone/lit.local.cfg
+++ b/mlir/test/Examples/standalone/lit.local.cfg
@@ -9,9 +9,11 @@ config.substitutions.append(("%cmake_exe", config.host_cmake))
config.substitutions.append(("%cmake_generator", config.host_cmake_generator))
config.substitutions.append(("%host_cxx", config.host_cxx))
config.substitutions.append(("%host_cc", config.host_cc))
+config.substitutions.append(("%hostc_compiler_launcher", config.host_c_compiler_launcher))
+config.substitutions.append(("%hostcxx_compiler_launcher", config.host_cxx_compiler_launcher))
config.substitutions.append(("%enable_libcxx", config.enable_libcxx))
config.substitutions.append(("%mlir_cmake_dir", config.mlir_cmake_dir))
-config.substitutions.append(("%mlir_tools_dir", config.mlir_tools_dir))
+config.substitutions.append(("%mlir_obj_root", config.mlir_obj_root))
config.substitutions.append(("%llvm_use_linker", config.llvm_use_linker))
config.substitutions.append(("%cmake_build_type", config.cmake_build_type))
diff --git a/mlir/test/Examples/standalone/test.wheel.toy b/mlir/test/Examples/standalone/test.wheel.toy
index 0dcf547529165..217c2e3322887 100644
--- a/mlir/test/Examples/standalone/test.wheel.toy
+++ b/mlir/test/Examples/standalone/test.wheel.toy
@@ -1,7 +1,14 @@
-# RUN: export LLVM_NATIVE_TOOL_DIR="%mlir_tools_dir"
+# RUN: export CMAKE_BUILD_TYPE=%cmake_build_type
+# RUN: export CMAKE_CXX_COMPILER=%host_cxx
+# RUN: export CMAKE_CXX_COMPILER_LAUNCHER=%hostcxx_compiler_launcher
+# RUN: export CMAKE_C_COMPILER=%host_cc
+# RUN: export CMAKE_C_COMPILER_LAUNCHER=%hostc_compiler_launcher
+# RUN: export CMAKE_GENERATOR=%cmake_generator
+# RUN: export LLVM_USE_LINKER=%llvm_use_linker
# RUN: export MLIR_DIR="%mlir_cmake_dir"
-# RUN: %python -m pip wheel "%mlir_src_root/examples/standalone/python" -v | tee %t
-# RUN: %python -m pip install standalone_python_bindings-*.whl -v | tee -a %t
+
+# RUN: %python -m pip wheel "%mlir_src_root/examples/standalone" -w "%mlir_obj_root/wheelhouse" -v | tee %t
+# RUN: %python -m pip install standalone_python_bindings -f "%mlir_obj_root/wheelhouse" -v | tee -a %t
# RUN: %python "%mlir_src_root/examples/standalone/test/python/smoketest.py" nanobind | tee -a %t
# RUN: FileCheck --input-file=%t %s
diff --git a/mlir/test/lit.site.cfg.py.in b/mlir/test/lit.site.cfg.py.in
index 2fc595dfabbf5..940e2ad3c4365 100644
--- a/mlir/test/lit.site.cfg.py.in
+++ b/mlir/test/lit.site.cfg.py.in
@@ -15,6 +15,8 @@ config.native_target = "@LLVM_NATIVE_ARCH@"
config.host_os = "@HOST_OS@"
config.host_cc = "@HOST_CC@"
config.host_cxx = "@HOST_CXX@"
+config.host_c_compiler_launcher = "@CMAKE_C_COMPILER_LAUNCHER@"
+config.host_cxx_compiler_launcher = "@CMAKE_CXX_COMPILER_LAUNCHER@"
config.enable_libcxx = "@LLVM_ENABLE_LIBCXX@"
config.host_cmake = "@CMAKE_COMMAND@"
config.host_cmake_generator = "@CMAKE_GENERATOR@"
>From 73d99842a9883e4fdbd0c2a8f91a71980bd297c7 Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Tue, 23 Sep 2025 18:25:20 -0700
Subject: [PATCH 3/3] trim pyproject
---
mlir/examples/standalone/pyproject.toml | 33 ++++---------
mlir/examples/standalone/version.py | 50 --------------------
mlir/test/Examples/standalone/test.wheel.toy | 12 ++++-
3 files changed, 20 insertions(+), 75 deletions(-)
delete mode 100644 mlir/examples/standalone/version.py
diff --git a/mlir/examples/standalone/pyproject.toml b/mlir/examples/standalone/pyproject.toml
index ac2c2a608437e..cfb04205beebe 100644
--- a/mlir/examples/standalone/pyproject.toml
+++ b/mlir/examples/standalone/pyproject.toml
@@ -30,21 +30,11 @@ requires = [
build-backend = "scikit_build_core.build"
[tool.scikit-build]
-# The metadata.version.provider stuff below requires a "plugin" and
-# experimental must be enabled currently to use plugins.
-experimental = true
-# This is all optional (i.e., if you want hardcode your own version string).
-metadata.version.provider = "version"
-metadata.version.provider-path = "."
-
# This is the minimum version of scikit-build-core.
-minimum-version = "0.10"
+minimum-version = "0.10.7"
+# This pyproject.toml must be adjacent to the root CMakeLists.txt (wherever project(...) is specified).
cmake.source-dir = "."
-# The conventional LLVM install distribution will have all of these directories; this excludes them
-# so they don't get included in the wheel file.
-wheel.exclude = ["bin", "include", "lib", "src", "share"]
# This is for installing/distributing the python bindings target and only the python bindings target.
-# See LLVM_DISTRIBUTIONS below.
build.targets = ["StandalonePythonModules"]
install.components = ["StandalonePythonModules"]
@@ -54,27 +44,22 @@ CMAKE_C_COMPILER = { env = "CMAKE_C_COMPILER", default = "" }
CMAKE_CXX_COMPILER = { env = "CMAKE_CXX_COMPILER", default = "" }
CMAKE_C_COMPILER_LAUNCHER = { env = "CMAKE_C_COMPILER_LAUNCHER", default = "" }
CMAKE_CXX_COMPILER_LAUNCHER = { env = "CMAKE_CXX_COMPILER_LAUNCHER", default = "" }
-CMAKE_GENERATOR = { env = "CMAKE_GENERATOR", default = "" }
-CMAKE_VERBOSE_MAKEFILE = "ON"
+CMAKE_GENERATOR = { env = "CMAKE_GENERATOR", default = "Ninja" }
LLVM_USE_LINKER = { env = "LLVM_USE_LINKER", default = "" }
-# Optional but highly recommended.
+# Optional but highly recommended (this makes the bindings compatible with other bindings packages
+# by preventing symbol collisions).
CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
CMAKE_C_VISIBILITY_PRESET = "hidden"
CMAKE_CXX_VISIBILITY_PRESET = "hidden"
-# Non-optinal (alternatively you could have CMAKE_PREFIX_PATH here).
+# Non-optinal (alternatively you could use CMAKE_PREFIX_PATH here).
MLIR_DIR = { env = "MLIR_DIR", default = "" }
# Non-optinal
CMAKE_BUILD_TYPE = { env = "CMAKE_BUILD_TYPE", default = "Release" }
MLIR_ENABLE_BINDINGS_PYTHON = "ON"
# Effectively non-optional (any downstream project should specify this).
MLIR_PYTHON_PACKAGE_PREFIX = "mlir_standalone"
-
-# This is for installing/distributing the python bindings target and only the python bindings target.
-LLVM_DISTRIBUTIONS = "StandalonePython"
-LLVM_StandalonePython_DISTRIBUTION_COMPONENTS = "StandalonePythonModules"
-
-# This specifies the directory in the build directory where _mlir_libs, dialects, etc. are installed.
-# Thus, this will be the package (and the name of the package) that pip assumes is the package root.
-# Alternatively, you can use something like wheel.packages = ["python_packages/standalone/mlir_standalone"].
+# This specifies the directory in the install directory (i.e., /tmp/pip-wheel/platlib) where _mlir_libs, dialects, etc.
+# are installed. Thus, this will be the package location (and the name of the package) that pip assumes is
+# the root package.
MLIR_BINDINGS_PYTHON_INSTALL_PREFIX = "mlir_standalone"
diff --git a/mlir/examples/standalone/version.py b/mlir/examples/standalone/version.py
deleted file mode 100644
index 4d8fd170a84c4..0000000000000
--- a/mlir/examples/standalone/version.py
+++ /dev/null
@@ -1,50 +0,0 @@
-# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-# Copyright (c) 2025.
-
-from __future__ import annotations
-from pathlib import Path
-from datetime import datetime
-import os
-import re
-
-__all__ = ["dynamic_metadata"]
-
-
-def __dir__() -> list[str]:
- return __all__
-
-
-def dynamic_metadata(
- field: str,
- settings: dict[str, object] | None = None,
- _project: dict[str, object] = None,
-) -> str:
- if field != "version":
- msg = "Only the 'version' field is supported"
- raise ValueError(msg)
-
- if settings:
- msg = "No inline configuration is supported"
- raise ValueError(msg)
-
- now = datetime.now()
- llvm_datetime = os.environ.get(
- "DATETIME", f"{now.year}{now.month:02}{now.day:02}{now.hour:02}"
- )
-
- llvm_src_root = Path(__file__).parent.parent.parent.parent.parent
- cmake_version_path = llvm_src_root / "cmake/Modules/LLVMVersion.cmake"
- if not cmake_version_path.exists():
- cmake_version_path = llvm_src_root / "llvm/CMakeLists.txt"
- if not cmake_version_path.exists():
- return llvm_datetime
- cmake_txt = open(cmake_version_path).read()
- llvm_version = []
- for v in ["LLVM_VERSION_MAJOR", "LLVM_VERSION_MINOR", "LLVM_VERSION_PATCH"]:
- vn = re.findall(rf"set\({v} (\d+)\)", cmake_txt)
- assert vn, f"couldn't find {v} in cmake txt"
- llvm_version.append(vn[0])
-
- return f"{llvm_version[0]}.{llvm_version[1]}.{llvm_version[2]}.{llvm_datetime}"
diff --git a/mlir/test/Examples/standalone/test.wheel.toy b/mlir/test/Examples/standalone/test.wheel.toy
index 217c2e3322887..b5202b9dd424a 100644
--- a/mlir/test/Examples/standalone/test.wheel.toy
+++ b/mlir/test/Examples/standalone/test.wheel.toy
@@ -1,3 +1,8 @@
+# There's no real issue with windows here, it's just that some CMake generated paths for targets end up being longer
+# than 255 chars when combined with the fact that pip wants to install into a tmp directory buried under
+# C/Users/ContainerAdministrator/AppData/Local/Temp.
+# UNSUPPORTED: target={{.*(windows).*}}
+
# RUN: export CMAKE_BUILD_TYPE=%cmake_build_type
# RUN: export CMAKE_CXX_COMPILER=%host_cxx
# RUN: export CMAKE_CXX_COMPILER_LAUNCHER=%hostcxx_compiler_launcher
@@ -8,7 +13,11 @@
# RUN: export MLIR_DIR="%mlir_cmake_dir"
# RUN: %python -m pip wheel "%mlir_src_root/examples/standalone" -w "%mlir_obj_root/wheelhouse" -v | tee %t
-# RUN: %python -m pip install standalone_python_bindings -f "%mlir_obj_root/wheelhouse" -v | tee -a %t
+
+# RUN: rm -rf "%mlir_obj_root/standalone-python-bindings-install"
+# RUN: %python -m pip install standalone_python_bindings -f "%mlir_obj_root/wheelhouse" --target "%mlir_obj_root/standalone-python-bindings-install" -v | tee -a %t
+
+# RUN: export PYTHONPATH="%mlir_obj_root/standalone-python-bindings-install"
# RUN: %python "%mlir_src_root/examples/standalone/test/python/smoketest.py" nanobind | tee -a %t
# RUN: FileCheck --input-file=%t %s
@@ -19,3 +28,4 @@
# CHECK: %[[C2:.*]] = arith.constant 2 : i32
# CHECK: %[[V0:.*]] = standalone.foo %[[C2]] : i32
# CHECK: }
+
More information about the Mlir-commits
mailing list