|
| 1 | +from distutils import log |
| 2 | +from distutils.command.check import check |
| 3 | +from distutils.command.clean import clean |
| 4 | +from distutils.command.install import install |
| 5 | + |
| 6 | +from setuptools.command.build_ext import build_ext |
| 7 | + |
| 8 | +try: |
| 9 | + from wheel.bdist_wheel import bdist_wheel |
| 10 | +except ImportError: |
| 11 | + bdist_wheel = None |
| 12 | + |
| 13 | + |
| 14 | +def add_rust_extension(dist): |
| 15 | + build_ext_base_class = dist.cmdclass.get('build_ext', build_ext) |
| 16 | + |
| 17 | + class build_ext_rust_extension(build_ext_base_class): |
| 18 | + def run(self): |
| 19 | + if self.distribution.rust_extensions: |
| 20 | + log.info("running build_rust") |
| 21 | + build_rust = self.get_finalized_command("build_rust") |
| 22 | + build_rust.inplace = self.inplace |
| 23 | + build_rust.run() |
| 24 | + |
| 25 | + build_ext_base_class.run(self) |
| 26 | + dist.cmdclass['build_ext'] = build_ext_rust_extension |
| 27 | + |
| 28 | + clean_base_class = dist.cmdclass.get('clean', clean) |
| 29 | + |
| 30 | + class clean_rust_extension(clean_base_class): |
| 31 | + def run(self): |
| 32 | + clean_base_class.run(self) |
| 33 | + if not self.dry_run: |
| 34 | + self.run_command("clean_rust") |
| 35 | + dist.cmdclass['clean'] = clean_rust_extension |
| 36 | + |
| 37 | + check_base_class = dist.cmdclass.get('check', check) |
| 38 | + |
| 39 | + class check_rust_extension(check_base_class): |
| 40 | + def run(self): |
| 41 | + check_base_class.run(self) |
| 42 | + self.run_command("check_rust") |
| 43 | + dist.cmdclass["check"] = check_rust_extension |
| 44 | + |
| 45 | + install_base_class = dist.cmdclass.get('install', install) |
| 46 | + |
| 47 | + # this is required because, install directly access distribution's |
| 48 | + # ext_modules attr to check if dist has ext modules |
| 49 | + class install_rust_extension(install_base_class): |
| 50 | + def finalize_options(self): |
| 51 | + ext_modules = self.distribution.ext_modules |
| 52 | + |
| 53 | + # all ext modules |
| 54 | + mods = [] |
| 55 | + if self.distribution.ext_modules: |
| 56 | + mods.extend(self.distribution.ext_modules) |
| 57 | + if self.distribution.rust_extensions: |
| 58 | + mods.extend(self.distribution.rust_extensions) |
| 59 | + |
| 60 | + scripts = [] |
| 61 | + for ext in self.distribution.rust_extensions: |
| 62 | + scripts.extend(ext.entry_points()) |
| 63 | + |
| 64 | + if scripts: |
| 65 | + if not self.distribution.entry_points: |
| 66 | + self.distribution.entry_points = {"console_scripts": scripts} |
| 67 | + else: |
| 68 | + ep_scripts = self.distribution.entry_points.get("console_scripts") |
| 69 | + if ep_scripts: |
| 70 | + for script in scripts: |
| 71 | + if script not in ep_scripts: |
| 72 | + ep_scripts.append(scripts) |
| 73 | + else: |
| 74 | + ep_scripts = scripts |
| 75 | + |
| 76 | + self.distribution.entry_points["console_scripts"] = ep_scripts |
| 77 | + |
| 78 | + self.distribution.ext_modules = mods |
| 79 | + |
| 80 | + install_base_class.finalize_options(self) |
| 81 | + |
| 82 | + # restore ext_modules |
| 83 | + self.distribution.ext_modules = ext_modules |
| 84 | + dist.cmdclass["install"] = install_rust_extension |
| 85 | + |
| 86 | + if bdist_wheel is not None: |
| 87 | + bdist_wheel_base_class = dist.cmdclass.get("bdist_wheel", bdist_wheel) |
| 88 | + |
| 89 | + # this is for console entries |
| 90 | + class bdist_wheel_rust_extension(bdist_wheel_base_class): |
| 91 | + def finalize_options(self): |
| 92 | + scripts = [] |
| 93 | + for ext in self.distribution.rust_extensions: |
| 94 | + scripts.extend(ext.entry_points()) |
| 95 | + |
| 96 | + if scripts: |
| 97 | + if not self.distribution.entry_points: |
| 98 | + self.distribution.entry_points = {"console_scripts": scripts} |
| 99 | + else: |
| 100 | + ep_scripts = self.distribution.entry_points.get("console_scripts") |
| 101 | + if ep_scripts: |
| 102 | + for script in scripts: |
| 103 | + if script not in ep_scripts: |
| 104 | + ep_scripts.append(scripts) |
| 105 | + else: |
| 106 | + ep_scripts = scripts |
| 107 | + |
| 108 | + self.distribution.entry_points["console_scripts"] = ep_scripts |
| 109 | + |
| 110 | + bdist_wheel_base_class.finalize_options(self) |
| 111 | + dist.cmdclass["bdist_wheel"] = bdist_wheel_rust_extension |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +def rust_extensions(dist, attr, value): |
| 116 | + assert attr == "rust_extensions" |
| 117 | + |
| 118 | + orig_has_ext_modules = dist.has_ext_modules |
| 119 | + dist.has_ext_modules = lambda: ( |
| 120 | + orig_has_ext_modules() or bool(dist.rust_extensions) |
| 121 | + ) |
| 122 | + |
| 123 | + if dist.rust_extensions: |
| 124 | + add_rust_extension(dist) |
0 commit comments