-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.mk
85 lines (68 loc) · 1.88 KB
/
make.mk
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
# 15-462 project makefile
# Authors: Eric Butler
# DO NOT EDIT THIS FILE
#########
# USAGE #
#########
# the makefile assumes the following variables are defined:
# TOP_OBJ_DIR: the top object level directory
# MODE: the mode, either "debug" or "release"
# SRCS: the source files
# TARGET: the target (executable) name
ifeq ($(TARGET),)
ERRORMSG = "No target specified"
else ifeq ($(TOP_OBJ_DIR),)
ERRORMSG = "No object directory specified"
else ifeq ($(SRCS),)
ERRORMSG = "No sources specified"
endif
# the current directory
CURR_DIR = $(shell pwd)
# global compiler flags
CXX = g++
CXXFLAGS += -Wall -ansi -pedantic -I"$(CURR_DIR)/$(SRC_DIR)"
LDFLAGS = -lGL -lGLU -lSDLmain -lSDL -lpng
# object directories, mode flags
ifeq ($(MODE), release)
SUB_OBJ_DIR = release
CXXFLAGS += -O2
else ifeq ($(MODE), debug)
SUB_OBJ_DIR = debug
CXXFLAGS += -g -O0
else
ERRORMSG = "unknown build mode: $(MODE)"
endif
OBJ_DIR = $(TOP_OBJ_DIR)/$(SUB_OBJ_DIR)
# list of all object files
OBJS = $(SRCS:.cpp=.o)
# list of all dep files
DEPS = $(OBJS:.o=.d)
# full list of paths to all objs
OBJS_FULL = $(addprefix $(OBJ_DIR)/,$(OBJS))
# full list of paths to all deps
DEPS_FULL = $(addprefix $(OBJ_DIR)/,$(DEPS))
# sanity check for '.cpp' suffix
TMP_SRCS_NOT_CPP = $(filter-out %.cpp,$(SRCS))
ifneq (,$(TMP_SRCS_NOT_CPP))
ERRORMSG = "Feeling nervous about '$(TMP_SRCS_NOT_CPP)'; I only know how to build .cpp files"
endif
# targets
.PHONY: target
ifneq ($(ERRORMSG),)
target:
$(error $(ERRORMSG))
else
target: $(OBJ_DIR)/$(TARGET).exe
cp $< $(TARGET)
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.cpp
mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -MM -MP -MT $(@:.d=.o) -o $@ $<
# don't need to mkdir for object files since d files already exist
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS_FULL)
endif
$(OBJ_DIR)/$(TARGET).exe: $(OBJS_FULL)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^
endif