lk/system/Makefiles/patterns.md
2025-02-07 23:40:13 +01:00

1.3 KiB


title: "Makefile Patterns" tags: [ "system", "makefiles" ]

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

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

The make rules start by processing variables:

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

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

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

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

current_minute != date +%M

storage_directory = backups

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

README.md: Makefile
	echo "Basic makefile example." > $@
	echo "" >> $@
	echo '```' >> $@
	cat $< >> $@
	echo '```' >> $@

$(storage_directory)/backup_$(current_minute).md: README.md
	mkdir -p $(@D)
	cp $< $@