aboutsummaryrefslogtreecommitdiffhomepage
path: root/templates/Makefile.template
blob: b9ae217054ee5c2158719d7a231590be56af3024 (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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# GRPC global makefile
# This currently builds C and C++ code.
<%!
  import re
  import os

  proto_re = re.compile('(.*)\\.proto')

  def excluded(filename, exclude_res):
    for r in exclude_res:
      if r.match(filename):
        return True
    return False

  def proto_to_cc(filename):
    m = proto_re.match(filename)
    if not m:
      return filename
    return 'gens/' + m.group(1) + '.pb.cc'
%>


# Basic platform detection
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
ifeq ($(SYSTEM),)
SYSTEM = $(HOST_SYSTEM)
endif


# Configurations

VALID_CONFIG_opt = 1
CC_opt = gcc
CXX_opt = g++
LD_opt = gcc
LDXX_opt = g++
CPPFLAGS_opt = -O2
LDFLAGS_opt =
DEFINES_opt = NDEBUG

VALID_CONFIG_dbg = 1
CC_dbg = gcc
CXX_dbg = g++
LD_dbg = gcc
LDXX_dbg = g++
CPPFLAGS_dbg = -O0
LDFLAGS_dbg =
DEFINES_dbg = _DEBUG DEBUG

VALID_CONFIG_valgrind = 1
REQUIRE_CUSTOM_LIBRARIES_valgrind = 1
CC_valgrind = gcc
CXX_valgrind = g++
LD_valgrind = gcc
LDXX_valgrind = g++
CPPFLAGS_valgrind = -O0
OPENSSL_CFLAGS_valgrind = -DPURIFY
LDFLAGS_valgrind =
DEFINES_valgrind = _DEBUG DEBUG

VALID_CONFIG_tsan = 1
REQUIRE_CUSTOM_LIBRARIES_tsan = 1
CC_tsan = clang
CXX_tsan = clang++
LD_tsan = clang
LDXX_tsan = clang++
CPPFLAGS_tsan = -O1 -fsanitize=thread -fno-omit-frame-pointer
OPENSSL_CONFIG_tsan = no-asm
LDFLAGS_tsan = -fsanitize=thread
DEFINES_tsan = NDEBUG

VALID_CONFIG_asan = 1
REQUIRE_CUSTOM_LIBRARIES_asan = 1
CC_asan = clang
CXX_asan = clang++
LD_asan = clang
LDXX_asan = clang++
CPPFLAGS_asan = -O1 -fsanitize=address -fno-omit-frame-pointer
OPENSSL_CONFIG_asan = no-asm
LDFLAGS_asan = -fsanitize=address
DEFINES_asan = NDEBUG

VALID_CONFIG_msan = 1
REQUIRE_CUSTOM_LIBRARIES_msan = 1
CC_msan = clang
CXX_msan = clang++
LD_msan = clang
LDXX_msan = clang++
CPPFLAGS_msan = -O1 -fsanitize=memory -fno-omit-frame-pointer
OPENSSL_CFLAGS_msan = -DPURIFY
OPENSSL_CONFIG_msan = no-asm
LDFLAGS_msan = -fsanitize=memory
DEFINES_msan = NDEBUG

VALID_CONFIG_ubsan = 1
REQUIRE_CUSTOM_LIBRARIES_ubsan = 1
CC_ubsan = clang
CXX_ubsan = clang++
LD_ubsan = clang
LDXX_ubsan = clang++
CPPFLAGS_ubsan = -O1 -fsanitize=undefined -fno-omit-frame-pointer
OPENSSL_CFLAGS_ubsan = -DPURIFY
OPENSSL_CONFIG_ubsan = no-asm
LDFLAGS_ubsan = -fsanitize=undefined
DEFINES_ubsan = NDEBUG

VALID_CONFIG_gcov = 1
CC_gcov = gcc
CXX_gcov = g++
LD_gcov = gcc
LDXX_gcov = g++
CPPFLAGS_gcov = -O0 -fprofile-arcs -ftest-coverage
LDFLAGS_gcov = -fprofile-arcs -ftest-coverage
DEFINES_gcov = NDEBUG


# General settings.
# You may want to change these depending on your system.

prefix ?= /usr/local

PROTOC = protoc
CONFIG ?= opt
CC = $(CC_$(CONFIG))
CXX = $(CXX_$(CONFIG))
LD = $(LD_$(CONFIG))
LDXX = $(LDXX_$(CONFIG))
AR = ar
STRIP = strip --strip-unneeded
INSTALL = install -D
RM = rm -f

ifndef VALID_CONFIG_$(CONFIG)
$(error Invalid CONFIG value '$(CONFIG)')
endif


# The HOST compiler settings are used to compile the protoc plugins.
# In most cases, you won't have to change anything, but if you are
# cross-compiling, you can override these variables from GNU make's
# command line: make CC=cross-gcc HOST_CC=gcc

HOST_CC = $(CC)
HOST_CXX = $(CXX)
HOST_LD = $(LD)
HOST_LDXX = $(LDXX)

CPPFLAGS += $(CPPFLAGS_$(CONFIG))
DEFINES += $(DEFINES_$(CONFIG))
LDFLAGS += $(LDFLAGS_$(CONFIG))

CFLAGS += -std=c89 -pedantic
CXXFLAGS += -std=c++11
CPPFLAGS += -g -fPIC -Wall -Wextra -Werror -Wno-long-long -Wno-unused-parameter
LDFLAGS += -g -fPIC

INCLUDES = . include gens
ifeq ($(SYSTEM),Darwin)
LIBS = m z
else
LIBS = rt m z pthread
LDFLAGS += -pthread
endif

ifneq ($(wildcard /usr/src/gtest/src/gtest-all.cc),)
GTEST_LIB = /usr/src/gtest/src/gtest-all.cc -I/usr/src/gtest
else
GTEST_LIB = -lgtest
endif
GTEST_LIB += -lgflags
ifeq ($(V),1)
E = @:
Q =
else
E = @echo
Q = @
endif

VERSION = ${settings.version.major}.${settings.version.minor}.${settings.version.micro}.${settings.version.build}

CPPFLAGS_NO_ARCH += $(addprefix -I, $(INCLUDES)) $(addprefix -D, $(DEFINES))
CPPFLAGS += $(CPPFLAGS_NO_ARCH) $(ARCH_FLAGS)

LDFLAGS += $(ARCH_FLAGS)
LDLIBS += $(addprefix -l, $(LIBS))
LDLIBSXX += $(addprefix -l, $(LIBSXX))

HOST_CPPFLAGS = $(CPPFLAGS)
HOST_CFLAGS = $(CFLAGS)
HOST_CXXFLAGS = $(CXXFLAGS)
HOST_LDFLAGS = $(LDFLAGS)
HOST_LDLIBS = $(LDLIBS)


# These are automatically computed variables.
# There shouldn't be any need to change anything from now on.

ifeq ($(SYSTEM),MINGW32)
SHARED_EXT = dll
endif
ifeq ($(SYSTEM),Darwin)
SHARED_EXT = dylib
endif
ifeq ($(SHARED_EXT),)
SHARED_EXT = so.$(VERSION)
endif

ifeq ($(wildcard .git),)
IS_GIT_FOLDER = false
else
IS_GIT_FOLDER = true
endif

OPENSSL_ALPN_CHECK_CMD = $(CC) $(CFLAGS) $(CPPFLAGS) -o /dev/null test/build/openssl-alpn.c -lssl -lcrypto -ldl $(LDFLAGS)
ZLIB_CHECK_CMD = $(CC) $(CFLAGS) $(CPPFLAGS) -o /dev/null test/build/zlib.c -lz $(LDFLAGS)
PERFTOOLS_CHECK_CMD = $(CC) $(CFLAGS) $(CPPFLAGS) -o /dev/null test/build/perftools.c -lprofiler $(LDFLAGS)
PROTOBUF_CHECK_CMD = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -o /dev/null test/build/protobuf.cc -lprotobuf $(LDFLAGS)
PROTOC_CHECK_CMD = protoc --version | grep -q libprotoc.3

ifndef REQUIRE_CUSTOM_LIBRARIES_$(CONFIG)
HAS_SYSTEM_PERFTOOLS = $(shell $(PERFTOOLS_CHECK_CMD) 2> /dev/null && echo true || echo false)
ifeq ($(HAS_SYSTEM_PERFTOOLS),true)
DEFINES += GRPC_HAVE_PERFTOOLS
LIBS += profiler
endif
endif

ifndef REQUIRE_CUSTOM_LIBRARIES_$(CONFIG)
HAS_SYSTEM_OPENSSL_ALPN = $(shell $(OPENSSL_ALPN_CHECK_CMD) 2> /dev/null && echo true || echo false)
HAS_SYSTEM_ZLIB = $(shell $(ZLIB_CHECK_CMD) 2> /dev/null && echo true || echo false)
HAS_SYSTEM_PROTOBUF = $(shell $(PROTOBUF_CHECK_CMD) 2> /dev/null && echo true || echo false)
else
# override system libraries if the config requires a custom compiled library
HAS_SYSTEM_OPENSSL_ALPN = false
HAS_SYSTEM_ZLIB = false
HAS_SYSTEM_PROTOBUF = false
endif

HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false)

