finish examples

This commit is contained in:
Malin Freeborn 2024-11-13 16:06:46 +01:00
parent e59538a3bd
commit 7bc84ab8a3
Signed by: andonome
GPG Key ID: 52295D2377F4D70F
6 changed files with 257 additions and 40 deletions

1
slides/makefiles/examples/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*

View File

@ -0,0 +1,8 @@
CHECKER = command -v
.PHONY: check
check:
$(CHECKER) fortune >/dev/null
$(CHECKER) cowsay >/dev/null
file: | check
fortune | cowsay > $@

View File

@ -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 $< $@

View File

@ -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

View File

@ -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) >> $@

View File

@ -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="<h1> HTML Headers </h1>"
```
***
# 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