simplify api: recursive="heuristic" by default

This commit is contained in:
Harmen Stoppels 2024-11-11 12:26:48 +01:00
parent a3dbbae861
commit 4d979b0676
2 changed files with 17 additions and 23 deletions

View File

@ -2108,9 +2108,8 @@ def add_macro(self, macro):
def find_headers( def find_headers(
headers: Union[List[str], str], headers: Union[List[str], str],
root: str, root: str,
recursive: bool = False, recursive: Union[bool, str] = "heuristic",
*, *,
heuristic: bool = True,
max_depth: Optional[int] = None, max_depth: Optional[int] = None,
) -> HeaderList: ) -> HeaderList:
"""Returns an iterable object containing a list of full paths to """Returns an iterable object containing a list of full paths to
@ -2130,10 +2129,8 @@ def find_headers(
Parameters: Parameters:
headers: Header name(s) to search for headers: Header name(s) to search for
root: The root directory to start searching from root: The root directory to start searching from
recursive: if False (default) search only root folder, if True recurse from the root. Note recursive: if False search only root folder, if True recurse from the root. Defaults to
that recursive search does not imply exhaustive search if heuristic is True. "heuristic", which uses a non-exhaustive, faster search.
heuristic: if True (default), use a non-exhaustive, faster search. Has no effect
if recursive is False.
max_depth: if set, don't search below this depth. Cannot be set if recursive is False. max_depth: if set, don't search below this depth. Cannot be set if recursive is False.
Returns: Returns:
@ -2147,6 +2144,9 @@ def find_headers(
f"first argument [got {type(headers)} instead]" f"first argument [got {type(headers)} instead]"
) )
if recursive is False and max_depth is not None:
raise ValueError(f"max_depth ({max_depth}) cannot be set if recursive is False")
# Construct the right suffix for the headers # Construct the right suffix for the headers
suffixes = [ suffixes = [
# C # C
@ -2167,8 +2167,8 @@ def find_headers(
# List of headers we are searching with suffixes # List of headers we are searching with suffixes
headers = [f"{header}.{suffix}" for header in headers for suffix in suffixes] headers = [f"{header}.{suffix}" for header in headers for suffix in suffixes]
if not recursive or not heuristic: if isinstance(recursive, bool):
return HeaderList(find(root, headers, recursive=recursive)) return HeaderList(find(root, headers, recursive=recursive, max_depth=max_depth))
# The heuristic here is simpler than the one for libraries: restrict search to <root>/include # The heuristic here is simpler than the one for libraries: restrict search to <root>/include
# (if root isn't an include directory itself) and limit search depth so that headers are found # (if root isn't an include directory itself) and limit search depth so that headers are found
@ -2351,9 +2351,9 @@ def find_libraries(
libraries: Union[List[str], str], libraries: Union[List[str], str],
root: str, root: str,
shared: bool = True, shared: bool = True,
recursive: bool = False, recursive: Union[bool, str] = "heuristic",
runtime: bool = True, runtime: bool = True,
heuristic: bool = True, *,
max_depth: Optional[int] = None, max_depth: Optional[int] = None,
) -> LibraryList: ) -> LibraryList:
"""Find libraries in the specified root directory. """Find libraries in the specified root directory.
@ -2373,12 +2373,10 @@ def find_libraries(
libraries: library name(s) to search for libraries: library name(s) to search for
root: the root directory to start searching from root: the root directory to start searching from
shared: if True searches for shared libraries, otherwise for static. Defaults to True. shared: if True searches for shared libraries, otherwise for static. Defaults to True.
recursive: if False (default) search only root folder, if True recurse from the root. Note recursive: if False search only root folder, if True recurse from the root. Defaults to
that recursive search does not imply exhaustive search if heuristic is True. "heuristic", which uses a non-exhaustive, faster search.
runtime: Windows only option, no-op elsewhere. If True (default), search for runtime shared runtime: Windows only option, no-op elsewhere. If True (default), search for runtime shared
libs (.DLL), otherwise, search for .Lib files. If shared is False, this has no meaning. libs (.DLL), otherwise, search for .Lib files. If shared is False, this has no meaning.
heuristic: if True (default), use a non-exhaustive, faster search. Has no effect if
recursive is False.
max_depth: if set, don't search below this depth. Cannot be set if recursive is False. max_depth: if set, don't search below this depth. Cannot be set if recursive is False.
Returns: Returns:
@ -2393,7 +2391,7 @@ def find_libraries(
f"first argument [got {type(libraries)} instead]" f"first argument [got {type(libraries)} instead]"
) )
if not recursive and max_depth is not None: if recursive is False and max_depth is not None:
raise ValueError(f"max_depth ({max_depth}) cannot be set if recursive is False") raise ValueError(f"max_depth ({max_depth}) cannot be set if recursive is False")
if sys.platform == "win32": if sys.platform == "win32":
@ -2419,10 +2417,8 @@ def find_libraries(
# List of libraries we are searching with suffixes # List of libraries we are searching with suffixes
libraries = [f"{lib}.{suffix}" for lib in libraries for suffix in suffixes] libraries = [f"{lib}.{suffix}" for lib in libraries for suffix in suffixes]
if not recursive: if isinstance(recursive, bool):
return LibraryList(find(root, libraries, recursive=False)) return LibraryList(find(root, libraries, recursive=recursive, max_depth=max_depth))
elif not heuristic:
return LibraryList(find(root, libraries, recursive=True, max_depth=max_depth))
# Heuristic search: a form of non-exhaustive iterative deepening, in order to return early if # Heuristic search: a form of non-exhaustive iterative deepening, in order to return early if
# libraries are found in their usual locations. This is the default behavior for recursive # libraries are found in their usual locations. This is the default behavior for recursive

View File

@ -1109,7 +1109,7 @@ def _headers_default_handler(spec: "Spec"):
NoHeadersError: If no headers are found NoHeadersError: If no headers are found
""" """
home = getattr(spec.package, "home") home = getattr(spec.package, "home")
headers = fs.find_headers("*", root=home, recursive=True, heuristic=True) headers = fs.find_headers("*", root=home)
if headers: if headers:
return headers return headers
@ -1159,9 +1159,7 @@ def _libs_default_handler(spec: "Spec"):
for shared in search_shared: for shared in search_shared:
# Since we are searching for link libraries, on Windows search only for # Since we are searching for link libraries, on Windows search only for
# ".Lib" extensions by default as those represent import libraries for implicit links. # ".Lib" extensions by default as those represent import libraries for implicit links.
libs = fs.find_libraries( libs = fs.find_libraries(name, home, shared=shared, runtime=False)
name, home, shared=shared, recursive=True, runtime=False, heuristic=True
)
if libs: if libs:
return libs return libs