ifeq ($(wildcard third_party/openssl/ssl/ssl.h),)
HAS_EMBEDDED_OPENSSL_ALPN = false
else
HAS_EMBEDDED_OPENSSL_ALPN = true
endif

ifeq ($(wildcard third_party/zlib/zlib.h),)
HAS_EMBEDDED_ZLIB = false
else
HAS_EMBEDDED_ZLIB = true
endif

ifeq ($(wildcard third_party/protobuf/src/google/protobuf/descriptor.pb.h),)
HAS_EMBEDDED_PROTOBUF = false
ifneq ($(HAS_VALID_PROTOC),true)
NO_PROTOC = true
endif
else
HAS_EMBEDDED_PROTOBUF = true
endif

ifeq ($(HAS_SYSTEM_ZLIB),false)
ifeq ($(HAS_EMBEDDED_ZLIB),true)
ZLIB_DEP = libs/$(CONFIG)/zlib/libz.a
CPPFLAGS += -Ithird_party/zlib
LDFLAGS += -Llibs/$(CONFIG)/zlib
else
DEP_MISSING += zlib
endif
endif

ifeq ($(HAS_SYSTEM_OPENSSL_ALPN),false)
ifeq ($(HAS_EMBEDDED_OPENSSL_ALPN),true)
OPENSSL_DEP = libs/$(CONFIG)/openssl/libssl.a
OPENSSL_MERGE_LIBS += libs/$(CONFIG)/openssl/libssl.a libs/$(CONFIG)/openssl/libcrypto.a
CPPFLAGS += -Ithird_party/openssl/include
LDFLAGS += -Llibs/$(CONFIG)/openssl
LIBS_SECURE = dl
else
NO_SECURE = true
endif
else
LIBS_SECURE = ssl crypto dl
endif

