Skip to content

_makefile

rhiza_hooks._makefile

Read the targets a repository's makefiles define.

Two hooks need this: check_makefile_targets asks whether the recommended targets are present, and check_workflow_make_targets asks whether every target CI invokes exists. Both questions start from the same parse, so the parser lives here rather than in either hook.

The placement is deliberate. A console script's public surface is its main(); when one entrypoint imports a helper out of another, the dependency between the two CLIs is invisible from either one's interface, and a change made for the sake of one hook's command line silently reaches the other. A _-prefixed leaf module states the shared part explicitly and imports nothing from the package.

collect_targets(repo_root)

Return every target defined by the root Makefile and its includes, transitively.

Parameters:

Name Type Description Default
repo_root Path

Root directory of the repository.

required

Returns:

Type Description
set[str]

Set of target names. Empty when there is no Makefile at all.

Source code in rhiza_hooks/_makefile.py
def collect_targets(repo_root: Path) -> set[str]:
    """Return every target defined by the root Makefile and its includes, transitively.

    Args:
        repo_root: Root directory of the repository.

    Returns:
        Set of target names. Empty when there is no Makefile at all.
    """
    targets: set[str] = set()
    seen: set[Path] = set()
    queue = [repo_root / "Makefile"]

    while queue:
        path = queue.pop()
        resolved = path.resolve()
        if resolved in seen or not path.is_file():
            continue
        seen.add(resolved)
        try:
            content = path.read_text(encoding="utf-8")
        except (OSError, UnicodeDecodeError):
            continue
        targets |= extract_targets(content)
        queue.extend(_include_paths(content, repo_root))

    return targets

extract_targets(content)

Extract target names from Makefile content.

Parameters:

Name Type Description Default
content str

Contents of a Makefile

required

Returns:

Type Description
set[str]

Set of target names found

Source code in rhiza_hooks/_makefile.py
def extract_targets(content: str) -> set[str]:
    """Extract target names from Makefile content.

    Args:
        content: Contents of a Makefile

    Returns:
        Set of target names found
    """
    matches = TARGET_PATTERN.findall(content)
    return set(matches)