handmade/Makefile
2022-01-04 09:05:11 -05:00

195 lines
4.1 KiB
Makefile

.PHONY: all clean
.POSIX:
# {{{ Flags
SRCDIR := src
OBJDIR := .o
DEPDIR := .d
TESTDIR := test
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)
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
$(shell $(MKDIR) $(dir $(OBJS)) >/dev/null)
$(shell $(MKDIR) $(dir $(DEPS)) >/dev/null)
COMMON_OBJS := \
$(OBJDIR)/src/lib_lua_common.o \
$(OBJDIR)/src/lib_SDL_common.o \
$(OBJDIR)/src/signal_common.o \
$(OBJDIR)/src/util.o
SERVER_OBJS := \
$(OBJDIR)/src/server_main.o \
$(OBJDIR)/src/Server.o \
$(COMMON_OBJS)
CLIENT_OBJS := \
$(OBJDIR)/src/camera.o \
$(OBJDIR)/src/client_main.o \
$(OBJDIR)/src/Client.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 \
$(OBJDIR)/src/UDP_Write.o \
$(COMMON_OBJS)
IMGUI_OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(IMGUI_SRCS)))
TEST_OBJ_COMMON := \
$(OBJDIR)/src/FileIO.o \
$(COMMON_OBJS)
TESTS := test_FileIO test_File_Write_UDP test_Object test_lua
TEST_PROGS := $(addprefix $(TESTDIR)/,$(TESTS))
# {{{ 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)
test/test_FileIO: $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
test/test_File_Write_UDP: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
test/test_Object: $(TEST_OBJ_COMMON) $(OBJDIR)/src/object.o $(OBJDIR)/src/quad.o $(OBJDIR)/src/cube.o $(OBJDIR)/src/lib_GL_common.o
test/test_lua: $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
test/test_Client: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
test/test_UDP_DumpPacket: $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
$(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:
@echo $(RM) $(DEPDIR) $(OBJDIR) $(PROGS)
@echo $(RM) $(TEST_PROGS)
.PRECIOUS: $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;
-include $(DEPS)