LDLIBS_SECURE += $(addprefix -l, $(LIBS_SECURE))

ifeq ($(HAS_SYSTEM_PROTOBUF),false)
ifeq ($(HAS_EMBEDDED_PROTOBUF),true)
PROTOBUF_DEP = libs/$(CONFIG)/protobuf/libprotobuf.a
CPPFLAGS += -Ithird_party/protobuf/src
LDFLAGS += -Llibs/$(CONFIG)/protobuf
PROTOC = bins/$(CONFIG)/protobuf/protoc
else
NO_PROTOBUF = true
endif
else
endif

LIBS_PROTOBUF = protobuf
LIBS_PROTOC = protoc protobuf

LDLIBS_PROTOBUF += $(addprefix -l, $(LIBS_PROTOBUF))
HOST_LDLIBS_PROTOC += $(addprefix -l, $(LIBS_PROTOC))

ifeq ($(MAKECMDGOALS),clean)
NO_DEPS = true
endif

.SECONDARY = %.pb.h %.pb.cc

PROTOC_PLUGINS=\
% for tgt in targets:
% if tgt.build == 'protoc':
 bins/$(CONFIG)/${tgt.name}\
% endif
% endfor

ifeq ($(DEP_MISSING),)
all: static shared\
% for tgt in targets:
% if tgt.build == 'all':
 bins/$(CONFIG)/${tgt.name}\
% endif
% endfor

dep_error:
	@echo "You shouldn't see this message - all of your dependencies are correct."
else
all: dep_error git_update stop

dep_error:
	@echo
	@echo "DEPENDENCY ERROR"
	@echo
	@echo "You are missing system dependencies that are essential to build grpc,"
	@echo "and the third_party directory doesn't have them:"
	@echo
	@echo "  $(DEP_MISSING)"
	@echo
	@echo "Installing the development packages for your system will solve"
	@echo "this issue. Please consult INSTALL to get more information."
	@echo
	@echo "If you need information about why these tests failed, run:"
	@echo
	@echo "  make run_dep_checks"
	@echo
endif

git_update:
ifeq ($(IS_GIT_FOLDER),true)
	@echo "Additionally, since you are in a git clone, you can download the"
	@echo "missing dependencies in third_party by running the following command:"
	@echo
	@echo "  git submodule update --init"
	@echo
endif

openssl_dep_error: openssl_dep_message git_update stop

protobuf_dep_error: protobuf_dep_message git_update stop

protoc_dep_error: protoc_dep_message git_update stop

openssl_dep_message:
	@echo
	@echo "DEPENDENCY ERROR"
	@echo
	@echo "The target you are trying to run requires OpenSSL with ALPN support."
	@echo "Your system doesn't have it, and neither does the third_party directory."
	@echo
	@echo "Please consult INSTALL to get more information."
	@echo
	@echo "If you need information about why these tests failed, run:"
	@echo
	@echo "  make run_dep_checks"
	@echo

