From 9fea5e31722b35ee66c564538fb84c96f10eee40 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 5 Nov 2024 12:01:47 -0500 Subject: [PATCH] build: respect the "pure" argument at metadata generation time When a pure build is requested, no compiled extensions are produced. The setuptools_rust dependency ends up imported-but-not-used. Teach setuptools to avoid build-requiring it in this case. Closes: https://github.com/jelmer/dulwich/issues/1405 --- pyproject.toml | 2 +- setup.py | 58 ++++++++++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2f7c59681..aaa873223 100644 --- pyproject.toml +++ pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=61.2", "setuptools-rust"] +requires = ["setuptools>=61.2"] build-backend = "setuptools.build_meta" [project] diff --git a/setup.py b/setup.py index 6eada1487..07381eee8 100755 --- setup.py +++ setup.py @@ -6,7 +6,6 @@ import sys from setuptools import setup -from setuptools_rust import Binding, RustExtension tests_require = ["fastimport"] @@ -18,35 +17,45 @@ optional = os.environ.get("CIBUILDWHEEL", "0") != "1" -rust_extensions = [ - RustExtension( - "dulwich._objects", - "crates/objects/Cargo.toml", - binding=Binding.PyO3, - optional=optional, - ), - RustExtension( - "dulwich._pack", - "crates/pack/Cargo.toml", - binding=Binding.PyO3, - optional=optional, - ), - RustExtension( - "dulwich._diff_tree", - "crates/diff-tree/Cargo.toml", - binding=Binding.PyO3, - optional=optional, - ), -] - # Ideally, setuptools would just provide a way to do this -if "--pure" in sys.argv: - sys.argv.remove("--pure") +if "PURE" in os.environ or "--pure" in sys.argv: + if "--pure" in sys.argv: + sys.argv.remove("--pure") + setup_requires = [] rust_extensions = [] +else: + setup_requires = ["setuptools_rust"] + # We check for egg_info since that indicates we are running prepare_metadata_for_build_* + if "egg_info" in sys.argv: + rust_extensions = [] + else: + from setuptools_rust import Binding, RustExtension + + rust_extensions = [ + RustExtension( + "dulwich._objects", + "crates/objects/Cargo.toml", + binding=Binding.PyO3, + optional=optional, + ), + RustExtension( + "dulwich._pack", + "crates/pack/Cargo.toml", + binding=Binding.PyO3, + optional=optional, + ), + RustExtension( + "dulwich._diff_tree", + "crates/diff-tree/Cargo.toml", + binding=Binding.PyO3, + optional=optional, + ), + ] setup( package_data={"": ["py.typed"]}, rust_extensions=rust_extensions, + setup_requires=setup_requires, tests_require=tests_require, )