From 47a78077b46004b510041f6ac87d8c02fba7eb51 Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sun, 26 Nov 2023 15:48:58 +0100 Subject: [PATCH] Add helpers for cross-compilation --- api/python/backend/config.py | 2 ++ api/python/backend/setup.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/api/python/backend/config.py b/api/python/backend/config.py index f86b66da66..eafc7d475f 100644 --- a/api/python/backend/config.py +++ b/api/python/backend/config.py @@ -87,6 +87,8 @@ def cmake_dump(self) -> List[str]: class CrossCompilation(BaseModel): osx_arch: Optional[str] = Field(None, alias="osx-arch") platform: Optional[str] = None + pyversion: Optional[str] = None + abi: Optional[str] = None def cmake_dump(self) -> List[str]: out: List[str] = [] diff --git a/api/python/backend/setup.py b/api/python/backend/setup.py index ac4aa41ffd..649dfc189e 100644 --- a/api/python/backend/setup.py +++ b/api/python/backend/setup.py @@ -13,6 +13,8 @@ DEFAULT_CONF = CWD / ".." / "config-default.toml" +_WHEEL_TAG_COMPUTE_BEST = None + @lru_cache(maxsize=1) def get_config(): from config import Config @@ -21,6 +23,24 @@ def get_config(): return Config.from_file(config_path) return Config.from_file(DEFAULT_CONF.as_posix()) + +def _compute_best(cls, *arg, **kwargs): + from scikit_build_core.builder.wheel_tag import WheelTag + best_tag = _WHEEL_TAG_COMPUTE_BEST(cls, *arg, **kwargs) + config = get_config() + + pyvers = best_tag.pyvers + abis = best_tag.abis + archs = best_tag.archs + + if version := config.cross_compilation.pyversion: + pyvers = [version] + + if abi := config.cross_compilation.abi: + abis = [abi] + + return WheelTag(pyvers, abis, archs) + def _fix_env(): config = get_config() if sys.platform.startswith("win"): @@ -42,6 +62,11 @@ def _fix_env(): os.environ["ARCHFLAGS"]= f"-arch {config.osx_arch}" if platform := config.cross_compilation.platform: + global _WHEEL_TAG_COMPUTE_BEST + from scikit_build_core.builder.wheel_tag import WheelTag + _WHEEL_TAG_COMPUTE_BEST = WheelTag.compute_best + WheelTag.compute_best = _compute_best + os.environ["_PYTHON_HOST_PLATFORM"] = platform if os.name == 'nt' and platform == 'win32':