protobuf_dep_message:
	@echo
	@echo "DEPENDENCY ERROR"
	@echo
	@echo "The target you are trying to run requires protobuf 3.0.0+"
	@echo "Your system doesn't have it, and neither does the third_party directory."
	@echo
	@echo "Please consult INSTALL to get more information."
	@echo
	@echo "If you need information about why these tests failed, run:"
	@echo
	@echo "  make run_dep_checks"
	@echo

protoc_dep_message:
	@echo
	@echo "DEPENDENCY ERROR"
	@echo
	@echo "The target you are trying to run requires protobuf-compiler 3.0.0+"
	@echo "Your system doesn't have it, and neither does the third_party directory."
	@echo
	@echo "Please consult INSTALL to get more information."
	@echo
	@echo "If you need information about why these tests failed, run:"
	@echo
	@echo "  make run_dep_checks"
	@echo

stop:
	@false

% for tgt in targets:
${tgt.name}: bins/$(CONFIG)/${tgt.name}
% endfor

run_dep_checks:
	$(OPENSSL_ALPN_CHECK_CMD) || true
	$(ZLIB_CHECK_CMD) || true
	$(PERFTOOLS_CHECK_CMD) || true
	$(PROTOBUF_CHECK_CMD) || true
	$(PROTOC_CHECK_CMD) || true

libs/$(CONFIG)/zlib/libz.a:
	$(E) "[MAKE]    Building zlib"
	$(Q)(cd third_party/zlib ; CC="$(CC)" CFLAGS="-fPIC -fvisibility=hidden $(CPPFLAGS_$(CONFIG))" ./configure --static)
	$(Q)$(MAKE) -C third_party/zlib clean
	$(Q)$(MAKE) -C third_party/zlib
	$(Q)mkdir -p libs/$(CONFIG)/zlib
	$(Q)cp third_party/zlib/libz.a libs/$(CONFIG)/zlib

libs/$(CONFIG)/openssl/libssl.a:
	$(E) "[MAKE]    Building openssl for $(SYSTEM)"
ifeq ($(SYSTEM),Darwin)
	$(Q)(cd third_party/openssl ; CC="$(CC) -fPIC -fvisibility=hidden $(CPPFLAGS_$(CONFIG)) $(OPENSSL_CFLAGS_$(CONFIG))" ./Configure darwin64-x86_64-cc $(OPENSSL_CONFIG_$(CONFIG)))
else
	$(Q)(cd third_party/openssl ; CC="$(CC) -fPIC -fvisibility=hidden $(CPPFLAGS_$(CONFIG)) $(OPENSSL_CFLAGS_$(CONFIG))" ./config $(OPENSSL_CONFIG_$(CONFIG)))
endif
	$(Q)$(MAKE) -C third_party/openssl clean
	$(Q)$(MAKE) -C third_party/openssl build_crypto build_ssl
	$(Q)mkdir -p libs/$(CONFIG)/openssl
	$(Q)cp third_party/openssl/libssl.a third_party/openssl/libcrypto.a libs/$(CONFIG)/openssl

third_party/protobuf/configure:
	$(E) "[AUTOGEN] Preparing protobuf"
	$(Q)(cd third_party/protobuf ; autoreconf -f -i -Wall,no-obsolete)

libs/$(CONFIG)/protobuf/libprotobuf.a: third_party/protobuf/configure
	$(E) "[MAKE]    Building protobuf"
	$(Q)(cd third_party/protobuf ; CC="$(CC)" CXX="$(CXX)" LDFLAGS="$(LDFLAGS_$(CONFIG)) -g" CXXFLAGS="-DLANG_CXX11 -std=c++11" CPPFLAGS="$(CPPFLAGS_$(CONFIG)) -g" ./configure --disable-shared --enable-static)
	$(Q)$(MAKE) -C third_party/protobuf clean
	$(Q)$(MAKE) -C third_party/protobuf
	$(Q)mkdir -p libs/$(CONFIG)/protobuf
	$(Q)mkdir -p bins/$(CONFIG)/protobuf
	$(Q)cp third_party/protobuf/src/.libs/libprotoc.a libs/$(CONFIG)/protobuf
	$(Q)cp third_party/protobuf/src/.libs/libprotobuf.a libs/$(CONFIG)/protobuf
	$(Q)cp third_party/protobuf/src/protoc bins/$(CONFIG)/protobuf

static: static_c static_cxx

static_c: \
% for lib in libs:
% if lib.build == 'all' and lib.language == 'c':
 libs/$(CONFIG)/lib${lib.name}.a\
% endif
% endfor


