0

My Makefile looks like this:

.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) .

My question is, without adding all of my targets manually, is there a way for me to generate a help section that lists down all the targets in my file ?

Muhammad Lukman Low
  • 8,177
  • 11
  • 44
  • 54
  • 1
    How can Make possibly know what "getip" means? Or do you just want a list of targets *without* descriptions? – Beta Oct 24 '15 at 04:32
  • See related questions: [How do you get the list of targets in a makefile?](https://stackoverflow.com/questions/4219255/how-do-you-get-the-list-of-targets-in-a-makefile) - [How to automatically generate a Makefile help command](https://stackoverflow.com/questions/35730218/how-to-automatically-generate-a-makefile-help-command) [How to document a makefile?](https://stackoverflow.com/questions/8889035/how-to-document-a-makefile) – Iwan Aucamp Nov 29 '21 at 14:49

2 Answers2

1

I have always used this. It is not intuitive, but it does output a list of available targets. Granted this does not provide an explanation of what each target does, but I've found that this is so much better than nothing.

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

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

No.

As others have mentioned, make has no default way of printing out what your targets do.

As for listing all targets from the command line, in bash you can issue the following command to get a list of all possible targets:

make  <TAB> <TAB>
wizurd
  • 3,541
  • 3
  • 33
  • 50
  • That's not Make listing possible targets, that's the shell listing local files. – Beta Oct 24 '15 at 05:17
  • @Beta. No, that command actually does list targets for a makefile. Try it out for yourself... – wizurd Oct 24 '15 at 05:29
  • I did. What version of Make are you using, and in what shell? – Beta Oct 24 '15 at 05:58
  • GNU make: 3.82 / bash 4.2.45 – wizurd Oct 24 '15 at 06:04
  • 1
    In order for the tab to work you have to have the right shell completion customization packages installed. Only filename completion is built into the shell directly, but most GNU/Linux distributions these days do provide an extensive set of additional completion customizations, which recognize commands (like `make`) and customize the shell's TAB completion to do special things for that command. – MadScientist Oct 24 '15 at 13:28