config_brpc.sh 11.8 KB
Newer Older
gejun's avatar
gejun committed
1 2
SYSTEM=$(uname -s)
if [ "$SYSTEM" = "Darwin" ]; then
3 4 5 6 7
    if [ -z "$BASH" ] || [ "$BASH" = "/bin/sh" ] ; then
        ECHO=echo
    else
        ECHO='echo -e'
    fi
gejun's avatar
gejun committed
8 9 10 11 12 13
    SO=dylib
    LDD="otool -L"
    if [ "$(getopt -V)" = " --" ]; then
        >&2 $ECHO "gnu-getopt must be installed and used"
        exit 1
    fi
14
else
gejun's avatar
gejun committed
15 16 17 18 19 20 21
    if [ -z "$BASH" ]; then
        ECHO=echo
    else
        ECHO='echo -e'
    fi
    SO=so
    LDD=ldd
22
fi
gejun's avatar
gejun committed
23

24
TEMP=`getopt -o v: --long headers:,libs:,cc:,cxx:,with-glog,with-thrift,nodebugsymbols -n 'config_brpc' -- "$@"`
25
WITH_GLOG=0
26
WITH_THRIFT=0
27
DEBUGSYMBOLS=-g
28

gejun's avatar
gejun committed
29
if [ $? != 0 ] ; then >&2 $ECHO "Terminating..."; exit 1 ; fi
30 31 32 33

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

34 35 36 37 38 39
if [ "$SYSTEM" = "Darwin" ]; then
    REALPATH=realpath
else
    REALPATH="readlink -f"
fi

40 41 42
# Convert to abspath always so that generated mk is include-able from everywhere
while true; do
    case "$1" in
43 44
        --headers ) HDRS_IN="$(${REALPATH} $2)"; shift 2 ;;
        --libs ) LIBS_IN="$(${REALPATH} $2)"; shift 2 ;;
45 46
        --cc ) CC=$2; shift 2 ;;
        --cxx ) CXX=$2; shift 2 ;;
47
        --with-glog ) WITH_GLOG=1; shift 1 ;;
48
        --with-thrift) WITH_THRIFT=1; shift 1 ;;
49
        --nodebugsymbols ) DEBUGSYMBOLS=; shift 1 ;;
50 51 52 53
        -- ) shift; break ;;
        * ) break ;;
    esac
done
54

55 56 57 58 59 60 61
if [ -z "$CC" ]; then
    if [ ! -z "$CXX" ]; then
        >&2 $ECHO "--cc and --cxx must be both set or unset"
        exit 1
    fi
    CC=gcc
    CXX=g++
62 63 64 65
    if [ "$SYSTEM" = "Darwin" ]; then
        CC=clang
        CXX=clang++
    fi
66 67 68 69
elif [ -z "$CXX" ]; then
    >&2 $ECHO "--cc and --cxx must be both set or unset"
    exit 1
fi
70

71
GCC_VERSION=$($CXX tools/print_gcc_version.cc -o print_gcc_version && ./print_gcc_version && rm ./print_gcc_version)
72 73 74 75
if [ $GCC_VERSION -gt 0 ] && [ $GCC_VERSION -lt 40800 ]; then
    >&2 $ECHO "GCC is too old, please install a newer version supporting C++11"
    exit 1
fi
76

77
if [ -z "$HDRS_IN" ] || [ -z "$LIBS_IN" ]; then
78
    >&2 $ECHO "config_brpc: --headers=HDRPATHS --libs=LIBPATHS must be specified"
79 80
    exit 1
fi
81

82
find_dir_of_lib() {
有张纸's avatar
有张纸 committed
83
    local lib=$(find ${LIBS_IN} -name "lib${1}.a" -o -name "lib${1}.$SO" 2>/dev/null | head -n1)
84 85 86
    if [ ! -z "$lib" ]; then
        dirname $lib
    fi
87
}
88 89 90
find_dir_of_lib_or_die() {
    local dir=$(find_dir_of_lib $1)
    if [ -z "$dir" ]; then
91
        >&2 $ECHO "Fail to find $1 from --libs"
92 93
        exit 1
    else
94
        $ECHO $dir
95 96
    fi
}
97