static_cxx: \
% for lib in libs:
% if lib.build == 'all' and lib.language == 'c++':
 libs/$(CONFIG)/lib${lib.name}.a\
% endif
% endfor


shared: shared_c shared_cxx

shared_c: \
% for lib in libs:
% if lib.build == 'all' and lib.language == 'c':
 libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)\
% endif
% endfor


shared_cxx: \
% for lib in libs:
% if lib.build == 'all' and lib.language == 'c++':
 libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)\
% endif
% endfor


privatelibs: privatelibs_c privatelibs_cxx

privatelibs_c: \
% for lib in libs:
% if lib.build == 'private' and lib.language == 'c':
 libs/$(CONFIG)/lib${lib.name}.a\
% endif
% endfor


privatelibs_cxx: \
% for lib in libs:
% if lib.build == 'private' and lib.language == 'c++':
 libs/$(CONFIG)/lib${lib.name}.a\
% endif
% endfor


buildtests: buildtests_c buildtests_cxx

buildtests_c: privatelibs_c\
% for tgt in targets:
% if tgt.build == 'test' and not tgt.language == 'c++':
 bins/$(CONFIG)/${tgt.name}\
% endif
% endfor


buildtests_cxx: privatelibs_cxx\
% for tgt in targets:
% if tgt.build == 'test' and tgt.language == 'c++':
 bins/$(CONFIG)/${tgt.name}\
% endif
% endfor


test: test_c test_cxx

test_c: buildtests_c
% for tgt in targets:
% if tgt.build == 'test' and tgt.get('run', True) and not tgt.language == 'c++':
	$(E) "[RUN]     Testing ${tgt.name}"
	$(Q) ./bins/$(CONFIG)/${tgt.name} || ( echo test ${tgt.name} failed ; exit 1 )
% endif
% endfor


test_cxx: buildtests_cxx
% for tgt in targets:
% if tgt.build == 'test' and tgt.get('run', True) and tgt.language == 'c++':
	$(E) "[RUN]     Testing ${tgt.name}"
	$(Q) ./bins/$(CONFIG)/${tgt.name} || ( echo test ${tgt.name} failed ; exit 1 )
% endif
% endfor


tools: privatelibs\
% for tgt in targets:
% if tgt.build == 'tool':
 bins/$(CONFIG)/${tgt.name}\
% endif
% endfor


buildbenchmarks: privatelibs\
% for tgt in targets:
% if tgt.build == 'benchmark':
 bins/$(CONFIG)/${tgt.name}\
% endif
% endfor


benchmarks: buildbenchmarks

strip: strip-static strip-shared

strip-static: strip-static_c strip-static_cxx

strip-shared: strip-shared_c strip-shared_cxx


# TODO(nnoble): the strip target is stripping in-place, instead
# of copying files in a temporary folder.
# This prevents proper debugging after running make install.

