-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
156 lines (106 loc) · 3.48 KB
/
Makefile
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
.SUFFIXES:
.SUFFIXES: .cpp .hpp .o .a .c++m
.DEFAULT_GOAL := all
PROJECT := $(lastword $(notdir $(CURDIR)))
ifeq ($(MAKELEVEL),0)
ifndef OS
OS = $(shell uname -s)
endif
ifeq ($(OS),Linux)
CC = /usr/lib/llvm-18/bin/clang
CXX = /usr/lib/llvm-18/bin/clang++
CXXFLAGS = -pthread -I/usr/lib/llvm-18/include/c++/v1
LDFLAGS = -lc++ -lc++experimental -L/usr/lib/llvm-18s/lib/c++
endif
ifeq ($(OS),Darwin)
CC = /opt/homebrew/opt/llvm/bin/clang
CXX = /opt/homebrew/opt/llvm/bin/clang++
CXXFLAGS =-I/opt/homebrew/opt/llvm/include/c++/v1
LDFLAGS = -L/opt/homebrew/opt/llvm/lib/c++
endif
ifeq ($(OS),Github)
CC = /usr/local/opt/llvm/bin/clang
CXX = /usr/local/opt/llvm/bin/clang++
CXXFLAGS = -I/usr/local/opt/llvm/include/ -I/usr/local/opt/llvm/include/c++/v1
LDFLAGS = -L/usr/local/opt/llvm/lib/c++ -Wl,-rpath,/usr/local/opt/llvm/lib/c++
endif
CXXFLAGS += -std=c++23 -stdlib=libc++ -Wall -Wextra #-DDEBUG
endif #($(MAKELEVEL),0)
CXXFLAGS += -MMD -I$(SRCDIR)
LDFLAGS +=
############
PREFIX = .
SRCDIR = src
OBJDIR = obj
TESTDIR = test
BINDIR =$(PREFIX)/bin
LIBDIR = $(PREFIX)/lib
INCDIR = $(PREFIX)/include
PCMDIR = $(PREFIX)/pcm
GTESTDIR = $(PREFIX)/googletest
############
# Make does not offer a recursive wildcard function, so here's one:
rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
############
ifeq ($(basename $(basename $(shell $(CXX) -dumpversion))),18) # This section only works with Clang 18
MODULES = $(call rwildcard,$(SRCDIR)/,*.c++m)
PCMS = $(MODULES:$(SRCDIR)/%.c++m=$(PCMDIR)/%.pcm)
OBJECTS += $(MODULES:$(SRCDIR)/%.c++m=$(OBJDIR)/%.o)
$(PCMDIR)/%.pcm: $(SRCDIR)/%.c++m
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $< --precompile -c -o $@
$(OBJDIR)/%.o: $(PCMDIR)/%.pcm
@mkdir -p $(@D)
$(CXX) $< -c -o $@
LIBRARY = $(addprefix $(LIBDIR)/, lib$(PROJECT).a)
$(LIBRARY) : $(OBJECTS)
@mkdir -p $(@D)
$(AR) $(ARFLAGS) $@ $^
endif # Clang 15 and above
############
SOURCES = $(call rwildcard,$(SRCDIR)/,*.cpp)
OBJECTS += $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -I$(SRCDIR) -c $< -o $@
############
HEADERS = $(call rwildcard,$(SRCDIR)/,*.hpp)
INCLUDES = $(HEADERS:$(SRCDIR)/%.hpp=$(INCDIR)/%.hpp)
$(INCDIR)/%.hpp: $(SRCDIR)/%.hpp
@mkdir -p $(@D)
cp $< $@
############
GTESTLIBS = $(addprefix $(LIBDIR)/, libgtest.a libgtest_main.a)
$(GTESTLIBS):
git submodule update --init --recursive --depth 1
cd $(GTESTDIR) && cmake -DCMAKE_INSTALL_PREFIX=.. -DCMAKE_C_COMPILER="$(CC)" -DCMAKE_CXX_COMPILER="$(CXX)" -DCMAKE_CXX_FLAGS="$(CXXFLAGS)" . && make install
############
TEST_SOURCES = $(call rwildcard,$(TESTDIR)/,*.cpp)
TEST_OBJECTS = $(TEST_SOURCES:$(TESTDIR)/%.cpp=$(OBJDIR)/$(TESTDIR)/%.o)
TEST_TARGET = $(BINDIR)/test_$(PROJECT)
$(OBJDIR)/$(TESTDIR)/%.o: $(TESTDIR)/%.cpp $(GTESTLIBS) $(INCLUDES)
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -I$(INCDIR) -c $< -o $@
$(TEST_TARGET): $(TEST_OBJECTS) $(LIBRARY)
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(TEST_OBJECTS) $(LIBRARY) $(GTESTLIBS) -o $@
############
DEPENDENCIES = $(MAINS:$(SRCDIR)/%.cpp=$(OBJDIR)/%.d) $(OBJECTS:%.o=%.d) $(TEST_OBJECTS:%.o=%.d)
############
.PHONY: all
all: lib
.PHONY: lib
lib: $(INCLUDES) $(LIBRARY)
.PHONY: module
module: $(PCMS) $(LIBRARY)
.PHONY: test
test: $(TEST_TARGET)
$(TEST_TARGET)
.PHONY: clean
clean:
@rm -rf $(OBJDIR) $(BINDIR) $(LIBDIR) $(INCDIR) $(PCMDIR)
.PHONY: dump
dump:
$(foreach v, $(sort $(.VARIABLES)), $(if $(filter file,$(origin $(v))), $(info $(v)=$($(v)))))
@echo ''
-include $(DEPENDENCIES)