98
find_bin() {
99 100 101 102
    TARGET_BIN=$(which "$1" 2>/dev/null)
    if [ ! -z "$TARGET_BIN" ]; then
        $ECHO $TARGET_BIN
    else
有张纸's avatar
有张纸 committed
103
        find ${LIBS_IN} -name "$1" 2>/dev/null | head -n1
104 105 106 107 108 109 110 111 112
    fi
}
find_bin_or_die() {
    TARGET_BIN=$(find_bin "$1")
    if [ -z "$TARGET_BIN" ]; then
        >&2 $ECHO "Fail to find $1 from --libs"
        exit 1
    fi
    $ECHO $TARGET_BIN
113 114
}

115
find_dir_of_header() {
116
    find -L ${HDRS_IN} -path "*/$1" | head -n1 | sed "s|$1||g"
117
}
118 119

find_dir_of_header_excluding() {
120
    find -L ${HDRS_IN} -path "*/$1" | grep -v "$2\$" | head -n1 | sed "s|$1||g"
121 122
}

123
find_dir_of_header_or_die() {
124 125 126 127 128
    if [ -z "$2" ]; then
        local dir=$(find_dir_of_header $1)
    else
        local dir=$(find_dir_of_header_excluding $1 $2)
    fi
129
    if [ -z "$dir" ]; then
130
        >&2 $ECHO "Fail to find $1 from --headers"
131
        exit 1
132
    fi
133
    $ECHO $dir
134 135
}

136 137
# Inconvenient to check these headers in baidu-internal
#PTHREAD_HDR=$(find_dir_of_header_or_die pthread.h)
gejun's avatar
gejun committed
138
OPENSSL_HDR=$(find_dir_of_header_or_die openssl/ssl.h)
139

140
STATIC_LINKINGS=
141 142
DYNAMIC_LINKINGS="-lpthread -lssl -lcrypto -ldl -lz"
if [ "$SYSTEM" = "Linux" ]; then
wangxuefeng's avatar
wangxuefeng committed
143
    DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lrt"
144 145
fi
if [ "$SYSTEM" = "Darwin" ]; then
wangxuefeng's avatar
wangxuefeng committed
146 147 148 149 150 151 152 153 154
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreFoundation"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreGraphics"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreData"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework CoreText"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework Security"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -framework Foundation"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_MallocExtension_ReleaseFreeMemory"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStart"
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_ProfilerStop"
155
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -Wl,-U,_RegisterThriftProtocol"
156
fi
157 158
append_linking() {
    if [ -f $1/lib${2}.a ]; then
159 160 161 162 163 164
        if [ "$SYSTEM" = "Darwin" ]; then
            # *.a must be explicitly specified in clang
            STATIC_LINKINGS="$STATIC_LINKINGS $1/lib${2}.a"
        else
            STATIC_LINKINGS="$STATIC_LINKINGS -l$2"
        fi
165
        export STATICALLY_LINKED_$2=1
166
    else
167 168
        DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -l$2"
        export STATICALLY_LINKED_$2=0
169 170
    fi
}
171

172 173
GFLAGS_LIB=$(find_dir_of_lib_or_die gflags)
append_linking $GFLAGS_LIB gflags
174

175 176
PROTOBUF_LIB=$(find_dir_of_lib_or_die protobuf)
append_linking $PROTOBUF_LIB protobuf
177

178
LEVELDB_LIB=$(find_dir_of_lib_or_die leveldb)
179
# required by leveldb
180
if [ -f $LEVELDB_LIB/libleveldb.a ]; then
gejun's avatar
gejun committed
181 182
    if [ -f $LEVELDB_LIB/libleveldb.$SO ]; then
        if $LDD $LEVELDB_LIB/libleveldb.$SO | grep -q libsnappy; then
