summaryrefslogtreecommitdiff
path: root/Makefile
blob: 49823c8cf23e986b7897717452699e56431c18e4 (plain)
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
CC ?= gcc
CC ?= g++

SRCDIR ?= src
INCDIR ?= inc
OBJDIR ?= obj
TSTDIR ?= test
EXMDIR ?= examples

PKGS = sdl2 opengl
LIBS = -lm $(shell pkg-config --libs $(PKGS))
INCS = -Ithirdparty/cglm/include -Ithirdparty/ -Iinc/

CFLAGS ?= -D_FORTIFY_SOURCE=2 -Wall -Werror -Wno-error=unused-variable -g -Og
CFLAGS += -std=gnu23 -fms-extensions -flto $(shell pkg-config --cflags $(PKGS)) -DCIMGUI_USE_OPENGL3 -DCIMGUI_USE_SDL2
CXXFLAGS += -flto $(shell pkg-config --cflags $(PKGS)) -DCIMGUI_USE_OPENGL3 -DCIMGUI_USE_SDL2 -DIMGUI_IMPL_API='extern "C"'

APP_MAIN_SOURCES = $(SRCDIR)/main.c
APP_MAIN_OBJS = $(APP_MAIN_SOURCES:%.c=$(OBJDIR)/%.o)
APP_MAIN_DEPS = $(APP_MAIN_OBJS:%.o=%.d)

APP_SOURCES = $(filter-out $(APP_MAIN_SOURCES),$(shell find $(SRCDIR) -name "*.c"))
APP_OBJS = $(APP_SOURCES:%.c=$(OBJDIR)/%.o)
APP_DEPS = $(APP_OBJS:%.o=%.d)

TST_SOURCES = $(shell find $(TSTDIR) -name "*.c")
TST_OBJS = $(TST_SOURCES:%.c=$(OBJDIR)/%.o)
TST_DEPS = $(TST_OBJS:%.o=%.d)
TST_APPS = $(TST_SOURCES:%.c=$(OBJDIR)/%)

EXM_SOURCES = $(shell find $(EXMDIR) -name "*.c")
EXM_OBJS = $(EXM_SOURCES:%.c=$(OBJDIR)/%.o)
EXM_DEPS = $(EXM_OBJS:%.o=%.d)
EXM_APPS = $(EXM_SOURCES:%.c=$(OBJDIR)/%)

IMGUI_OBJS = $(OBJDIR)/thirdparty/cimgui/imgui/backends/imgui_impl_sdl2.o $(OBJDIR)/thirdparty/cimgui/imgui/backends/imgui_impl_opengl3.o

-include $(APP_DEPS) $(APP_MAIN_DEPS) $(TST_DEPS) $(EXM_DEPS)

# We don't really need to run the tests for bear to record them
compile_commands.json: clean Makefile $(APP_SOURCES)
	@rm -f "$@"
	bear -- make main

.PHONY: thirdparty/cimgui/cimgui.so
thirdparty/cimgui/cimgui.so:
	make -C thirdparty/cimgui

LIBS += -Lthirdparty/cimgui -Wl,-rpath,'$$ORIGIN/thirdparty/cimgui' -lcimgui -lstdc++

main: $(APP_MAIN_OBJS) $(APP_OBJS) thirdparty/cimgui/cimgui.so $(IMGUI_OBJS)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(APP_MAIN_OBJS) $(APP_OBJS) $(IMGUI_OBJS) $(LIBS)

$(OBJDIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(INCS) -MMD -o $@ -c $<

$(OBJDIR)/test/%: $(OBJDIR)/test/%.o $(APP_OBJS)
	@mkdir -p $(dir $@)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $< $(APP_OBJS) $(LIBS)

$(OBJDIR)/examples/%: $(OBJDIR)/examples/%.o $(APP_OBJS)
	@mkdir -p $(dir $@)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $< $(APP_OBJS) $(LIBS)

clean:
	@rm -rf $(OBJDIR)
	@rm -f main
	@make -C thirdparty/cimgui clean

test: $(TST_APPS)
	@echo "Running tests"
	@for t in $(TST_APPS); do echo "$$t"; $$t; done

examples: $(EXM_APPS)

.DEFAULT_GOAL := all
all: main test examples

# Imgui specific compile rules
$(OBJDIR)/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(INCS) -Ithirdparty/cimgui/imgui/ -MMD -o $@ -c $<