1 | # Preamble >> Make config |
2 | SHELL := bash |
3 | .ONESHELL: |
4 | .SHELLFLAGS := -eu -o pipefail -c |
5 | .DELETE_ON_ERROR: |
6 | MAKEFLAGS += --warn-undefined-variables |
7 | MAKEFLAGS += --no-builtin-rules |
8 |
|
9 | ifeq ($(origin .RECIPEPREFIX), undefined) |
10 | $(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later) |
11 | endif |
12 | .RECIPEPREFIX = > |
13 |
|
14 |
|
15 | # Compiler settings |
16 | CC := gcc -std=c11 |
17 | CFLAGS := -O3 |
18 | CWARNINGS := -Werror -Wall -Wextra -pedantic |
19 | CINCLUDES := -Isrc |
20 | CLIBS := -lncurses |
21 | CLINT := --syntax-only |
22 |
|
23 | SOURCE_DIR := src |
24 | BIN_DIR := bin |
25 | SOURCES := $(wildcard $(SOURCE_DIR)/*.c) |
26 | OBJECTS := $(patsubst %.c, $(BIN_DIR)/%.o, $(SOURCES)) |
27 | OUTFILE := $(BIN_DIR)/minesweeper |
28 |
|
29 | .PHONY: all lint clean |
30 | all: $(OUTFILE) |
31 |
|
32 |
|
33 | # Automatically create targets and gather their prerequisites |
34 | define create-ctarget |
35 | $1 |
36 | > @echo Compiling $$@ from $$^ |
37 | > @mkdir -p $$(dir $$@) |
38 | > @$(CC) -o $$@ -c $(CFLAGS) $(CWARNINGS) $(CINCLUDES) $$< |
39 | endef |
40 |
|
41 | $(foreach src,$(SOURCES),$(eval $(call create-ctarget,$(shell $(CC) -MM -MT $(patsubst %.c, $(BIN_DIR)/%.o, $(src)) $(src))))) |
42 |
|
43 |
|
44 | # Build outfile |
45 | $(OUTFILE): $(OBJECTS) |
46 | > @echo Building $@ from $^ |
47 | > @mkdir -p $(dir $@) |
48 | > $(CC) -o $@ $^ $(CLIBS) |
49 | > @echo Done! |
50 |
|
51 |
|
52 | # Lint |
53 | lint: $(SOURCES) |
54 | > @echo Linting... |
55 | > @$(foreach src,$^,$(CC) $(CLINT) $(CWARNINGS) $(CINCLUDES) $(src);) |
56 |
|
57 |
|
58 | # Clean |
59 | clean: |
60 | > @rm -r $(BIN_DIR) |
61 |
|