183
            SNAPPY_LIB=$(find_dir_of_lib snappy)
gejun's avatar
gejun committed
184
            REQUIRE_SNAPPY="yes"
185 186
        fi
    fi
gejun's avatar
gejun committed
187
    if [ -z "$REQUIRE_SNAPPY" ]; then
188 189 190 191 192
        if [ "$SYSTEM" = "Darwin" ]; then
	        STATIC_LINKINGS="$STATIC_LINKINGS $LEVELDB_LIB/libleveldb.a"
        else
	        STATIC_LINKINGS="$STATIC_LINKINGS -lleveldb"
        fi
193
    elif [ -f $SNAPPY_LIB/libsnappy.a ]; then
194 195 196 197 198
        if [ "$SYSTEM" = "Darwin" ]; then
	        STATIC_LINKINGS="$STATIC_LINKINGS $LEVELDB_LIB/libleveldb.a $SNAPPY_LIB/libsnappy.a"
        else
	        STATIC_LINKINGS="$STATIC_LINKINGS -lleveldb -lsnappy"
        fi
199 200 201
    else
	    DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lleveldb"
    fi
202 203
else
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lleveldb"
204 205
fi

206
PROTOC=$(find_bin_or_die protoc)
207

208
GFLAGS_HDR=$(find_dir_of_header_or_die gflags/gflags.h)
209
# namespace of gflags may not be google, grep it from source.
gejun's avatar
gejun committed
210
GFLAGS_NS=$(grep "namespace [_A-Za-z0-9]\+ {" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $2}')
gejun's avatar
gejun committed
211
if [ "$GFLAGS_NS" = "GFLAGS_NAMESPACE" ]; then
gejun's avatar
gejun committed
212 213
    GFLAGS_NS=$(grep "#define GFLAGS_NAMESPACE [_A-Za-z0-9]\+" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $3}')
fi
214
if [ -z "$GFLAGS_NS" ]; then
gejun's avatar
gejun committed
215
    >&2 $ECHO "Fail to grep namespace of gflags source $GFLAGS_HDR/gflags/gflags_declare.h"
216 217 218
    exit 1
fi

219 220
PROTOBUF_HDR=$(find_dir_of_header_or_die google/protobuf/message.h)
LEVELDB_HDR=$(find_dir_of_header_or_die leveldb/db.h)
221

gejun's avatar
gejun committed
222
HDRS=$($ECHO "$GFLAGS_HDR\n$PROTOBUF_HDR\n$LEVELDB_HDR\n$OPENSSL_HDR" | sort | uniq)
223
LIBS=$($ECHO "$GFLAGS_LIB\n$PROTOBUF_LIB\n$LEVELDB_LIB\n$SNAPPY_LIB" | sort | uniq)
224 225

absent_in_the_list() {
gejun's avatar
gejun committed
226
    TMP=`$ECHO "$1\n$2" | sort | uniq`
227 228 229 230 231
    if [ "${TMP}" = "$2" ]; then
        return 1
    fi
    return 0
}
232

233 234 235 236 237 238 239 240
OUTPUT_CONTENT="# Generated by config_brpc.sh, don't modify manually"
append_to_output() {
    OUTPUT_CONTENT="${OUTPUT_CONTENT}\n$*"
}
# $1: libname, $2: indentation
append_to_output_headers() {
    if absent_in_the_list "$1" "$HDRS"; then
        append_to_output "${2}HDRS+=$1"
gejun's avatar
gejun committed
241
        HDRS=`$ECHO "${HDRS}\n$1" | sort | uniq`
242
    fi
243 244 245 246 247
}
# $1: libname, $2: indentation
append_to_output_libs() {
    if absent_in_the_list "$1" "$LIBS"; then
        append_to_output "${2}LIBS+=$1"
gejun's avatar
gejun committed
248
        LIBS=`$ECHO "${LIBS}\n$1" | sort | uniq`
249
    fi
250 251 252 253
}
# $1: libdir, $2: libname, $3: indentation
append_to_output_linkings() {
    if [ -f $1/lib$2.a ]; then
254 255 256 257 258 259
        append_to_output_libs $1 $3
        if [ "$SYSTEM" = "Darwin" ]; then
            append_to_output "${3}STATIC_LINKINGS+=$1/lib$2.a"
        else
            append_to_output "${3}STATIC_LINKINGS+=-l$2"
        fi
260
        export STATICALLY_LINKED_$2=1
261
    else
262
        append_to_output_libs $1 $3
263 264
        append_to_output "${3}DYNAMIC_LINKINGS+=-l$2"
        export STATICALLY_LINKED_$2=0
265
    fi
266 267 268
}

