summaryrefslogtreecommitdiff
path: root/Makefile
blob: 38fa821386cb3b7935d3bdcd9c61cc40b65e03fa (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
CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++14 -g -O3 -flto
LDFLAGS :=

OBJDIR := .objs

TERMIO_LIB := termio/libtermio.a

AI_SOURCES := $(wildcard ai_*.cpp)
AI_ALL_LOWER := $(patsubst ai_%.cpp,%,$(AI_SOURCES))

MAIN_DEP_FILES := $(patsubst %,$(OBJDIR)/main_%.d,$(AI_ALL_LOWER))

AI_CXX_SOURCES := $(wildcard ai_*.cpp)
AI_OBJ_FILES := $(patsubst %.cpp,$(OBJDIR)/%.o,$(AI_CXX_SOURCES))
AI_DEP_FILES := $(patsubst %.cpp,$(OBJDIR)/%.d,$(AI_CXX_SOURCES))

CXX_SOURCES := $(filter-out main.cpp $(AI_CXX_SOURCES),$(wildcard *.cpp))
OBJ_FILES := $(patsubst %.cpp,$(OBJDIR)/%.o,$(CXX_SOURCES))
DEP_FILES := $(patsubst %.cpp,$(OBJDIR)/%.d,$(CXX_SOURCES))

ALL_OBJ_FILES := $(OBJ_FILES) $(AI_OBJ_FILES)
ALL_DEP_FILES := $(DEP_FILES) $(AI_DEP_FILES)

TARGETS := $(patsubst %,ai%,$(AI_ALL_LOWER))


.PHONY: all clean

all: $(TARGETS)

clean:
	rm -f $(TARGETS)
	rm -rf $(OBJDIR)

$(TARGETS): ai%: $(OBJ_FILES) $(OBJDIR)/main_%.o $(OBJDIR)/ai_%.o $(TERMIO_LIB)
	@echo LD $@
	@$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

$(ALL_OBJ_FILES): $(OBJDIR)/%.o: %.cpp $(TERMIO_LIB)
	@mkdir -p $(dir $@)
	@echo CXX $<
	@$(CXX) $(CXXFLAGS) -c -o $@ $<

$(ALL_DEP_FILES): $(OBJDIR)/%.d: %.cpp $(TERMIO_LIB)
	@mkdir -p $(dir $@)
	@echo DEP $<
	@$(CXX) -MT $(OBJDIR)/$*.o -MM $(CXXFLAGS) $< >$@

$(OBJDIR)/main_%.o: main.cpp $(TERMIO_LIB)
	@mkdir -p $(dir $@)
	@echo CXX "$< -o $(notdir $@)"
	@$(CXX) $(CXXFLAGS) -DAI=$(shell echo "$*" | tr a-z A-Z) -DAI_HEADER=\"ai_$*.h\" -c -o $@ $<

$(OBJDIR)/main_%.d: main.cpp $(TERMIO_LIB)
	@mkdir -p $(dir $@)
	@echo DEP "$< -o $(notdir $@)"
	@$(CXX) -MT $(OBJDIR)/$*.o -MM $(CXXFLAGS) -DAI=$(shell echo "$*" | tr a-z A-Z) -DAI_HEADER=\"ai_$*.h\" $< >$@

termio/.git:
	git submodule update --init

$(TERMIO_LIB): termio/.git $(wildcard termio/*.c termio/*.h)
	make -C termio

-include $(ALL_DEP_FILES) $(MAIN_DEP_FILES)