Index

ncurses-minesweeper / 57786ab

Terminal game of Minesweeper, implemented in C with ncurses.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
912 Sep 2020 10:444fbaed5Use shell find instead of wildcard in MakefileJosh Stockin122G

Blob @ ncurses-minesweeper / Makefile

text/plain3259 bytesdownload raw
1# Preamble >> Make config
2SHELL := bash
3.ONESHELL:
4.SHELLFLAGS := -eu -o pipefail -c
5.DELETE_ON_ERROR:
6MAKEFLAGS += --warn-undefined-variables
7MAKEFLAGS += --no-builtin-rules
8
9ifeq ($(origin .RECIPEPREFIX), undefined)
10 $(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
11endif
12.RECIPEPREFIX = >
13
14
15# Custom Make settings
16PROJECT_NAME := minesweeper
17
18CC := gcc -std=c11
19CFLAGS := -O3
20CWARNINGS := -Werror -Wall -Wextra -pedantic
21CINCLUDES := -Isrc
22CLIBS := -lncurses
23
24CFORMAT := clang-format -i
25CTIDY := clang-tidy --checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus-*
26
27SOURCE_DIR := src
28BIN_DIR := bin
29
30# Inputs and outputs
31SOURCES := $(shell find $(SOURCE_DIR) -name *.c)
32HEADERS := $(shell find $(SOURCE_DIR) -name *.h)
33OBJECTS := $(patsubst %.c, $(BIN_DIR)/%.o, $(SOURCES))
34OUTFILE := $(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
39all: lint compile build run done
40
41
42# Automatically create targets and gather their prerequisites
43define 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
52endef
53
54compile: precompile $(OBJECTS) postcompile
55precompile:
56> @echo -e "\e[1m\e[35mCOMPILING\e[0m"
57postcompile:
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
63build: prebuild $(OUTFILE) postbuild
64prebuild:
65> @echo -e "\e[1m\e[35mLINKING\e[0m"
66postbuild:
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
77done:
78> @echo -e "\e[1m\e[48;5;28m\e[37mDone!\e[0m"
79
80
81# Run outfile
82run: $(OUTFILE)
83> @$(OUTFILE)
84
85
86# Create linter targets for source and header files
87define 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 $$@
101endef
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
107lint: prelint $(patsubst %.h, $(BIN_DIR)/%.hl, $(HEADERS)) $(patsubst %.c, $(BIN_DIR)/%.cl, $(SOURCES)) postlint
108prelint:
109> @echo -e "\e[1m\e[35mLINTING\e[0m"
110postlint:
111> @echo -e "\e[94mDone linting\e[0m\n"
112
113
114# Clean target
115clean:
116> @rm -r $(BIN_DIR)
117