有没有一种方法可以在Makefile中编写“标准”注释,以便稍后将它们提供给类似Doxygen的程序,以输出漂亮的(例如HTML或man)文档?我想要在某个地方清晰地概述我的主要目标,但不需要太花哨。
有没有一种方法可以在Makefile中编写“标准”注释,以便稍后将它们提供给类似Doxygen的程序,以输出漂亮的(例如HTML或man)文档?我想要在某个地方清晰地概述我的主要目标,但不需要太花哨。
# This is a regular comment, that will not be displayed
## ----------------------------------------------------------------------
## This is a help comment. The purpose of this Makefile is to demonstrate
## a simple help mechanism that uses comments defined alongside the rules
## they describe without the need of additional help files or echoing of
## descriptions. Help comments are displayed in the order defined within
## the Makefile.
## ----------------------------------------------------------------------
help: ## Show this help.
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)
build: ## Build something.
install: ## Install something.
deploy: ## Deploy something.
format: ## Help comments are display with their leading whitespace. For
## example, all comments in this snippet are aligned with spaces.
运行 make
或 make help
会产生以下结果:
----------------------------------------------------------------------
This is a help comment. The purpose of this Makefile is to demonstrate
a simple help mechanism that uses comments defined alongside the rules
they describe without the need of additional help files or echoing of
descriptions. Help comments are displayed in the order defined within
the Makefile.
----------------------------------------------------------------------
help: Show this help.
build: Build something.
install: Install something.
deploy: Deploy something.
format: Help comments are display with their leading whitespace. For
example, all comments in this snippet are aligned with spaces.
Makefile:target:## docs
。可以通过更改awk分隔符为 FS =“:[^:]*?##”
来解决这个问题。 - Amnon一个不错的点子是提供一个假的help
目标,用于打印目标和选项的摘要。来自Linux内核的Makefile
:
help:
@echo 'Cleaning targets:'
@echo ' clean - Remove most generated files but keep the config and'
@echo ' enough build support to build external modules'
@echo ' mrproper - Remove all generated files + config + various backup files'
@echo ' distclean - mrproper + remove editor backup and patch files'
@echo ''
@echo 'Configuration targets:'
@$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help
@echo ''
这种方式可能需要一些工作来维护文档,但是我发现这种方式很好地区分了面向“用户”的内容和面向Makefile
本身的人员(行内注释)的内容。
SCRIPT_VERSION=v1.0
SCRIPT_AUTHOR=John Doe
all: ##@Build Build all the project
clean: ##@Cleaning Remove all intermediate objects
mrproper: clean ##@Cleaning Remove all output and interemediate objects
HELP_FUN = \
%help; while(<>){push@{$$help{$$2//'options'}},[$$1,$$3] \
if/^([\w-_]+)\s*:.*\#\#(?:@(\w+))?\s(.*)$$/}; \
print"$$_:\n", map" $$_->[0]".(" "x(20-length($$_->[0])))."$$_->[1]\n",\
@{$$help{$$_}},"\n" for keys %help; \
help: ##@Miscellaneous Show this help
@echo -e "Usage: make [target] ...\n"
@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST)
@echo -e "Written by $(SCRIPT_AUTHOR), version $(SCRIPT_VERSION)"
@echo -e "Please report any bug or error to the author."
这样就得到了这个结果:
$ make help
Usage: make [target] ...
Miscellaneous:
help Show this help
Build:
all Build all the project
Cleaning:
clean Remove all intermediate objects
mrproper Remove all output and interemediate objects
Written by John Doe, version v1.0
Please report any bug or error to the author.
if/^(\w+)\s*:
替换为 if/^([\w-_]+)\s*:
,以允许类型为 "this-target" 或 "that_target" 的目标。 - th3n3rd自文档的Makefile(约翰·格雷厄姆-卡明,2005年)允许您在每个规则旁边编写帮助文档。这是一种轻量级且非常巧妙的解决方案,至少适用于GNU Make。
这是一个略微修改过的版本(def-help-section有助于组织长列表的规则)。
这是我的版本,它还剥离了目标的依赖关系,并将帮助字符串对齐到一个均匀的列。
help: ## Show this help.
@sed -ne 's/^\([^[:space:]]*\):.*##/\1:\t/p' $(MAKEFILE_LIST) | column -t -s $$'\t'
image: src/*.s src/*.c src/**/*.c ## Build the image
riscv32-unknown-elf-gcc $^ -march=rv32i_zicsr -Wall -ggdb -O0 -o image -ffreestanding -nostdlib -lgcc
launch: ## Image Launch the image in renode
renode --net vexriscv.resc --console
输出
help: Show this help.
image Build the image
launch: Image Launch the image in renode
## Another help message with data: in a separate column
test::
echo ""
## Print this help
help::
@awk '/^## /{c=substr($$0,3);next}c&&/^[[:alpha:]][[:alnum:]_/-]+:/{print substr($$1,1,index($$1,":")),c}1{c=0}' $(MAKEFILE_LIST) | column -s: -t -W 2,3 -o " "
输出:
test Another help message with data in a separate column
help Print this help
@sed
命令中添加| column -tl 2
。 - balki| column -tl
的完整命令? - Demetry Pascal@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST) | column -tl 2
参见:https://gitea.balki.me/balki/makepac/src/branch/main/Makefile#L10 - balki