strip-static_c: static_c
ifeq ($(CONFIG),opt)
% for lib in libs:
% if lib.language == "c":
% if lib.build == "all":
	$(E) "[STRIP]   Stripping lib${lib.name}.a"
	$(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.a
% endif
% endif
% endfor
endif

strip-static_cxx: static_cxx
ifeq ($(CONFIG),opt)
% for lib in libs:
% if lib.language == "c++":
% if lib.build == "all":
	$(E) "[STRIP]   Stripping lib${lib.name}.a"
	$(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.a
% endif
% endif
% endfor
endif

strip-shared_c: shared_c
ifeq ($(CONFIG),opt)
% for lib in libs:
% if lib.language == "c":
% if lib.build == "all":
	$(E) "[STRIP]   Stripping lib${lib.name}.so"
	$(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)
% endif
% endif
% endfor
endif

strip-shared_cxx: shared_cxx
ifeq ($(CONFIG),opt)
% for lib in libs:
% if lib.language == "c++":
% if lib.build == "all":
	$(E) "[STRIP]   Stripping lib${lib.name}.so"
	$(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)
% endif
% endif
% endfor
endif

% for p in protos:
ifeq ($(NO_PROTOC),true)
gens/${p}.pb.cc: protoc_dep_error
else
gens/${p}.pb.cc: ${p}.proto $(PROTOBUF_DEP) $(PROTOC_PLUGINS)
	$(E) "[PROTOC]  Generating protobuf CC file from $<"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(PROTOC) --cpp_out=gens --grpc_out=gens --plugin=protoc-gen-grpc=bins/$(CONFIG)/cpp_plugin $<
endif

% endfor

objs/$(CONFIG)/%.o : %.c
	$(E) "[C]       Compiling $<"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(CC) $(CFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<

objs/$(CONFIG)/%.o : gens/%.pb.cc
	$(E) "[CXX]     Compiling $<"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<

objs/$(CONFIG)/src/compiler/%.o : src/compiler/%.cc
	$(E) "[HOSTCXX] Compiling $<"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<

objs/$(CONFIG)/%.o : %.cc
	$(E) "[CXX]     Compiling $<"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<


install: install_c install_cxx

install_c: install-headers_c install-static_c install-shared_c

install_cxx: install-headers_cxx install-static_cxx install-shared_cxx

install-headers: install-headers_c install-headers_cxx

install-headers_c:
	$(E) "[INSTALL] Installing public C headers"
	$(Q) $(foreach h, $(PUBLIC_HEADERS_C), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1

install-headers_cxx:
	$(E) "[INSTALL] Installing public C++ headers"
	$(Q) $(foreach h, $(PUBLIC_HEADERS_CXX), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1

install-static: install-static_c install-static_cxx

install-static_c: static_c strip-static_c
% for lib in libs:
% if lib.language == "c":
% if lib.build == "all":
	$(E) "[INSTALL] Installing lib${lib.name}.a"
	$(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
% endif
% endif
% endfor

install-static_cxx: static_cxx strip-static_cxx
% for lib in libs:
% if lib.language == "c++":
% if lib.build == "all":
	$(E) "[INSTALL] Installing lib${lib.name}.a"
	$(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
% endif
% endif
% endfor

install-shared_c: shared_c strip-shared_c
% for lib in libs:
% if lib.language == "c":
% if lib.build == "all":
ifeq ($(SYSTEM),MINGW32)
	$(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
	$(Q) $(INSTALL) libs/$(CONFIG)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
	$(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
else
	$(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
	$(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
ifneq ($(SYSTEM),Darwin)
	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
endif
endif
% endif
% endif
% endfor
ifneq ($(SYSTEM),MINGW32)
ifneq ($(SYSTEM),Darwin)
	$(Q) ldconfig
endif
endif

install-shared_cxx: shared_cxx strip-shared_cxx
% for lib in libs:
% if lib.language == "c++":
% if lib.build == "all":
ifeq ($(SYSTEM),MINGW32)
	$(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
	$(Q) $(INSTALL) libs/$(CONFIG)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
	$(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
else
	$(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
	$(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
ifneq ($(SYSTEM),Darwin)
	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
endif
endif
% endif
% endif
% endfor
ifneq ($(SYSTEM),MINGW32)
ifneq ($(SYSTEM),Darwin)
	$(Q) ldconfig
endif
endif

clean:
	$(Q) $(RM) -rf objs libs bins gens


# The various libraries

% for lib in libs:
${makelib(lib)}
% endfor


# All of the test targets, and protoc plugins

% for tgt in targets:
${maketarget(tgt)}
% endfor

<%def name="makelib(lib)">
LIB${lib.name.upper()}_SRC = \\

% for src in lib.src:
    ${proto_to_cc(src)} \\

% endfor

% if "public_headers" in lib:
% if lib.language == "c++":
PUBLIC_HEADERS_CXX += \\

% else:
PUBLIC_HEADERS_C += \\

% endif
% for hdr in lib.public_headers:
    ${hdr} \\

% endfor
% endif

LIB${lib.name.upper()}_OBJS = $(addprefix objs/$(CONFIG)/, $(addsuffix .o, $(basename $(LIB${lib.name.upper()}_SRC))))

## If the library requires OpenSSL with ALPN, let's add some restrictions.
% if lib.get('secure', True):
ifeq ($(NO_SECURE),true)

# You can't build secure libraries if you don't have OpenSSL with ALPN.

libs/$(CONFIG)/lib${lib.name}.a: openssl_dep_error

% if lib.build == "all":
ifeq ($(SYSTEM),MINGW32)
libs/$(CONFIG)/${lib.name}.$(SHARED_EXT): openssl_dep_error
else
libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT): openssl_dep_error
endif
% endif

else

% if lib.language == 'c++':
ifeq ($(NO_PROTOBUF),true)

# You can't build a C++ library if you don't have protobuf - a bit overreached, but still okay.

libs/$(CONFIG)/lib${lib.name}.a: protobuf_dep_error

% if lib.build == "all":
ifeq ($(SYSTEM),MINGW32)
libs/$(CONFIG)/${lib.name}.$(SHARED_EXT): protobuf_dep_error
else
libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT): protobuf_dep_error
endif
% endif

else
% endif

ifneq ($(OPENSSL_DEP),)
# This is to ensure the embedded OpenSSL is built beforehand, properly
# installing headers to their final destination on the drive. We need this
# otherwise parallel compilation will fail if a source is compiled first.
% for src in lib.src:
${src}: $(OPENSSL_DEP)
% endfor
endif

libs/$(CONFIG)/lib${lib.name}.a: $(ZLIB_DEP) $(OPENSSL_DEP)\
## The else here corresponds to the if secure earlier.
% else:
% if lib.language == 'c++':
ifeq ($(NO_PROTOBUF),true)

# You can't build a C++ library if you don't have protobuf - a bit overreached, but still okay.

libs/$(CONFIG)/lib${lib.name}.a: protobuf_dep_error

% if lib.build == "all":
ifeq ($(SYSTEM),MINGW32)
libs/$(CONFIG)/${lib.name}.$(SHARED_EXT): protobuf_dep_error
else
libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT): protobuf_dep_error
endif
% endif

else

% endif
libs/$(CONFIG)/lib${lib.name}.a: $(ZLIB_DEP)\
% endif
% if lib.language == 'c++':
 $(PROTOBUF_DEP)\
% endif
 $(LIB${lib.name.upper()}_OBJS)
	$(E) "[AR]      Creating $@"
	$(Q) mkdir -p `dirname $@`
	$(Q) rm -f libs/$(CONFIG)/lib${lib.name}.a
	$(Q) $(AR) rcs libs/$(CONFIG)/lib${lib.name}.a $(LIB${lib.name.upper()}_OBJS)
% if lib.get('baselib', False):
% if lib.get('secure', True):
	$(Q) rm -rf tmp-merge
	$(Q) mkdir tmp-merge
	$(Q) ( cd tmp-merge ; $(AR) x ../libs/$(CONFIG)/lib${lib.name}.a )
	$(Q) for l in $(OPENSSL_MERGE_LIBS) ; do ( cd tmp-merge ; <%text>ar x ../$${l}</%text> ) ; done
	$(Q) rm -f libs/$(CONFIG)/lib${lib.name}.a tmp-merge/__.SYMDEF*
	$(Q) ar rcs libs/$(CONFIG)/lib${lib.name}.a tmp-merge/*
	$(Q) rm -rf tmp-merge
% endif
% endif
ifeq ($(SYSTEM),Darwin)
	$(Q) ranlib libs/$(CONFIG)/lib${lib.name}.a 
endif

<%
  if lib.language == 'c++':
    ld = '$(LDXX)'
  else:
    ld = '$(LD)'

  out_base = 'libs/$(CONFIG)/' + lib.name
  out_libbase = 'libs/$(CONFIG)/lib' + lib.name

  common = '$(LIB' + lib.name.upper() + '_OBJS) $(LDLIBS)'

  libs = ''
  lib_deps = ' $(ZLIB_DEP)'
  mingw_libs = ''
  mingw_lib_deps = ' $(ZLIB_DEP)'
  for dep in lib.get('deps', []):
    libs = libs + ' -l' + dep
    lib_deps = lib_deps + ' libs/$(CONFIG)/lib' + dep + '.$(SHARED_EXT)'
    mingw_libs = mingw_libs + ' -l' + dep + '-imp'
    mingw_lib_deps = mingw_lib_deps + 'libs/$(CONFIG)/' + dep + '.$(SHARED_EXT)'

  if lib.get('secure', True):
    common = common + ' $(LDLIBS_SECURE) $(OPENSSL_MERGE_LIBS)'
    lib_deps = lib_deps + ' $(OPENSSL_DEP)'
    mingw_lib_deps = mingw_lib_deps + ' $(OPENSSL_DEP)'
%>

% if lib.build == "all":
ifeq ($(SYSTEM),MINGW32)
${out_base}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${mingw_lib_deps}
	$(E) "[LD]      Linking $@"
	$(Q) mkdir -p `dirname $@`
	$(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -shared -Wl,--output-def=${out_base}.def -Wl,--out-implib=${out_libbase}-imp.a -o ${out_base}.$(SHARED_EXT) ${common}${mingw_libs}
else
${out_libbase}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${lib_deps}
	$(E) "[LD]      Linking $@"
	$(Q) mkdir -p `dirname $@`
ifeq ($(SYSTEM),Darwin)
	$(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -dynamiclib -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
else
	$(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) ${out_libbase}.so.${settings.version.major}
	$(Q) ln -sf lib${lib.name}.$(SHARED_EXT) ${out_libbase}.so
endif
endif
% endif
% if lib.get('secure', True):
## If the lib was secure, we have to close the Makefile's if that tested
## the presence of an ALPN-capable OpenSSL.

endif
% endif
% if lib.language == 'c++':
## If the lib was C++, we have to close the Makefile's if that tested
## the presence of protobuf 3.0.0+

endif
% endif

% if lib.get('secure', True):
ifneq ($(NO_SECURE),true)
% endif
ifneq ($(NO_DEPS),true)
-include $(LIB${lib.name.upper()}_OBJS:.o=.dep)
endif
% if lib.get('secure', True):
endif
% endif

% for src in lib.src:
% if not proto_re.match(src):
objs/$(CONFIG)/${os.path.splitext(src)[0]}.o: \
% for src2 in lib.src:
% if proto_re.match(src2):
    ${proto_to_cc(src2)}\
% endif
% endfor
% endif

% endfor
</%def>

<%def name="maketarget(tgt)">
${tgt.name.upper()}_SRC = \\

% for src in tgt.src:
    ${proto_to_cc(src)} \\

% endfor

${tgt.name.upper()}_OBJS = $(addprefix objs/$(CONFIG)/, $(addsuffix .o, $(basename $(${tgt.name.upper()}_SRC))))

% if tgt.get('secure', True):
ifeq ($(NO_SECURE),true)

# You can't build secure targets if you don't have OpenSSL with ALPN.

bins/$(CONFIG)/${tgt.name}: openssl_dep_error

else

% endif
##
## We're not trying to add a dependency on building zlib and openssl here,
## as it's already done in the libraries. We're assuming that the build
## trickles down, and that a secure target requires a secure version of
## a library.
##
## That simplifies the codegen a bit, but prevents a fully defined Makefile.
## I can live with that.
##
% if tgt.build == 'protoc':

ifeq ($(NO_PROTOBUF),true)

# You can't build the protoc plugins if you don't have protobuf 3.0.0+.

bins/$(CONFIG)/${tgt.name}: protobuf_dep_error

else

bins/$(CONFIG)/${tgt.name}: $(PROTOBUF_DEP) $(${tgt.name.upper()}_OBJS)\
% else:
bins/$(CONFIG)/${tgt.name}: $(${tgt.name.upper()}_OBJS)\
% endif
% for dep in tgt.deps:
 libs/$(CONFIG)/lib${dep}.a\
% endfor

% if tgt.language == "c++":
## C++ targets specificies.
% if tgt.build == 'protoc':
	$(E) "[HOSTLD]  Linking $@"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(HOST_LDXX) $(HOST_LDFLAGS) $(${tgt.name.upper()}_OBJS)\
% else:
	$(E) "[LD]      Linking $@"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(LDXX) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
% endif
% if tgt.build == 'test':
 $(GTEST_LIB)\
% endif
% else:
## C-only targets specificities.
	$(E) "[LD]      Linking $@"
	$(Q) mkdir -p `dirname $@`
	$(Q) $(LD) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
% endif
% for dep in tgt.deps:
 libs/$(CONFIG)/lib${dep}.a\
% endfor
% if tgt.language == "c++":
% if tgt.build == 'protoc':
 $(HOST_LDLIBSXX) $(HOST_LDLIBS_PROTOC)\
% else:
 $(LDLIBSXX) $(LDLIBS_PROTOBUF)\
% endif
% endif
% if tgt.build == 'protoc':
 $(HOST_LDLIBS)\
% else:
 $(LDLIBS)\
% endif
% if tgt.build == 'protoc':
 $(HOST_LDLIBS_PROTOC)\
% elif tgt.get('secure', True):
 $(LDLIBS_SECURE)\
% endif
 -o bins/$(CONFIG)/${tgt.name}
% if tgt.build == 'protoc':

endif
% endif
% if tgt.get('secure', True):

endif
% endif

% for src in tgt.src:
objs/$(CONFIG)/${os.path.splitext(src)[0]}.o: \
% for dep in tgt.deps:
 libs/$(CONFIG)/lib${dep}.a\
% endfor

% endfor

deps_${tgt.name}: $(${tgt.name.upper()}_OBJS:.o=.dep)

% if tgt.get('secure', True):
ifneq ($(NO_SECURE),true)
% endif
ifneq ($(NO_DEPS),true)
-include $(${tgt.name.upper()}_OBJS:.o=.dep)
endif
% if tgt.get('secure', True):
endif
% endif
</%def>

.PHONY: all strip tools \
dep_error openssl_dep_error openssl_dep_message git_update stop \
buildtests buildtests_c buildtests_cxx \
test test_c test_cxx \
install install_c install_cxx \
install-headers install-headers_c install-headers_cxx \
install-shared install-shared_c install-shared_cxx \
install-static install-static_c install-static_cxx \
strip strip-shared strip-static \
strip_c strip-shared_c strip-static_c \
strip_cxx strip-shared_cxx strip-static_cxx \
dep_c dep_cxx bins_dep_c bins_dep_cxx \
clean