|
| 1 | +import pathlib |
| 2 | +import platform |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +from distutils.command.build import build |
| 6 | +from distutils.command.install import install |
| 7 | + |
| 8 | +from setuptools import setup |
| 9 | +from wheel.bdist_wheel import bdist_wheel |
| 10 | + |
| 11 | +PROJECT_DIR = pathlib.Path(__file__).parent |
| 12 | +MPY_CROSS_DIR = PROJECT_DIR / "micropython" / "mpy-cross" |
| 13 | + |
| 14 | +# map of distutils platform id to compiler target triple |
| 15 | +PLAT_TO_CLANG_TARGET = { |
| 16 | + "macosx-10.9-x86_64": "-target x86_64-apple-macos10.9", |
| 17 | + "macosx-10.12-x86_64": "-target x86_64-apple-macos10.12", |
| 18 | + "macosx-11.0-arm64": "-target arm64-apple-macos11", |
| 19 | + "macosx-12-arm64": "-target arm64-apple-macos12", |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +class custom_build(build): |
| 24 | + def run(self): |
| 25 | + super().run() |
| 26 | + |
| 27 | + mpy_cross_exe = ( |
| 28 | + MPY_CROSS_DIR / f"build-{self.plat_name}" / "mpy-cross" |
| 29 | + ).with_suffix(".exe" if platform.system() == "Windows" else "") |
| 30 | + |
| 31 | + # all builds put exe in the same place, so we have to remove to avoid |
| 32 | + # make not rebuilding for different arch |
| 33 | + mpy_cross_exe.unlink(missing_ok=True) |
| 34 | + |
| 35 | + make_command = [ |
| 36 | + "make", |
| 37 | + "-C", |
| 38 | + MPY_CROSS_DIR, |
| 39 | + f"BUILD=build-{self.plat_name}", |
| 40 | + ] |
| 41 | + |
| 42 | + # special case to handle potential cross-compiling on macOS |
| 43 | + if self.plat_name.startswith("macosx"): |
| 44 | + cflags = PLAT_TO_CLANG_TARGET[self.plat_name] |
| 45 | + |
| 46 | + make_command.extend( |
| 47 | + [ |
| 48 | + f"CFLAGS_EXTRA={cflags}", |
| 49 | + f"LDFLAGS_EXTRA={cflags}", |
| 50 | + ] |
| 51 | + ) |
| 52 | + |
| 53 | + subprocess.check_call(make_command) |
| 54 | + |
| 55 | + shutil.copy(str(mpy_cross_exe), self.build_lib + "/mpy_cross_v5") |
| 56 | + |
| 57 | + |
| 58 | +class custom_install(install): |
| 59 | + def finalize_options(self) -> None: |
| 60 | + super().finalize_options() |
| 61 | + self.install_lib = self.install_lib.replace("/purelib/", "/platlib/") |
| 62 | + |
| 63 | + |
| 64 | +class custom_bdist_wheel(bdist_wheel): |
| 65 | + def finalize_options(self): |
| 66 | + super().finalize_options() |
| 67 | + |
| 68 | + # this indicates that there is a binary component so that we get the |
| 69 | + # binary platform tag later instead of "any" |
| 70 | + self.root_is_pure = False |
| 71 | + |
| 72 | + def get_tag(self): |
| 73 | + _, _, platform_tag = super().get_tag() |
| 74 | + |
| 75 | + # since this isn't an extension that calls CPython APIs, we don't need |
| 76 | + # specific Python or ABI tags, only the platform |
| 77 | + return "py3", "none", platform_tag |
| 78 | + |
| 79 | + |
| 80 | +setup( |
| 81 | + cmdclass={ |
| 82 | + "build": custom_build, |
| 83 | + "install": custom_install, |
| 84 | + "bdist_wheel": custom_bdist_wheel, |
| 85 | + }, |
| 86 | +) |
0 commit comments