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 | # Custom Make settings |
16 | PROJECT_NAME := minesweeper |
17 |
|
18 | CC := gcc -std=c11 |
19 | CFLAGS := -O3 |
20 | CWARNINGS := -Werror -Wall -Wextra -pedantic |
21 | CINCLUDES := -Isrc |
22 | CLIBS := -lncurses |
23 |
|
24 | CFORMAT := clang-format -i |
25 | CTIDY := clang-tidy --checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus-* |
26 |
|
27 | SOURCE_DIR := src |
28 | BIN_DIR := bin |
29 |
|
30 | # Inputs and outputs |
31 | SOURCES := $(shell find $(SOURCE_DIR) -name *.c) |
32 | HEADERS := $(shell find $(SOURCE_DIR) -name *.h) |
33 | OBJECTS := $(patsubst %.c, $(BIN_DIR)/%.o, $(SOURCES)) |
34 | OUTFILE := $(BIN_DIR)/$(PROJECT_NAME) |
35 |
|
36 |
|
37 | # Make target config |
38 | .PHONY: all prelint postlint lint precompile postcompile compile prebuild postbuild build clean run done |
39 | all: lint compile build run done |
40 |
|
41 |
|
42 | # Automatically create targets and gather their prerequisites |
43 | define create-ctarget |
44 | $1 |
45 | > @echo -en "\e[37m[\e[34m?\e[37m]\e[0m $$< \e[36m⇒\e[0m $$@" |
46 | > @mkdir -p $$(dir $$@) |
47 | > @if $(CC) -o $$@ -c $(CFLAGS) $(CWARNINGS) $(CINCLUDES) $$<>/dev/null 2>&1; |
48 | > @then echo -e "\r\e[37m[\e[32m✓\e[37m]\e[0m"; |
49 | > @else echo -e "\r\e[37m[\e[31m✗\e[37m]\e[0m"; |
50 | > @$(CC) -o $$@ -c $(CFLAGS) $(CWARNINGS) $(CINCLUDES) $$<; |
51 | > @fi |
52 | endef |
53 |
|
54 | compile: precompile $(OBJECTS) postcompile |
55 | precompile: |
56 | > @echo -e "\e[1m\e[35mCOMPILING\e[0m" |
57 | postcompile: |
58 | > @echo -e "\e[94mDone compiling\e[0m\n" |
59 | $(foreach src,$(SOURCES),$(eval $(call create-ctarget,$(shell $(CC) -MM -MT $(patsubst %.c, $(BIN_DIR)/%.o, $(src)) $(src))))) |
60 |
|
61 |
|
62 | # Link and build outfile |
63 | build: prebuild $(OUTFILE) postbuild |
64 | prebuild: |
65 | > @echo -e "\e[1m\e[35mLINKING\e[0m" |
66 | postbuild: |
67 | > @echo -e "\e[94mDone linking\e[0m\n" |
68 | $(OUTFILE): $(OBJECTS) |
69 | > @echo -en "\e[37m[\e[34m?\e[37m]\e[0m $^ \e[36m⇒\e[0m $@" |
70 | > @mkdir -p $(dir $@) |
71 | > @if $(CC) -o $@ $^ $(CLIBS)>/dev/null 2>&1; |
72 | > @then echo -e "\r\e[37m[\e[32m✓\e[37m]\e[0m"; |
73 | > @else echo -e "\r\e[37m[\e[31m✗\e[37m]\e[0m"; |
74 | > @$(CC) -o $@ $^ $(CLIBS); |
75 | > @fi |
76 |
|
77 | done: |
78 | > @echo -e "\e[1m\e[48;5;28m\e[37mDone!\e[0m" |
79 |
|
80 |
|
81 | # Run outfile |
82 | run: $(OUTFILE) |
83 | > @$(OUTFILE) |
84 |
|
85 |
|
86 | # Create linter targets for source and header files |
87 | define create-linttarget |
88 | $1 |
89 | > @echo -en "\e[37m[\e[34m?\e[37m]\e[0m $$<" |
90 | > @if ! $(CFORMAT) $$<>/dev/null 2>&1; |
91 | > @then echo -e "\r\e[37m[\e[31m✗\e[37m]\e[0m"; |
92 | > @$(CFORMAT) $$<; |
93 | > @fi; |
94 | > @if $(CTIDY) $$<>/dev/null 2>&1; |
95 | > @then echo -e "\r\e[37m[\e[32m✓\e[37m]\e[0m"; |
96 | > @else echo -e "\r\e[37m[\e[31m✗\e[37m]\e[0m"; |
97 | > @$(CTIDY) $$<; |
98 | > @fi; |
99 | > @mkdir -p $$(dir $$@) |
100 | > @touch $$@ |
101 | endef |
102 |
|
103 | # Translate %.(c|h) to bin/%.(c|h)l and create targets for their sources |
104 | $(foreach src,$(SOURCES),$(eval $(call create-linttarget,$(patsubst %.c, $(BIN_DIR)/%.cl, $(src)): $(src)))) |
105 | $(foreach header,$(HEADERS),$(eval $(call create-linttarget,$(patsubst %.h, $(BIN_DIR)/%.hl, $(header)): $(header)))) |
106 |
|
107 | lint: prelint $(patsubst %.h, $(BIN_DIR)/%.hl, $(HEADERS)) $(patsubst %.c, $(BIN_DIR)/%.cl, $(SOURCES)) postlint |
108 | prelint: |
109 | > @echo -e "\e[1m\e[35mLINTING\e[0m" |
110 | postlint: |
111 | > @echo -e "\e[94mDone linting\e[0m\n" |
112 |
|
113 |
|
114 | # Clean target |
115 | clean: |
116 | > @rm -r $(BIN_DIR) |
117 |
|