-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
84 lines (66 loc) · 2.16 KB
/
Copy pathMakefile
File metadata and controls
84 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
SHELL := /bin/sh
PROJECT := 2d-platformer
CXX ?= c++
PKG_CONFIG ?= pkg-config
MODE ?= debug
BUILD_ROOT := build
BUILD_DIR := $(BUILD_ROOT)/$(MODE)
TARGET := $(BUILD_DIR)/$(PROJECT)
SOURCES := $(sort $(wildcard src/*.cpp))
OBJECTS := $(patsubst src/%.cpp,$(BUILD_DIR)/%.o,$(SOURCES))
DEPFILES := $(OBJECTS:.o=.d)
SDL_PACKAGES := sdl2 SDL2_image
SDL_CFLAGS := $(shell $(PKG_CONFIG) --cflags $(SDL_PACKAGES) 2>/dev/null)
SDL_LIBS := $(shell $(PKG_CONFIG) --libs $(SDL_PACKAGES) 2>/dev/null)
CPPFLAGS += -Iinclude $(SDL_CFLAGS)
CXXFLAGS += -std=c++23 -Wall -Wextra -Wpedantic -MMD -MP
LDLIBS += $(SDL_LIBS)
ifeq ($(MODE),debug)
CXXFLAGS += -O0 -g3
CPPFLAGS += -DDEBUG
else ifeq ($(MODE),release)
CXXFLAGS += -O2
CPPFLAGS += -DNDEBUG
else
$(error MODE must be either "debug" or "release")
endif
.DEFAULT_GOAL := all
.PHONY: all run clean rebuild release help check-deps
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(BUILD_DIR)/%.o: src/%.cpp | check-deps
@mkdir -p $(@D)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
check-deps:
@command -v "$(PKG_CONFIG)" >/dev/null 2>&1 || { \
printf '%s\n' "error: pkg-config is required but was not found."; \
printf '%s\n' "macOS: brew install pkg-config sdl2 sdl2_image"; \
printf '%s\n' "Debian/Ubuntu: sudo apt install pkg-config libsdl2-dev libsdl2-image-dev"; \
exit 1; \
}
@"$(PKG_CONFIG)" --exists $(SDL_PACKAGES) || { \
printf '%s\n' "error: SDL2 and SDL2_image development packages are required."; \
printf '%s\n' "macOS: brew install sdl2 sdl2_image"; \
printf '%s\n' "Debian/Ubuntu: sudo apt install libsdl2-dev libsdl2-image-dev"; \
exit 1; \
}
run: $(TARGET)
@./$(TARGET)
release:
@$(MAKE) MODE=release all
rebuild:
@$(MAKE) clean
@$(MAKE) MODE=$(MODE) all
clean:
$(RM) -r $(BUILD_ROOT)
help:
@printf '%s\n' \
"make Build a debug executable" \
"make run Build and run the game" \
"make release Build an optimized release executable" \
"make MODE=release run Build and run the release executable" \
"make clean Remove generated build output" \
"" \
"Overrides: CXX=<compiler> PKG_CONFIG=<pkg-config> MODE=debug|release"
-include $(DEPFILES)