2024-11-12 02:58:47 +00:00
|
|
|
---
|
|
|
|
title:
|
|
|
|
- Making Makefiles
|
|
|
|
author:
|
|
|
|
- Malin
|
|
|
|
|
|
|
|
theme:
|
|
|
|
- Warsaw
|
|
|
|
colortheme:
|
|
|
|
- orchid
|
|
|
|
---
|
|
|
|
|
|
|
|
# Intro
|
|
|
|
|
2024-11-13 13:23:44 +00:00
|
|
|
The fundamental problem:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
git submodule update --init soundscape
|
|
|
|
cargo build --release
|
|
|
|
install -pm755 target/release/tap /usr/local/bin/
|
|
|
|
```
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
*But you can't type this for run.*
|
2024-11-13 13:23:44 +00:00
|
|
|
|
|
|
|
***
|
|
|
|
|
|
|
|
## First Solution
|
|
|
|
|
|
|
|
```bash
|
|
|
|
#!/bin/sh
|
|
|
|
git submodule update --init soundscape
|
|
|
|
cargo build --release
|
|
|
|
install -pm755 target/release/tap /usr/local/bin/
|
|
|
|
```
|
|
|
|
|
|
|
|
At this point, every test takes 10 minutes.
|
|
|
|
|
|
|
|
## Second Solution
|
|
|
|
|
|
|
|
```bash
|
|
|
|
#!/bin/sh
|
|
|
|
[ -f soundscape/.git ] || git submodule update --init soundscape
|
|
|
|
[ -f target/release/tap ] || cargo build --release
|
|
|
|
[ -f target/release/tap ] || \
|
|
|
|
install -pm755 target/release/tap /usr/local/bin/
|
|
|
|
```
|
|
|
|
|
|
|
|
***
|
|
|
|
|
|
|
|
# Makefile Solutions
|
|
|
|
|
2024-11-12 02:58:47 +00:00
|
|
|
- input, process, output.
|
|
|
|
- automatic idempotence
|
2024-11-13 13:23:44 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
INSTALL_DIR = /usr/local/bin
|
|
|
|
|
|
|
|
soundscape/.git:
|
|
|
|
git submodule update --init soundscape
|
|
|
|
|
|
|
|
/usr/local/bin/tap: target/release/tap
|
|
|
|
install -pm755 target/release/tap $(INSTALL_DIR)
|
|
|
|
|
|
|
|
target/release/tap: src soundscape/.git
|
|
|
|
cargo build --release
|
|
|
|
```
|
|
|
|
|
|
|
|
***
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
# The Three Sigils
|
2024-11-13 13:23:44 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
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
|
|
|
|
```
|
2024-11-12 02:58:47 +00:00
|
|
|
|
|
|
|
***
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
| Make this | From That | From These |
|
|
|
|
|:---------:|:---------:|:----------:|
|
|
|
|
| `$@` | `$<` | `$^` |
|
2024-11-13 13:23:44 +00:00
|
|
|
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
```make fort_2
|
|
|
|
forts/big_fort.txt: forts/short.txt forts/long.txt
|
|
|
|
cat $^ > $@
|
|
|
|
|
|
|
|
forts/: README.md
|
2024-11-13 13:23:44 +00:00
|
|
|
mkdir $@
|
2024-11-13 15:06:46 +00:00
|
|
|
forts/short.txt: forts/
|
|
|
|
fortune -s > $@
|
|
|
|
forts/long.txt: forts/
|
|
|
|
fortune -l > $@
|
|
|
|
```
|
2024-11-13 13:23:44 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
***
|
2024-11-13 13:23:44 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
# 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" > $@
|
2024-11-13 13:23:44 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
***
|
|
|
|
|
2024-11-12 02:58:47 +00:00
|
|
|
# Variables
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
```make vars
|
|
|
|
include /etc/os-release
|
|
|
|
DAY != date +%d
|
|
|
|
MESSAGE != fortune -s
|
|
|
|
|
|
|
|
motd_$(DAY):
|
|
|
|
$(info Placing message:)
|
|
|
|
echo "Welcome to $(NAME)" > $@
|
|
|
|
echo $(MESSAGE) >> $@
|
|
|
|
```
|
|
|
|
|
|
|
|
### Gotcha: Hanging Quotes
|
|
|
|
|
|
|
|
> echo "Welcome to "Arch Linux""
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
***
|
|
|
|
|
|
|
|
## Add New Variables
|
|
|
|
|
|
|
|
```make
|
|
|
|
make -f vars -e MESSAGE="Red alert, all hands on deck!"
|
|
|
|
```
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
### Gotcha: Quote, or Risk Escape
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
```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 > $@
|
|
|
|
```
|
2024-11-12 02:58:47 +00:00
|
|
|
|
|
|
|
***
|
|
|
|
|
|
|
|
|
|
|
|
# Phonies, and the Problems with Lies
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
## 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 > $@
|
|
|
|
```
|
2024-11-12 02:58:47 +00:00
|
|
|
|
|
|
|
***
|
|
|
|
|
|
|
|
|
|
|
|
## Non-Compiling Checks
|
|
|
|
|
|
|
|
- `make` -n
|
|
|
|
- *Gotcha*: shell output variables.
|
|
|
|
|
|
|
|
***
|
|
|
|
|
|
|
|
|
|
|
|
# The Fourth Sigil: `%`
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
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 $< $@
|
|
|
|
```
|
2024-11-12 02:58:47 +00:00
|
|
|
|
|
|
|
***
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
# Bling: makefile2graph
|
|
|
|
|
|
|
|
```graph
|
|
|
|
|
|
|
|
┌────────────────┐ ┌────────────────────┐
|
|
|
|
│ forts/long.txt │ ◀── │ forts/ │
|
|
|
|
└────────────────┘ └────────────────────┘
|
|
|
|
│ │
|
|
|
|
│ │
|
|
|
|
│ ▼
|
|
|
|
│ ┌────────────────────┐
|
|
|
|
│ │ forts/short.txt │
|
|
|
|
│ └────────────────────┘
|
|
|
|
│ │
|
|
|
|
│ │
|
|
|
|
│ ▼
|
|
|
|
│ ┌────────────────────┐
|
|
|
|
└──────────────────▶ │ forts/big_fort.txt │
|
|
|
|
└────────────────────┘
|
|
|
|
```
|
|
|
|
|
|
|
|
### Gotcha: The Binary is Called `make2graph`
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
- Works with GUI tools maybe, IDK, I don't use Windows.
|
|
|
|
- Some systems can use `graph-easy`, but it's outdated.
|
2024-11-12 02:58:47 +00:00
|
|
|
|
|
|
|
***
|
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
# Patsubst and Wildcards
|
|
|
|
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
```make pats
|
|
|
|
IMAGES = $(wildcard jpgs/*)
|
|
|
|
COL = $(patsubst jpgs/%,collection/%,$(IMAGES))
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
.PHONY: show
|
|
|
|
show:
|
|
|
|
$(info IMAGES is $(IMAGES))
|
|
|
|
$(info Col is $(COL))
|
|
|
|
|
|
|
|
.PHONY: output
|
|
|
|
output: $(COL)
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
collection/%.jpg: jpgs/%.jpg
|
|
|
|
mkdir -p $(@D)
|
|
|
|
cp $< $@
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
***
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
# Use Cases
|
2024-11-12 02:58:47 +00:00
|
|
|
|
2024-11-13 15:06:46 +00:00
|
|
|
- Backups
|
|
|
|
- Making Websites
|
2024-11-12 02:58:47 +00:00
|
|
|
|