razzi.abuissa.net

Razzi's guides to make

2023-09-18

make has got a lot of quirks, but it’s installed everywhere and gives more structure than a shell script. So let’s lay out a usage that works for 90% of cases. The remaining 10%, use a dedicated build tool, and if you’d like, add a Makefile to invoke it.

make shell script example

You don’t need to compile anything with make.

$ cat > example.sh
echo it works
$ chmod +x example.sh

Makefile:

all: example.sh

example: example.sh
	./example.sh

Note that you gotta use tabs for indentation.

And the filename is Makefile with a capital M.

Then use it as follows:

$ make
./example.sh
it works

For a build step that involves compilation, the kilo makefile is just about as basic as they come:

https://github.com/antirez/kilo/blob/master/Makefile

If you want to have a target that doesn’t depend on files at all, add it to .PHONY:

.PHONY: lint

# (other targets)

lint:
	shellcheck example.sh

Then invoke like so (try tab completing targets :)

$ make lint
shellcheck example.sh

In example.sh line 1:
echo it works
^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

Full example as git repository: https://git.sr.ht/~razzi/make-example/tree

project page

https://www.gnu.org/software/make/

source code

https://git.savannah.gnu.org/cgit/make.git

see also

https://makefiletutorial.com/

Depends on