|
23 | 23 |
|
24 | 24 | if TYPE_CHECKING:
|
25 | 25 | from collections.abc import Generator, Iterable, Mapping, Sequence
|
| 26 | + from typing import Any |
26 | 27 |
|
27 | 28 | from packaging.version import Version
|
28 | 29 |
|
@@ -84,6 +85,68 @@ def _filter_env_cmake_args(env_cmake_args: list[str]) -> Generator[str, None, No
|
84 | 85 | yield arg
|
85 | 86 |
|
86 | 87 |
|
| 88 | +# Type-hinting for Traversable is rather hard because they were introduced in python 3.11. |
| 89 | +# This avoids introducing importlib_resources dependency that may not be used in the actual package loader |
| 90 | +def _sanitize_path(path: Any) -> list[Path] | None: |
| 91 | + if isinstance(path, resources.readers.MultiplexedPath): |
| 92 | + # pylint: disable-next=protected-access |
| 93 | + return path._paths |
| 94 | + if isinstance(path, Path): |
| 95 | + return [path] |
| 96 | + logger.warning("Unknown path type: [{}] {}", type(path), path) |
| 97 | + return None |
| 98 | + |
| 99 | + |
| 100 | +def _handle_search_paths( |
| 101 | + entry_point: str, |
| 102 | + settings_val: list[str] | dict[str, str] | None, |
| 103 | + output: list[Path] | dict[str, list[Path]], |
| 104 | +) -> None: |
| 105 | + # Sanity checks |
| 106 | + if isinstance(output, dict): |
| 107 | + assert isinstance(settings_val, dict) |
| 108 | + |
| 109 | + # Get the search paths from the entry points |
| 110 | + search_paths_dict = {} |
| 111 | + eps = metadata.entry_points(group=entry_point) |
| 112 | + if eps: |
| 113 | + logger.debug( |
| 114 | + "Loading search paths {} from entry-points: {}", entry_point, len(eps) |
| 115 | + ) |
| 116 | + for ep in eps: |
| 117 | + ep_value = _sanitize_path(resources.files(ep.load())) |
| 118 | + logger.debug("{}: {} -> {}", ep.name, ep.value, ep_value) |
| 119 | + if ep_value: |
| 120 | + search_paths_dict[ep.name] = ep_value |
| 121 | + |
| 122 | + # Update the search paths from the settings options |
| 123 | + if isinstance(settings_val, dict) and settings_val: |
| 124 | + logger.debug( |
| 125 | + "Overriding search paths {} from config: {}", entry_point, settings_val |
| 126 | + ) |
| 127 | + for key, val in settings_val.items(): |
| 128 | + if val: |
| 129 | + # TODO: Allow settings_val to be dict[str, list[str]]? |
| 130 | + search_paths_dict[key] = [Path(val)] |
| 131 | + else: |
| 132 | + search_paths_dict.pop(key) |
| 133 | + |
| 134 | + # Write to the output |
| 135 | + if isinstance(output, list): |
| 136 | + search_paths_list = [ |
| 137 | + path for ep_values in search_paths_dict.values() for path in ep_values |
| 138 | + ] |
| 139 | + # If the settings options was a list the values are appended as-is |
| 140 | + if isinstance(settings_val, list) and settings_val: |
| 141 | + logger.debug( |
| 142 | + "Appending search paths {} with config: {}", entry_point, settings_val |
| 143 | + ) |
| 144 | + search_paths_list += map(Path, settings_val) |
| 145 | + output.extend(search_paths_list) |
| 146 | + return |
| 147 | + output.update(search_paths_dict) |
| 148 | + |
| 149 | + |
87 | 150 | @dataclasses.dataclass
|
88 | 151 | class Builder:
|
89 | 152 | settings: ScikitBuildSettings
|
@@ -123,47 +186,20 @@ def configure(
|
123 | 186 | }
|
124 | 187 |
|
125 | 188 | # Add any extra CMake modules
|
126 |
| - module_dirs_dict = { |
127 |
| - ep.name: resources.files(ep.load()) |
128 |
| - for ep in metadata.entry_points(group="cmake.module") |
129 |
| - } |
130 |
| - if isinstance(self.settings.search.modules, dict): |
131 |
| - # Allow to override any entry-point definition |
132 |
| - module_dirs_dict.update(self.settings.search.modules) |
133 |
| - module_dirs = list(module_dirs_dict.values()) |
134 |
| - if isinstance(self.settings.search.modules, list): |
135 |
| - # If it was a list, append to the entry-point definitions |
136 |
| - module_dirs += self.settings.search.modules |
137 |
| - # Remove any empty paths |
138 |
| - module_dirs = [path for path in module_dirs if path] |
139 |
| - self.config.module_dirs.extend(module_dirs) |
| 189 | + _handle_search_paths( |
| 190 | + "cmake.module", self.settings.search.modules, self.config.module_dirs |
| 191 | + ) |
140 | 192 |
|
141 | 193 | # Add any extra CMake prefixes
|
142 |
| - prefix_dirs_dict = { |
143 |
| - ep.name: resources.files(ep.load()) |
144 |
| - for ep in metadata.entry_points(group="cmake.prefix") |
145 |
| - } |
146 |
| - if isinstance(self.settings.search.prefixes, dict): |
147 |
| - # Allow to override any entry-point definition |
148 |
| - prefix_dirs_dict.update(self.settings.search.prefixes) |
149 |
| - prefix_dirs = list(prefix_dirs_dict.values()) |
150 |
| - if isinstance(self.settings.search.prefixes, list): |
151 |
| - # If it was a list, append to the entry-point definitions |
152 |
| - prefix_dirs += self.settings.search.prefixes |
153 |
| - # Remove any empty paths |
154 |
| - prefix_dirs = [path for path in prefix_dirs if path] |
155 |
| - self.config.prefix_dirs.extend(prefix_dirs) |
| 194 | + _handle_search_paths( |
| 195 | + "cmake.prefix", self.settings.search.prefixes, self.config.prefix_dirs |
| 196 | + ) |
156 | 197 |
|
157 | 198 | # Add all CMake roots
|
158 |
| - prefix_roots = { |
159 |
| - ep.name: resources.files(ep.load()) |
160 |
| - for ep in metadata.entry_points(group="cmake.root") |
161 |
| - } |
162 | 199 | # TODO: Check for unique uppercase names
|
163 |
| - prefix_roots.update(self.settings.search.roots) |
164 |
| - # Remove any empty paths |
165 |
| - prefix_roots = {pkg: path for pkg, path in prefix_roots.items() if path} |
166 |
| - self.config.prefix_roots.update(prefix_roots) |
| 200 | + _handle_search_paths( |
| 201 | + "cmake.root", self.settings.search.roots, self.config.prefix_roots |
| 202 | + ) |
167 | 203 |
|
168 | 204 | # Add site-packages to the prefix path for CMake
|
169 | 205 | site_packages = Path(sysconfig.get_path("purelib"))
|
|
0 commit comments