Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. NEED_LIBPROTOC=1
  18. include config.mk
  19. # Notes on the flags:
  20. # 1. Added -fno-omit-frame-pointer: perf/tcmalloc-profiler use frame pointers by default
  21. # 2. Added -D__const__= : Avoid over-optimizations of TLS variables by GCC>=4.8
  22. # 3. Removed -Werror: Not block compilation for non-vital warnings, especially when the
  23. # code is tested on newer systems. If the code is used in production, add -Werror back
  24. CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -DBRPC_REVISION=\"$(shell ./tools/get_brpc_revision.sh .)\"
  25. CXXFLAGS=$(CPPFLAGS) -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x
  26. CFLAGS=$(CPPFLAGS) -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-unused-parameter -fno-omit-frame-pointer
  27. DEBUG_CXXFLAGS = $(filter-out -DNDEBUG,$(CXXFLAGS)) -DUNIT_TEST -DBVAR_NOT_LINK_DEFAULT_VARIABLES
  28. DEBUG_CFLAGS = $(filter-out -DNDEBUG,$(CFLAGS)) -DUNIT_TEST
  29. HDRPATHS=-I./src $(addprefix -I, $(HDRS))
  30. LIBPATHS = $(addprefix -L, $(LIBS))
  31. COMMA = ,
  32. SOPATHS = $(addprefix -Wl$(COMMA)-rpath$(COMMA), $(LIBS))
  33. SRCEXTS = .c .cc .cpp .proto
  34. SOEXT = so
  35. ifeq ($(SYSTEM),Darwin)
  36. SOEXT = dylib
  37. endif
  38. #required by butil/crc32.cc to boost performance for 10x
  39. ifeq ($(shell test $(GCC_VERSION) -ge 40400; echo $$?),0)
  40. CXXFLAGS+=-msse4 -msse4.2
  41. endif
  42. #not solved yet
  43. ifeq ($(CC),gcc)
  44. ifeq ($(shell test $(GCC_VERSION) -ge 70000; echo $$?),0)
  45. CXXFLAGS+=-Wno-aligned-new
  46. endif
  47. endif
  48. BUTIL_SOURCES = \
  49. src/butil/third_party/dmg_fp/g_fmt.cc \
  50. src/butil/third_party/dmg_fp/dtoa_wrapper.cc \
  51. src/butil/third_party/dynamic_annotations/dynamic_annotations.c \
  52. src/butil/third_party/icu/icu_utf.cc \
  53. src/butil/third_party/superfasthash/superfasthash.c \
  54. src/butil/third_party/modp_b64/modp_b64.cc \
  55. src/butil/third_party/symbolize/demangle.cc \
  56. src/butil/third_party/symbolize/symbolize.cc \
  57. src/butil/third_party/snappy/snappy-sinksource.cc \
  58. src/butil/third_party/snappy/snappy-stubs-internal.cc \
  59. src/butil/third_party/snappy/snappy.cc \
  60. src/butil/third_party/murmurhash3/murmurhash3.cpp \
  61. src/butil/arena.cpp \
  62. src/butil/at_exit.cc \
  63. src/butil/atomicops_internals_x86_gcc.cc \
  64. src/butil/base64.cc \
  65. src/butil/big_endian.cc \
  66. src/butil/cpu.cc \
  67. src/butil/debug/alias.cc \
  68. src/butil/debug/asan_invalid_access.cc \
  69. src/butil/debug/crash_logging.cc \
  70. src/butil/debug/debugger.cc \
  71. src/butil/debug/debugger_posix.cc \
  72. src/butil/debug/dump_without_crashing.cc \
  73. src/butil/debug/proc_maps_linux.cc \
  74. src/butil/debug/stack_trace.cc \
  75. src/butil/debug/stack_trace_posix.cc \
  76. src/butil/environment.cc \
  77. src/butil/files/file.cc \
  78. src/butil/files/file_posix.cc \
  79. src/butil/files/file_enumerator.cc \
  80. src/butil/files/file_enumerator_posix.cc \
  81. src/butil/files/file_path.cc \
  82. src/butil/files/file_path_constants.cc \
  83. src/butil/files/memory_mapped_file.cc \
  84. src/butil/files/memory_mapped_file_posix.cc \
  85. src/butil/files/scoped_file.cc \
  86. src/butil/files/scoped_temp_dir.cc \
  87. src/butil/file_util.cc \
  88. src/butil/file_util_posix.cc \
  89. src/butil/guid.cc \
  90. src/butil/guid_posix.cc \
  91. src/butil/hash.cc \
  92. src/butil/lazy_instance.cc \
  93. src/butil/location.cc \
  94. src/butil/memory/aligned_memory.cc \
  95. src/butil/memory/ref_counted.cc \
  96. src/butil/memory/ref_counted_memory.cc \
  97. src/butil/memory/singleton.cc \
  98. src/butil/memory/weak_ptr.cc \
  99. src/butil/posix/file_descriptor_shuffle.cc \
  100. src/butil/posix/global_descriptors.cc \
  101. src/butil/process_util.cc \
  102. src/butil/rand_util.cc \
  103. src/butil/rand_util_posix.cc \
  104. src/butil/fast_rand.cpp \
  105. src/butil/safe_strerror_posix.cc \
  106. src/butil/sha1_portable.cc \
  107. src/butil/strings/latin1_string_conversions.cc \
  108. src/butil/strings/nullable_string16.cc \
  109. src/butil/strings/safe_sprintf.cc \
  110. src/butil/strings/string16.cc \
  111. src/butil/strings/string_number_conversions.cc \
  112. src/butil/strings/string_split.cc \
  113. src/butil/strings/string_piece.cc \
  114. src/butil/strings/string_util.cc \
  115. src/butil/strings/string_util_constants.cc \
  116. src/butil/strings/stringprintf.cc \
  117. src/butil/strings/utf_offset_string_conversions.cc \
  118. src/butil/strings/utf_string_conversion_utils.cc \
  119. src/butil/strings/utf_string_conversions.cc \
  120. src/butil/synchronization/cancellation_flag.cc \
  121. src/butil/synchronization/condition_variable_posix.cc \
  122. src/butil/synchronization/waitable_event_posix.cc \
  123. src/butil/threading/non_thread_safe_impl.cc \
  124. src/butil/threading/platform_thread_posix.cc \
  125. src/butil/threading/simple_thread.cc \
  126. src/butil/threading/thread_checker_impl.cc \
  127. src/butil/threading/thread_collision_warner.cc \
  128. src/butil/threading/thread_id_name_manager.cc \
  129. src/butil/threading/thread_local_posix.cc \
  130. src/butil/threading/thread_local_storage.cc \
  131. src/butil/threading/thread_local_storage_posix.cc \
  132. src/butil/threading/thread_restrictions.cc \
  133. src/butil/threading/watchdog.cc \
  134. src/butil/time/clock.cc \
  135. src/butil/time/default_clock.cc \
  136. src/butil/time/default_tick_clock.cc \
  137. src/butil/time/tick_clock.cc \
  138. src/butil/time/time.cc \
  139. src/butil/time/time_posix.cc \
  140. src/butil/version.cc \
  141. src/butil/logging.cc \
  142. src/butil/class_name.cpp \
  143. src/butil/errno.cpp \
  144. src/butil/find_cstr.cpp \
  145. src/butil/status.cpp \
  146. src/butil/string_printf.cpp \
  147. src/butil/thread_local.cpp \
  148. src/butil/unix_socket.cpp \
  149. src/butil/endpoint.cpp \
  150. src/butil/fd_utility.cpp \
  151. src/butil/files/temp_file.cpp \
  152. src/butil/files/file_watcher.cpp \
  153. src/butil/time.cpp \
  154. src/butil/zero_copy_stream_as_streambuf.cpp \
  155. src/butil/crc32c.cc \
  156. src/butil/containers/case_ignored_flat_map.cpp \
  157. src/butil/iobuf.cpp \
  158. src/butil/binary_printer.cpp \
  159. src/butil/recordio.cc \
  160. src/butil/popen.cpp
  161. ifeq ($(SYSTEM), Linux)
  162. BUTIL_SOURCES += src/butil/file_util_linux.cc \
  163. src/butil/threading/platform_thread_linux.cc \
  164. src/butil/strings/sys_string_conversions_posix.cc
  165. endif
  166. ifeq ($(SYSTEM), Darwin)
  167. BUTIL_SOURCES += src/butil/mac/bundle_locations.mm \
  168. src/butil/mac/foundation_util.mm \
  169. src/butil/file_util_mac.mm \
  170. src/butil/threading/platform_thread_mac.mm \
  171. src/butil/strings/sys_string_conversions_mac.mm \
  172. src/butil/time/time_mac.cc \
  173. src/butil/mac/scoped_mach_port.cc
  174. endif
  175. BUTIL_OBJS = $(addsuffix .o, $(basename $(BUTIL_SOURCES)))
  176. BVAR_DIRS = src/bvar src/bvar/detail
  177. BVAR_SOURCES = $(foreach d,$(BVAR_DIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
  178. BVAR_OBJS = $(addsuffix .o, $(basename $(BVAR_SOURCES)))
  179. BTHREAD_DIRS = src/bthread
  180. BTHREAD_SOURCES = $(foreach d,$(BTHREAD_DIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
  181. BTHREAD_OBJS = $(addsuffix .o, $(basename $(BTHREAD_SOURCES)))
  182. JSON2PB_DIRS = src/json2pb
  183. JSON2PB_SOURCES = $(foreach d,$(JSON2PB_DIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
  184. JSON2PB_OBJS = $(addsuffix .o, $(basename $(JSON2PB_SOURCES)))
  185. BRPC_DIRS = src/brpc src/brpc/details src/brpc/builtin src/brpc/policy
  186. THRIFT_SOURCES = $(foreach d,$(BRPC_DIRS),$(wildcard $(addprefix $(d)/thrift*,$(SRCEXTS))))
  187. BRPC_SOURCES_ALL = $(foreach d,$(BRPC_DIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
  188. BRPC_SOURCES = $(filter-out $(THRIFT_SOURCES), $(BRPC_SOURCES_ALL))
  189. BRPC_PROTOS = $(filter %.proto,$(BRPC_SOURCES))
  190. BRPC_CFAMILIES = $(filter-out %.proto %.pb.cc,$(BRPC_SOURCES))
  191. BRPC_OBJS = $(BRPC_PROTOS:.proto=.pb.o) $(addsuffix .o, $(basename $(BRPC_CFAMILIES)))
  192. MCPACK2PB_SOURCES = \
  193. src/mcpack2pb/field_type.cpp \
  194. src/mcpack2pb/mcpack2pb.cpp \
  195. src/mcpack2pb/parser.cpp \
  196. src/mcpack2pb/serializer.cpp
  197. MCPACK2PB_OBJS = src/idl_options.pb.o $(addsuffix .o, $(basename $(MCPACK2PB_SOURCES)))
  198. ifeq (ENABLE_THRIFT_FRAMED_PROTOCOL, $(findstring ENABLE_THRIFT_FRAMED_PROTOCOL, $(CPPFLAGS)))
  199. THRIFT_OBJS = $(addsuffix .o, $(basename $(THRIFT_SOURCES)))
  200. endif
  201. OBJS=$(BUTIL_OBJS) $(BVAR_OBJS) $(BTHREAD_OBJS) $(JSON2PB_OBJS) $(MCPACK2PB_OBJS) $(BRPC_OBJS) $(THRIFT_OBJS)
  202. BVAR_DEBUG_OBJS=$(BUTIL_OBJS:.o=.dbg.o) $(BVAR_OBJS:.o=.dbg.o)
  203. DEBUG_OBJS = $(OBJS:.o=.dbg.o)
  204. PROTOS=$(BRPC_PROTOS) src/idl_options.proto
  205. .PHONY:all
  206. all: protoc-gen-mcpack libbrpc.a libbrpc.$(SOEXT) output/include output/lib output/bin
  207. .PHONY:debug
  208. debug: test/libbrpc.dbg.$(SOEXT) test/libbvar.dbg.a
  209. .PHONY:clean
  210. clean:
  211. @echo "> Cleaning"
  212. rm -rf src/mcpack2pb/generator.o protoc-gen-mcpack libbrpc.a libbrpc.$(SOEXT) $(OBJS) output/include output/lib output/bin $(PROTOS:.proto=.pb.h) $(PROTOS:.proto=.pb.cc)
  213. .PHONY:clean_debug
  214. clean_debug:
  215. rm -rf test/libbrpc.dbg.$(SOEXT) test/libbvar.dbg.a $(DEBUG_OBJS)
  216. .PRECIOUS: %.o
  217. protoc-gen-mcpack: src/idl_options.pb.cc src/mcpack2pb/generator.o libbrpc.a
  218. @echo "> Linking $@"
  219. ifeq ($(SYSTEM),Linux)
  220. $(CXX) -o $@ $(HDRPATHS) $(LIBPATHS) -std=c++0x -Xlinker "-(" $^ -Wl,-Bstatic $(STATIC_LINKINGS) -Wl,-Bdynamic -Xlinker "-)" $(DYNAMIC_LINKINGS)
  221. else ifeq ($(SYSTEM),Darwin)
  222. $(CXX) -o $@ $(HDRPATHS) $(LIBPATHS) -std=c++0x $^ $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
  223. endif
  224. # force generation of pb headers before compiling to avoid fail-to-import issues in compiling pb.cc
  225. libbrpc.a:$(BRPC_PROTOS:.proto=.pb.h) $(OBJS)
  226. @echo "> Packing $@"
  227. ar crs $@ $(filter %.o,$^)
  228. libbrpc.$(SOEXT):$(BRPC_PROTOS:.proto=.pb.h) $(OBJS)
  229. @echo "> Linking $@"
  230. ifeq ($(SYSTEM),Linux)
  231. $(CXX) -shared -o $@ $(LIBPATHS) $(SOPATHS) -Xlinker "-(" $(filter %.o,$^) -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
  232. else ifeq ($(SYSTEM),Darwin)
  233. $(CXX) -dynamiclib -Wl,-headerpad_max_install_names -o $@ -install_name @rpath/$@ $(LIBPATHS) $(SOPATHS) $(filter %.o,$^) $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
  234. endif
  235. test/libbvar.dbg.a:$(BVAR_DEBUG_OBJS)
  236. @echo "> Packing $@"
  237. @ar crs $@ $^
  238. test/libbrpc.dbg.$(SOEXT):$(BRPC_PROTOS:.proto=.pb.h) $(DEBUG_OBJS)
  239. @echo "> Linking $@"
  240. ifeq ($(SYSTEM),Linux)
  241. $(CXX) -shared -o $@ $(LIBPATHS) $(SOPATHS) -Xlinker "-(" $(filter %.o,$^) -Xlinker "-)" $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
  242. else ifeq ($(SYSTEM),Darwin)
  243. $(CXX) -dynamiclib -Wl,-headerpad_max_install_names -o $@ -install_name @rpath/libbrpc.dbg.$(SOEXT) $(LIBPATHS) $(SOPATHS) $(filter %.o,$^) $(STATIC_LINKINGS) $(DYNAMIC_LINKINGS)
  244. endif
  245. .PHONY:output/include
  246. output/include:
  247. @echo "> Copying to $@"
  248. @for dir in `find src -type f -name "*.h" -exec dirname {} \\; | sed -e 's/^src\///g' -e '/^src$$/d' | sort | uniq`; do mkdir -p $@/$$dir && cp src/$$dir/*.h $@/$$dir/; done
  249. @for dir in `find src -type f -name "*.hpp" -exec dirname {} \\; | sed -e 's/^src\///g' -e '/^src$$/d' | sort | uniq`; do mkdir -p $@/$$dir && cp src/$$dir/*.hpp $@/$$dir/; done
  250. @cp src/idl_options.proto src/idl_options.pb.h $@
  251. .PHONY:output/lib
  252. output/lib:libbrpc.a libbrpc.$(SOEXT)
  253. @echo "> Copying to $@"
  254. @mkdir -p $@
  255. @cp $^ $@
  256. .PHONY:output/bin
  257. output/bin:protoc-gen-mcpack
  258. @echo "> Copying to $@"
  259. @mkdir -p $@
  260. @cp $^ $@
  261. %.pb.cc %.pb.h:%.proto
  262. @echo "> Generating $@"
  263. $(PROTOC) --cpp_out=./src --proto_path=./src --proto_path=$(PROTOBUF_HDR) $<
  264. %.o:%.cpp
  265. @echo "> Compiling $@"
  266. $(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
  267. %http2_rpc_protocol.dbg.o:%http2_rpc_protocol.cpp
  268. @echo "> Compiling $@ with O2"
  269. $(CXX) -c $(HDRPATHS) -O2 $(DEBUG_CXXFLAGS) $< -o $@
  270. %hpack.dbg.o:%hpack.cpp
  271. @echo "> Compiling $@ with O2"
  272. $(CXX) -c $(HDRPATHS) -O2 $(DEBUG_CXXFLAGS) $< -o $@
  273. %.dbg.o:%.cpp
  274. @echo "> Compiling $@"
  275. $(CXX) -c $(HDRPATHS) $(DEBUG_CXXFLAGS) $< -o $@
  276. %.o:%.cc
  277. @echo "> Compiling $@"
  278. $(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
  279. %.dbg.o:%.cc
  280. @echo "> Compiling $@"
  281. $(CXX) -c $(HDRPATHS) $(DEBUG_CXXFLAGS) $< -o $@
  282. %.o:%.mm
  283. @echo "> Compiling $@"
  284. $(CXX) -c $(HDRPATHS) $(CXXFLAGS) $< -o $@
  285. %.dbg.o:%.mm
  286. @echo "> Compiling $@"
  287. $(CXX) -c $(HDRPATHS) $(DEBUG_CXXFLAGS) $< -o $@
  288. %.o:%.c
  289. @echo "> Compiling $@"
  290. $(CC) -c $(HDRPATHS) $(CFLAGS) $< -o $@
  291. %.dbg.o:%.c
  292. @echo "> Compiling $@"
  293. $(CC) -c $(HDRPATHS) $(DEBUG_CFLAGS) $< -o $@