From 8dd23c9d2fcd5967729ecd4c1aab49b0b297bd68 Mon Sep 17 00:00:00 2001 From: Pieter Pas Date: Sat, 18 May 2024 21:54:57 +0200 Subject: [PATCH] Wrap quirks options in CMakeOption --- .../config/options/cmake_opt.py | 13 +++++++++++-- src/py_build_cmake/config/quirks.py | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/py_build_cmake/config/options/cmake_opt.py b/src/py_build_cmake/config/options/cmake_opt.py index 9d81bea..0f7e7e8 100644 --- a/src/py_build_cmake/config/options/cmake_opt.py +++ b/src/py_build_cmake/config/options/cmake_opt.py @@ -18,6 +18,15 @@ class CMakeOption: datatype: str | None = None strict: bool = True + @classmethod + def create(cls, x, datatype=None): + assert datatype is None or isinstance(datatype, str) + if isinstance(x, list): + assert all(isinstance(e, (bool, str)) for e in x) + return cls(values={"value": x}, datatype=datatype) + assert isinstance(x, (bool, str)) + return cls(values={"value": [x]}, datatype=datatype) + class CMakeOptConfigOption(DictOfStrConfigOption): """ @@ -246,7 +255,7 @@ def check_dict_keys(d, pth): valdict[k] = self._convert_to_option(valdict[k], pth) return valdict - def finalize(self, values: ValueReference): + def finalize(self, values: ValueReference) -> dict[str, str]: """Converts the internal dict of CMakeOptions back into a dict of string that can be passed as CMake command-line arguments""" @@ -261,7 +270,7 @@ def convert_key(k, x): assert isinstance(x, CMakeOption) return f"{k}:{x.datatype}" if x.datatype else k - options = {} + options: dict[str, str] = {} for k, v in values.values.items(): options[convert_key(k, v)] = convert(v) return options diff --git a/src/py_build_cmake/config/quirks.py b/src/py_build_cmake/config/quirks.py index af99e02..5e1b2c1 100644 --- a/src/py_build_cmake/config/quirks.py +++ b/src/py_build_cmake/config/quirks.py @@ -21,6 +21,7 @@ python_sysconfig_platform_to_cmake_platform_win, python_sysconfig_platform_to_cmake_processor_win, ) +from .options.cmake_opt import CMakeOption from .options.config_path import ConfPath from .options.value_reference import ValueReference @@ -50,7 +51,7 @@ def possible_locations(): def cross_compile_win( - config: ValueReference, plat_name, library_dirs, cmake_platform, cmake_proc + config: ValueReference, plat_name, library_dirs, cmake_plat, cmake_proc ): """Update the configuration to include a cross-compilation configuration that builds for the given platform and processor. If library_dirs contains @@ -60,7 +61,7 @@ def cross_compile_win( logger.info( "DIST_EXTRA_CONFIG.build_ext specified plat_name that is different from the current platform. " "Automatically enabling cross-compilation for %s", - cmake_platform, + cmake_plat, ) assert not config.is_value_set("cross") cross_cfg = { @@ -68,9 +69,9 @@ def cross_compile_win( "arch": platform_to_platform_tag(plat_name), "cmake": { "options": { - "CMAKE_SYSTEM_NAME": "Windows", - "CMAKE_SYSTEM_PROCESSOR": cmake_proc, - "CMAKE_GENERATOR_PLATFORM": cmake_platform, + "CMAKE_SYSTEM_NAME": CMakeOption.create("Windows", "STRING"), + "CMAKE_SYSTEM_PROCESSOR": CMakeOption.create(cmake_proc, "STRING"), + "CMAKE_GENERATOR_PLATFORM": CMakeOption.create(cmake_plat, "STRING"), } }, } @@ -157,8 +158,8 @@ def cross_compile_mac(config: ValueReference, archs): "os": "mac", "cmake": { "options": { - "CMAKE_SYSTEM_NAME": "Darwin", - "CMAKE_OSX_ARCHITECTURES": ";".join(archs), + "CMAKE_SYSTEM_NAME": CMakeOption.create("Darwin", "STRING"), + "CMAKE_OSX_ARCHITECTURES": CMakeOption.create(archs, "STRING"), } }, } @@ -182,7 +183,7 @@ def config_quirks_mac(config: ValueReference): archflags = os.getenv("ARCHFLAGS") if not archflags: return - archs = tuple(re.findall(r"-arch +(\S+)", archflags)) + archs = list(re.findall(r"-arch +(\S+)", archflags)) if not archs: logger.warning( "ARCHFLAGS was set, but its value was not valid, so I'm ignoring it" @@ -207,7 +208,7 @@ def config_quirks_mac(config: ValueReference): config.set_value_default(ConfPath.from_string("cmake/options"), {}) config.set_value_default( ConfPath.from_string("cmake/options/CMAKE_OSX_ARCHITECTURES"), - ";".join(archs), + CMakeOption.create(archs, "STRING"), )