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

20
TEMP=`getopt -o v: --long headers:,libs:,cc:,cxx:,with-glog,nodebugsymbols -n 'config_brpc' -- "$@"`
21
WITH_GLOG=0
22
DEBUGSYMBOLS=-g
23

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

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

# Convert to abspath always so that generated mk is include-able from everywhere
while true; do
    case "$1" in
32 33
        --headers ) HDRS_IN="$(realpath $2)"; shift 2 ;;
        --libs ) LIBS_IN="$(realpath $2)"; shift 2 ;;
34 35
        --cc ) CC=$2; shift 2 ;;
        --cxx ) CXX=$2; shift 2 ;;
36
        --with-glog ) WITH_GLOG=1; shift 1 ;;
37
        --nodebugsymbols ) DEBUGSYMBOLS=; shift 1 ;;
38 39 40 41
        -- ) shift; break ;;
        * ) break ;;
    esac
done
42

43 44 45 46 47 48 49 50 51 52 53
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++
elif [ -z "$CXX" ]; then
    >&2 $ECHO "--cc and --cxx must be both set or unset"
    exit 1
fi
54

55
GCC_VERSION=$($CXX tools/print_gcc_version.cc -o print_gcc_version && ./print_gcc_version && rm ./print_gcc_version)
56 57 58 59
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
60

61
if [ -z "$HDRS_IN" ] || [ -z "$LIBS_IN" ]; then
62
    >&2 $ECHO "config_brpc: --headers=HDRPATHS --libs=LIBPATHS must be specified"
63 64
    exit 1
fi
65

66
find_dir_of_lib() {
有张纸's avatar
有张纸 committed
67
    local lib=$(find ${LIBS_IN} -name "lib${1}.a" -o -name "lib${1}.$SO" 2>/dev/null | head -n1)
68 69 70
    if [ ! -z "$lib" ]; then
        dirname $lib
    fi
71
}
72 73 74
find_dir_of_lib_or_die() {
    local dir=$(find_dir_of_lib $1)
    if [ -z "$dir" ]; then
75
        >&2 $ECHO "Fail to find $1 from --libs"
76 77
        exit 1
    else
78
        $ECHO $dir
79 80
    fi
}
81

82
find_bin() {
83 84 85 86
    TARGET_BIN=$(which "$1" 2>/dev/null)
    if [ ! -z "$TARGET_BIN" ]; then
        $ECHO $TARGET_BIN
    else
有张纸's avatar
有张纸 committed
87
        find ${LIBS_IN} -name "$1" 2>/dev/null | head -n1
88 89 90 91 92 93 94 95 96
    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
97 98
}

99
find_dir_of_header() {
100
    find ${HDRS_IN} -path "*/$1" | head -n1 | sed "s|$1||g"
101
}
102 103 104 105 106

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

107
find_dir_of_header_or_die() {
108 109 110 111 112
    if [ -z "$2" ]; then
        local dir=$(find_dir_of_header $1)
    else
        local dir=$(find_dir_of_header_excluding $1 $2)
    fi
113
    if [ -z "$dir" ]; then
114
        >&2 $ECHO "Fail to find $1 from --headers"
115
        exit 1
116
    fi
117
    $ECHO $dir
118 119
}

120 121
# Inconvenient to check these headers in baidu-internal
#PTHREAD_HDR=$(find_dir_of_header_or_die pthread.h)
gejun's avatar
gejun committed
122
OPENSSL_HDR=$(find_dir_of_header_or_die openssl/ssl.h)
123

124 125
STATIC_LINKINGS=
DYNAMIC_LINKINGS="-lpthread -lrt -lssl -lcrypto -ldl -lz"
126 127
append_linking() {
    if [ -f $1/lib${2}.a ]; then
128 129
        STATIC_LINKINGS="$STATIC_LINKINGS -l$2"
        export STATICALLY_LINKED_$2=1
130
    else
131 132
        DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -l$2"
        export STATICALLY_LINKED_$2=0
133 134
    fi
}
135

136 137
GFLAGS_LIB=$(find_dir_of_lib_or_die gflags)
append_linking $GFLAGS_LIB gflags
138

139 140
PROTOBUF_LIB=$(find_dir_of_lib_or_die protobuf)
append_linking $PROTOBUF_LIB protobuf
141

142
LEVELDB_LIB=$(find_dir_of_lib_or_die leveldb)
143
# required by leveldb
144
if [ -f $LEVELDB_LIB/libleveldb.a ]; then
gejun's avatar
gejun committed
145 146
    if [ -f $LEVELDB_LIB/libleveldb.$SO ]; then
        if $LDD $LEVELDB_LIB/libleveldb.$SO | grep -q libsnappy; then
147
            SNAPPY_LIB=$(find_dir_of_lib snappy)
gejun's avatar
gejun committed
148
            REQUIRE_SNAPPY="yes"
149 150
        fi
    fi
gejun's avatar
gejun committed
151
    if [ -z "$REQUIRE_SNAPPY" ]; then
152 153 154 155 156 157
	    STATIC_LINKINGS="$STATIC_LINKINGS -lleveldb"
    elif [ -f $SNAPPY_LIB/libsnappy.a ]; then
	    STATIC_LINKINGS="$STATIC_LINKINGS -lleveldb -lsnappy"
    else
	    DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lleveldb"
    fi
158 159
else
	DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lleveldb"
160 161
fi

162
PROTOC=$(find_bin_or_die protoc)
163

