Skip to content

Commit 4b969ea

Browse files
committed
Add option to cargo vendor crates into sdist
1 parent 7b7ab01 commit 4b969ea

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Added
88
- Support building x86-64 wheel on arm64 macOS machine. [#114](https://github.com/PyO3/setuptools-rust/pull/114)
99
- Add macOS universal2 wheel building support. [#115](https://github.com/PyO3/setuptools-rust/pull/115)
10+
- Add option to cargo vendor crates into sdist. [#118](https://github.com/PyO3/setuptools-rust/pull/118)
1011

1112
### Changed
1213
- Respect `PYO3_PYTHON` and `PYTHON_SYS_EXECUTABLE` environment variables if set. [#96](https://github.com/PyO3/setuptools-rust/pull/96)

setuptools_rust/setuptools_ext.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from setuptools.command.build_ext import build_ext
77
from setuptools.command.install import install
8+
from distutils.command.sdist import sdist
9+
import subprocess
810

911
try:
1012
from wheel.bdist_wheel import bdist_wheel
@@ -13,6 +15,45 @@
1315

1416

1517
def add_rust_extension(dist):
18+
sdist_base_class = dist.cmdclass.get("sdist", sdist)
19+
sdist_options = sdist_base_class.user_options.copy()
20+
sdist_boolean_options = sdist_base_class.boolean_options.copy()
21+
sdist_negative_opt = sdist_base_class.negative_opt.copy()
22+
sdist_options.extend([
23+
('vendor-crates', None,
24+
"vendor Rust crates"),
25+
('no-vendor-crates', None,
26+
"don't vendor Rust crates."
27+
"[default; enable with --vendor-crates]"),
28+
])
29+
sdist_boolean_options.append('vendor-crates')
30+
sdist_negative_opt['no-vendor-crates'] = 'vendor-crates'
31+
32+
class sdist_rust_extension(sdist_base_class):
33+
user_options = sdist_options
34+
boolean_options = sdist_boolean_options
35+
negative_opt = sdist_negative_opt
36+
37+
def initialize_options(self):
38+
super().initialize_options()
39+
self.vendor_crates = 0
40+
41+
def get_file_list(self):
42+
if self.vendor_crates:
43+
base_dir = self.distribution.get_fullname()
44+
vendor_path = os.path.join(base_dir, "vendored-crates")
45+
cargo_config = subprocess.check_output(["cargo", "vendor", vendor_path])
46+
cargo_config = cargo_config.replace(base_dir.encode() + b'/', b'')
47+
dot_cargo_path = os.path.join(base_dir, ".cargo")
48+
self.mkpath(dot_cargo_path)
49+
cargo_config_path = os.path.join(dot_cargo_path, "config.toml")
50+
with open(cargo_config_path, "wb") as f:
51+
f.write(cargo_config)
52+
self.filelist.append(vendor_path)
53+
self.filelist.append(cargo_config_path)
54+
super().get_file_list()
55+
dist.cmdclass["sdist"] = sdist_rust_extension
56+
1657
build_ext_base_class = dist.cmdclass.get('build_ext', build_ext)
1758

1859
class build_ext_rust_extension(build_ext_base_class):

0 commit comments

Comments
 (0)