|
5 | 5 |
|
6 | 6 | from setuptools.command.build_ext import build_ext
|
7 | 7 | from setuptools.command.install import install
|
| 8 | +from distutils.command.sdist import sdist |
| 9 | +import sys |
| 10 | +import subprocess |
8 | 11 |
|
9 | 12 | try:
|
10 | 13 | from wheel.bdist_wheel import bdist_wheel
|
|
13 | 16 |
|
14 | 17 |
|
15 | 18 | def add_rust_extension(dist):
|
| 19 | + sdist_base_class = dist.cmdclass.get("sdist", sdist) |
| 20 | + sdist_options = sdist_base_class.user_options.copy() |
| 21 | + sdist_boolean_options = sdist_base_class.boolean_options.copy() |
| 22 | + sdist_negative_opt = sdist_base_class.negative_opt.copy() |
| 23 | + sdist_options.extend([ |
| 24 | + ('vendor-crates', None, |
| 25 | + "vendor Rust crates"), |
| 26 | + ('no-vendor-crates', None, |
| 27 | + "don't vendor Rust crates." |
| 28 | + "[default; enable with --vendor-crates]"), |
| 29 | + ]) |
| 30 | + sdist_boolean_options.append('vendor-crates') |
| 31 | + sdist_negative_opt['no-vendor-crates'] = 'vendor-crates' |
| 32 | + |
| 33 | + class sdist_rust_extension(sdist_base_class): |
| 34 | + user_options = sdist_options |
| 35 | + boolean_options = sdist_boolean_options |
| 36 | + negative_opt = sdist_negative_opt |
| 37 | + |
| 38 | + def initialize_options(self): |
| 39 | + super().initialize_options() |
| 40 | + self.vendor_crates = 0 |
| 41 | + |
| 42 | + def get_file_list(self): |
| 43 | + super().get_file_list() |
| 44 | + if self.vendor_crates: |
| 45 | + manifest_paths = [] |
| 46 | + for ext in self.distribution.rust_extensions: |
| 47 | + manifest_paths.append(ext.path) |
| 48 | + if manifest_paths: |
| 49 | + base_dir = self.distribution.get_fullname() |
| 50 | + dot_cargo_path = os.path.join(base_dir, ".cargo") |
| 51 | + self.mkpath(dot_cargo_path) |
| 52 | + cargo_config_path = os.path.join(dot_cargo_path, "config.toml") |
| 53 | + vendor_path = os.path.join(dot_cargo_path, "vendor") |
| 54 | + command = [ |
| 55 | + "cargo", "vendor" |
| 56 | + ] |
| 57 | + # additional Cargo.toml for extension 1..n |
| 58 | + for extra_path in manifest_paths[1:]: |
| 59 | + command.append("--sync") |
| 60 | + command.append(extra_path) |
| 61 | + # `cargo vendor --sync` accepts multiple values, for example |
| 62 | + # `cargo vendor --sync a --sync b --sync c vendor_path` |
| 63 | + # but it would also consider vendor_path as --sync value |
| 64 | + # set --manifest-path before vendor_path and after --sync to workaround that |
| 65 | + # See https://docs.rs/clap/latest/clap/struct.Arg.html#method.multiple for detail |
| 66 | + command.extend(["--manifest-path", manifest_paths[0], vendor_path]) |
| 67 | + cargo_config = subprocess.check_output(command) |
| 68 | + base_dir_bytes = base_dir.encode(sys.getfilesystemencoding()) + os.sep.encode() |
| 69 | + if os.sep == '\\': |
| 70 | + # TOML escapes backslash \ |
| 71 | + base_dir_bytes += os.sep.encode() |
| 72 | + cargo_config = cargo_config.replace(base_dir_bytes, b'') |
| 73 | + if os.altsep: |
| 74 | + cargo_config = cargo_config.replace(base_dir_bytes + os.altsep.encode(), b'') |
| 75 | + |
| 76 | + # Check whether `.cargo/config`/`.cargo/config.toml` already exists |
| 77 | + existing_cargo_config = None |
| 78 | + for filename in (f".cargo{os.sep}config", f".cargo{os.sep}config.toml"): |
| 79 | + if filename in self.filelist.allfiles: |
| 80 | + existing_cargo_config = filename |
| 81 | + break |
| 82 | + if existing_cargo_config: |
| 83 | + cargo_config_path = os.path.join(base_dir, existing_cargo_config) |
| 84 | + # Append vendor config to original cargo config |
| 85 | + with open(existing_cargo_config, "rb") as f: |
| 86 | + cargo_config = f.read() + b'\n' + cargo_config |
| 87 | + |
| 88 | + with open(cargo_config_path, "wb") as f: |
| 89 | + f.write(cargo_config) |
| 90 | + self.filelist.append(vendor_path) |
| 91 | + self.filelist.append(cargo_config_path) |
| 92 | + dist.cmdclass["sdist"] = sdist_rust_extension |
| 93 | + |
16 | 94 | build_ext_base_class = dist.cmdclass.get('build_ext', build_ext)
|
17 | 95 |
|
18 | 96 | class build_ext_rust_extension(build_ext_base_class):
|
|
0 commit comments