164
GFLAGS_HDR=$(find_dir_of_header_or_die gflags/gflags.h)
165
# namespace of gflags may not be google, grep it from source.
gejun's avatar
gejun committed
166
GFLAGS_NS=$(grep "namespace [_A-Za-z0-9]\+ {" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $2}')
gejun's avatar
gejun committed
167
if [ "$GFLAGS_NS" = "GFLAGS_NAMESPACE" ]; then
gejun's avatar
gejun committed
168 169
    GFLAGS_NS=$(grep "#define GFLAGS_NAMESPACE [_A-Za-z0-9]\+" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $3}')
fi
170
if [ -z "$GFLAGS_NS" ]; then
gejun's avatar
gejun committed
171
    >&2 $ECHO "Fail to grep namespace of gflags source $GFLAGS_HDR/gflags/gflags_declare.h"
172 173 174
    exit 1
fi

175 176
PROTOBUF_HDR=$(find_dir_of_header_or_die google/protobuf/message.h)
LEVELDB_HDR=$(find_dir_of_header_or_die leveldb/db.h)
177

gejun's avatar
gejun committed
178
HDRS=$($ECHO "$GFLAGS_HDR\n$PROTOBUF_HDR\n$LEVELDB_HDR\n$OPENSSL_HDR" | sort | uniq)
179
LIBS=$($ECHO "$GFLAGS_LIB\n$PROTOBUF_LIB\n$LEVELDB_LIB\n$SNAPPY_LIB" | sort | uniq)
180 181

absent_in_the_list() {
gejun's avatar
gejun committed
182
    TMP=`$ECHO "$1\n$2" | sort | uniq`
183 184 185 186 187
    if [ "${TMP}" = "$2" ]; then
        return 1
    fi
    return 0
}
188

189 190 191 192 193 194 195 196
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
197
        HDRS=`$ECHO "${HDRS}\n$1" | sort | uniq`
198
    fi
199 200 201 202 203
}
# $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
204
        LIBS=`$ECHO "${LIBS}\n$1" | sort | uniq`
205
    fi
206 207 208 209
}
# $1: libdir, $2: libname, $3: indentation
append_to_output_linkings() {
    if [ -f $1/lib$2.a ]; then
210
	append_to_output_libs $1 $3
211 212
        append_to_output "${3}STATIC_LINKINGS+=-l$2"
        export STATICALLY_LINKED_$2=1
213
    else
214
	append_to_output_libs $1 $3
215 216
        append_to_output "${3}DYNAMIC_LINKINGS+=-l$2"
        export STATICALLY_LINKED_$2=0
217
    fi
218 219 220
}

#can't use \n in texts because sh does not support -e
gejun's avatar
gejun committed
221
append_to_output "SYSTEM=$SYSTEM"
222 223 224 225 226 227
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"
228
append_to_output "GCC_VERSION=$GCC_VERSION"
229 230
append_to_output "STATIC_LINKINGS=$STATIC_LINKINGS"
append_to_output "DYNAMIC_LINKINGS=$DYNAMIC_LINKINGS"
gejun's avatar
gejun committed
231 232 233 234 235 236 237 238
CPPFLAGS="-DBRPC_WITH_GLOG=$WITH_GLOG -DGFLAGS_NS=$GFLAGS_NS"
if [ ! -z "$DEBUGSYMBOLS" ]; then
    CPPFLAGS="${CPPFLAGS} $DEBUGSYMBOLS"
fi
if [ "$SYSTEM" = "Darwin" ]; then
    CPPFLAGS="${CPPFLAGS} -Wno-deprecated-declarations"
fi
append_to_output "CPPFLAGS=${CPPFLAGS}"
239 240 241 242 243 244 245 246 247 248

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
        append_to_output "    STATIC_LINKINGS+=-lprotoc"
249
    else
250
        append_to_output "    DYNAMIC_LINKINGS+=-lprotoc"
251
    fi
252
fi
253 254
append_to_output "endif"

gejun's avatar
gejun committed
255 256
OLD_HDRS=$HDRS
OLD_LIBS=$LIBS
257 258 259 260 261 262 263
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" "    "
264
    if [ -f $TCMALLOC_LIB/libtcmalloc.$SO ]; then
265
        append_to_output "    DYNAMIC_LINKINGS+=-ltcmalloc_and_profiler"
266 267
    else
        append_to_output "    STATIC_LINKINGS+=-ltcmalloc_and_profiler"
268 269 270
    fi
fi
append_to_output "endif"
271

272
if [ $WITH_GLOG != 0 ]; then
273
    GLOG_LIB=$(find_dir_of_lib_or_die glog)
274
    GLOG_HDR=$(find_dir_of_header_or_die glog/logging.h windows/glog/logging.h)
275 276 277 278
    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"
279
    else
280
        append_to_output "STATIC_LINKINGS+=-lglog"
281 282
    fi
fi
283

284 285 286
# required by UT
#gtest
GTEST_LIB=$(find_dir_of_lib gtest)
gejun's avatar
gejun committed
287 288
HDRS=$OLD_HDRS
LIBS=$OLD_LIBS
289
append_to_output "ifeq (\$(NEED_GTEST), 1)"
290
if [ -z "$GTEST_LIB" ]; then
291
    append_to_output "    \$(error \"Fail to find gtest\")"
292
else
293 294 295 296 297
    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 "    "
298
fi
299
append_to_output "endif"
gejun's avatar
gejun committed
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314
# 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

315 316
# write to config.mk
$ECHO "$OUTPUT_CONTENT" > config.mk