Makefile.template 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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 -Wextra -Werror -Wno-long-long -Wno-unused-parameter
  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. ifeq ($(CONFIG),opt)
  393. % for lib in libs:
  394. % if lib.language == "c":
  395. % if lib.build == "all":
  396. $(E) "[STRIP] Stripping lib${lib.name}.a"
  397. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.a
  398. % endif
  399. % endif
  400. % endfor
  401. endif
  402. strip-static_cxx: static_cxx
  403. ifeq ($(CONFIG),opt)
  404. % for lib in libs:
  405. % if lib.language == "c++":
  406. % if lib.build == "all":
  407. $(E) "[STRIP] Stripping lib${lib.name}.a"
  408. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.a
  409. % endif
  410. % endif
  411. % endfor
  412. endif
  413. strip-shared_c: shared_c
  414. ifeq ($(CONFIG),opt)
  415. % for lib in libs:
  416. % if lib.language == "c":
  417. % if lib.build == "all":
  418. $(E) "[STRIP] Stripping lib${lib.name}.so"
  419. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)
  420. % endif
  421. % endif
  422. % endfor
  423. endif
  424. strip-shared_cxx: shared_cxx
  425. ifeq ($(CONFIG),opt)
  426. % for lib in libs:
  427. % if lib.language == "c++":
  428. % if lib.build == "all":
  429. $(E) "[STRIP] Stripping lib${lib.name}.so"
  430. $(Q) $(STRIP) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)
  431. % endif
  432. % endif
  433. % endfor
  434. endif
  435. % for p in protos:
  436. gens/${p}.pb.cc: ${p}.proto $(PROTOC_PLUGINS)
  437. $(E) "[PROTOC] Generating protobuf CC file from $<"
  438. $(Q) mkdir -p `dirname $@`
  439. $(Q) $(PROTOC) --cpp_out=gens --grpc_out=gens --plugin=protoc-gen-grpc=bins/$(CONFIG)/cpp_plugin $<
  440. % endfor
  441. objs/$(CONFIG)/%.o : %.c
  442. $(E) "[C] Compiling $<"
  443. $(Q) mkdir -p `dirname $@`
  444. $(Q) $(CC) $(CFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  445. objs/$(CONFIG)/%.o : gens/%.pb.cc
  446. $(E) "[CXX] Compiling $<"
  447. $(Q) mkdir -p `dirname $@`
  448. $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  449. objs/$(CONFIG)/src/compiler/%.o : src/compiler/%.cc
  450. $(E) "[HOSTCXX] Compiling $<"
  451. $(Q) mkdir -p `dirname $@`
  452. $(Q) $(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  453. objs/$(CONFIG)/%.o : %.cc
  454. $(E) "[CXX] Compiling $<"
  455. $(Q) mkdir -p `dirname $@`
  456. $(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -MF $(addsuffix .dep, $(basename $@)) -c -o $@ $<
  457. install: install_c install_cxx
  458. install_c: install-headers_c install-static_c install-shared_c
  459. install_cxx: install-headers_cxx install-static_cxx install-shared_cxx
  460. install-headers: install-headers_c install-headers_cxx
  461. install-headers_c:
  462. $(E) "[INSTALL] Installing public C headers"
  463. $(Q) $(foreach h, $(PUBLIC_HEADERS_C), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1
  464. install-headers_cxx:
  465. $(E) "[INSTALL] Installing public C++ headers"
  466. $(Q) $(foreach h, $(PUBLIC_HEADERS_CXX), $(INSTALL) $(h) $(prefix)/$(h) && ) exit 0 || exit 1
  467. install-static: install-static_c install-static_cxx
  468. install-static_c: static_c strip-static_c
  469. % for lib in libs:
  470. % if lib.language == "c":
  471. % if lib.build == "all":
  472. $(E) "[INSTALL] Installing lib${lib.name}.a"
  473. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
  474. % endif
  475. % endif
  476. % endfor
  477. install-static_cxx: static_cxx strip-static_cxx
  478. % for lib in libs:
  479. % if lib.language == "c++":
  480. % if lib.build == "all":
  481. $(E) "[INSTALL] Installing lib${lib.name}.a"
  482. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.a $(prefix)/lib/lib${lib.name}.a
  483. % endif
  484. % endif
  485. % endfor
  486. install-shared_c: shared_c strip-shared_c
  487. % for lib in libs:
  488. % if lib.language == "c":
  489. % if lib.build == "all":
  490. ifeq ($(SYSTEM),MINGW32)
  491. $(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
  492. $(Q) $(INSTALL) libs/$(CONFIG)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
  493. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
  494. else
  495. $(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
  496. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
  497. ifneq ($(SYSTEM),Darwin)
  498. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
  499. endif
  500. endif
  501. % endif
  502. % endif
  503. % endfor
  504. ifneq ($(SYSTEM),MINGW32)
  505. ifneq ($(SYSTEM),Darwin)
  506. $(Q) ldconfig
  507. endif
  508. endif
  509. install-shared_cxx: shared_cxx strip-shared_cxx
  510. % for lib in libs:
  511. % if lib.language == "c++":
  512. % if lib.build == "all":
  513. ifeq ($(SYSTEM),MINGW32)
  514. $(E) "[INSTALL] Installing ${lib.name}.$(SHARED_EXT)"
  515. $(Q) $(INSTALL) libs/$(CONFIG)/${lib.name}.$(SHARED_EXT) $(prefix)/lib/${lib.name}.$(SHARED_EXT)
  516. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}-imp.a $(prefix)/lib/lib${lib.name}-imp.a
  517. else
  518. $(E) "[INSTALL] Installing lib${lib.name}.$(SHARED_EXT)"
  519. $(Q) $(INSTALL) libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.$(SHARED_EXT)
  520. ifneq ($(SYSTEM),Darwin)
  521. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) $(prefix)/lib/lib${lib.name}.so
  522. endif
  523. endif
  524. % endif
  525. % endif
  526. % endfor
  527. ifneq ($(SYSTEM),MINGW32)
  528. ifneq ($(SYSTEM),Darwin)
  529. $(Q) ldconfig
  530. endif
  531. endif
  532. clean:
  533. $(Q) $(RM) -rf objs libs bins gens
  534. # The various libraries
  535. % for lib in libs:
  536. ${makelib(lib)}
  537. % endfor
  538. # All of the test targets, and protoc plugins
  539. % for tgt in targets:
  540. ${maketarget(tgt)}
  541. % endfor
  542. <%def name="makelib(lib)">
  543. LIB${lib.name.upper()}_SRC = \\
  544. % for src in lib.src:
  545. ${proto_to_cc(src)} \\
  546. % endfor
  547. % if "public_headers" in lib:
  548. % if lib.language == "c++":
  549. PUBLIC_HEADERS_CXX += \\
  550. % else:
  551. PUBLIC_HEADERS_C += \\
  552. % endif
  553. % for hdr in lib.public_headers:
  554. ${hdr} \\
  555. % endfor
  556. % endif
  557. LIB${lib.name.upper()}_OBJS = $(addprefix objs/$(CONFIG)/, $(addsuffix .o, $(basename $(LIB${lib.name.upper()}_SRC))))
  558. ## If the library requires OpenSSL with ALPN, let's add some restrictions.
  559. % if lib.get('secure', True):
  560. ifeq ($(NO_SECURE),true)
  561. # You can't build secure libraries if you don't have OpenSSL with ALPN.
  562. libs/$(CONFIG)/lib${lib.name}.a: openssl_dep_error
  563. % if lib.build == "all":
  564. ifeq ($(SYSTEM),MINGW32)
  565. libs/$(CONFIG)/${lib.name}.$(SHARED_EXT): openssl_dep_error
  566. else
  567. libs/$(CONFIG)/lib${lib.name}.$(SHARED_EXT): openssl_dep_error
  568. endif
  569. % endif
  570. else
  571. ifneq ($(OPENSSL_DEP),)
  572. % for src in lib.src:
  573. ${src}: $(OPENSSL_DEP)
  574. % endfor
  575. endif
  576. libs/$(CONFIG)/lib${lib.name}.a: $(ZLIB_DEP) $(OPENSSL_DEP) $(LIB${lib.name.upper()}_OBJS)
  577. ## The else here corresponds to the if secure earlier.
  578. % else:
  579. libs/$(CONFIG)/lib${lib.name}.a: $(ZLIB_DEP) $(LIB${lib.name.upper()}_OBJS)
  580. % endif
  581. $(E) "[AR] Creating $@"
  582. $(Q) mkdir -p `dirname $@`
  583. $(Q) rm -f libs/$(CONFIG)/lib${lib.name}.a
  584. $(Q) $(AR) rcs libs/$(CONFIG)/lib${lib.name}.a $(LIB${lib.name.upper()}_OBJS)
  585. % if lib.get('baselib', False):
  586. % if lib.get('secure', True):
  587. $(Q) rm -rf tmp-merge
  588. $(Q) mkdir tmp-merge
  589. $(Q) ( cd tmp-merge ; $(AR) x ../libs/$(CONFIG)/lib${lib.name}.a )
  590. $(Q) for l in $(OPENSSL_MERGE_LIBS) ; do ( cd tmp-merge ; <%text>ar x ../$${l}</%text> ) ; done
  591. $(Q) rm -f libs/$(CONFIG)/lib${lib.name}.a tmp-merge/__.SYMDEF*
  592. $(Q) ar rcs libs/$(CONFIG)/lib${lib.name}.a tmp-merge/*
  593. $(Q) rm -rf tmp-merge
  594. % endif
  595. % endif
  596. ifeq ($(SYSTEM),Darwin)
  597. $(Q) ranlib libs/$(CONFIG)/lib${lib.name}.a
  598. endif
  599. <%
  600. if lib.language == 'c++':
  601. ld = '$(LDXX)'
  602. else:
  603. ld = '$(LD)'
  604. out_base = 'libs/$(CONFIG)/' + lib.name
  605. out_libbase = 'libs/$(CONFIG)/lib' + lib.name
  606. common = '$(LIB' + lib.name.upper() + '_OBJS) $(LDLIBS)'
  607. libs = ''
  608. lib_deps = ' $(ZLIB_DEP)'
  609. mingw_libs = ''
  610. mingw_lib_deps = ' $(ZLIB_DEP)'
  611. for dep in lib.get('deps', []):
  612. libs = libs + ' -l' + dep
  613. lib_deps = lib_deps + ' libs/$(CONFIG)/lib' + dep + '.$(SHARED_EXT)'
  614. mingw_libs = mingw_libs + ' -l' + dep + '-imp'
  615. mingw_lib_deps = mingw_lib_deps + 'libs/$(CONFIG)/' + dep + '.$(SHARED_EXT)'
  616. if lib.get('secure', True):
  617. common = common + ' $(LDLIBS_SECURE) $(OPENSSL_MERGE_LIBS)'
  618. lib_deps = lib_deps + ' $(OPENSSL_DEP)'
  619. mingw_lib_deps = mingw_lib_deps + ' $(OPENSSL_DEP)'
  620. %>
  621. % if lib.build == "all":
  622. ifeq ($(SYSTEM),MINGW32)
  623. ${out_base}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${mingw_lib_deps}
  624. $(E) "[LD] Linking $@"
  625. $(Q) mkdir -p `dirname $@`
  626. $(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}
  627. else
  628. ${out_libbase}.$(SHARED_EXT): $(LIB${lib.name.upper()}_OBJS) ${lib_deps}
  629. $(E) "[LD] Linking $@"
  630. $(Q) mkdir -p `dirname $@`
  631. ifeq ($(SYSTEM),Darwin)
  632. $(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -dynamiclib -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
  633. else
  634. $(Q) ${ld} $(LDFLAGS) -Llibs/$(CONFIG) -shared -Wl,-soname,lib${lib.name}.so.${settings.version.major} -o ${out_libbase}.$(SHARED_EXT) ${common}${libs}
  635. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) ${out_libbase}.so.${settings.version.major}
  636. $(Q) ln -sf lib${lib.name}.$(SHARED_EXT) ${out_libbase}.so
  637. endif
  638. endif
  639. % endif
  640. ## If the lib was secure, we have to close the Makefile's if that tested
  641. ## the presence of an ALPN-capable OpenSSL.
  642. % if lib.get('secure', True):
  643. endif
  644. % endif
  645. % if lib.get('secure', True):
  646. ifneq ($(NO_SECURE),true)
  647. % endif
  648. ifneq ($(NO_DEPS),true)
  649. -include $(LIB${lib.name.upper()}_OBJS:.o=.dep)
  650. endif
  651. % if lib.get('secure', True):
  652. endif
  653. % endif
  654. % for src in lib.src:
  655. % if not proto_re.match(src):
  656. objs/$(CONFIG)/${os.path.splitext(src)[0]}.o: \
  657. % for src2 in lib.src:
  658. % if proto_re.match(src2):
  659. ${proto_to_cc(src2)}\
  660. % endif
  661. % endfor
  662. % endif
  663. % endfor
  664. </%def>
  665. <%def name="maketarget(tgt)">
  666. ${tgt.name.upper()}_SRC = \\
  667. % for src in tgt.src:
  668. ${proto_to_cc(src)} \\
  669. % endfor
  670. ${tgt.name.upper()}_OBJS = $(addprefix objs/$(CONFIG)/, $(addsuffix .o, $(basename $(${tgt.name.upper()}_SRC))))
  671. % if tgt.get('secure', True):
  672. ifeq ($(NO_SECURE),true)
  673. # You can't build secure targets if you don't have OpenSSL with ALPN.
  674. bins/$(CONFIG)/${tgt.name}: openssl_dep_error
  675. else
  676. % endif
  677. ##
  678. ## We're not trying to add a dependency on building zlib and openssl here,
  679. ## as it's already done in the libraries. We're assuming that the build
  680. ## trickles down, and that a secure target requires a secure version of
  681. ## a library.
  682. ##
  683. ## That simplifies the codegen a bit, but prevents a fully defined Makefile.
  684. ## I can live with that.
  685. ##
  686. bins/$(CONFIG)/${tgt.name}: $(${tgt.name.upper()}_OBJS)\
  687. % for dep in tgt.deps:
  688. libs/$(CONFIG)/lib${dep}.a\
  689. % endfor
  690. % if tgt.language == "c++":
  691. ## C++ targets specificies.
  692. % if tgt.build == 'protoc':
  693. $(E) "[HOSTLD] Linking $@"
  694. $(Q) mkdir -p `dirname $@`
  695. $(Q) $(HOST_LDXX) $(HOST_LDFLAGS) $(${tgt.name.upper()}_OBJS)\
  696. % else:
  697. $(E) "[LD] Linking $@"
  698. $(Q) mkdir -p `dirname $@`
  699. $(Q) $(LDXX) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
  700. % endif
  701. % if tgt.build == 'test':
  702. $(GTEST_LIB)\
  703. % endif
  704. % else:
  705. ## C-only targets specificities.
  706. $(E) "[LD] Linking $@"
  707. $(Q) mkdir -p `dirname $@`
  708. $(Q) $(LD) $(LDFLAGS) $(${tgt.name.upper()}_OBJS)\
  709. % endif
  710. % for dep in tgt.deps:
  711. libs/$(CONFIG)/lib${dep}.a\
  712. % endfor
  713. % if tgt.language == "c++":
  714. % if tgt.build == 'protoc':
  715. $(HOST_LDLIBSXX)\
  716. % else:
  717. $(LDLIBSXX)\
  718. % endif
  719. % endif
  720. % if tgt.build == 'protoc':
  721. $(HOST_LDLIBS)\
  722. % else:
  723. $(LDLIBS)\
  724. % endif
  725. % if tgt.build == 'protoc':
  726. $(HOST_LDLIBS_PROTOC)\
  727. % elif tgt.get('secure', True):
  728. $(LDLIBS_SECURE)\
  729. % endif
  730. -o bins/$(CONFIG)/${tgt.name}
  731. % if tgt.get('secure', True):
  732. endif
  733. % endif
  734. % for src in tgt.src:
  735. objs/$(CONFIG)/${os.path.splitext(src)[0]}.o: \
  736. % for dep in tgt.deps:
  737. libs/$(CONFIG)/lib${dep}.a\
  738. % endfor
  739. % endfor
  740. deps_${tgt.name}: $(${tgt.name.upper()}_OBJS:.o=.dep)
  741. % if tgt.get('secure', True):
  742. ifneq ($(NO_SECURE),true)
  743. % endif
  744. ifneq ($(NO_DEPS),true)
  745. -include $(${tgt.name.upper()}_OBJS:.o=.dep)
  746. endif
  747. % if tgt.get('secure', True):
  748. endif
  749. % endif
  750. </%def>
  751. .PHONY: all strip tools \
  752. dep_error openssl_dep_error openssl_dep_message git_update stop \
  753. buildtests buildtests_c buildtests_cxx \
  754. test test_c test_cxx \
  755. install install_c install_cxx \
  756. install-headers install-headers_c install-headers_cxx \
  757. install-shared install-shared_c install-shared_cxx \
  758. install-static install-static_c install-static_cxx \
  759. strip strip-shared strip-static \
  760. strip_c strip-shared_c strip-static_c \
  761. strip_cxx strip-shared_cxx strip-static_cxx \
  762. dep_c dep_cxx bins_dep_c bins_dep_cxx \
  763. clean