cheatsheets/makefile.md

129 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: Makefile
2018-12-06 22:15:40 +00:00
prism_languages: [makefile]
category: CLI
2013-10-14 02:36:58 +00:00
---
### Var assignment
2012-03-16 06:14:31 +00:00
2015-04-16 19:03:21 +00:00
```makefile
foo = "bar"
bar = $(foo) foo # dynamic (renewing) assignment
foo := "boo" # one time assignment, $(bar) now is "boo foo"
foo ?= /usr/local # safe assignment, $(foo) and $(bar) still the same
bar += world # append, "boo foo world"
foo != echo fooo # exec shell command and assign to foo
# $(bar) now is "fooo foo world"
2015-04-16 19:03:21 +00:00
```
2012-03-16 06:14:31 +00:00
2019-01-20 06:49:16 +00:00
`=` expressions are only evaluated when they're being used.
### Magic variables
2012-10-11 08:13:19 +00:00
2015-04-16 19:03:21 +00:00
```makefile
out.o: src.c src.h
$@ # "out.o" (target)
$< # "src.c" (first prerequisite)
$^ # "src.c src.h" (all prerequisites)
2014-07-15 13:35:11 +00:00
2015-04-16 19:03:21 +00:00
%.o: %.c
$* # the 'stem' with which an implicit rule matches ("foo" in "foo.c")
2012-10-11 08:13:19 +00:00
2015-04-16 19:03:21 +00:00
also:
$+ # prerequisites (all, with duplication)
$? # prerequisites (new ones)
$| # prerequisites (order-only?)
2014-07-15 13:35:11 +00:00
2015-04-16 19:03:21 +00:00
$(@D) # target directory
```
2012-10-11 08:13:19 +00:00
### Command prefixes
2013-09-17 10:06:00 +00:00
2019-12-28 00:27:37 +00:00
| Prefix | Description |
| ------ | ------------------------------------------- |
| `-` | Ignore errors |
| `@` | Don't print command |
| `+` | Run even if Make is in 'don't execute' mode |
2013-09-17 10:06:00 +00:00
2015-04-16 19:03:21 +00:00
```makefile
build:
@echo "compiling"
-gcc $< $@
2013-09-17 10:06:00 +00:00
2015-04-16 19:03:21 +00:00
-include .depend
```
2013-09-17 10:06:00 +00:00
### Find files
2012-10-11 08:13:19 +00:00
2015-04-16 19:03:21 +00:00
```makefile
js_files := $(wildcard test/*.js)
all_files := $(shell find images -name "*")
```
2012-10-11 08:13:19 +00:00
### Substitutions
2012-03-16 06:14:31 +00:00
2015-04-16 19:03:21 +00:00
```makefile
file = $(SOURCE:.cpp=.o) # foo.cpp => foo.o
outputs = $(files:src/%.coffee=lib/%.js)
2014-07-15 13:35:11 +00:00
2015-04-16 19:03:21 +00:00
outputs = $(patsubst %.c, %.o, $(wildcard *.c))
assets = $(patsubst images/%, assets/%, $(wildcard images/*))
```
2014-07-15 13:35:11 +00:00
### More functions
2012-03-16 06:14:31 +00:00
2015-04-16 19:03:21 +00:00
```makefile
$(strip $(string_var))
2012-10-11 08:13:19 +00:00
2015-04-16 19:03:21 +00:00
$(filter %.less, $(files))
$(filter-out %.less, $(files))
```
2012-10-11 08:13:19 +00:00
### Building files
2012-03-16 06:14:31 +00:00
2015-04-16 19:03:21 +00:00
```makefile
%.o: %.c
ffmpeg -i $< > $@ # Input and output
foo $^
```
2012-11-15 21:57:56 +00:00
### Includes
2014-07-15 13:35:11 +00:00
2015-04-16 19:03:21 +00:00
```makefile
-include foo.make
```
2014-07-15 13:35:11 +00:00
### Options
2014-07-15 13:35:11 +00:00
2015-04-16 19:03:21 +00:00
```sh
make
-e, --environment-overrides
-B, --always-make
-s, --silent
-j, --jobs=N # parallel processing
```
2014-07-15 13:35:11 +00:00
### Conditionals
2012-11-15 21:57:56 +00:00
2015-04-16 19:03:21 +00:00
```makefile
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
2015-04-16 19:03:21 +00:00
else
$(CC) -o foo $(objects) $(normal_libs)
2015-04-16 19:03:21 +00:00
endif
```
2015-04-04 02:11:51 +00:00
### Recursive
2015-04-04 02:11:51 +00:00
2015-04-16 19:03:21 +00:00
```makefile
deploy:
$(MAKE) deploy2
```
2015-04-04 02:11:51 +00:00
### Further reading
2015-04-04 02:11:51 +00:00
2019-12-28 00:27:37 +00:00
- [isaacs's Makefile](https://gist.github.com/isaacs/62a2d1825d04437c6f08)
- [Your Makefiles are wrong](https://tech.davis-hansson.com/p/make/)
- [Manual](https://www.gnu.org/software/make/manual/html_node/index.html)