Makefile.template 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. # GRPC global makefile
  2. # This currently builds C and C++ code.
  3. <%!
  4. import re
  5. import os
  6. proto_re = re.compile('(.*)\\.proto')
  7. def excluded(filename, exclude_res):
  8. for r in exclude_res:
  9. if r.match(filename):
  10. return True
  11. return False
  12. def proto_to_cc(filename):
  13. m = proto_re.match(filename)
  14. if not m:
  15. return filename
  16. return 'gens/' + m.group(1) + '.pb.cc'
  17. %>
  18. # Basic platform detection
  19. HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
  20. ifeq ($(SYSTEM),)
  21. SYSTEM = $(HOST_SYSTEM)
  22. endif
  23. # Configurations
  24. VALID_CONFIG_opt = 1
  25. CC_opt = gcc
  26. CXX_opt = g++
  27. LD_opt = gcc
  28. LDXX_opt = g++
  29. CPPFLAGS_opt = -O2
  30. LDFLAGS_opt =
  31. DEFINES_opt = NDEBUG
  32. VALID_CONFIG_dbg = 1
  33. CC_dbg = gcc
  34. CXX_dbg = g++
  35. LD_dbg = gcc
  36. LDXX_dbg = g++
  37. CPPFLAGS_dbg = -O0
  38. LDFLAGS_dbg =
  39. DEFINES_dbg = _DEBUG DEBUG
  40. VALID_CONFIG_valgrind = 1
  41. REQUIRE_CUSTOM_LIBRARIES_valgrind = 1
  42. CC_valgrind = gcc
  43. CXX_valgrind = g++
  44. LD_valgrind = gcc
  45. LDXX_valgrind = g++
  46. CPPFLAGS_valgrind = -O0
  47. OPENSSL_CFLAGS_valgrind = -DPURIFY
  48. LDFLAGS_valgrind =
  49. DEFINES_valgrind = _DEBUG DEBUG
  50. VALID_CONFIG_tsan = 1
  51. REQUIRE_CUSTOM_LIBRARIES_tsan = 1
  52. CC_tsan = clang
  53. CXX_tsan = clang++
  54. LD_tsan = clang
  55. LDXX_tsan = clang++
  56. CPPFLAGS_tsan = -O1 -fsanitize=thread -fno-omit-frame-pointer
  57. OPENSSL_CONFIG_tsan = no-asm
  58. LDFLAGS_tsan = -fsanitize=thread
  59. DEFINES_tsan = NDEBUG
  60. VALID_CONFIG_asan = 1
  61. REQUIRE_CUSTOM_LIBRARIES_asan = 1
  62. CC_asan = clang
  63. CXX_asan = clang++
  64. LD_asan = clang
  65. LDXX_asan = clang++
  66. CPPFLAGS_asan = -O1 -fsanitize=address -fno-omit-frame-pointer
  67. OPENSSL_CONFIG_asan = no-asm
  68. LDFLAGS_asan = -fsanitize=address
  69. DEFINES_asan = NDEBUG
  70. VALID_CONFIG_msan = 1
  71. REQUIRE_CUSTOM_LIBRARIES_msan = 1
  72. CC_msan = clang
  73. CXX_msan = clang++
  74. LD_msan = clang
  75. LDXX_msan = clang++
  76. CPPFLAGS_msan = -O1 -fsanitize=memory -fno-omit-frame-pointer
  77. OPENSSL_CFLAGS_msan = -DPURIFY
  78. OPENSSL_CONFIG_msan = no-asm
  79. LDFLAGS_msan = -fsanitize=memory
  80. DEFINES_msan = NDEBUG
  81. VALID_CONFIG_gcov = 1
  82. CC_gcov = gcc
  83. CXX_gcov = g++
  84. LD_gcov = gcc
  85. LDXX_gcov = g++
  86. CPPFLAGS_gcov = -O0 -fprofile-arcs -ftest-coverage
  87. LDFLAGS_gcov = -fprofile-arcs -ftest-coverage
  88. DEFINES_gcov = NDEBUG
  89. # General settings.
  90. # You may want to change these depending on your system.
  91. prefix ?= /usr/local
  92. PROTOC = protoc
  93. CONFIG ?= opt
  94. CC = $(CC_$(CONFIG))
  95. CXX = $(CXX_$(CONFIG))
  96. LD = $(LD_$(CONFIG))
  97. LDXX = $(LDXX_$(CONFIG))
  98. AR = ar
  99. STRIP = strip --strip-unneeded
  100. INSTALL = install -D
  101. RM = rm -f
  102. ifndef VALID_CONFIG_$(CONFIG)
  103. $(error Invalid CONFIG value '$(CONFIG)')
  104. endif
  105. # The HOST compiler settings are used to compile the protoc plugins.
  106. # In most cases, you won't have to change anything, but if you are
  107. # cross-compiling, you can override these variables from GNU make's
  108. # command line: make CC=cross-gcc HOST_CC=gcc
  109. HOST_CC = $(CC)
  110. HOST_CXX = $(CXX)
  111. HOST_LD = $(LD)
  112. HOST_LDXX = $(LDXX)
  113. CPPFLAGS += $(CPPFLAGS_$(CONFIG))
  114. DEFINES += $(DEFINES_$(CONFIG))
  115. LDFLAGS += $(LDFLAGS_$(CONFIG))
  116. CFLAGS += -std=c89 -pedantic
  117. CXXFLAGS += -std=c++11
  118. CPPFLAGS += -g -fPIC -Wall -Werror -Wno-long-long
  119. LDFLAGS += -g -fPIC
  120. INCLUDES = . include gens
  121. ifeq ($(SYSTEM),Darwin)
  122. LIBS = m z
  123. else
  124. LIBS = rt m z pthread
  125. LDFLAGS += -pthread
  126. endif
  127. LIBSXX = protobuf
  128. LIBS_PROTOC = protoc protobuf
  129. ifneq ($(wildcard /usr/src/gtest/src/gtest-all.cc),)
  130. GTEST_LIB = /usr/src/gtest/src/gtest-all.cc -I/usr/src/gtest
  131. else
  132. GTEST_LIB = -lgtest
  133. endif
  134. GTEST_LIB += -lgflags
  135. ifeq ($(V),1)
  136. E = @:
  137. Q =
  138. else
  139. E = @echo
  140. Q = @
  141. endif
  142. VERSION = ${settings.version.major}.${settings.version.minor}.${settings.version.micro}.${settings.version.build}
  143. CPPFLAGS_NO_ARCH += $(addprefix -I, $(INCLUDES)) $(addprefix -D, $(DEFINES))
  144. CPPFLAGS += $(CPPFLAGS_NO_ARCH) $(ARCH_FLAGS)
  145. LDFLAGS += $(ARCH_FLAGS)
  146. LDLIBS += $(addprefix -l, $(LIBS))
  147. LDLIBSXX += $(addprefix -l, $(LIBSXX))
  148. HOST_LDLIBS_PROTOC += $(addprefix -l, $(LIBS_PROTOC))
  149. HOST_CPPFLAGS = $(CPPFLAGS)
  150. HOST_CFLAGS = $(CFLAGS)
  151. HOST_CXXFLAGS = $(CXXFLAGS)
  152. HOST_LDFLAGS = $(LDFLAGS)
  153. HOST_LDLIBS = $(LDLIBS)
  154. # These are automatically computed variables.
  155. # There shouldn't be any need to change anything from now on.
  156. ifeq ($(SYSTEM),MINGW32)
  157. SHARED_EXT = dll
  158. endif
  159. ifeq ($(SYSTEM),Darwin)
  160. SHARED_EXT = dylib
  161. endif
  162. ifeq ($(SHARED_EXT),)
  163. SHARED_EXT = so.$(VERSION)
  164. endif
  165. ifeq ($(wildcard .git),)
  166. IS_GIT_FOLDER = false
  167. else
  168. IS_GIT_FOLDER = true
  169. endif
  170. OPENSSL_ALPN_CHECK_CMD = $(CC) $(CFLAGS) $(CPPFLAGS) -o /dev/null test/build/openssl-alpn.c -lssl -lcrypto -ldl $(LDFLAGS)
  171. ZLIB_CHECK_CMD = $(CC) $(CFLAGS) $(CPPFLAGS) -o /dev/null test/build/zlib.c -lz $(LDFLAGS)
  172. PERFTOOLS_CHECK_CMD = $(CC) $(CFLAGS) $(CPPFLAGS) -o /dev/null test/build/perftools.c -lprofiler $(LDFLAGS)
  173. HAS_SYSTEM_PERFTOOLS = $(shell $(PERFTOOLS_CHECK_CMD) 2> /dev/null && echo true || echo false)
  174. ifeq ($(HAS_SYSTEM_PERFTOOLS),true)
  175. DEFINES += GRPC_HAVE_PERFTOOLS
  176. LIBS += profiler
  177. endif
  178. ifndef REQUIRE_CUSTOM_LIBRARIES_$(CONFIG)
  179. HAS_SYSTEM_OPENSSL_ALPN = $(shell $(OPENSSL_ALPN_CHECK_CMD) 2> /dev/null && echo true || echo false)
  180. HAS_SYSTEM_ZLIB = $(shell $(ZLIB_CHECK_CMD) 2> /dev/null && echo true || echo false)
  181. else
  182. # override system libraries if the config requires a custom compiled library
  183. HAS_SYSTEM_OPENSSL_ALPN = false
  184. HAS_SYSTEM_ZLIB = false
  185. endif
  186. ifeq ($(wildcard third_party/openssl/ssl/ssl.h),)
  187. HAS_EMBEDDED_OPENSSL_ALPN = false
  188. else
  189. HAS_EMBEDDED_OPENSSL_ALPN = true
  190. endif
  191. ifeq ($(wildcard third_party/zlib/zlib.h),)
  192. HAS_EMBEDDED_ZLIB = false
  193. else
  194. HAS_EMBEDDED_ZLIB = true
  195. endif
  196. ifeq ($(HAS_SYSTEM_ZLIB),false)
  197. ifeq ($(HAS_EMBEDDED_ZLIB),true)
  198. ZLIB_DEP = libs/$(CONFIG)/zlib/libz.a
  199. CPPFLAGS += -Ithird_party/zlib
  200. LDFLAGS += -Lthird_party/zlib
  201. else
  202. DEP_MISSING += zlib
  203. endif
  204. endif
  205. ifeq ($(HAS_SYSTEM_OPENSSL_ALPN),false)
  206. ifeq ($(HAS_EMBEDDED_OPENSSL_ALPN),true)
  207. OPENSSL_DEP = libs/$(CONFIG)/openssl/libssl.a
  208. OPENSSL_MERGE_LIBS += libs/$(CONFIG)/openssl/libssl.a libs/$(CONFIG)/openssl/libcrypto.a
  209. CPPFLAGS += -Ithird_party/openssl/include
  210. LDFLAGS += -Llibs/$(CONFIG)/openssl
  211. LIBS_SECURE = dl
  212. else
  213. NO_SECURE = true
  214. endif
  215. else
  216. LIBS_SECURE = ssl crypto dl
  217. endif
  218. LDLIBS_SECURE += $(addprefix -l, $(LIBS_SECURE))
  219. ifeq ($(MAKECMDGOALS),clean)
  220. NO_DEPS = true
  221. endif
  222. .SECONDARY = %.pb.h %.pb.cc
  223. PROTOC_PLUGINS=\
  224. % for tgt in targets:
  225. % if tgt.build == 'protoc':
  226. bins/$(CONFIG)/${tgt.name}\
  227. % endif
  228. % endfor
  229. ifeq ($(DEP_MISSING),)
  230. all: static shared\
  231. % for tgt in targets:
  232. % if tgt.build == 'all':
  233. bins/$(CONFIG)/${tgt.name}\
  234. % endif
  235. % endfor
  236. dep_error:
  237. @echo "You shouldn't see this message - all of your dependencies are correct."
  238. else
  239. all: dep_error git_update stop
  240. dep_error:
  241. @echo
  242. @echo "DEPENDENCY ERROR"
  243. @echo
  244. @echo "You are missing system dependencies that are essential to build grpc,"
  245. @echo "and the third_party directory doesn't have them:"
  246. @echo
  247. @echo " $(DEP_MISSING)"
  248. @echo
  249. @echo "Installing the development packages for your system will solve"
  250. @echo "this issue. Please consult INSTALL to get more information."
  251. @echo
  252. @echo "If you need information about why these tests failed, run:"
  253. @echo
  254. @echo " make run_dep_checks"
  255. @echo
  256. endif
  257. git_update:
  258. ifeq ($(IS_GIT_FOLDER),true)
  259. @echo "Additionally, since you are in a git clone, you can download the"
  260. @echo "missing dependencies in third_party by running the following command:"
  261. @echo
  262. @echo " git submodule update --init"
  263. @echo
  264. endif
  265. openssl_dep_error: openssl_dep_message git_update stop
  266. openssl_dep_message:
  267. @echo
  268. @echo "DEPENDENCY ERROR"
  269. @echo
  270. @echo "The target you are trying to run requires OpenSSL with ALPN support."
  271. @echo "Your system doesn't have it, and neither does the third_party directory."
  272. @echo
  273. @echo "Please consult INSTALL to get more information."
  274. @echo
  275. @echo "If you need information about why these tests failed, run:"
  276. @echo
  277. @echo " make run_dep_checks"
  278. @echo
  279. stop:
  280. @false
  281. % for tgt in targets:
  282. ${tgt.name}: bins/$(CONFIG)/${tgt.name}
  283. % endfor
  284. run_dep_checks:
  285. $(OPENSSL_ALPN_CHECK_CMD) || true
  286. $(ZLIB_CHECK_CMD) || true
  287. libs/$(CONFIG)/zlib/libz.a:
  288. $(E) "[MAKE] Building zlib"
  289. $(Q)(cd third_party/zlib ; CC="$(CC)" CFLAGS="-fPIC -fvisibility=hidden $(CPPFLAGS_$(CONFIG))" ./configure --static)
  290. $(Q)$(MAKE) -C third_party/zlib clean
  291. $(Q)$(MAKE) -C third_party/zlib
  292. $(Q)mkdir -p libs/$(CONFIG)/zlib
  293. $(Q)cp third_party/zlib/libz.a libs/$(CONFIG)/zlib
  294. libs/$(CONFIG)/openssl/libssl.a:
  295. $(E) "[MAKE] Building openssl for $(SYSTEM)"
  296. ifeq ($(SYSTEM),Darwin)
  297. $(Q)(cd third_party/openssl ; CC="$(CC) -fPIC -fvisibility=hidden $(CPPFLAGS_$(CONFIG)) $(OPENSSL_CFLAGS_$(CONFIG))" ./Configure darwin64-x86_64-cc $(OPENSSL_CONFIG_$(CONFIG)))
  298. else
  299. $(Q)(cd third_party/openssl ; CC="$(CC) -fPIC -fvisibility=hidden $(CPPFLAGS_$(CONFIG)) $(OPENSSL_CFLAGS_$(CONFIG))" ./config $(OPENSSL_CONFIG_$(CONFIG)))
  300. endif
  301. $(Q)$(MAKE) -C third_party/openssl clean
  302. $(Q)$(MAKE) -C third_party/openssl build_crypto build_ssl
  303. $(Q)mkdir -p libs/$(CONFIG)/openssl
  304. $(Q)cp third_party/openssl/libssl.a third_party/openssl/libcrypto.a libs/$(CONFIG)/openssl
  305. static: static_c static_cxx
  306. static_c: \
  307. % for lib in libs:
  308. % if lib.build == 'all' and lib.language == 'c':
  309. libs/$(CONFIG)/lib${lib.name}.a\
  310. % endif
  311. % endfor
  312. static_cxx: \
  313. % for lib in libs:
  314. % if lib.build == 'all' and lib.language == 'c++':
  315. libs/$(CONFIG)/lib${lib.name}.a\
  316. % endif
  317. % endfor
  318. shared: shared_c shared_cxx
  319. shared_c: \
  320. % for lib in libs:
  321. % if lib.build == 'all' and lib.language == 'c':
  322. libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)\
  323. % endif
  324. % endfor
  325. shared_cxx: \
  326. % for lib in libs:
  327. % if lib.build == 'all' and lib.language == 'c++':
  328. libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)\
  329. % endif
  330. % endfor
  331. privatelibs: privatelibs_c privatelibs_cxx
  332. privatelibs_c: \
  333. % for lib in libs:
  334. % if lib.build == 'private' and lib.language == 'c':
  335. libs/$(CONFIG)/lib${lib.name}.a\
  336. % endif
  337. % endfor
  338. privatelibs_cxx: \
  339. % for lib in libs:
  340. % if lib.build == 'private' and lib.language == 'c++':
  341. libs/$(CONFIG)/lib${lib.name}.a\
  342. % endif
  343. % endfor
  344. buildtests: buildtests_c buildtests_cxx
  345. buildtests_c: privatelibs_c\
  346. % for tgt in targets:
  347. % if tgt.build == 'test' and not tgt.language == 'c++':
  348. bins/$(CONFIG)/${tgt.name}\
  349. % endif
  350. % endfor
  351. buildtests_cxx: privatelibs_cxx\
  352. % for tgt in targets:
  353. % if tgt.build == 'test' and tgt.language == 'c++':
  354. bins/$(CONFIG)/${tgt.name}\
  355. % endif
  356. % endfor
  357. test: test_c test_cxx
  358. test_c: buildtests_c
  359. % for tgt in targets:
  360. % if tgt.build == 'test' and tgt.get('run', True) and not tgt.language == 'c++':
  361. $(E) "[RUN] Testing ${tgt.name}"
  362. $(Q) ./bins/$(CONFIG)/${tgt.name} || ( echo test ${tgt.name} failed ; exit 1 )
  363. % endif
  364. % endfor
  365. test_cxx: buildtests_cxx
  366. % for tgt in targets:
  367. % if tgt.build == 'test' and tgt.get('run', True) and tgt.language == 'c++':
  368. $(E) "[RUN] Testing ${tgt.name}"
  369. $(Q) ./bins/$(CONFIG)/${tgt.name} || ( echo test ${tgt.name} failed ; exit 1 )
  370. % endif
  371. % endfor
  372. tools: privatelibs\
  373. % for tgt in targets:
  374. % if tgt.build == 'tool':
  375. bins/$(CONFIG)/${tgt.name}\
  376. % endif
  377. % endfor
  378. buildbenchmarks: privatelibs\
  379. % for tgt in targets:
  380. % if tgt.build == 'benchmark':
  381. bins/$(CONFIG)/${tgt.name}\
  382. % endif
  383. % endfor
  384. benchmarks: buildbenchmarks
  385. strip: strip-static strip-shared
  386. strip-static: strip-static_c strip-static_cxx
  387. strip-shared: strip-shared_c strip-shared_cxx
  388. # TODO(nnoble): the strip target is stripping in-place, instead
  389. # of copying files in a temporary folder.
  390. # This prevents proper debugging after running make install.
  391. strip-static_c: static_c
  392. % for lib in libs:
  393. % if lib.language == "c":
  394. % if lib.build == "all":
  395. $(E) "[STRIP] Stripping lib${lib.name}.a"
  396. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.a
  397. % endif
  398. % endif
  399. % endfor
  400. strip-static_cxx: static_cxx
  401. % for lib in libs:
  402. % if lib.language == "c++":
  403. % if lib.build == "all":
  404. $(E) "[STRIP] Stripping lib${lib.name}.a"
  405. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.a
  406. % endif
  407. % endif
  408. % endfor
  409. strip-shared_c: shared_c
  410. % for lib in libs:
  411. % if lib.language == "c":
  412. % if lib.build == "all":
  413. $(E) "[STRIP] Stripping lib${lib.name}.so"
  414. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)
  415. % endif
  416. % endif
  417. % endfor
  418. strip-shared_cxx: shared_cxx
  419. % for lib in libs:
  420. % if lib.language == "c++":
  421. % if lib.build == "all":
  422. $(E) "[STRIP] Stripping lib${lib.name}.so"
  423. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)
  424. % endif
  425. % endif
  426. % endfor
  427. % for p in protos:
  428. gens/${p}.pb.cc: ${p}.proto $(PROTOC_PLUGINS)
  429. $(E) "[PROTOC] Generating protobuf CC file from $<"
  430. $(Q) mkdir -p `dirname $@`
  431. $(Q) $(PROTOC) --cpp_out=gens --grpc_out=gens --plugin=protoc-gen-grpc=bins/$(CONFIG)/cpp_plugin $<
  432. % endfor
  433. objs/$(CONFIG)/%.o : %.c
  434. $(E) "[C] Compiling $<"
  435. $(Q) mkdir -p `dirname $@`
  436. $(Q) $(CC) $(CFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  437. objs/$(CONFIG)/%.o : gens/%.pb.cc
  438. $(E) "[CXX] Compiling $<"
  439. $(Q) mkdir -p `dirname $@`
  440. $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  441. objs/$(CONFIG)/src/compiler/%.o : src/compiler/%.cc
  442. $(E) "[HOSTCXX] Compiling $<"
  443. $(Q) mkdir -p `dirname $@`
  444. $(Q) $(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  445. objs/$(CONFIG)/%.o : %.cc
  446. $(E) "[CXX] Compiling $<"
  447. $(Q) mkdir -p `dirname $@`
  448. $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  449. install: install_c install_cxx
  450. install_c: install-headers_c install-static_c install-shared_c
  451. install_cxx: install-headers_cxx install-static_cxx install-shared_cxx
  452. install-headers: install-headers_c install-headers_cxx
  453. install-headers_c:
  454. $(E) "[INSTALL] Installing public C headers"
  455. $(Q) $(foreach h, $(PUBLIC_HEADERS_C), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1
  456. install-headers_cxx:
  457. $(E) "[INSTALL] Installing public C++ headers"
  458. $(Q) $(foreach h, $(PUBLIC_HEADERS_CXX), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1
  459. install-static: install-static_c install-static_cxx
  460. install-static_c: static_c strip-static_c
  461. % for lib in libs:
  462. % if lib.language == "c":
  463. % if lib.build == "all":
  464. $(E) "[INSTALL] Installing lib${lib.name}.a"
  465. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
  466. % endif
  467. % endif
  468. % endfor
  469. install-static_cxx: static_cxx strip-static_cxx
  470. % for lib in libs:
  471. % if lib.language == "c++":
  472. % if lib.build == "all":
  473. $(E) "[INSTALL] Installing lib${lib.name}.a"
  474. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
  475. % endif
  476. % endif
  477. % endfor
  478. install-shared_c: shared_c strip-shared_c
  479. % for lib in libs:
  480. % if lib.language == "c":
  481. % if lib.build == "all":
  482. ifeq ($(SYSTEM),MINGW32)
  483. $(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
  484. $(Q) $(INSTALL) libs/$(CONFIG)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
  485. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
  486. else
  487. $(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
  488. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
  489. ifneq ($(SYSTEM),Darwin)
  490. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
  491. endif
  492. endif
  493. % endif
  494. % endif
  495. % endfor
  496. ifneq ($(SYSTEM),MINGW32)
  497. ifneq ($(SYSTEM),Darwin)
  498. $(Q) ldconfig
  499. endif
  500. endif
  501. install-shared_cxx: shared_cxx strip-shared_cxx
  502. % for lib in libs:
  503. % if lib.language == "c++":
  504. % if lib.build == "all":
  505. ifeq ($(SYSTEM),MINGW32)
  506. $(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
  507. $(Q) $(INSTALL) libs/$(CONFIG)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
  508. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
  509. else
  510. $(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
  511. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
  512. ifneq ($(SYSTEM),Darwin)
  513. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
  514. endif
  515. endif
  516. % endif
  517. % endif
  518. % endfor
  519. ifneq ($(SYSTEM),MINGW32)
  520. ifneq ($(SYSTEM),Darwin)
  521. $(Q) ldconfig
  522. endif
  523. endif
  524. clean:
  525. $(Q) $(RM) -rf objs libs bins gens
  526. # The various libraries
  527. % for lib in libs:
  528. ${makelib(lib)}
  529. % endfor
  530. # All of the test targets, and protoc plugins
  531. % for tgt in targets:
  532. ${maketarget(tgt)}
  533. % endfor
  534. <%def name="makelib(lib)">
  535. LIB${lib.name.upper()}_SRC = \\
  536. % for src in lib.src:
  537. ${proto_to_cc(src)} \\
  538. % endfor
  539. % if "public_headers" in lib:
  540. % if lib.language == "c++":
  541. PUBLIC_HEADERS_CXX += \\
  542. % else:
  543. PUBLIC_HEADERS_C += \\
  544. % endif
  545. % for hdr in lib.public_headers:
  546. ${hdr} \\
  547. % endfor
  548. % endif
  549. LIB${lib.name.upper()}_OBJS = $(addprefix objs/$(CONFIG)/, $(addsuffix .o, $(basename $(LIB${lib.name.upper()}_SRC))))
  550. ## If the library requires OpenSSL with ALPN, let's add some restrictions.
  551. % if lib.get('secure', True):
  552. ifeq ($(NO_SECURE),true)
  553. # You can't build secure libraries if you don't have OpenSSL with ALPN.
  554. libs/$(CONFIG)/lib${lib.name}.a: openssl_dep_error
  555. % if lib.build == "all":
  556. ifeq ($(SYSTEM),MINGW32)
  557. libs/$(CONFIG)/${lib.name}.$(SHARED_EXT): openssl_dep_error
  558. else
  559. libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT): openssl_dep_error
  560. endif
  561. % endif
  562. else
  563. ifneq ($(OPENSSL_DEP),)
  564. % for src in lib.src:
  565. ${src}: $(OPENSSL_DEP)
  566. % endfor
  567. endif
  568. libs/$(CONFIG)/lib${lib.name}.a: $(ZLIB_DEP) $(OPENSSL_DEP) $(LIB${lib.name.upper()}_OBJS)
  569. ## The else here corresponds to the if secure earlier.
  570. % else:
  571. libs/$(CONFIG)/lib${lib.name}.a: $(ZLIB_DEP) $(LIB${lib.name.upper()}_OBJS)
  572. % endif
  573. $(E) "[AR] Creating $@"
  574. $(Q) mkdir -p `dirname $@`
  575. $(Q) rm -f libs/$(CONFIG)/lib${lib.name}.a
  576. $(Q) $(AR) rcs libs/$(CONFIG)/lib${lib.name}.a $(LIB${lib.name.upper()}_OBJS)
  577. % if lib.get('baselib', False):
  578. % if lib.get('secure', True):
  579. $(Q) rm -rf tmp-merge
  580. $(Q) mkdir tmp-merge
  581. $(Q) ( cd tmp-merge ; $(AR) x ../libs/$(CONFIG)/lib${lib.name}.a )
  582. $(Q) for l in $(OPENSSL_MERGE_LIBS) ; do ( cd tmp-merge ; <%text>ar x ../$${l}</%text> ) ; done
  583. $(Q) rm -f libs/$(CONFIG)/lib${lib.name}.a tmp-merge/__.SYMDEF*
  584. $(Q) ar rcs libs/$(CONFIG)/lib${lib.name}.a tmp-merge/*
  585. $(Q) rm -rf tmp-merge
  586. % endif
  587. % endif
  588. ifeq ($(SYSTEM),Darwin)
  589. $(Q) ranlib libs/$(CONFIG)/lib${lib.name}.a
  590. endif
  591. <%
  592. if lib.language == 'c++':
  593. ld = '$(LDXX)'
  594. else:
  595. ld = '$(LD)'
  596. out_base = 'libs/$(CONFIG)/' + lib.name
  597. out_libbase = 'libs/$(CONFIG)/lib' + lib.name
  598. common = '$(LIB' + lib.name.upper() + '_OBJS) $(LDLIBS)'
  599. libs = ''
  600. lib_deps = ' $(ZLIB_DEP)'
  601. mingw_libs = ''
  602. mingw_lib_deps = ' $(ZLIB_DEP)'
  603. for dep in lib.get('deps', []):
  604. libs = libs + ' -l' + dep
  605. lib_deps = lib_deps + ' libs/$(CONFIG)/lib' + dep + '.$(SHARED_EXT)'
  606. mingw_libs = mingw_libs + ' -l' + dep + '-imp'
  607. mingw_lib_deps = mingw_lib_deps + 'libs/$(CONFIG)/' + dep + '.$(SHARED_EXT)'
  608. if lib.get('secure', True):
  609. common = common + ' $(LDLIBS_SECURE) $(OPENSSL_MERGE_LIBS)'
  610. lib_deps = lib_deps + ' $(OPENSSL_DEP)'
  611. mingw_lib_deps = mingw_lib_deps + ' $(OPENSSL_DEP)'
  612. %>
  613. % if lib.build == "all":
  614. ifeq ($(SYSTEM),MINGW32)
  615. ${out_base}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${mingw_lib_deps}
  616. $(E) "[LD] Linking $@"
  617. $(Q) mkdir -p `dirname $@`
  618. $(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}
  619. else
  620. ${out_libbase}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${lib_deps}
  621. $(E) "[LD] Linking $@"
  622. $(Q) mkdir -p `dirname $@`
  623. ifeq ($(SYSTEM),Darwin)
  624. $(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -dynamiclib -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
  625. else
  626. $(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
  627. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) ${out_libbase}.so
  628. endif
  629. endif
  630. % endif
  631. ## If the lib was secure, we have to close the Makefile's if that tested
  632. ## the presence of an ALPN-capable OpenSSL.
  633. % if lib.get('secure', True):
  634. endif
  635. % endif
  636. % if lib.get('secure', True):
  637. ifneq ($(NO_SECURE),true)
  638. % endif
  639. ifneq ($(NO_DEPS),true)
  640. -include $(LIB${lib.name.upper()}_OBJS:.o=.dep)
  641. endif
  642. % if lib.get('secure', True):
  643. endif
  644. % endif
  645. % for src in lib.src:
  646. % if not proto_re.match(src):
  647. objs/$(CONFIG)/${os.path.splitext(src)[0]}.o: \
  648. % for src2 in lib.src:
  649. % if proto_re.match(src2):
  650. ${proto_to_cc(src2)}\
  651. % endif
  652. % endfor
  653. % endif
  654. % endfor
  655. </%def>
  656. <%def name="maketarget(tgt)">
  657. ${tgt.name.upper()}_SRC = \\
  658. % for src in tgt.src:
  659. ${proto_to_cc(src)} \\
  660. % endfor
  661. ${tgt.name.upper()}_OBJS = $(addprefix objs/$(CONFIG)/, $(addsuffix .o, $(basename $(${tgt.name.upper()}_SRC))))
  662. % if tgt.get('secure', True):
  663. ifeq ($(NO_SECURE),true)
  664. # You can't build secure targets if you don't have OpenSSL with ALPN.
  665. bins/$(CONFIG)/${tgt.name}: openssl_dep_error
  666. else
  667. % endif
  668. ##
  669. ## We're not trying to add a dependency on building zlib and openssl here,
  670. ## as it's already done in the libraries. We're assuming that the build
  671. ## trickles down, and that a secure target requires a secure version of
  672. ## a library.
  673. ##
  674. ## That simplifies the codegen a bit, but prevents a fully defined Makefile.
  675. ## I can live with that.
  676. ##
  677. bins/$(CONFIG)/${tgt.name}: $(${tgt.name.upper()}_OBJS)\
  678. % for dep in tgt.deps:
  679. libs/$(CONFIG)/lib${dep}.a\
  680. % endfor
  681. % if tgt.language == "c++":
  682. ## C++ targets specificies.
  683. % if tgt.build == 'protoc':
  684. $(E) "[HOSTLD] Linking $@"
  685. $(Q) mkdir -p `dirname $@`
  686. $(Q) $(HOST_LDXX) $(HOST_LDFLAGS) $(${tgt.name.upper()}_OBJS)\
  687. % else:
  688. $(E) "[LD] Linking $@"
  689. $(Q) mkdir -p `dirname $@`
  690. $(Q) $(LDXX) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
  691. % endif
  692. % if tgt.build == 'test':
  693. $(GTEST_LIB)\
  694. % endif
  695. % else:
  696. ## C-only targets specificities.
  697. $(E) "[LD] Linking $@"
  698. $(Q) mkdir -p `dirname $@`
  699. $(Q) $(LD) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
  700. % endif
  701. % for dep in tgt.deps:
  702. libs/$(CONFIG)/lib${dep}.a\
  703. % endfor
  704. % if tgt.language == "c++":
  705. % if tgt.build == 'protoc':
  706. $(HOST_LDLIBSXX)\
  707. % else:
  708. $(LDLIBSXX)\
  709. % endif
  710. % endif
  711. % if tgt.build == 'protoc':
  712. $(HOST_LDLIBS)\
  713. % else:
  714. $(LDLIBS)\
  715. % endif
  716. % if tgt.build == 'protoc':
  717. $(HOST_LDLIBS_PROTOC)\
  718. % elif tgt.get('secure', True):
  719. $(LDLIBS_SECURE)\
  720. % endif
  721. -o bins/$(CONFIG)/${tgt.name}
  722. % if tgt.get('secure', True):
  723. endif
  724. % endif
  725. % for src in tgt.src:
  726. objs/$(CONFIG)/${os.path.splitext(src)[0]}.o: \
  727. % for dep in tgt.deps:
  728. libs/$(CONFIG)/lib${dep}.a\
  729. % endfor
  730. % endfor
  731. deps_${tgt.name}: $(${tgt.name.upper()}_OBJS:.o=.dep)
  732. % if tgt.get('secure', True):
  733. ifneq ($(NO_SECURE),true)
  734. % endif
  735. ifneq ($(NO_DEPS),true)
  736. -include $(${tgt.name.upper()}_OBJS:.o=.dep)
  737. endif
  738. % if tgt.get('secure', True):
  739. endif
  740. % endif
  741. </%def>
  742. .PHONY: all strip tools \
  743. dep_error openssl_dep_error openssl_dep_message git_update stop \
  744. buildtests buildtests_c buildtests_cxx \
  745. test test_c test_cxx \
  746. install install_c install_cxx \
  747. install-headers install-headers_c install-headers_cxx \
  748. install-shared install-shared_c install-shared_cxx \
  749. install-static install-static_c install-static_cxx \
  750. strip strip-shared strip-static \
  751. strip_c strip-shared_c strip-static_c \
  752. strip_cxx strip-shared_cxx strip-static_cxx \
  753. dep_c dep_cxx bins_dep_c bins_dep_cxx \
  754. clean