#can't use \n in texts because sh does not support -e
gejun's avatar
gejun committed
269
append_to_output "SYSTEM=$SYSTEM"
270 271 272 273 274 275
append_to_output "HDRS=$($ECHO $HDRS)"
append_to_output "LIBS=$($ECHO $LIBS)"
append_to_output "PROTOC=$PROTOC"
append_to_output "PROTOBUF_HDR=$PROTOBUF_HDR"
append_to_output "CC=$CC"
append_to_output "CXX=$CXX"
276
append_to_output "GCC_VERSION=$GCC_VERSION"
277 278
append_to_output "STATIC_LINKINGS=$STATIC_LINKINGS"
append_to_output "DYNAMIC_LINKINGS=$DYNAMIC_LINKINGS"
gejun's avatar
gejun committed
279
CPPFLAGS="-DBRPC_WITH_GLOG=$WITH_GLOG -DGFLAGS_NS=$GFLAGS_NS"
280

gejun's avatar
gejun committed
281 282 283 284 285
if [ ! -z "$DEBUGSYMBOLS" ]; then
    CPPFLAGS="${CPPFLAGS} $DEBUGSYMBOLS"
fi
if [ "$SYSTEM" = "Darwin" ]; then
    CPPFLAGS="${CPPFLAGS} -Wno-deprecated-declarations"
zyearn's avatar
zyearn committed
286
    version=`sw_vers -productVersion | awk -F '.' '{print $1 "." $2}'`
287 288 289
    if [[ `echo "$version<10.12" | bc -l` == 1 ]]; then
        CPPFLAGS="${CPPFLAGS} -DNO_CLOCK_GETTIME_IN_MAC"
    fi
gejun's avatar
gejun committed
290
fi
291 292 293 294 295 296 297 298 299 300

if [ $WITH_THRIFT != 0 ]; then
    THRIFT_LIB=$(find_dir_of_lib_or_die thriftnb)
    THRIFT_HDR=$(find_dir_of_header_or_die thrift/Thrift.h)
    append_to_output_libs "$THRIFT_LIB"
    append_to_output_headers "$THRIFT_HDR"

    CPPFLAGS="${CPPFLAGS} -DENABLE_THRIFT_FRAMED_PROTOCOL"

    if [ -f "$THRIFT_LIB/libthriftnb.$SO" ]; then
wangxuefeng's avatar
wangxuefeng committed
301
        append_to_output "DYNAMIC_LINKINGS+=-lthriftnb -levent -lthrift"
302 303 304 305 306
    else
        append_to_output "STATIC_LINKINGS+=-lthriftnb"
    fi
fi

gejun's avatar
gejun committed
307
append_to_output "CPPFLAGS=${CPPFLAGS}"
308 309 310 311 312 313 314 315 316

append_to_output "ifeq (\$(NEED_LIBPROTOC), 1)"
PROTOC_LIB=$(find $PROTOBUF_LIB -name "libprotoc.*" | head -n1)
if [ -z "$PROTOC_LIB" ]; then
    append_to_output "   \$(error \"Fail to find libprotoc\")"
else
    # libprotobuf and libprotoc must be linked same statically or dynamically
    # otherwise the bin will crash.
    if [ $STATICALLY_LINKED_protobuf -gt 0 ]; then
