Skip to content

Commit

Permalink
Unify search path handling
Browse files Browse the repository at this point in the history
- Validate the path types of the entry-points
- Update the entry-points values with the settings value
- Flatten the entry-points to a list or add them as a dict

- Print more debug messages
- Fix the settings type to string

Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Sep 3, 2024
1 parent 0cf744c commit 5770c6a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 45 deletions.
17 changes: 16 additions & 1 deletion src/scikit_build_core/_compat/importlib/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@
else:
from importlib.resources import files

__all__ = ["files"]
if sys.version_info < (3, 10):
# Readers were introduced in 3.10
from importlib_resources import readers
elif sys.version_info < (3, 11):
# In 3.10 readers were implemented in importlib.readers
from importlib import readers
else:
# From 3.11 it is accessed as importlib.resources.readers
from importlib.resources import readers

if sys.version_info < (3, 11):
from importlib_resources import abc
else:
from importlib.resources import abc

__all__ = ["files", "readers", "abc"]


def __dir__() -> list[str]:
Expand Down
104 changes: 68 additions & 36 deletions src/scikit_build_core/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,65 @@ def _filter_env_cmake_args(env_cmake_args: list[str]) -> Generator[str, None, No
yield arg


def _sanitize_path(path: resources.abc.Traversable) -> list[Path] | None:
if isinstance(path, resources.readers.MultiplexedPath):
return path._paths

Check warning on line 89 in src/scikit_build_core/builder/builder.py

View workflow job for this annotation

GitHub Actions / Format

W0212

Access to a protected member _paths of a client class
if isinstance(path, Path):
return [path]
logger.warning("Unknown path type: [{}] {}", type(path), path)
return None


def _handle_search_paths(
entry_point: str,
settings_val: list[str] | dict[str, str] | None,
output: list[Path] | dict[str, list[Path]],
) -> None:
# Sanity checks
if isinstance(output, dict):
assert isinstance(settings_val, dict)

# Get the search paths from the entry points
search_paths_dict = {}
eps = metadata.entry_points(group=entry_point)
if eps:
logger.debug(
"Loading search paths {} from entry-points: {}", entry_point, len(eps)
)
for ep in eps:
ep_value = _sanitize_path(resources.files(ep.load()))
logger.debug("{}: {} -> {}", ep.name, ep.value, ep_value)
if ep_value:
search_paths_dict[ep.name] = ep_value

# Update the search paths from the settings options
if isinstance(settings_val, dict) and settings_val:
logger.debug(
"Overriding search paths {} from config: {}", entry_point, settings_val
)
for key, val in settings_val.items():
if val:
# TODO: Allow settings_val to be dict[str, list[str]]?
search_paths_dict[key] = [Path(val)]
else:
search_paths_dict.pop(key)

# Write to the output
if isinstance(output, list):
search_paths_list = [
path for ep_values in search_paths_dict.values() for path in ep_values
]
# If the settings options was a list the values are appended as-is
if isinstance(settings_val, list) and settings_val:
logger.debug(
"Appending search paths {} with config: {}", entry_point, settings_val
)
search_paths_list += map(Path, settings_val)
output.extend(search_paths_list)
return
output.update(search_paths_dict)


@dataclasses.dataclass
class Builder:
settings: ScikitBuildSettings
Expand Down Expand Up @@ -123,47 +182,20 @@ def configure(
}

# Add any extra CMake modules
module_dirs_dict = {
ep.name: resources.files(ep.load())
for ep in metadata.entry_points(group="cmake.module")
}
if isinstance(self.settings.search.modules, dict):
# Allow to override any entry-point definition
module_dirs_dict.update(self.settings.search.modules)
module_dirs = list(module_dirs_dict.values())
if isinstance(self.settings.search.modules, list):
# If it was a list, append to the entry-point definitions
module_dirs += self.settings.search.modules
# Remove any empty paths
module_dirs = [path for path in module_dirs if path]
self.config.module_dirs.extend(module_dirs)
_handle_search_paths(
"cmake.module", self.settings.search.modules, self.config.module_dirs
)

# Add any extra CMake prefixes
prefix_dirs_dict = {
ep.name: resources.files(ep.load())
for ep in metadata.entry_points(group="cmake.prefix")
}
if isinstance(self.settings.search.prefixes, dict):
# Allow to override any entry-point definition
prefix_dirs_dict.update(self.settings.search.prefixes)
prefix_dirs = list(prefix_dirs_dict.values())
if isinstance(self.settings.search.prefixes, list):
# If it was a list, append to the entry-point definitions
prefix_dirs += self.settings.search.prefixes
# Remove any empty paths
prefix_dirs = [path for path in prefix_dirs if path]
self.config.prefix_dirs.extend(prefix_dirs)
_handle_search_paths(
"cmake.prefix", self.settings.search.prefixes, self.config.prefix_dirs
)

# Add all CMake roots
prefix_roots = {
ep.name: resources.files(ep.load())
for ep in metadata.entry_points(group="cmake.root")
}
# TODO: Check for unique uppercase names
prefix_roots.update(self.settings.search.roots)
# Remove any empty paths
prefix_roots = {pkg: path for pkg, path in prefix_roots.items() if path}
self.config.prefix_roots.update(prefix_roots)
_handle_search_paths(
"cmake.root", self.settings.search.roots, self.config.prefix_roots
)

# Add site-packages to the prefix path for CMake
site_packages = Path(sysconfig.get_path("purelib"))
Expand Down
10 changes: 5 additions & 5 deletions src/scikit_build_core/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CMaker:
build_type: str
module_dirs: list[Path] = dataclasses.field(default_factory=list)
prefix_dirs: list[Path] = dataclasses.field(default_factory=list)
prefix_roots: dict[str, Path] = dataclasses.field(default_factory=dict)
prefix_roots: dict[str, list[Path]] = dataclasses.field(default_factory=dict)
init_cache_file: Path = dataclasses.field(init=False, default=Path())
env: dict[str, str] = dataclasses.field(init=False, default_factory=os.environ.copy)
single_config: bool = not sysconfig.get_platform().startswith("win")
Expand Down Expand Up @@ -185,14 +185,14 @@ def init_cache(
f.write('set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE "BOTH" CACHE PATH "")\n')

if self.prefix_roots:
for pkg, path in self.prefix_roots.items():
path_str = str(path).replace("\\", "/")
for pkg, path_list in self.prefix_roots.items():
paths_str = ";".join(map(str, path_list)).replace("\\", "/")
f.write(
f'set({pkg}_ROOT [===[{path_str}]===] CACHE PATH "" FORCE)\n'
f'set({pkg}_ROOT [===[{paths_str}]===] CACHE PATH "" FORCE)\n'
)
# Available since CMake 3.27 with CMP0144
f.write(
f'set({pkg.upper()}_ROOT [===[{path_str}]===] CACHE PATH "" FORCE)\n'
f'set({pkg.upper()}_ROOT [===[{paths_str}]===] CACHE PATH "" FORCE)\n'
)

contents = self.init_cache_file.read_text(encoding="utf-8").strip()
Expand Down
6 changes: 3 additions & 3 deletions src/scikit_build_core/settings/skbuild_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ class SearchSettings:
Add the wheel build path to the CMake prefix paths.
"""

modules: Optional[Union[List[Path], Dict[str, Path]]] = None
modules: Optional[Union[List[str], Dict[str, str]]] = None
"""
List or dict of CMake module search paths. Dict from is used to override
another package's entry-point definition. Populates `CMAKE_MODULE_PATH`.
"""

prefixes: Optional[Union[List[Path], Dict[str, Path]]] = None
prefixes: Optional[Union[List[str], Dict[str, str]]] = None
"""
List or dict of CMake prefix search paths. Dict from is used to override
another package's entry-point definition. Populates `CMAKE_PREFIX_PATH`.
"""

roots: Dict[str, Path] = dataclasses.field(default_factory=dict)
roots: Dict[str, str] = dataclasses.field(default_factory=dict)
"""
Dict of package names and prefix paths. Populates `<Pkg>_ROOT`.
"""
Expand Down

0 comments on commit 5770c6a

Please sign in to comment.