Skip to content

check_makefile_targets

rhiza_hooks.check_makefile_targets

Check that Makefile contains expected targets for rhiza projects.

check_makefile(filepath)

Check a Makefile for recommended targets.

Parameters:

Name Type Description Default
filepath Path

Path to the Makefile

required

Returns:

Type Description
list[str]

List of warning messages (empty if all recommended targets exist)

Source code in src/rhiza_hooks/check_makefile_targets.py
def check_makefile(filepath: Path) -> list[str]:
    """Check a Makefile for recommended targets.

    Args:
        filepath: Path to the Makefile

    Returns:
        List of warning messages (empty if all recommended targets exist)
    """
    warnings: list[str] = []

    try:
        content = filepath.read_text()
    except FileNotFoundError:
        return [f"File not found: {filepath}"]

    targets = extract_targets(content)

    # Only check the main Makefile for recommended targets
    if filepath.name == "Makefile":
        missing = RECOMMENDED_TARGETS - targets
        if missing:
            warnings.append(f"Missing recommended targets: {', '.join(sorted(missing))}")

    return warnings

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 src/rhiza_hooks/check_makefile_targets.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)

main(argv=None)

Main entry point for the hook.

Source code in src/rhiza_hooks/check_makefile_targets.py
def main(argv: list[str] | None = None) -> int:
    """Main entry point for the hook."""
    parser = argparse.ArgumentParser(description="Check Makefile for recommended targets")
    parser.add_argument(
        "filenames",
        nargs="*",
        help="Filenames to check",
    )
    parser.add_argument(
        "--strict",
        action="store_true",
        help="Exit with error if recommended targets are missing",
    )
    args = parser.parse_args(argv)

    retval = 0
    for filename in args.filenames:
        filepath = Path(filename)
        warnings = check_makefile(filepath)
        if warnings:
            print(f"{filename}:")
            for warning in warnings:
                print(f"  - {warning}")
            if args.strict:
                retval = 1

    return retval