-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
235 lines (199 loc) · 6.98 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
cxx ?= clang++
cxxflags = -fPIC -std=c++17 -Wall -Wextra -Wno-unused-function
dllflags = -shared -rdynamic
ldflags =
debug ?= 1
ifeq ($(debug), 1)
cppflags = -DDEBUG -DPROTO_DEBUG
cxxflags += -g
#rdynamic for function names in glibc backtrace()
ldflags += -rdynamic
endif
ifeq ($(OS),Windows_NT)
platform := WINDOWS
cppflags += -D WIN32
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
cppflags += -D AMD64
else
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
cppflags += -D AMD64
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
cppflags += -D IA32
endif
endif
else
uname_s := $(shell uname -s)
ifeq ($(uname_s),Linux)
platform := LINUX
cppflags += -D LINUX
endif
ifeq ($(uname_s),Darwin)
platform := OSX
cppflags += -D OSX
endif
uname_p := $(shell uname -p)
ifeq ($(uname_p),x86_64)
cppflags += -D AMD64
endif
ifneq ($(filter %86,$(uname_p)),)
cppflags += -D IA32
endif
ifneq ($(filter arm%,$(uname_p)),)
cppflags += -D ARM
endif
endif
# todo get rid of that, no mercy for windows people, get coreutil tools, scrub!
#ifeq ($(platform), LINUX)
#define makedir
# mkdir -p $(1)
#endef
#define remove
# rm -f $(1)
#endef
#define if-file-not-exist
# if [ ! -f "$(1)" ]; then $(2); fi
#endef
#
#else ifeq ($(platform), WINDOWS)
#define winpath
# $(subst /,\\,$(1))
#endef
#define unixpath
# $(subst \,/,$(subst \\,/,$(1)))
#endef
#define makedir
# $(eval ARG := $(call winpath, $(patsubst %/,%,$(1))))
# if not exist $(ARG) mkdir $(ARG)
#endef
#define remove
# del /Q /F $(call winpath, $(1)) 2> nul
#endef
#define if-file-exist
# if exist $(call winpath,$(1)) ($(2))
#endef
#define if-file-not-exist
# if not exist $(call winpath,$(1)) ($(2))
#endef
#
#endif
src_dir := src/proto
obj_dir := obj
ar_obj_dir := obj/ar
bin_dir := bin
lib_dir := lib
test_src_dir := test
vendor_dir := vendor
ifeq ($(platform), LINUX)
runtime_name = proto-runtime
library_name = libproto.a
runtime = $(bin_dir)/$(runtime_name)
library = $(lib_dir)/$(library_name)
standalone = $(bin_dir)/$(client_name)$($(client_name),-,proto-)standalone
proto_srcs = $(shell find $(src_dir) -name *.cc)
proto_objs := $(proto_srcs:$(src_dir)/%.cc=$(obj_dir)/%.o)
proto_runtime_objs = \
$(filter %/entry-point.o %-runtime.o, $(proto_objs))
proto_lib_objs = \
$(filter-out $(proto_runtime_objs), $(proto_objs))
proto_lib_ar_objs := \
$(addprefix $(ar_obj_dir)/,$(subst /,_,$(proto_lib_objs:$(obj_dir)/%.o=%.o)))
proto_deps := $(proto_objs:.o=.d)
test_srcs = $(shell find $(test_src_dir)/proto -name *.cc)
test_objs := $(test_srcs:$(test_src_dir)/proto/%.cc=$(obj_dir)/test/%.o)
includes := -I src/ -I vendor/ -I vendor/gl3w/include/
libs := -L./lib -L/usr/lib -lX11 -lGL -lGLX -lGLEW -ldl
endif
ifdef client_src_dir
ifdef client_name
client_srcs = $(shell find $(client_src_dir) -name *.cc)
client_obj_dir = $(obj_dir)/client/$(client_name)
client_objs = $(client_srcs:$(client_src_dir)/%.cc=$(client_obj_dir)/%.o)
client_deps := $(client_objs:.o=.d)
endif
endif
# TARGETS
.PHONY: runtime
runtime: $(runtime)
.PHONY: test
test: $(test)
.PHONY: standalone_setup
standalone_setup:
$(eval cppflags += "-DPROTO_STANDALONE")
.PHONY: standalone
standalone: standalone_setup runtime $(standalone)
.PHONY: client
client: runtime $(client_objs) $(library)
$(if $(client_src_dir),,\
$(error Client sources directory path variable 'client_src_dir' is not set. ))
$(if $(client_name),,\
$(error Client dynamic library name 'client_name' is not set. ))
$(if $(client_srcs),,\
$(error No source files in $(client_src_dir) ))
# While hot-swapping dl there runtime records that
# file changed and tries to fetch it before linker has written
# all of it, hence outputting to part. rename should to be atomic.
$(cxx) -fPIC $(dllflags) $(ldflags) -o $(bin_dir)/$(client_name).so.part \
$(client_objs) $(library) $(libs) $(client_libs)
mv $(bin_dir)/$(client_name).so.part $(bin_dir)/$(client_name).so
$(eval outfile = $(bin_dir)/run-$(client_name).sh)
# NOTE(kacper): bit of a headache to write $@ to file in make...
# let me know if you have better idea how to do it.
@echo "#!/bin/bash" > $(outfile)
@echo "$(runtime) $(bin_dir)/$(client_name).so \"__dollar__at\"" >> $(outfile)
@sed -i '$ s/__dollar/$$/' $(outfile)
@sed -i '$ s/__at/@/' $(outfile)
@chmod +x $(bin_dir)/run-$(client_name).sh
$(standalone): $(proto_runtime_objs) $(client_objs) $(library)
$(if $(client_src_dir),,\
$(error Client sources directory path variable 'client_src_dir' is not set. ))
$(if $(client_name),,\
$(error Client dynamic library name 'client_name' is not set. ))
$(if $(client_srcs),,\
$(error No source files in $(client_src_dir) ))
$(cxx) $(ldflags) -o $@ $(proto_runtime_objs) $(client_objs) $(library) $(libs)
$(client_objs): $(client_obj_dir)/%.o: $(client_src_dir)/%.cc
@mkdir -p $(dir $@)
$(cxx) -c -fPIC $(cxxflags) -MMD -MP $< $(includes) $(client_includes) -o $@
$(library): $(proto_lib_objs)
ar rcs $(library) $(proto_lib_ar_objs)
$(runtime): $(proto_runtime_objs) $(library) Makefile
$(cxx) $(ldflags) -o $@ $(proto_runtime_objs) $(library) $(libs)
#$(CATCH2GCH): $(VENDORDIR)/catch2/catch.hpp Makefile
## Precompilation of rather hefty catch2 header
# $(CXX) -fPIC -D CATCH_CONFIG_MAIN $(CXXFLAGS) -c $< $(INCLUDES) -o $@
$(test): $(test_objs) $(library)
# Filtering out entry-point as catch2 provide its own.
# Alternatively recompile all proto_objs with -DPROTO_MAIN=0
$(cxx) $(ldflags) -o $@ $(library) $(test_objs) $(libs)
$(proto_runtime_objs): $(obj_dir)/%.o: $(src_dir)/%.cc
mkdir -p $(dir $@)
$(cxx) -fPIC $(cxxflags) $(cppflags) -MMD -MP -c $< $(includes) -o $@
$(proto_lib_objs): $(obj_dir)/%.o: $(src_dir)/%.cc
mkdir -p $(dir $@)
$(cxx) -fPIC $(cxxflags) $(cppflags) -MMD -MP -c $< $(includes) -o $@
# because ar is stupid
mkdir -p $(ar_obj_dir)
$(eval ar_obj_name := $(ar_obj_dir)/$(subst /,_,$(@:$(obj_dir)/%.o=%.o)))
cp $@ $(ar_obj_name)
$(test_objs): $(obj_dir)/test/%.o: $(test_src_dir)/proto/%.cc
$(call makedir, $(dir $@))
$(cxx) -fPIC $(cxxflags) $(cppflags) -MMD -MP -c $< $(includes) -o $@
#obj/gl3w.o: vendor/gl3w/src/gl3w.c
# $(CXX) -fPIC $(CXXFLAGS) -MMD -MP -c $< $(INCLUDES) -o $@
# cp $@ $(AROBJDIR)/$@
# TODO(kacper): auto downloading this stuff?
# doesnt everybody have this alredy in /usr/lib?
#$(GL_EXT): $(GL_EXT_DIR)/%.h: Makefile
# $(call makedir, $(GL_EXT_DIR))
# $(call if-file-not-exist, $@, \
# echo fetching $(notdir $@))
# $(call if-file-not-exist, $@, \
# wget $(GL_EXT_REPO_URL)/$(notdir $@) -O $@)
.PHONY: clean
clean: Makefile
$(call remove, $(runtime) $(proto_objs) \
$(proto_ar_objs) $(proto_deps) $(TESTOBJS) \
$(client_objs) $(client_deps) $(CATCH2GCH) $(OBJDIR)/glfw3.o)
-include $(proto_deps)
-include $(client_deps)