--- title: - Making Makefiles author: - Malin theme: - Warsaw colortheme: - orchid --- # Intro The fundamental problem: ```bash git submodule update --init soundscape cargo build --release install -pm755 target/release/tap /usr/local/bin/ ``` *But you can't type this for run.* *** ## 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 - input, process, output. - automatic idempotence ```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 ``` *** # The Three Sigils 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 ``` *** | Make this | From That | From These | |:---------:|:---------:|:----------:| | `$@` | `$<` | `$^` | ```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 > $@ ``` *** # 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 ```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"" *** ## 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="