-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
78 lines (65 loc) · 2.22 KB
/
Makefile
File metadata and controls
78 lines (65 loc) · 2.22 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
# Lesson 14: Conditional Logic
#
# Make supports conditionals:
# ifeq ($(VAR),value) — string equality
# ifneq — string inequality
# ifdef / ifndef — variable defined or not
#
# Common uses:
# - Debug vs release builds (optimization, symbols, defines)
# - Platform detection (different flags for Linux vs macOS)
# - Optional features via user-defined variables
#
# Usage:
# make → debug build (default)
# make BUILD=release → release build
# make BUILD=release VERSION=1.2.3
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Iinclude -MMD -MP
BUILDDIR = build
TARGET = $(BUILDDIR)/app
# ── Build mode ────────────────────────────────────────────
BUILD ?= debug
ifeq ($(BUILD),release)
CXXFLAGS += -O2 -DNDEBUG
BUILDDIR = build/release
else ifeq ($(BUILD),debug)
CXXFLAGS += -g -O0 -fsanitize=address
LDFLAGS += -fsanitize=address
BUILDDIR = build/debug
else
$(error Unknown BUILD mode: $(BUILD). Use 'debug' or 'release')
endif
# ── Optional version define ───────────────────────────────
ifdef VERSION
CXXFLAGS += -DAPP_VERSION=$(VERSION)
endif
# ── Platform detection ────────────────────────────────────
UNAME := $(shell uname)
ifeq ($(UNAME),Linux)
CXXFLAGS += -pthread
endif
# ── Sources ───────────────────────────────────────────────
SRCS = src/main.cpp src/app.cpp
OBJS = $(SRCS:%.cpp=$(BUILDDIR)/%.o)
DEPS = $(OBJS:.o=.d)
TARGET = $(BUILDDIR)/app
.PHONY: all clean info
all: $(TARGET)
$(TARGET): $(OBJS)
@mkdir -p $(@D)
$(CXX) $(LDFLAGS) -o $@ $^
$(BUILDDIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
rm -rf build
# Print resolved configuration (handy for debugging Makefiles)
info:
@echo "BUILD = $(BUILD)"
@echo "CXX = $(CXX)"
@echo "CXXFLAGS = $(CXXFLAGS)"
@echo "LDFLAGS = $(LDFLAGS)"
@echo "TARGET = $(TARGET)"
@echo "UNAME = $(UNAME)"
-include $(DEPS)