Last active
September 4, 2023 02:27
-
-
Save pplmx/11f6515fa12d7b96d856a18f6373d3f6 to your computer and use it in GitHub Desktop.
Makefile help generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .PHONY: help | |
| .DEFAULT_GOAL := help | |
| # Show help | |
| help: | |
| @echo "" | |
| @echo "Usage:" | |
| @echo " make [target]" | |
| @echo "" | |
| @echo "Targets:" | |
| @awk '/^[a-zA-Z\-_0-9]+:/ \ | |
| { \ | |
| helpMessage = match(lastLine, /^# (.*)/); \ | |
| if (helpMessage) { \ | |
| helpCommand = substr($$1, 0, index($$1, ":")-1); \ | |
| helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \ | |
| printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \ | |
| } \ | |
| } { lastLine = $$0 }' $(MAKEFILE_LIST) |
When you run make help from the command line, the target performs the following actions:
- Prints basic usage information, such as
make [target]. - Uses the
awkcommand to read theMakefileand find all lines that start with a letter, number, hyphen, or underscore and end with a colon. These lines represent targets defined in theMakefile. - For each target found, the
awkcommand checks if the line above it starts with a#. If it does, it assumes that line contains help information for that target. - If help information is found, it uses the
printfcommand to print the target name and help information.
This way, you can add help information for each target in your Makefile, and then use the make help command to automatically generate a help document.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
当您在命令行中运行
make help时,该目标会执行以下操作:make [target]。awk命令读取Makefile文件,并查找所有以字母、数字、短划线或下划线开头且以冒号结尾的行。这些行表示Makefile中定义的目标。awk命令会检查该目标上方的行是否以#开头。如果是,则假定该行包含有关该目标的帮助信息。printf命令打印出目标名称和帮助信息。这样,您可以在
Makefile中为每个目标添加帮助信息,然后使用make help命令自动生成帮助文档。