|
| 1 | +# Copyright 2023 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Custom setup.py for platform-specific wheel tagging. |
| 16 | +
|
| 17 | +This file exists solely to customize the wheel platform tag. All package metadata |
| 18 | +is defined in pyproject.toml. |
| 19 | +
|
| 20 | +The native FFI libraries (.so/.dylib/.dll) require specific platform tags that |
| 21 | +respect MACOSX_DEPLOYMENT_TARGET and ARCHFLAGS environment variables set by |
| 22 | +cibuildwheel, rather than using sysconfig.get_platform() which returns Python's |
| 23 | +compile-time values. |
| 24 | +""" |
| 25 | + |
| 26 | +import os |
| 27 | +import platform |
| 28 | +import sys |
| 29 | + |
| 30 | +import setuptools # type: ignore |
| 31 | +from wheel.bdist_wheel import bdist_wheel as _bdist_wheel # type: ignore |
| 32 | + |
| 33 | + |
| 34 | +def get_platform_tag(): |
| 35 | + """Get the wheel platform tag for the current/target platform.""" |
| 36 | + if sys.platform == "darwin": |
| 37 | + # Get deployment target from environment (set by cibuildwheel) or fall back |
| 38 | + target = os.environ.get("MACOSX_DEPLOYMENT_TARGET") |
| 39 | + if not target: |
| 40 | + target = platform.mac_ver()[0] |
| 41 | + parts = target.split(".") |
| 42 | + target = f"{parts[0]}.{parts[1] if len(parts) > 1 else '0'}" |
| 43 | + |
| 44 | + version_tag = target.replace(".", "_") |
| 45 | + |
| 46 | + # Check ARCHFLAGS for cross-compilation (cibuildwheel sets this) |
| 47 | + archflags = os.environ.get("ARCHFLAGS", "") |
| 48 | + if "-arch arm64" in archflags: |
| 49 | + arch = "arm64" |
| 50 | + elif "-arch x86_64" in archflags: |
| 51 | + arch = "x86_64" |
| 52 | + else: |
| 53 | + arch = platform.machine() |
| 54 | + |
| 55 | + return f"macosx_{version_tag}_{arch}" |
| 56 | + elif sys.platform == "linux": |
| 57 | + return f"linux_{platform.machine()}" |
| 58 | + elif sys.platform == "win32": |
| 59 | + arch = platform.machine() |
| 60 | + if arch == "AMD64": |
| 61 | + arch = "amd64" |
| 62 | + return f"win_{arch}" |
| 63 | + else: |
| 64 | + return f"{platform.system().lower()}_{platform.machine()}" |
| 65 | + |
| 66 | + |
| 67 | +class bdist_wheel(_bdist_wheel): |
| 68 | + def finalize_options(self): |
| 69 | + self.plat_name = get_platform_tag() |
| 70 | + _bdist_wheel.finalize_options(self) |
| 71 | + |
| 72 | + |
| 73 | +setuptools.setup( |
| 74 | + cmdclass={ |
| 75 | + "bdist_wheel": bdist_wheel, |
| 76 | + }, |
| 77 | +) |
0 commit comments