Skip to content

Makefile

makefile

Makefile adapter — detect and use Makefile targets.

detect_makefile_targets(project_path)

Detect available targets in a project's Makefile.

Parameters:

Name Type Description Default
project_path Path

Root directory of the project.

required

Returns:

Type Description
set[str]

Set of target names found in the Makefile.

Source code in packages/axm-init/src/axm_init/adapters/makefile.py
def detect_makefile_targets(project_path: Path) -> set[str]:
    """Detect available targets in a project's Makefile.

    Args:
        project_path: Root directory of the project.

    Returns:
        Set of target names found in the Makefile.
    """
    makefile = project_path / "Makefile"
    if not makefile.exists():
        return set()

    try:
        content = makefile.read_text()
    except (OSError, UnicodeDecodeError):
        return set()

    # Match target definitions: "target_name:" at start of line
    target_pattern = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_-]*):", re.MULTILINE)
    targets = set(target_pattern.findall(content))

    return targets

get_tool_command(project_path, makefile_target, fallback_cmd)

Get the command to run a tool, preferring Makefile targets.

If the project has a Makefile with the specified target, uses make <target>. Otherwise, returns the fallback command.

Parameters:

Name Type Description Default
project_path Path

Root directory of the project.

required
makefile_target str

Target name to look for in Makefile.

required
fallback_cmd list[str]

Command to use if target not found.

required

Returns:

Type Description
list[str]

Command list to execute.

Source code in packages/axm-init/src/axm_init/adapters/makefile.py
def get_tool_command(
    project_path: Path,
    makefile_target: str,
    fallback_cmd: list[str],
) -> list[str]:
    """Get the command to run a tool, preferring Makefile targets.

    If the project has a Makefile with the specified target, uses `make <target>`.
    Otherwise, returns the fallback command.

    Args:
        project_path: Root directory of the project.
        makefile_target: Target name to look for in Makefile.
        fallback_cmd: Command to use if target not found.

    Returns:
        Command list to execute.
    """
    targets = detect_makefile_targets(project_path)

    if makefile_target in targets:
        return ["make", makefile_target]

    return fallback_cmd