blob: 7c58c879feb1e8e917219c712ca875a7d7f051a4 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001# GRPC global makefile
2# This currently builds C and C++ code.
3<%!
4 from copy import deepcopy
5 import re
6
7 def excluded(filename, exclude_res):
8 for r in exclude_res:
9 if r.match(filename):
10 return True
11 return False
12%>
13
14<%
15 altlibs = []
16 for lib in libs:
17 for alt in lib.get('alternates', []):
18 new = deepcopy(lib)
19 new.name = alt.name
20 new.alternates = []
21 exclude_res = [re.compile(str) for str in alt.get('exclude_res', [])]
22
23 src = [file for file in new.get('src', []) if not excluded(file, exclude_res)]
24 src.extend(alt.get('include_src', []))
25 new.src = src
26
27 headers = [file for file in new.get('headers', []) if not excluded(file, exclude_res)]
28 headers.extend(alt.get('include_headers', []))
29 new.headers = headers
30
31 public_headers = [file for file in new.get('public_headers', []) if not excluded(file, exclude_res)]
32 public_headers.extend(alt.get('include_public_headers', []))
33 new.public_headers = public_headers
34
35 for prop in alt.properties:
36 new[prop.name] = prop.value
37
38 altlibs.append(new)
39 libs.extend(altlibs)
40
41 protos_dict = {}
42 proto_re = re.compile('\.proto$')
43 for lib in libs:
44 for src in lib.src:
45 if proto_re.match(src):
46 protos_dict[src] = True
47 for tgt in targets:
48 for src in tgt.src:
49 if proto_re.match(src):
50 protos_dict[src] = True
51
52 protos = []
53 for k, v in protos_dict:
54 protos.append(k)
55%>
56
57# General settings.
58# You may want to change these depending on your system.
59
60prefix ?= /usr/local
61
62PROTOC = protoc
63CC = gcc
64CXX = g++
65LD = gcc
66LDXX = g++
67AR = ar
68STRIP = strip --strip-unneeded
69INSTALL = install -D
70RM = rm -f
71
72ifeq ($(DEBUG),)
73CPPFLAGS += -O2
74DEFINES += NDEBUG
75else
76CPPFLAGS += -O0
77DEFINES += _DEBUG DEBUG
78endif
79
80CFLAGS += -std=c89 -pedantic
81CXXFLAGS += -std=c++11
82CPPFLAGS += -g -fPIC -Wall -Werror -Wno-long-long
83LDFLAGS += -g -pthread -fPIC
84
85INCLUDES = . include gens
86LIBS = rt m z event event_pthreads pthread
87LIBSXX = protobuf
88LIBS_SECURE = ssl crypto dl
89
90ifneq ($(wildcard /usr/src/gtest/src/gtest-all.cc),)
91GTEST_LIB = /usr/src/gtest/src/gtest-all.cc -I/usr/src/gtest
92else
93GTEST_LIB = -lgtest
94endif
chenwa8fd44a2014-12-10 15:13:55 -080095GTEST_LIB += -lgflags
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096ifeq ($(V),1)
97E = @:
98Q =
99else
100E = @echo
101Q = @
102endif
103
104VERSION = ${settings.version.major}.${settings.version.minor}.${settings.version.micro}.${settings.version.build}
105
106CPPFLAGS_NO_ARCH += $(addprefix -I, $(INCLUDES)) $(addprefix -D, $(DEFINES))
107CPPFLAGS += $(CPPFLAGS_NO_ARCH) $(ARCH_FLAGS)
108
109LDFLAGS += $(ARCH_FLAGS)
110LDLIBS += $(addprefix -l, $(LIBS))
111LDLIBSXX += $(addprefix -l, $(LIBSXX))
112LDLIBS_SECURE += $(addprefix -l, $(LIBS_SECURE))
113
114.SECONDARY = %.pb.h %.pb.cc
115
116all: static shared\
117% for tgt in targets:
118% if tgt.build == 'all':
119 bins/${tgt.name}\
120% endif
121% endfor
122
nnoble29e1d292014-12-01 10:27:40 -0800123static: static_c static_cxx
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124
nnoble85a49262014-12-08 18:14:03 -0800125static_c: dep_c\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126% for lib in libs:
nnoble29e1d292014-12-01 10:27:40 -0800127% if lib.build == 'all' and not lib.get('c++', False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128 libs/lib${lib.name}.a\
129% endif
130% endfor
131
132
nnoble85a49262014-12-08 18:14:03 -0800133static_cxx: dep_cxx\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800134% for lib in libs:
nnoble29e1d292014-12-01 10:27:40 -0800135% if lib.build == 'all' and lib.get('c++', False):
136 libs/lib${lib.name}.a\
137% endif
138% endfor
139
140
141shared: shared_c shared_cxx
142
nnoble85a49262014-12-08 18:14:03 -0800143shared_c: dep_c\
nnoble29e1d292014-12-01 10:27:40 -0800144% for lib in libs:
145% if lib.build == 'all' and not lib.get('c++', False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 libs/lib${lib.name}.so.$(VERSION)\
147% endif
148% endfor
149
150
nnoble85a49262014-12-08 18:14:03 -0800151shared_cxx: dep_cxx\
nnoble29e1d292014-12-01 10:27:40 -0800152% for lib in libs:
153% if lib.build == 'all' and lib.get('c++', False):
154 libs/lib${lib.name}.so.$(VERSION)\
155% endif
156% endfor
157
158
159privatelibs: privatelibs_c privatelibs_cxx
160
nnoble85a49262014-12-08 18:14:03 -0800161privatelibs_c: dep_c\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162% for lib in libs:
163% if lib.build == 'private':
164 libs/lib${lib.name}.a\
165% endif
166% endfor
167
168
nnoble85a49262014-12-08 18:14:03 -0800169privatelibs_cxx: dep_cxx\
nnoble29e1d292014-12-01 10:27:40 -0800170% for lib in libs:
171% if lib.build == 'private':
172 libs/lib${lib.name}.a\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173% endif
174% endfor
175
176
nnoble29e1d292014-12-01 10:27:40 -0800177buildtests: buildtests_c buildtests_cxx
178
179buildtests_c: privatelibs_c\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180% for tgt in targets:
181% if tgt.build == 'test' and not tgt.get('c++', False):
182 bins/${tgt.name}\
183% endif
184% endfor
185
186
nnoble29e1d292014-12-01 10:27:40 -0800187buildtests_cxx: privatelibs_cxx\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188% for tgt in targets:
nnoble29e1d292014-12-01 10:27:40 -0800189% if tgt.build == 'test' and tgt.get('c++', False):
190 bins/${tgt.name}\
191% endif
192% endfor
193
194
nnoble85a49262014-12-08 18:14:03 -0800195test: test_c test_cxx
nnoble29e1d292014-12-01 10:27:40 -0800196
nnoble85a49262014-12-08 18:14:03 -0800197test_c: buildtests_c
nnoble29e1d292014-12-01 10:27:40 -0800198% for tgt in targets:
199% if tgt.build == 'test' and tgt.get('run', True) and not tgt.get('c++', False):
200 $(E) "[RUN] Testing ${tgt.name}"
201 $(Q) ./bins/${tgt.name} || ( echo test ${tgt.name} failed ; exit 1 )
202% endif
203% endfor
204
205
nnoble85a49262014-12-08 18:14:03 -0800206test_cxx: buildtests_cxx
nnoble29e1d292014-12-01 10:27:40 -0800207% for tgt in targets:
208% if tgt.build == 'test' and tgt.get('run', True) and tgt.get('c++', False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 $(E) "[RUN] Testing ${tgt.name}"
210 $(Q) ./bins/${tgt.name} || ( echo test ${tgt.name} failed ; exit 1 )
211% endif
212% endfor
213
214
215tools: privatelibs\
216% for tgt in targets:
217% if tgt.build == 'tool':
218 bins/${tgt.name}\
219% endif
220% endfor
221
222
nnobleebebb7e2014-12-10 16:31:01 -0800223protoc_plugins:\
224% for tgt in targets:
225% if tgt.build == 'protoc':
226 bins/${tgt.name}\
227% endif
228% endfor
229
230
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231buildbenchmarks: privatelibs\
232% for tgt in targets:
233% if tgt.build == 'benchmark':
234 bins/${tgt.name}\
235% endif
236% endfor
237
238
239benchmarks: buildbenchmarks
240
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800241strip: strip-static strip-shared
242
nnoble85a49262014-12-08 18:14:03 -0800243strip-static_c: static_c
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244% for lib in libs:
nnoble85a49262014-12-08 18:14:03 -0800245% if not lib.get("c++", False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246% if lib.build == "all":
247 $(E) "[STRIP] Stripping lib${lib.name}.a"
248 $(Q) $(STRIP) libs/lib${lib.name}.a
249% endif
nnoble85a49262014-12-08 18:14:03 -0800250% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251% endfor
252
nnoble85a49262014-12-08 18:14:03 -0800253strip-static_cxx: static_cxx
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800254% for lib in libs:
nnoble85a49262014-12-08 18:14:03 -0800255% if lib.get("c++", False):
256% if lib.build == "all":
257 $(E) "[STRIP] Stripping lib${lib.name}.a"
258 $(Q) $(STRIP) libs/lib${lib.name}.a
259% endif
260% endif
261% endfor
262
263strip-shared_c: shared_c
264% for lib in libs:
265% if not lib.get("c++", False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800266% if lib.build == "all":
267 $(E) "[STRIP] Stripping lib${lib.name}.so"
268 $(Q) $(STRIP) libs/lib${lib.name}.so.$(VERSION)
269% endif
nnoble85a49262014-12-08 18:14:03 -0800270% endif
271% endfor
272
273strip-shared_cxx: shared_cxx
274% for lib in libs:
275% if lib.get("c++", False):
276% if lib.build == "all":
277 $(E) "[STRIP] Stripping lib${lib.name}.so"
278 $(Q) $(STRIP) libs/lib${lib.name}.so.$(VERSION)
279% endif
280% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281% endfor
282
283gens/%.pb.cc : %.proto
284 $(E) "[PROTOC] Generating protobuf CC file from $<"
285 $(Q) mkdir -p `dirname $@`
286 $(Q) $(PROTOC) --cpp_out=gens $<
287
288deps/%.dep : %.c
289 $(E) "[DEP] Generating dependencies for $<"
290 $(Q) mkdir -p `dirname $@`
291 $(Q) $(CC) $(CFLAGS) $(CPPFLAGS_NO_ARCH) -MG -M $< > $@
292
293deps/%.dep : gens/%.pb.cc
294 $(E) "[DEP] Generating dependencies for $<"
295 $(Q) mkdir -p `dirname $@`
296 $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS_NO_ARCH) -MG -M $< > $@
297
298deps/%.dep : %.cc
299 $(E) "[DEP] Generating dependencies for $<"
300 $(Q) mkdir -p `dirname $@`
301 $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS_NO_ARCH) -MG -M $< > $@
302
303objs/%.o : %.c
304 $(E) "[C] Compiling $<"
305 $(Q) mkdir -p `dirname $@`
306 $(Q) $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
307
308objs/%.o : gens/%.pb.cc
309 $(E) "[CXX] Compiling $<"
310 $(Q) mkdir -p `dirname $@`
311 $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
312
313objs/%.o : %.cc
314 $(E) "[CXX] Compiling $<"
315 $(Q) mkdir -p `dirname $@`
316 $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
317
nnoble0c475f02014-12-05 15:37:39 -0800318dep: dep_c dep_cxx
319
320dep_c:\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321% for lib in libs:
nnoble0c475f02014-12-05 15:37:39 -0800322% if not lib.get('c++', False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800323 deps_lib${lib.name}\
nnoble0c475f02014-12-05 15:37:39 -0800324% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325% endfor
326% for tgt in targets:
nnoble0c475f02014-12-05 15:37:39 -0800327% if not tgt.get('c++', False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800328 deps_${tgt.name}\
nnoble0c475f02014-12-05 15:37:39 -0800329% endif
330% endfor
331
332
333dep_cxx:\
334% for lib in libs:
335% if lib.get('c++', False):
336 deps_lib${lib.name}\
337% endif
338% endfor
339% for tgt in targets:
340% if tgt.get('c++', False):
341 deps_${tgt.name}\
342% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343% endfor
344
345
nnoble85a49262014-12-08 18:14:03 -0800346install: install_c install_cxx
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800347
nnoble85a49262014-12-08 18:14:03 -0800348install_c: install-headers_c install-static_c install-shared_c
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800349
nnoble85a49262014-12-08 18:14:03 -0800350install_cxx: install-headers_cxx install-static_cxx install-shared_cxx
351
352install-headers: install-headers_c install-headers_cxx
353
354install-headers_c:
355 $(E) "[INSTALL] Installing public C headers"
356 $(Q) $(foreach h, $(PUBLIC_HEADERS_C), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1
357
358install-headers_cxx:
359 $(E) "[INSTALL] Installing public C++ headers"
360 $(Q) $(foreach h, $(PUBLIC_HEADERS_CXX), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1
361
362install-static: install-static_c install-static_cxx
363
364install-static_c: static_c strip-static_c
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365% for lib in libs:
nnoble85a49262014-12-08 18:14:03 -0800366% if not lib.get("c++", False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800367% if lib.build == "all":
368 $(E) "[INSTALL] Installing lib${lib.name}.a"
369 $(Q) $(INSTALL) libs/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
370% endif
nnoble85a49262014-12-08 18:14:03 -0800371% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800372% endfor
373
nnoble85a49262014-12-08 18:14:03 -0800374install-static_cxx: static_cxx strip-static_cxx
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800375% for lib in libs:
nnoble85a49262014-12-08 18:14:03 -0800376% if lib.get("c++", False):
377% if lib.build == "all":
378 $(E) "[INSTALL] Installing lib${lib.name}.a"
379 $(Q) $(INSTALL) libs/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
380% endif
381% endif
382% endfor
383
384install-shared_c: shared_c strip-shared_c
385% for lib in libs:
386% if not lib.get("c++", False):
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800387% if lib.build == "all":
388 $(E) "[INSTALL] Installing lib${lib.name}.so"
389 $(Q) $(INSTALL) libs/lib${lib.name}.so.$(VERSION) $(prefix)/lib/lib${lib.name}.so.$(VERSION)
390% endif
nnoble85a49262014-12-08 18:14:03 -0800391% endif
392% endfor
393
394install-shared_cxx: shared_cxx strip-shared_cxx
395% for lib in libs:
396% if lib.get("c++", False):
397% if lib.build == "all":
398 $(E) "[INSTALL] Installing lib${lib.name}.so"
399 $(Q) $(INSTALL) libs/lib${lib.name}.so.$(VERSION) $(prefix)/lib/lib${lib.name}.so.$(VERSION)
400% endif
401% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800402% endfor
403
404clean:\
405% for lib in libs:
406 clean_lib${lib.name}\
407% endfor
408% for tgt in targets:
409 clean_${tgt.name}\
410% endfor
411
412 $(Q) $(RM) -r deps objs libs bins gens
413
414
415# The various libraries
416
417% for lib in libs:
418${makelib(lib)}
419% endfor
420
421
422# All of the test targets
423
424% for tgt in targets:
425${maketarget(tgt)}
426% endfor
427
428<%def name="makelib(lib)">
429LIB${lib.name.upper()}_SRC = \\
430
431% for src in lib.src:
432 ${src} \\
433
434% endfor
435
436% if "public_headers" in lib:
nnoble85a49262014-12-08 18:14:03 -0800437% if lib.get("c++", False):
438PUBLIC_HEADERS_CXX += \\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800439
nnoble85a49262014-12-08 18:14:03 -0800440% else:
441PUBLIC_HEADERS_C += \\
442
443% endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800444% for hdr in lib.public_headers:
445 ${hdr} \\
446
447% endfor
448% endif
449
450LIB${lib.name.upper()}_OBJS = $(addprefix objs/, $(addsuffix .o, $(basename $(LIB${lib.name.upper()}_SRC))))
451LIB${lib.name.upper()}_DEPS = $(addprefix deps/, $(addsuffix .dep, $(basename $(LIB${lib.name.upper()}_SRC))))
452
453libs/lib${lib.name}.a: $(LIB${lib.name.upper()}_OBJS)
454 $(E) "[AR] Creating $@"
nnoble85a49262014-12-08 18:14:03 -0800455 $(Q) mkdir -p `dirname $@`
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800456 $(Q) $(AR) rcs libs/lib${lib.name}.a $(LIB${lib.name.upper()}_OBJS)
457
458% if lib.build == "all":
459libs/lib${lib.name}.so.$(VERSION): $(LIB${lib.name.upper()}_OBJS)
460 $(E) "[LD] Linking $@"
nnoble85a49262014-12-08 18:14:03 -0800461 $(Q) mkdir -p `dirname $@`
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800462% if lib.get('c++', False):
463 $(Q) $(LDXX) $(LDFLAGS) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} \
464% else:
465 $(Q) $(LD) $(LDFLAGS) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} \
466% endif
467-o libs/lib${lib.name}.so.$(VERSION) $(LIB${lib.name.upper()}_OBJS) $(LDLIBS)\
468% if lib.secure:
469 $(LDLIBS_SECURE)
470% endif
471% endif
472
473
474deps_lib${lib.name}: $(LIB${lib.name.upper()}_DEPS)
475
476ifneq ($(MAKECMDGOALS),clean)
477-include $(LIB${lib.name.upper()}_DEPS)
478endif
479
480clean_lib${lib.name}:
481 $(E) "[CLEAN] Cleaning lib${lib.name} files"
482 $(Q) $(RM) $(LIB${lib.name.upper()}_OBJS)
483 $(Q) $(RM) $(LIB${lib.name.upper()}_DEPS)
484 $(Q) $(RM) libs/lib${lib.name}.a
485 $(Q) $(RM) libs/lib${lib.name}.so.$(VERSION)
486</%def>
487
488<%def name="maketarget(tgt)">
489${tgt.name.upper()}_SRC = \\
490
491% for src in tgt.src:
492 ${src} \\
493
494% endfor
495
496${tgt.name.upper()}_OBJS = $(addprefix objs/, $(addsuffix .o, $(basename $(${tgt.name.upper()}_SRC))))
497${tgt.name.upper()}_DEPS = $(addprefix deps/, $(addsuffix .dep, $(basename $(${tgt.name.upper()}_SRC))))
498
499bins/${tgt.name}: $(${tgt.name.upper()}_OBJS)\
500% for dep in tgt.deps:
501 libs/lib${dep}.a\
502% endfor
503
504 $(E) "[LD] Linking $@"
nnoble85a49262014-12-08 18:14:03 -0800505 $(Q) mkdir -p `dirname $@`
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800506% if tgt.get("c++", False):
507 $(Q) $(LDXX) $(LDFLAGS) $(${tgt.name.upper()}_OBJS) $(GTEST_LIB) -Llibs\
508% else:
509 $(Q) $(LD) $(LDFLAGS) $(${tgt.name.upper()}_OBJS) -Llibs\
510% endif
511% for dep in tgt.deps:
512 -l${dep}\
513% endfor
514% if tgt.get("c++", False):
515 $(LDLIBSXX)\
516% endif
517 $(LDLIBS)\
518% if tgt.get('secure', True):
519 $(LDLIBS_SECURE)\
520% endif
521 -o bins/${tgt.name}
522
523deps_${tgt.name}: $(${tgt.name.upper()}_DEPS)
524
525ifneq ($(MAKECMDGOALS),clean)
526-include $(${tgt.name.upper()}_DEPS)
527endif
528
529clean_${tgt.name}:
530 $(E) "[CLEAN] Cleaning ${tgt.name} files"
531 $(Q) $(RM) $(${tgt.name.upper()}_OBJS)
532 $(Q) $(RM) $(${tgt.name.upper()}_DEPS)
533 $(Q) $(RM) bins/${tgt.name}
534</%def>
535
nnoble85a49262014-12-08 18:14:03 -0800536.PHONY: all strip tools \
537buildtests buildtests_c buildtests_cxx \
538test test_c test_cxx \
539install install_c install_cxx \
540install-headers install-headers_c install-headers_cxx \
541install-shared install-shared_c install-shared_cxx \
542install-static install-static_c install-static_cxx \
543strip strip-shared strip-static \
544strip_c strip-shared_c strip-static_c \
545strip_cxx strip-shared_cxx strip-static_cxx \
546clean\
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800547% for lib in libs:
548 deps_lib${lib.name} clean_lib${lib.name}\
549% endfor
550% for tgt in targets:
551 deps_${tgt.name} clean_${tgt.name}\
552% endfor
553