Makefile Help System

Read time: 4 minutes (1065 words)

In searching GitHub for ideas on how to structure this system, I ran across several interesting approaches to generating a help system. However, most of those I found relied on tools only available on Linux/Mac systems, leaving out those who use Windows. The solution is simple, but it requires users on those Windows systems to have one simple tool installed that can do the work needed in several places in this system: Python.

Python is usually available in Linux/Mac systems, and is an easy tool to install on Windows. However, I am using Python3, which means users might need to install the latest Python on their system.

Adding Help Messages

The help system used in modular-make placed a short tex message on any target lines the user might like to run, The message will start after a marker, consisting of two successive sharp characters (##), and continuing to the end of the target line.

A simple Python script will scan all loaded Makefile component files, and produce a display of the target names, and the help message.

Here is the Python script:

mk/pyhelp.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import sys
import re

def main():
    help_re = re.compile(r"^([a-zA-Zi_-]*:).*?##(.*)$")

    modules = sys.argv
    del modules[0]
    for m in modules:
        fin = open(m,'r')
        lines = fin.readlines()
        for l in lines:
            m = help_re.match(l)
            if m:
                item = m.group(1).strip()
                defn = m.group(2).strip()
                print("%-20s %s" %(item,defn))

main()

The help target looks like this:

mk/help.mk
1
2
3
4
5
# Makefile help system

.PHONY: help
help:	## display help messages
	@python mk/pyhelp.py$(MAKEFILE_LIST)