From 7bc84ab8a3e47fe2109e82e131b9419eea08ca69 Mon Sep 17 00:00:00 2001 From: Malin Freeborn Date: Wed, 13 Nov 2024 16:06:46 +0100 Subject: [PATCH] finish examples --- slides/makefiles/examples/.gitignore | 1 + slides/makefiles/examples/check | 8 + slides/makefiles/examples/pats | 18 ++ slides/makefiles/examples/rules | 15 ++ slides/makefiles/examples/vars | 10 ++ slides/makefiles/makefiles.md | 245 ++++++++++++++++++++++----- 6 files changed, 257 insertions(+), 40 deletions(-) create mode 100644 slides/makefiles/examples/.gitignore create mode 100644 slides/makefiles/examples/check create mode 100644 slides/makefiles/examples/pats create mode 100644 slides/makefiles/examples/rules create mode 100644 slides/makefiles/examples/vars diff --git a/slides/makefiles/examples/.gitignore b/slides/makefiles/examples/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/slides/makefiles/examples/.gitignore @@ -0,0 +1 @@ +* diff --git a/slides/makefiles/examples/check b/slides/makefiles/examples/check new file mode 100644 index 0000000..57c1f4f --- /dev/null +++ b/slides/makefiles/examples/check @@ -0,0 +1,8 @@ +CHECKER = command -v +.PHONY: check +check: + $(CHECKER) fortune >/dev/null + $(CHECKER) cowsay >/dev/null + +file: | check + fortune | cowsay > $@ diff --git a/slides/makefiles/examples/pats b/slides/makefiles/examples/pats new file mode 100644 index 0000000..a3e489c --- /dev/null +++ b/slides/makefiles/examples/pats @@ -0,0 +1,18 @@ +IMAGES = $(wildcard jpgs/*) +COL = $(patsubst jpgs/%,collection/%,$(IMAGES)) + +.PHONY: help +help: + $(info try 'show' or 'output') +.PHONY: show +show: + $(info IMAGES is $(IMAGES)) + $(info Col is $(COL)) + +.PHONY: output +output: $(COL) + +collection/%.jpg: jpgs/%.jpg + mkdir -p $(@D) + cp $< $@ + diff --git a/slides/makefiles/examples/rules b/slides/makefiles/examples/rules new file mode 100644 index 0000000..0b93dfd --- /dev/null +++ b/slides/makefiles/examples/rules @@ -0,0 +1,15 @@ +CP = ln -f + +output: pngs/ldap.png + +jpgs/ldap.jpg: ../../ldap/ldap.jpg + mkdir -p $(dir $@) + $(CP) $< $@ + +pngs/%.png: jpgs/%.jpg + mkdir -p $(dir $@) + $(info making $(@F) in $(@D)) + magick $< $@ + +clean: + $(RM) -r pngs jpgs diff --git a/slides/makefiles/examples/vars b/slides/makefiles/examples/vars new file mode 100644 index 0000000..6dec96a --- /dev/null +++ b/slides/makefiles/examples/vars @@ -0,0 +1,10 @@ +include /etc/os-release +DAY != date +%d +MESSAGE != fortune -s + +motd_$(DAY): + $(info Hello $(USER)) + $(info Placing message:) + echo "Welcome to $(NAME)" > $@ + echo $(MESSAGE) >> $@ + diff --git a/slides/makefiles/makefiles.md b/slides/makefiles/makefiles.md index 7d760b2..e13da97 100644 --- a/slides/makefiles/makefiles.md +++ b/slides/makefiles/makefiles.md @@ -20,7 +20,7 @@ cargo build --release install -pm755 target/release/tap /usr/local/bin/ ``` -You can't type this after every test. +*But you can't type this for run.* *** @@ -67,52 +67,170 @@ target/release/tap: src soundscape/.git *** -# Basic +# The Three Sigils -- *Gotcha*: directories -- This, that, and these +Readable, but slow: + +```make fort_1 +forts/big_fort.txt: forts/short.txt forts/long.txt + cat forts/short.txt forts/long.txt > forts/big_fort.txt + +forts/: + mkdir forts + +forts/short.txt: forts/ + fortune -s > forts/short.txt + +forts/long.txt: forts/ + fortune -l > forts/long.txt +``` *** -## Repetition, Repetition, Repetition +| Make this | From That | From These | +|:---------:|:---------:|:----------:| +| `$@` | `$<` | `$^` | -```make -.PHONY: output -output: release/index.html -release/: backups.zip +```make fort_2 +forts/big_fort.txt: forts/short.txt forts/long.txt + cat $^ > $@ + +forts/: README.md mkdir $@ +forts/short.txt: forts/ + fortune -s > $@ +forts/long.txt: forts/ + fortune -l > $@ +``` -release/index.html: index.html release/ - cp $< $@ +*** -index.html: backups.zip - unzip $< +# Gotcha: Directories + + +```make fort_2 +forts/big_fort.txt: forts/short.txt forts/long.txt + cat $^ > $@ + +forts/: README.md + mkdir $@ +forts/short.txt: forts/ + fortune -s > $@ +forts/long.txt: forts/ + fortune -l > $@ + +README.md: + echo "Find the fortunes in the fort dir" > $@ ``` *** # Variables -- From your shell. -- New variables. -- Filenames as variables. -- *Gotcha*: no shell variables, only make! +```make vars +include /etc/os-release +DAY != date +%d +MESSAGE != fortune -s -## Equality +motd_$(DAY): + $(info Placing message:) + echo "Welcome to $(NAME)" > $@ + echo $(MESSAGE) >> $@ +``` -- Variables with '=', never ':='. -- *Gotcha*: one shell per line. -- Variables as shell output. +### Gotcha: Hanging Quotes +> echo "Welcome to "Arch Linux"" + +*** + +## Add New Variables + +```make +make -f vars -e MESSAGE="Red alert, all hands on deck!" +``` + +### Gotcha: Quote, or Risk Escape + +```make +make -f vars -e MESSAGE="

HTML Headers

" +``` + +*** + +# Gotcha: Variables from Shell + +This works: + +```make +file: + $(info Hello $(USER)) +``` + +...but not this: + +```make +file: + user=bob + echo $user +``` + +Nor this: + +```make +numbers: + for x in 1 2 3 4 5; do + echo $x + done > $@ +``` + +Nor this: + +```make +numbers: + for x in 1 2 3 4 5; do echo $x ; done > $@ +``` + +*** + +### Ugly Fixes are Ugly + +This works, but don't. + +```make +file: + for x in 1 2 ;\ + do echo $$x ;\ + done > $@ +``` *** # Phonies, and the Problems with Lies -- clean (common) -- check (excellent) +## Clean + + +```make +.PHONY: clean +clean: + git clean -fdX +``` + +## Gotcha: Recompiling without Changes + +```make +CHECKER = command -v +.PHONY: check +check: + $(CHECKER) fortune >/dev/null + $(CHECKER) cowsay >/dev/null + +file: check # Needs a '|' + fortune | cowsay > $@ +``` *** @@ -127,32 +245,79 @@ index.html: backups.zip # The Fourth Sigil: `%` -- Standard rules, e.g. ImageMagick, copying, et c. -- *Gotcha*: intermediaries are deleted. +Create standardized rules with `%`. + + +```make rules +CP = ln -f + +output: pngs/ldap.png + +jpgs/ldap.jpg: ../../ldap/ldap.jpg + mkdir -p $(dir $@) + $(CP) $< $@ + +pngs/%.png: jpgs/%.jpg + mkdir -p $(dir $@) + $(info making $(@F) in $(@D)) + magick $< $@ +``` *** -# Bling: make2graph +# Bling: makefile2graph -- *Gotcha*: completely outdated. +```graph + +┌────────────────┐ ┌────────────────────┐ +│ forts/long.txt │ ◀── │ forts/ │ +└────────────────┘ └────────────────────┘ + │ │ + │ │ + │ ▼ + │ ┌────────────────────┐ + │ │ forts/short.txt │ + │ └────────────────────┘ + │ │ + │ │ + │ ▼ + │ ┌────────────────────┐ + └──────────────────▶ │ forts/big_fort.txt │ + └────────────────────┘ +``` + +### Gotcha: The Binary is Called `make2graph` + +- Works with GUI tools maybe, IDK, I don't use Windows. +- Some systems can use `graph-easy`, but it's outdated. *** -# Inclusivity - -- `include dir/Makefile` -- *Gotcha*: directory has not changed -- `$(MAKEFILE_LIST)` +# Patsubst and Wildcards -# Special Commands +```make pats +IMAGES = $(wildcard jpgs/*) +COL = $(patsubst jpgs/%,collection/%,$(IMAGES)) -- dir -- `DIR = test -d $(dir $@) || mkdir $(dir $@)` -- patsubst -- Wildcards (or not?) +.PHONY: show +show: + $(info IMAGES is $(IMAGES)) + $(info Col is $(COL)) -# Maintanance +.PHONY: output +output: $(COL) + +collection/%.jpg: jpgs/%.jpg + mkdir -p $(@D) + cp $< $@ + +``` + +*** + +# Use Cases + +- Backups +- Making Websites -- backups -- saving files