我使用过一些rake(一个Ruby make程序),它有一个选项,可以获得所有可用目标的列表,例如
> rake --tasks
rake db:charset # retrieve the charset for your data...
rake db:collation # retrieve the collation for your da...
rake db:create # Creates the databases defined in y...
rake db:drop # Drops the database for your curren...
...
但是在GNU make中似乎没有这样做的选项。
显然,代码几乎已经有了,截至2007年- http://www.mail-archive.com/help-make@gnu.org/msg06434.html。
不管怎样,我做了一个小hack来从makefile中提取目标,你可以将它包含在makefile中。
list:
@grep '^[^#[:space:]].*:' Makefile
它会给你一个已定义目标的列表。这只是一个开始——例如,它并没有过滤掉依赖关系。
> make list
list:
copy:
run:
plot:
turnin:
专注于描述make目标的简单语法,并有一个干净的输出,我选择了以下方法:
help:
@grep -B1 -E "^[a-zA-Z0-9_-]+\:([^\=]|$$)" Makefile \
| grep -v -- -- \
| sed 'N;s/\n/###/' \
| sed -n 's/^#: \(.*\)###\(.*\):.*/\2###\1/p' \
| column -t -s '###'
#: Starts the container stack
up: a b
command
#: Pulls in new container images
pull: c d
another command
make-target-not-shown:
# this does not count as a description, so leaving
# your implementation comments alone, e.g TODOs
also-not-shown:
因此,将上面的文件作为Makefile来处理并运行它会给您带来类似于
> make help
up Starts the container stack
pull Pulls in new container images
命令链的解释:
First, grep all targets and their preceeding line, see https://unix.stackexchange.com/a/320709/223029.
Then, get rid of the group separator, see https://stackoverflow.com/a/2168139/1242922.
Then, we collapse each pair of lines to parse it later, see https://stackoverflow.com/a/9605559/1242922.
Then, we parse for valid lines and remove those which do not match, see https://stackoverflow.com/a/8255627/1242922, and also give the output our desired order: command, then description.
Lastly, we arrange the output like a table.
Make默认情况下不支持此功能,其他回答已经展示了如何自动提取可能目标的列表。
然而,如果您想对清单有更多的控制,而不产生任何副作用(例如使用. phony目标标记文档,这阻止了使用目标名称作为Make用来决定需要重建哪些目标的实际文件的逻辑),您可以为文档发明自己的语法。我更喜欢这样使用###:
CPUS ?= $(shell nproc)
MAKEFLAGS += -j $(CPUS) -l $(CPUS) -s
# Basic paths
PREFIX ?= usr
BINDIR ?= $(PREFIX)/bin
ETCDIR ?= etc
MANDIR ?= $(PREFIX)/share/man
# ...
### help: Show help message (default target)
# use "help" as the default target (first target in the Makefile)
.PHONY: help
help:
@printf "%s\n\n" "make: List of possible targets:"
@grep '^### .*:' $(lastword $(MAKEFILE_LIST)) | sed 's/^### \([^:]*\): \(.*\)/\1:\t\2/' | column -ts "$$(printf '\t')"
### install: Install all files in $PREFIX (used by debian binary package build scripts)
install:
install -D -o root -g root -m 755 ...
...
### release: Increase package version number
release:
debchange --release
(像往常一样,缩进文件必须精确地从一个制表器开始,但stackoverflow不能正确地再现该细节。)
输出如下所示:
$ make
make: List of possible targets:
help: Show help message (default target)
install: Install all files in $PREFIX (used by debian binary package build scripts)
release: Increase package version number
This works because only lines starting with ### and having a : character are considered as the documentation to output. Note that this intentionally does not extract the actual target name but fully trusts the documentation lines only. This allows always emitting correct output for very complex Makefile tricks, too. Also note that this avoids needing to put the documentation line on any specific position relative to actual rule. I also intentionally avoid sorting the output because the order of output can be fully controlled from the Makefile itself simply by listing the documentation lines in preferred order.
显然,您可以发明任何其他您喜欢的语法,甚至可以做一些
### en: install: Install all files in $PREFIX
### fi: asennus: asenna kaikki tiedostot hakemistoon $PREFIX
并且只打印与当前语言环境匹配的行,以支持多种语言,并具有别名来本地化目标名称:
.PHONY: asennus
asennus: install
最重要的问题是为什么要列出目标?您想要实际的文档还是某种调试信息?
这里有很多可行的解决方案,但正如我喜欢说的,“如果值得做一次,就值得再做一次。”
我确实赞成使用(tab)(tab)的建议,但正如一些人指出的那样,您可能没有补全支持,或者,如果您有许多包含文件,您可能想要一种更简单的方法来知道目标定义在哪里。
我还没有测试下面的子制作…我认为这行不通。我们知道,递归是有害的。
.PHONY: list ls
ls list :
@# search all include files for targets.
@# ... excluding special targets, and output dynamic rule definitions unresolved.
@for inc in $(MAKEFILE_LIST); do \
echo ' =' $$inc '= '; \
grep -Eo '^[^\.#[:blank:]]+.*:.*' $$inc | grep -v ':=' | \
cut -f 1 | sort | sed 's/.*/ &/' | sed -n 's/:.*$$//p' | \
tr $$ \\\ | tr $(open_paren) % | tr $(close_paren) % \
; done
# to get around escaping limitations:
open_paren := \(
close_paren := \)
我喜欢它是因为:
通过包含文件列出目标。
输出原始动态目标定义(用模替换变量分隔符)
在新行上输出每个目标
似乎更清楚了(主观意见)
解释:
MAKEFILE_LIST中的foreach文件
输出文件的名称
包含冒号的Grep行,不缩进,没有注释,也不以句号开头
排除立即赋值表达式(:=)
切、排序、缩进和切规则依赖项(冒号后)
蒙格变量分隔符以防止扩展
样例输出:
= Makefile =
includes
ls list
= util/kiss/snapshots.mk =
rotate-db-snapshots
rotate-file-snapshots
snap-db
snap-files
snapshot
= util/kiss/main.mk =
dirs
install
%MK_DIR_PREFIX%env-config.php
%MK_DIR_PREFIX%../srdb