Makefile Patterns

Using the basic example, you can make a complete backup of all backup files. This file will depend upon everything inside the $(storage_directory). Unlike bash, you can't just say storage_directory/*: the pattern must be stated as a 'wildcard'.

1$(storage_directory)/backup.tgz: $(wildcard $(storage_directory)/*.md)
2	tar czf $@ $^

The make rules start by processing variables:

1backups/backup.tgz: $(wildcard backups/*.md)
2	tar czf backups/backup.tgz $^

Then the wildcard variable equals whichever backup files are in the backups/ directory:

1backups/backup.tgz: backups/backup_29.md backups/backup_30.md
2    tar czf backups/backup.tgz backups/backup_29.md backups/backup_30.md

The phony backup target should now point to this tar backup.

 1current_minute != date +%M
 2
 3storage_directory = backups
 4
 5.PHONY: backup
 6backup: $(storage_directory)/backup.tgz
 7$(storage_directory)/backup.tgz: $(wildcard $(storage_directory)/*.md)
 8	tar czf $@ $^
 9
10README.md: Makefile
11	echo "Basic makefile example." > $@
12	echo "" >> $@
13	echo '```' >> $@
14	cat $< >> $@
15	echo '```' >> $@
16
17$(storage_directory)/backup_$(current_minute).md: README.md
18	mkdir -p $(@D)
19	cp $< $@