config_brpc.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #!/usr/bin/env sh
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # 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, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. SYSTEM=$(uname -s)
  17. if [ "$SYSTEM" = "Darwin" ]; then
  18. if [ -z "$BASH" ] || [ "$BASH" = "/bin/sh" ] ; then
  19. ECHO=echo
  20. else
  21. ECHO='echo -e'
  22. fi
  23. SO=dylib
  24. LDD="otool -L"
  25. if [ "$(getopt -V)" = " --" ]; then
  26. >&2 $ECHO "gnu-getopt must be installed and used"
  27. exit 1
  28. fi
  29. else
  30. if [ -z "$BASH" ]; then
  31. ECHO=echo
  32. else
  33. ECHO='echo -e'
  34. fi
  35. SO=so
  36. LDD=ldd
  37. fi
  38. TEMP=`getopt -o v: --long headers:,libs:,cc:,cxx:,with-glog,with-thrift,with-mesalink,nodebugsymbols -n 'config_brpc' -- "$@"`
  39. WITH_GLOG=0
  40. WITH_THRIFT=0
  41. WITH_MESALINK=0
  42. DEBUGSYMBOLS=-g
  43. if [ $? != 0 ] ; then >&2 $ECHO "Terminating..."; exit 1 ; fi
  44. # Note the quotes around `$TEMP': they are essential!
  45. eval set -- "$TEMP"
  46. if [ "$SYSTEM" = "Darwin" ]; then
  47. REALPATH=realpath
  48. else
  49. REALPATH="readlink -f"
  50. fi
  51. # Convert to abspath always so that generated mk is include-able from everywhere
  52. while true; do
  53. case "$1" in
  54. --headers ) HDRS_IN="$(${REALPATH} $2)"; shift 2 ;;
  55. --libs ) LIBS_IN="$(${REALPATH} $2)"; shift 2 ;;
  56. --cc ) CC=$2; shift 2 ;;
  57. --cxx ) CXX=$2; shift 2 ;;
  58. --with-glog ) WITH_GLOG=1; shift 1 ;;
  59. --with-thrift) WITH_THRIFT=1; shift 1 ;;
  60. --with-mesalink) WITH_MESALINK=1; shift 1 ;;
  61. --nodebugsymbols ) DEBUGSYMBOLS=; shift 1 ;;
  62. -- ) shift; break ;;
  63. * ) break ;;
  64. esac
  65. done
  66. if [ -z "$CC" ]; then
  67. if [ ! -z "$CXX" ]; then
  68. >&2 $ECHO "--cc and --cxx must be both set or unset"
  69. exit 1
  70. fi
  71. CC=gcc
  72. CXX=g++
  73. if [ "$SYSTEM" = "Darwin" ]; then
  74. CC=clang
  75. CXX=clang++
  76. fi
  77. elif [ -z "$CXX" ]; then
  78. >&2 $ECHO "--cc and --cxx must be both set or unset"
  79. exit 1
  80. fi
  81. GCC_VERSION=$($CXX tools/print_gcc_version.cc -o print_gcc_version && ./print_gcc_version && rm ./print_gcc_version)
  82. if [ $GCC_VERSION -gt 0 ] && [ $GCC_VERSION -lt 40800 ]; then
  83. >&2 $ECHO "GCC is too old, please install a newer version supporting C++11"
  84. exit 1
  85. fi
  86. if [ -z "$HDRS_IN" ] || [ -z "$LIBS_IN" ]; then
  87. >&2 $ECHO "config_brpc: --headers=HDRPATHS --libs=LIBPATHS must be specified"
  88. exit 1
  89. fi
  90. find_dir_of_lib() {
  91. local lib=$(find ${LIBS_IN} -name "lib${1}.a" -o -name "lib${1}.$SO" 2>/dev/null | head -n1)
  92. if [ ! -z "$lib" ]; then
  93. dirname $lib
  94. fi
  95. }
  96. find_dir_of_lib_or_die() {
  97. local dir=$(find_dir_of_lib $1)
  98. if [ -z "$dir" ]; then
  99. >&2 $ECHO "Fail to find $1 from --libs"
  100. exit 1
  101. else
  102. $ECHO $dir
  103. fi
  104. }
  105. find_bin() {
  106. TARGET_BIN=$(find -L ${LIBS_IN} -type f -name "$1" 2>/dev/null | head -n1)
  107. if [ ! -z "$TARGET_BIN" ]; then
  108. $ECHO $TARGET_BIN
  109. else
  110. which "$1" 2>/dev/null
  111. fi
  112. }
  113. find_bin_or_die() {
  114. TARGET_BIN=$(find_bin "$1")
  115. if [ ! -z "$TARGET_BIN" ]; then
  116. $ECHO $TARGET_BIN
  117. else
  118. >&2 $ECHO "Fail to find $1"
  119. exit 1
  120. fi
  121. }
  122. find_dir_of_header() {
  123. find -L ${HDRS_IN} -path "*/$1" | head -n1 | sed "s|$1||g"
  124. }
  125. find_dir_of_header_excluding() {
  126. find -L ${HDRS_IN} -path "*/$1" | grep -v "$2\$" | head -n1 | sed "s|$1||g"
  127. }
  128. find_dir_of_header_or_die() {
  129. if [ -z "$2" ]; then
  130. local dir=$(find_dir_of_header $1)
  131. else
  132. local dir=$(find_dir_of_header_excluding $1 $2)
  133. fi
  134. if [ -z "$dir" ]; then
  135. >&2 $ECHO "Fail to find $1 from --headers"
  136. exit 1
  137. fi
  138. $ECHO $dir
  139. }
  140. if [ "$SYSTEM" = "Darwin" ]; then
  141. OPENSSL_LIB="/usr/local/opt/openssl/lib"
  142. OPENSSL_HDR="/usr/local/opt/openssl/include"
  143. else
  144. # User specified path of openssl, if not given it's empty
  145. OPENSSL_LIB=$(find_dir_of_lib ssl)
  146. # Inconvenient to check these headers in baidu-internal
  147. #PTHREAD_HDR=$(find_dir_of_header_or_die pthread.h)
  148. OPENSSL_HDR=$(find_dir_of_header_or_die openssl/ssl.h)
  149. fi
  150. if [ $WITH_MESALINK != 0 ]; then
  151. MESALINK_HDR=$(find_dir_of_header_or_die mesalink/openssl/ssl.h)
  152. OPENSSL_HDR="$OPENSSL_HDR\n$MESALINK_HDR"
  153. fi
  154. STATIC_LINKINGS=
  155. DYNAMIC_LINKINGS="-lpthread -lssl -lcrypto -ldl -lz"
  156. if [ $WITH_MESALINK != 0 ]; then
  157. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lmesalink"
  158. fi
  159. if [ "$SYSTEM" = "Linux" ]; then
  160. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lrt"
  161. fi
  162. if [ "$SYSTEM" = "Darwin" ]; then
  163. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreFoundation"
  164. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreGraphics"
  165. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreData"
  166. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreText"
  167. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework Security"
  168. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework Foundation"
  169. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_MallocExtension_ReleaseFreeMemory"
  170. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStart"
  171. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
  172. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
  173. fi
  174. append_linking() {
  175. if [ -f $1/lib${2}.a ]; then
  176. if [ "$SYSTEM" = "Darwin" ]; then
  177. # *.a must be explicitly specified in clang
  178. STATIC_LINKINGS="$STATIC_LINKINGS $1/lib${2}.a"
  179. else
  180. STATIC_LINKINGS="$STATIC_LINKINGS -l$2"
  181. fi
  182. export STATICALLY_LINKED_$2=1
  183. else
  184. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -l$2"
  185. export STATICALLY_LINKED_$2=0
  186. fi
  187. }
  188. GFLAGS_LIB=$(find_dir_of_lib_or_die gflags)
  189. append_linking $GFLAGS_LIB gflags
  190. PROTOBUF_LIB=$(find_dir_of_lib_or_die protobuf)
  191. append_linking $PROTOBUF_LIB protobuf
  192. LEVELDB_LIB=$(find_dir_of_lib_or_die leveldb)
  193. # required by leveldb
  194. if [ -f $LEVELDB_LIB/libleveldb.a ]; then
  195. if [ -f $LEVELDB_LIB/libleveldb.$SO ]; then
  196. if $LDD $LEVELDB_LIB/libleveldb.$SO | grep -q libsnappy; then
  197. SNAPPY_LIB=$(find_dir_of_lib snappy)
  198. REQUIRE_SNAPPY="yes"
  199. fi
  200. fi
  201. if [ -z "$REQUIRE_SNAPPY" ]; then
  202. if [ "$SYSTEM" = "Darwin" ]; then
  203. STATIC_LINKINGS="$STATIC_LINKINGS $LEVELDB_LIB/libleveldb.a"
  204. else
  205. STATIC_LINKINGS="$STATIC_LINKINGS -lleveldb"
  206. fi
  207. elif [ -f $SNAPPY_LIB/libsnappy.a ]; then
  208. if [ "$SYSTEM" = "Darwin" ]; then
  209. STATIC_LINKINGS="$STATIC_LINKINGS $LEVELDB_LIB/libleveldb.a $SNAPPY_LIB/libsnappy.a"
  210. else
  211. STATIC_LINKINGS="$STATIC_LINKINGS -lleveldb -lsnappy"
  212. fi
  213. else
  214. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lleveldb"
  215. fi
  216. else
  217. DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lleveldb"
  218. fi
  219. PROTOC=$(find_bin_or_die protoc)
  220. GFLAGS_HDR=$(find_dir_of_header_or_die gflags/gflags.h)
  221. # namespace of gflags may not be google, grep it from source.
  222. GFLAGS_NS=$(grep "namespace [_A-Za-z0-9]\+ {" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $2}')
  223. if [ "$GFLAGS_NS" = "GFLAGS_NAMESPACE" ]; then
  224. GFLAGS_NS=$(grep "#define GFLAGS_NAMESPACE [_A-Za-z0-9]\+" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $3}')
  225. fi
  226. if [ -z "$GFLAGS_NS" ]; then
  227. >&2 $ECHO "Fail to grep namespace of gflags source $GFLAGS_HDR/gflags/gflags_declare.h"
  228. exit 1
  229. fi
  230. PROTOBUF_HDR=$(find_dir_of_header_or_die google/protobuf/message.h)
  231. LEVELDB_HDR=$(find_dir_of_header_or_die leveldb/db.h)
  232. HDRS=$($ECHO "$GFLAGS_HDR\n$PROTOBUF_HDR\n$LEVELDB_HDR\n$OPENSSL_HDR" | sort | uniq)
  233. LIBS=$($ECHO "$GFLAGS_LIB\n$PROTOBUF_LIB\n$LEVELDB_LIB\n$OPENSSL_LIB\n$SNAPPY_LIB" | sort | uniq)
  234. absent_in_the_list() {
  235. TMP=`$ECHO "$1\n$2" | sort | uniq`
  236. if [ "${TMP}" = "$2" ]; then
  237. return 1
  238. fi
  239. return 0
  240. }
  241. OUTPUT_CONTENT="# Generated by config_brpc.sh, don't modify manually"
  242. append_to_output() {
  243. OUTPUT_CONTENT="${OUTPUT_CONTENT}\n$*"
  244. }
  245. # $1: libname, $2: indentation
  246. append_to_output_headers() {
  247. if absent_in_the_list "$1" "$HDRS"; then
  248. append_to_output "${2}HDRS+=$1"
  249. HDRS=`$ECHO "${HDRS}\n$1" | sort | uniq`
  250. fi
  251. }
  252. # $1: libname, $2: indentation
  253. append_to_output_libs() {
  254. if absent_in_the_list "$1" "$LIBS"; then
  255. append_to_output "${2}LIBS+=$1"
  256. LIBS=`$ECHO "${LIBS}\n$1" | sort | uniq`
  257. fi
  258. }
  259. # $1: libdir, $2: libname, $3: indentation
  260. append_to_output_linkings() {
  261. if [ -f $1/lib$2.a ]; then
  262. append_to_output_libs $1 $3
  263. if [ "$SYSTEM" = "Darwin" ]; then
  264. append_to_output "${3}STATIC_LINKINGS+=$1/lib$2.a"
  265. else
  266. append_to_output "${3}STATIC_LINKINGS+=-l$2"
  267. fi
  268. export STATICALLY_LINKED_$2=1
  269. else
  270. append_to_output_libs $1 $3
  271. append_to_output "${3}DYNAMIC_LINKINGS+=-l$2"
  272. export STATICALLY_LINKED_$2=0
  273. fi
  274. }
  275. #can't use \n in texts because sh does not support -e
  276. append_to_output "SYSTEM=$SYSTEM"
  277. append_to_output "HDRS=$($ECHO $HDRS)"
  278. append_to_output "LIBS=$($ECHO $LIBS)"
  279. append_to_output "PROTOC=$PROTOC"
  280. append_to_output "PROTOBUF_HDR=$PROTOBUF_HDR"
  281. append_to_output "CC=$CC"
  282. append_to_output "CXX=$CXX"
  283. append_to_output "GCC_VERSION=$GCC_VERSION"
  284. append_to_output "STATIC_LINKINGS=$STATIC_LINKINGS"
  285. append_to_output "DYNAMIC_LINKINGS=$DYNAMIC_LINKINGS"
  286. CPPFLAGS="-DBRPC_WITH_GLOG=$WITH_GLOG -DGFLAGS_NS=$GFLAGS_NS"
  287. if [ ! -z "$DEBUGSYMBOLS" ]; then
  288. CPPFLAGS="${CPPFLAGS} $DEBUGSYMBOLS"
  289. fi
  290. if [ "$SYSTEM" = "Darwin" ]; then
  291. CPPFLAGS="${CPPFLAGS} -Wno-deprecated-declarations -Wno-inconsistent-missing-override"
  292. version=`sw_vers -productVersion | awk -F '.' '{print $1 "." $2}'`
  293. if [[ `echo "$version<10.12" | bc -l` == 1 ]]; then
  294. CPPFLAGS="${CPPFLAGS} -DNO_CLOCK_GETTIME_IN_MAC"
  295. fi
  296. fi
  297. if [ $WITH_THRIFT != 0 ]; then
  298. THRIFT_LIB=$(find_dir_of_lib_or_die thriftnb)
  299. THRIFT_HDR=$(find_dir_of_header_or_die thrift/Thrift.h)
  300. append_to_output_libs "$THRIFT_LIB"
  301. append_to_output_headers "$THRIFT_HDR"
  302. CPPFLAGS="${CPPFLAGS} -DENABLE_THRIFT_FRAMED_PROTOCOL"
  303. if [ -f "$THRIFT_LIB/libthriftnb.$SO" ]; then
  304. append_to_output "DYNAMIC_LINKINGS+=-lthriftnb -levent -lthrift"
  305. else
  306. append_to_output "STATIC_LINKINGS+=-lthriftnb"
  307. fi
  308. fi
  309. if [ $WITH_MESALINK != 0 ]; then
  310. CPPFLAGS="${CPPFLAGS} -DUSE_MESALINK"
  311. fi
  312. append_to_output "CPPFLAGS=${CPPFLAGS}"
  313. append_to_output "ifeq (\$(NEED_LIBPROTOC), 1)"
  314. PROTOC_LIB=$(find $PROTOBUF_LIB -name "libprotoc.*" | head -n1)
  315. if [ -z "$PROTOC_LIB" ]; then
  316. append_to_output " \$(error \"Fail to find libprotoc\")"
  317. else
  318. # libprotobuf and libprotoc must be linked same statically or dynamically
  319. # otherwise the bin will crash.
  320. if [ $STATICALLY_LINKED_protobuf -gt 0 ]; then
  321. if [ "$SYSTEM" = "Darwin" ]; then
  322. append_to_output " STATIC_LINKINGS+=$(find $PROTOBUF_LIB -name "libprotoc.a" | head -n1)"
  323. else
  324. append_to_output " STATIC_LINKINGS+=-lprotoc"
  325. fi
  326. else
  327. append_to_output " DYNAMIC_LINKINGS+=-lprotoc"
  328. fi
  329. fi
  330. append_to_output "endif"
  331. OLD_HDRS=$HDRS
  332. OLD_LIBS=$LIBS
  333. append_to_output "ifeq (\$(NEED_GPERFTOOLS), 1)"
  334. # required by cpu/heap profiler
  335. TCMALLOC_LIB=$(find_dir_of_lib tcmalloc_and_profiler)
  336. if [ -z "$TCMALLOC_LIB" ]; then
  337. append_to_output " \$(error \"Fail to find gperftools\")"
  338. else
  339. append_to_output_libs "$TCMALLOC_LIB" " "
  340. if [ -f $TCMALLOC_LIB/libtcmalloc.$SO ]; then
  341. append_to_output " DYNAMIC_LINKINGS+=-ltcmalloc_and_profiler"
  342. else
  343. if [ "$SYSTEM" = "Darwin" ]; then
  344. append_to_output " STATIC_LINKINGS+=$TCMALLOC_LIB/libtcmalloc.a"
  345. else
  346. append_to_output " STATIC_LINKINGS+=-ltcmalloc_and_profiler"
  347. fi
  348. fi
  349. fi
  350. append_to_output "endif"
  351. if [ $WITH_GLOG != 0 ]; then
  352. GLOG_LIB=$(find_dir_of_lib_or_die glog)
  353. GLOG_HDR=$(find_dir_of_header_or_die glog/logging.h windows/glog/logging.h)
  354. append_to_output_libs "$GLOG_LIB"
  355. append_to_output_headers "$GLOG_HDR"
  356. if [ -f "$GLOG_LIB/libglog.$SO" ]; then
  357. append_to_output "DYNAMIC_LINKINGS+=-lglog"
  358. else
  359. if [ "$SYSTEM" = "Darwin" ]; then
  360. append_to_output "STATIC_LINKINGS+=$GLOG_LIB/libglog.a"
  361. else
  362. append_to_output "STATIC_LINKINGS+=-lglog"
  363. fi
  364. fi
  365. fi
  366. # required by UT
  367. #gtest
  368. GTEST_LIB=$(find_dir_of_lib gtest)
  369. HDRS=$OLD_HDRS
  370. LIBS=$OLD_LIBS
  371. append_to_output "ifeq (\$(NEED_GTEST), 1)"
  372. if [ -z "$GTEST_LIB" ]; then
  373. append_to_output " \$(error \"Fail to find gtest\")"
  374. else
  375. GTEST_HDR=$(find_dir_of_header_or_die gtest/gtest.h)
  376. append_to_output_libs $GTEST_LIB " "
  377. append_to_output_headers $GTEST_HDR " "
  378. append_to_output_linkings $GTEST_LIB gtest " "
  379. append_to_output_linkings $GTEST_LIB gtest_main " "
  380. fi
  381. append_to_output "endif"
  382. # generate src/butil/config.h
  383. cat << EOF > src/butil/config.h
  384. // This file is auto-generated by $(basename "$0"). DON'T edit it!
  385. #ifndef BUTIL_CONFIG_H
  386. #define BUTIL_CONFIG_H
  387. #ifdef BRPC_WITH_GLOG
  388. #undef BRPC_WITH_GLOG
  389. #endif
  390. #define BRPC_WITH_GLOG $WITH_GLOG
  391. #endif // BUTIL_CONFIG_H
  392. EOF
  393. # write to config.mk
  394. $ECHO "$OUTPUT_CONTENT" > config.mk