This commit is contained in:
Rico Sta. Cruz 2015-04-17 03:03:21 +08:00
parent e839dca089
commit 871a4ba4f8
2 changed files with 75 additions and 51 deletions

View File

@ -1,16 +1,18 @@
---
title: Make for assets
layout: default
hljs_languages: [makefile]
---
## Basic compiling
bin := ./node_modules/.bin
```makefile
bin := ./node_modules/.bin
all: build/foo.js
all: build/foo.js
build/%.js: src/%.coffee
@$(bin)/coffee < $^ > $@
build/%.js: src/%.coffee
@$(bin)/coffee < $^ > $@
```
## Stylus + Autoprefixer

View File

@ -1,31 +1,35 @@
---
title: Makefile
layout: default
hljs_languages: [makefile]
---
## Var assignment
uglify = $(uglify) # assignment
compressor := $(uglify) # lazy assignment
prefix ?= /usr/local # safe assignment
```makefile
uglify = $(uglify) # assignment
compressor := $(uglify) # lazy assignment
prefix ?= /usr/local # safe assignment
```
## Magic variables
out.o: src.c src.h
$@ - "out.o" (target)
$< - "src.c" (first prerequisite)
$^ - "src.c src.h" (all prerequisites)
```makefile
out.o: src.c src.h
$@ # "out.o" (target)
$< # "src.c" (first prerequisite)
$^ # "src.c src.h" (all prerequisites)
%.o: %.c
$% - target member name ("foo" in "foo.c")
%.o: %.c
$% # target member name ("foo" in "foo.c")
also:
$+ - prerequisites (all, with duplication)
$? - prerequisites (new ones)
$| - prerequisites (order-only?)
$* - basename without extension of the target (?)
also:
$+ # prerequisites (all, with duplication)
$? # prerequisites (new ones)
$| # prerequisites (order-only?)
$* # basename without extension of the target (?)
$(@D) - target directory
$(@D) # target directory
```
## Command prefixes
@ -35,64 +39,82 @@ layout: default
| `@` | Don't print command |
| `+` | Run even if Make is in 'don't execute' mode |
build:
@echo "compiling"
-gcc $< $@
```makefile
build:
@echo "compiling"
-gcc $< $@
-include .depend
-include .depend
```
## Find files
js_files := $(wildcard test/*.js)
all_files := $(shell find images -name "*")
```makefile
js_files := $(wildcard test/*.js)
all_files := $(shell find images -name "*")
```
## Substitutions
file = $(SOURCE:.cpp=.o) # foo.cpp => foo.o
outputs = $(files:src/%.coffee=lib/%.js)
```makefile
file = $(SOURCE:.cpp=.o) # foo.cpp => foo.o
outputs = $(files:src/%.coffee=lib/%.js)
outputs = $(patsubst %.c, %.o, $(wildcard *.c))
assets = $(patsubst images/%, assets/%, $(wildcard images/*))
outputs = $(patsubst %.c, %.o, $(wildcard *.c))
assets = $(patsubst images/%, assets/%, $(wildcard images/*))
```
## More functions
$(strip $(string_var))
```makefile
$(strip $(string_var))
$(filter %.less, $(files))
$(filter-out %.less, $(files))
$(filter %.less, $(files))
$(filter-out %.less, $(files))
```
## Building files
%.o: %.c
ffmpeg -i $< > $@ # Input and output
foo $^
```makefile
%.o: %.c
ffmpeg -i $< > $@ # Input and output
foo $^
```
## Includes
-include foo.make
```makefile
-include foo.make
```
## Options
make
-e, --environment-overrides
-B, --always-make
-s, --silent
```sh
make
-e, --environment-overrides
-B, --always-make
-s, --silent
-j, --jobs=N # parallel processing
-j, --jobs=N # parallel processing
```
## Conditionals
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
```makefile
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
```
## Recursive
deploy:
$(MAKE) deploy2
```makefile
deploy:
$(MAKE) deploy2
```
## Further reading