#!/usr/bin/env awk -f
#
# mftl - find and list usable‐looking targets in Makefiles
#
# try to find the targets in a Makefile that look like they're targets the user
# could be reasonably expected to call directly
#
# Usage:
#    mftl Makefile
#    mftl Makefile1 Makefile2
#    grep ‐v foo Makefile | mftl

# Separators are space, tab, or colon
BEGIN { FS = "[ \t:]" }

# Skip comments
/^#/ { next }

# Join backslash-broken lines
/\\$/ {
    sub(/\\$/, "")
    line = line $0
    next
}
{
    $0 = line $0
    line = ""
}

# Check lines matching expected "targets:dependencies" format
/^[a-zA-Z0-9][a-zA-Z0-9./ \t_-]+:([^=]|$)/ {

    # Iterate through the targets that don't look like substitutions or
    # inference rules and stack them up into an array's keys to keep them
    # unique; this probably needs refinement
    for (i = 1; i < NF; i++)
        if ($i ~ /^[a-zA-Z0-9][a-zA-Z0-9.\/_-]*$/)
            ats[$i]++
}

# Print unique determined targets, sorted
END {
    sort = ""
    for (t in ats) {
        if (!sort)
            sort = "sort"
        print t | sort
    }
    if (sort)
        close(sort)
}
