我使用过一些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:
我个人为我构建的每个Makefile复制粘贴相同的帮助目标。
.SILENT:
.PHONY: help
## This help screen
help:
printf "Available targets\n\n"
awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf "%-30s %s\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
我也在这个Github要点中保留了它的副本:
https://gist.github.com/Olshansk/689fc2dee28a44397c6e31a0776ede30
我最喜欢的答案是Chris Down在Unix & Linux Stack Exchange上发布的。我将引用。
这是make的bash完成模块获取列表的方式:
使得qp | awk - f ': ' ' / ^ [a-zA-Z0-9] [^ $ # \ / \ t =]*:([^=]|$)/ { 分(1美元,/ /);(我的)打印一个[我]}’
它输出以换行符分隔的目标列表,不进行分页。
用户Brainstone建议使用sort -u来删除重复的条目:
make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}' | sort -u
来源:如何列出所有的目标在使?(unix和linux SE)
我把这两个答案结合起来:https://stackoverflow.com/a/9524878/86967和https://stackoverflow.com/a/7390874/86967
并做了一些转义,以便可以从makefile中使用。
.PHONY: no_targets__ list
no_targets__:
list:
sh -c "$(MAKE) -p no_targets__ | awk -F':' '/^[a-zA-Z0-9][^\$$#\/\\t=]*:([^=]|$$)/ {split(\$$1,A,/ /);for(i in A)print A[i]}' | grep -v '__\$$' | sort"
.
$ make -s list
build
clean
default
distclean
doc
fresh
install
list
makefile ## this is kind of extraneous, but whatever...
run
这个帮助目标只打印带有##后跟描述的目标。这允许同时记录公共目标和私有目标。使用. default_goal使帮助更容易被发现。
只使用sed, xargs和printf,这是非常常见的。
使用< $(MAKEFILE_LIST)允许将makefile命名为makefile以外的名称,例如makefile .github
您可以在printf中定制输出以满足您的偏好。这个示例的设置是为了匹配OP对rake样式输出的请求
在剪切和粘贴下面的make文件时,不要忘记将4个空格缩进改为制表符。
# vim:ft=make
# Makefile
.DEFAULT_GOAL := help
.PHONY: test help
help: ## these help instructions
@sed -rn 's/^([a-zA-Z_-]+):.*?## (.*)$$/"\1" "\2"/p' < $(MAKEFILE_LIST) | xargs printf "make %-20s# %s\n"
lint: ## style, bug and quality checker
pylint src test
private: # for internal usage only
@true
test: private ## run pytest with coverage
pytest --cov test
下面是上面Makefile的输出。注意,私有目标没有得到输出,因为它的注释只有一个#。
$ make
make help # these help instructions
make lint # style, bug and quality checker
make test # run pytest with coverage
@nobar的回答很有帮助地展示了如何使用TAB补全来列出makefile的目标。
这对于默认提供此功能的平台(例如Debian、Fedora)非常有效。
在其他平台上(例如Ubuntu),你必须显式加载这个功能,正如@hek2mgl的回答所暗示的那样:
. /etc/bash_completion安装几个制表符补全函数,包括make函数
或者,为make只安装制表符补全:
. /usr/share/bash-completion /完成/
对于根本不提供这个功能的平台,比如OSX,你可以使用下面的命令来实现它:
_complete_make() { COMPREPLY=($(compgen -W "$(make -pRrq : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($1 !~ "^[#.]") {print $1}}' | egrep -v '^[^[:alnum:]]' | sort | xargs)" -- "${COMP_WORDS[$COMP_CWORD]}")); }
complete -F _complete_make make
注意:这并不像Linux发行版附带的制表符补全功能那么复杂:最值得注意的是,它总是以当前目录中的makefile为目标,即使命令行以-f <file>为目标的另一个makefile也是如此。