Skip to content

Commit

Permalink
Wrap quirks options in CMakeOption
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed May 18, 2024
1 parent e26ce3f commit 8dd23c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/py_build_cmake/config/options/cmake_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"""

Expand All @@ -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
19 changes: 10 additions & 9 deletions src/py_build_cmake/config/quirks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -60,17 +61,17 @@ 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 = {
"os": "windows",
"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"),
}
},
}
Expand Down Expand Up @@ -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"),
}
},
}
Expand All @@ -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"
Expand All @@ -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"),
)


Expand Down

0 comments on commit 8dd23c9

Please sign in to comment.