handmade/Makefile
Bubblegumdrop d2d7c82ca4 Split SafeUDPpacket and SafeUDPpacketV.
SafeUDPpacketV is much more complex due to the UDP packet splitting machinery.
2022-01-16 17:55:26 -05:00

202 lines
4.4 KiB
Makefile

.PHONY: all clean reallyclean test
.POSIX:
# {{{ Flags
SRCDIR := src
OBJDIR := .o
DEPDIR := .d
TESTDIR := test
TEST_BINDIR := $(TESTDIR)/bin
MKDIR := mkdir -p
# }}}
PROGS := main_client main_server
IMGUI_INCLUDE := -Iimgui -Iimgui/backends
IMGUI_SRCS := \
imgui/backends/imgui_impl_glfw.cpp \
imgui/backends/imgui_impl_opengl3.cpp \
imgui/imgui.cpp \
imgui/imgui_demo.cpp \
imgui/imgui_draw.cpp \
imgui/imgui_tables.cpp \
imgui/imgui_widgets.cpp
SRCS := $(wildcard $(SRCDIR)/*.cpp) $(IMGUI_SRCS) $(wildcard $(TESTDIR)/*.cpp)
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
COMMON_OBJS := \
$(OBJDIR)/src/lib_GetOpt.o \
$(OBJDIR)/src/lib_lua_common.o \
$(OBJDIR)/src/lib_SDL_common.o \
$(OBJDIR)/src/lib_SDL_Log.o \
$(OBJDIR)/src/signal_common.o \
$(OBJDIR)/src/UDPbase.o \
$(OBJDIR)/src/SafeUDPpacketV.o \
$(OBJDIR)/src/util.o
SERVER_OBJS := \
$(OBJDIR)/src/server_main.o \
$(OBJDIR)/src/Server.o \
$(OBJDIR)/src/Commands.o \
$(COMMON_OBJS)
CLIENT_OBJS := \
$(OBJDIR)/src/camera.o \
$(OBJDIR)/src/client_main.o \
$(OBJDIR)/src/cube.o \
$(OBJDIR)/src/FileIO.o \
$(OBJDIR)/src/lib_GL_common.o \
$(OBJDIR)/src/object.o \
$(OBJDIR)/src/quad.o \
$(OBJDIR)/src/shader.o \
$(COMMON_OBJS)
IMGUI_OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(IMGUI_SRCS)))
TEST_OBJ_COMMON := \
$(OBJDIR)/src/FileIO.o \
$(COMMON_OBJS)
TESTS := test_FileIO test_lua test_SafeUDPpacket
TEST_PROGS := $(addprefix $(TEST_BINDIR)/,$(TESTS))
$(shell $(MKDIR) $(dir $(OBJS)) >/dev/null)
$(shell $(MKDIR) $(dir $(DEPS)) >/dev/null)
$(shell $(MKDIR) $(dir $(TEST_PROGS)) >/dev/null)
# {{{ Flags
CC := gcc
CXX := g++
LD := g++
ifeq ($(CLANG),1)
CC := clang-12
CXX := clang++-12
LD := clang-12
endif
SRCDIR := src
OBJDIR := .o
DEPDIR := .d
RM := rm -rf
PKGS := SDL2_image SDL2_mixer SDL2_ttf SDL2_net sdl2 gl glew lua54
CPPFLAGS := $(shell pkg-config --cflags $(PKGS))
LDLIBS := $(shell pkg-config --libs $(PKGS)) -lm
DEFS := -D_FILE_OFFSET_BITS=64
INCS := -Iinclude $(IMGUI_INCLUDE)
DEBUG := -g
ifeq ($(D),1)
DEBUG := -ggdb
OPT := -O0
DEFS += -DDEBUG=1
endif
OPT := -O0
ifeq ($(O),1)
OPT := -O1 -g
endif
ifeq ($(O),2)
OPT := -O2 -g
endif
ifeq ($(O),3)
OPT := -O3 -g0
endif
ifeq ($(LTO),1)
LDFLAGS += -flto -fwhole-program
endif
ifeq ($(PG),1)
CFLAGS += -pg -coverage
endif
ifeq ($(PEDANTIC),1)
CFLAGS += -pedantic
endif
CFLAGS += -std=c99
CXXFLAGS += -std=c++14
CPPFLAGS += -fPIC -W -Wextra -Wall $(DEFS) $(DEBUG) $(INCS) $(OPT)
# flags required for dependency generation; passed to compilers
DEPFLAGS = -MT $@ -MD -MP -MF $(DEPDIR)/$*.Td
# compile C source files
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@
# compile C++ source files
COMPILE.cc = $(CXX) $(DEPFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c -o $@
# link object files to binary
LINK.o = $(LD) $(LDFLAGS) -o $@
LINK.so = $(LD) $(LDFLAGS) -o $@ -shared
# precompile step
PRECOMPILE =
# postcompile step
POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
# }}}
.SUFFIXES: .c .cc .cpp .cxx .o .1
all: $(OBJS) $(PROGS)
main_client: $(CLIENT_OBJS)
$(LINK.o) $^ $(LDLIBS)
main_server: $(SERVER_OBJS)
$(LINK.o) $^ $(LDLIBS)
test: $(TEST_PROGS)
@echo $(TEST_PROGS)
$(TEST_BINDIR)/test_FileIO: $(OBJDIR)/test/test_FileIO.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
$(LINK.o) $^ $(LDLIBS)
$(TEST_BINDIR)/test_lua: $(OBJDIR)/test/test_lua.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
$(LINK.o) $^ $(LDLIBS)
$(TEST_BINDIR)/test_SafeUDPpacket: $(OBJDIR)/test/test_SafeUDPpacket.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDPbase.o $(OBJDIR)/src/lib_SDL_common.o
$(LINK.o) $^ $(LDLIBS)
$(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.c) $<
$(POSTCOMPILE)
$(OBJDIR)/%.o: %.cpp
$(OBJDIR)/%.o: %.cpp $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.cc) $<
$(POSTCOMPILE)
$(OBJDIR)/%.o: %.cc
$(OBJDIR)/%.o: %.cc $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.cc) $<
$(POSTCOMPILE)
$(OBJDIR)/%.o: %.cxx
$(OBJDIR)/%.o: %.cxx $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.cc) $<
$(POSTCOMPILE)
clean: REALLYCLEAN?=@echo
clean:
$(REALLYCLEAN) $(RM) $(DEPDIR) $(OBJDIR) $(PROGS)
$(REALLYCLEAN) $(RM) $(TEST_PROGS)
reallyclean:
$(MAKE) REALLYCLEAN= clean
.PRECIOUS: $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;
-include $(DEPS)