317 318 319 320 321
        if [ "$SYSTEM" = "Darwin" ]; then
            append_to_output "    STATIC_LINKINGS+=$(find $PROTOBUF_LIB -name "libprotoc.a" | head -n1)"
        else
            append_to_output "    STATIC_LINKINGS+=-lprotoc"
        fi
322
    else
323
        append_to_output "    DYNAMIC_LINKINGS+=-lprotoc"
324
    fi
325
fi
326 327
append_to_output "endif"

gejun's avatar
gejun committed
328 329
OLD_HDRS=$HDRS
OLD_LIBS=$LIBS
330 331 332 333 334 335 336
append_to_output "ifeq (\$(NEED_GPERFTOOLS), 1)"
# required by cpu/heap profiler
TCMALLOC_LIB=$(find_dir_of_lib tcmalloc_and_profiler)
if [ -z "$TCMALLOC_LIB" ]; then
    append_to_output "    \$(error \"Fail to find gperftools\")"
else
    append_to_output_libs "$TCMALLOC_LIB" "    "
337
    if [ -f $TCMALLOC_LIB/libtcmalloc.$SO ]; then
338
        append_to_output "    DYNAMIC_LINKINGS+=-ltcmalloc_and_profiler"
339
    else
340 341 342 343 344
        if [ "$SYSTEM" = "Darwin" ]; then
            append_to_output "    STATIC_LINKINGS+=$TCMALLOC_LIB/libtcmalloc.a"
        else
            append_to_output "    STATIC_LINKINGS+=-ltcmalloc_and_profiler"
        fi
345 346 347
    fi
fi
append_to_output "endif"
348

349
if [ $WITH_GLOG != 0 ]; then
350
    GLOG_LIB=$(find_dir_of_lib_or_die glog)
351
    GLOG_HDR=$(find_dir_of_header_or_die glog/logging.h windows/glog/logging.h)
352 353 354 355
    append_to_output_libs "$GLOG_LIB"
    append_to_output_headers "$GLOG_HDR"
    if [ -f "$GLOG_LIB/libglog.$SO" ]; then
        append_to_output "DYNAMIC_LINKINGS+=-lglog"
356
    else
357 358 359 360 361
        if [ "$SYSTEM" = "Darwin" ]; then
            append_to_output "STATIC_LINKINGS+=$GLOG_LIB/libglog.a"
        else
            append_to_output "STATIC_LINKINGS+=-lglog"
        fi
362 363
    fi
fi
364

365 366 367
# required by UT
#gtest
GTEST_LIB=$(find_dir_of_lib gtest)
gejun's avatar
gejun committed
368 369
HDRS=$OLD_HDRS
LIBS=$OLD_LIBS
370
append_to_output "ifeq (\$(NEED_GTEST), 1)"
371
if [ -z "$GTEST_LIB" ]; then
372
    append_to_output "    \$(error \"Fail to find gtest\")"
373
else
374 375 376 377 378
    GTEST_HDR=$(find_dir_of_header_or_die gtest/gtest.h)
    append_to_output_libs $GTEST_LIB "    "
    append_to_output_headers $GTEST_HDR "    "
    append_to_output_linkings $GTEST_LIB gtest "    "
    append_to_output_linkings $GTEST_LIB gtest_main "    "
379
fi
380
append_to_output "endif"
gejun's avatar
gejun committed
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395
# generate src/butil/config.h
cat << EOF > src/butil/config.h
// This file is auto-generated by $(basename "$0"). DON'T edit it!
#ifndef  BUTIL_CONFIG_H
#define  BUTIL_CONFIG_H

#ifdef BRPC_WITH_GLOG
#undef BRPC_WITH_GLOG
#endif
#define BRPC_WITH_GLOG $WITH_GLOG

#endif  // BUTIL_CONFIG_H
EOF

396 397
# write to config.mk
$ECHO "$OUTPUT_CONTENT" > config.mk