我可以在不列出所有目标的情况下生成 Makefile 帮助吗?

Can I generate a Makefile help without listing down all targets?

我的 Makefile 如下所示:

.DEFAULT_GOAL := help

.PHONY: build clean-build start stop ssh getip

app = cp

help:
        @echo "Please use \`make <target>' where <target> is one of"
        @echo "build  To start building docker container"
        @echo "clean-build   Build from scratch without cache"
        @echo "start  Start up container build from build or clean-build"
        @echo "stop  Stop and delete container"
        @echo "ssh  Access container via ssh"
        @echo "getip   Get ip of container via docker inspect"

build:
        docker build -t cdn/$(app) .

clean-build:

        docker build --no-cache -t $(app) .

我的问题是,在不手动添加所有目标的情况下,有没有办法生成一个帮助部分,列出我文件中的所有目标?

is there a way for me to generate a help section that lists down all the targets in my file ?

没有

正如其他人所提到的,make 没有默认方式打印出 您的目标所做的

至于从命令行列出所有目标,在 bash 中,您可以发出以下命令以获取所有可能目标的列表:

make  <TAB> <TAB>

我一直用这个。它不直观,但它确实输出可用目标列表。当然,这并没有解释每个目标的作用,但我发现这总比没有好得多。

help:
    @echo "Available targets:"
    @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($ !~ "^[#.]") {print $}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs