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(
headers: Union[List[str], str],
root: str,
recursive: bool = False,
recursive: Union[bool, str] = "heuristic",
*,
heuristic: bool = True,
max_depth: Optional[int] = None,
) -> HeaderList:
"""Returns an iterable object containing a list of full paths to
@ -2130,10 +2129,8 @@ def find_headers(
Parameters:
headers: Header name(s) to search for
root: The root directory to start searching from
recursive: if False (default) search only root folder, if True recurse from the root. Note
that recursive search does not imply exhaustive search if heuristic is True.
heuristic: if True (default), use a non-exhaustive, faster search. Has no effect
if recursive is False.
recursive: if False search only root folder, if True recurse from the root. Defaults to
"heuristic", which uses a non-exhaustive, faster search.
max_depth: if set, don't search below this depth. Cannot be set if recursive is False.
Returns:
@ -2147,6 +2144,9 @@ def find_headers(
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
suffixes = [
# C
@ -2167,8 +2167,8 @@ def find_headers(
# List of headers we are searching with suffixes
headers = [f"{header}.{suffix}" for header in headers for suffix in suffixes]
if not recursive or not heuristic:
return HeaderList(find(root, headers, recursive=recursive))
if isinstance(recursive, bool):
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
# (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],
root: str,
shared: bool = True,
recursive: bool = False,
recursive: Union[bool, str] = "heuristic",
runtime: bool = True,
heuristic: bool = True,
*,
max_depth: Optional[int] = None,
) -> LibraryList:
"""Find libraries in the specified root directory.
@ -2373,12 +2373,10 @@ def find_libraries(
libraries: library name(s) to search for
root: the root directory to start searching from
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
that recursive search does not imply exhaustive search if heuristic is True.
recursive: if False search only root folder, if True recurse from the root. Defaults to
"heuristic", which uses a non-exhaustive, faster search.
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.
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.
Returns:
@ -2393,7 +2391,7 @@ def find_libraries(
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")
if sys.platform == "win32":
@ -2419,10 +2417,8 @@ def find_libraries(
# List of libraries we are searching with suffixes
libraries = [f"{lib}.{suffix}" for lib in libraries for suffix in suffixes]
if not recursive:
return LibraryList(find(root, libraries, recursive=False))
elif not heuristic:
return LibraryList(find(root, libraries, recursive=True, max_depth=max_depth))
if isinstance(recursive, bool):
return LibraryList(find(root, libraries, recursive=recursive, max_depth=max_depth))
# 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

View File

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