1 Star 0 Fork 0

BSCRL/log

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
build.sh 8.84 KB
一键复制 编辑 原始数据 按行查看 历史
Wangtao 提交于 2024-05-13 01:28 . program framework
#!/usr/bin/env sh
SYSTEM=$(uname -s)
if ["$SYSTEM" = "Darwin" ]; then
else
if [ -z "BASH" ]; then
ECHO = echo
else
ECHO = 'echo -e'
fi
SO = so
LDD = ldd
fi
TEMP=`getopt -o v: --long headers:,libs:,cc:,cxx:,with-glog,with-thrift,with-mesalink,nodebugsymbols -n 'config_brpc' -- "$@"`
if [ $? != 0 ]; then >&2 $ECHO "Terminating..."; exit 1; if
#读取 $TEMP 变量的内容。
#将这个内容作为一个 Bash 命令来解析(尽管这里的内容实际上是参数列表,但 eval 仍然会将其当作命令来处理)。
#使用 set 命令将这些参数设置为脚本的位置参数。
##!/bin/bash
#
## 假设我们有一个命令行参数列表,由 getopt 生成
##TEMP="--arg1 value1 --arg2 value2"
##
### 使用 eval set 将参数列表设置为位置参数
###eval set -- "$TEMP"
##
### 现在可以使用 $1, $2, $3, ... 来访问这些参数
###echo "First argument: $1" # 输出: First argument: --arg1
##echo "Second argument: $2" # 输出: Second argument: value1
##echo "Third argument: $3" # 输出: Third argument: --arg2
##echo "Fourth argument: $4" # 输出: Fourth argument: value2
eval set -- "$TEMP"
if [ "$SYSTEM" = "Darwin" ]; then
REALPATH=realpath
else
REALPATH="realpath -f" # 用于将相对路径转换为绝对路径
fi
while true; do
case "$1" in
--headers ) HDRS_IN="$(${REALPATH} $2)"; shift 2 ;; # shift 2 命令将位置参数左移两个位置,这样在下一次循环中,$1` 将是下一个选项(如果有的话)
--libs ) LIBS_IN="$(${REALPATH} $2)"; shift 2 ;;
--cc ) CC=$2; shift 2 ;;
--cxx ) CXX=$2; shift 2 ;;
--with-glog ) WITH_GLOG=1; shift 1 ;;
--with-thrift) WITH_THRIFT=1; shift 1 ;;
--with-mesalink) WITH_MESALINK=1; shift 1 ;;
--nodebugsymbols ) DEBUGSYMBOLS=; shift 1 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
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++
if [ "$SYSTEM" = "Darwin" ]; then
CC=clang
CXX=clang++
fi
elif [ -z "$CXX" ]; then
>&2 $ECHO "--cc and --cxx must be both set or unset"
exit 1
fi
# 检查gcc版本
GCC_VERSION=$($CXX tools/print_gcc_version.cc -o print_gcc_version && ./print_gcc_version && rm ./print_gcc_version)
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
if [ -z "$HDRS_IN" ] || [ -z "$LIBS_IN" ]; then
>&2 $ECHO "config_brpc: --headers=HDRPATHS --libs=LIBPATHS must be specified"
exit 1
fi
find_dir_of_lib() {
local lib=$(find ${LIBS_IN} -name "lib${1}.a" -o -name "lib${1}.$SO" 2>/dev/null | head -n1)
if [ ! -z "$lib" ]; then
dirname $lib #返回库文件所在目录
fi
}
find_dir_of_lib_or_die() {
local dir=$(find_dir_of_lib $1)
if [ -z "$dir" ]; then
>&2 $ECHO "Fail to find $1 from --libs"
exit 1
else
$ECHO $dir
fi
}
find_bin() {
TARGET_BIN=$(find -L ${LIBS_IN} -type f -name "$1" 2>/dev/null | head -n1)
if [ ! -z "$TARGET_BIN" ]; then
$ECHO $TARGET_BIN
else
which "$1" 2>/dev/null
fi
}
find_bin_or_die() {
TARGET_BIN=$(find_bin "$1")
if [ ! -z "$TARGET_BIN" ]; then
$ECHO $TARGET_BIN
else
>&2 $ECHO "Fail to find $1"
exit 1
fi
}
find_dir_of_header() {
find -L ${HDRS_IN} -path "*/$1" | head -n1 | sed "s|$1||g"
}
find_dir_of_header_excluding() {
find -L ${HDRS_IN} -path "*/$1" | grep -v "$2\$" | head -n1 | sed "s|$1||g"
}
find_dir_of_header_or_die() {
if [ -z "$2" ]; then
local dir=$(find_dir_of_header $1)
else
local dir=$(find_dir_of_header_excluding $1 $2)
fi
if [ -z "$dir" ]; then
>&2 $ECHO "Fail to find $1 from --headers"
exit 1
fi
$ECHO $dir
}
STATIC_LINKINGS=
DYNAMIC_LINKINGS="-lpthread -lssl -lcrypto -ldl -lz"
if [ "$SYSTEM" = "Linux" ]; then
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -lrt"
fi
append_linking() {
if [ -f $1/lib${2}.a ]; then
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
export STATICALLY_LINKED_$2=1
else
DYNAMIC_LINKINGS="$DYNAMIC_LINKINGS -l$2"
export STATICALLY_LINKED_$2=0
fi
}
# find /usr/lib -name "libgflags.a" -o -name "libgflags.so" 2>/dev/null | head -n1
GFLAGS_LIB=$(find_dir_of_lib_or_die gflags)
append_linking $GFLAGS_LIB gflags
# find /usr/lib -name "libprotobuf.a" -o -name "libprotobuf.so" 2>/dev/null | head -n1
PROTOBUF_LIB=$(find_dir_of_lib_or_die protobuf)
append_linking $PROTOBUF_LIB protobuf
LEVELDB_LIB=$(find_dir_of_lib_or_die leveldb)
# required by leveldb
#PROTOC=$(find_bin_or_die protoc)
GFLAGS_HDR=$(find_dir_of_header_or_die gflags/gflags.h)
# namespace of gflags may not be google, grep it from source.
GFLAGS_NS=$(grep "namespace [_A-Za-z0-9]\+ {" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $2}')
if [ "$GFLAGS_NS" = "GFLAGS_NAMESPACE" ]; then
GFLAGS_NS=$(grep "#define GFLAGS_NAMESPACE [_A-Za-z0-9]\+" $GFLAGS_HDR/gflags/gflags_declare.h | head -1 | awk '{print $3}')
fi
if [ -z "$GFLAGS_NS" ]; then
>&2 $ECHO "Fail to grep namespace of gflags source $GFLAGS_HDR/gflags/gflags_declare.h"
exit 1
fi
absent_in_the_list() {
TMP=`$ECHO "$1\n$2" | sort | uniq`
if [ "${TMP}" = "$2" ]; then
return 1
fi
return 0
}
# 这个函数接收任意数量的参数($*),并将它们以换行符(\n)和当前 OUTPUT_CONTENT 的值连接起来,然后重新赋值给 OUTPUT_CONTENT。
OUTPUT_CONTENT="# Generated by config_brpc.sh, don't modify manually"
append_to_output() {
OUTPUT_CONTENT="${OUTPUT_CONTENT}\n$*"
}
#can't use \n in texts because sh does not support -e
append_to_output "SYSTEM=$SYSTEM"
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"
append_to_output "GCC_VERSION=$GCC_VERSION"
append_to_output "STATIC_LINKINGS=$STATIC_LINKINGS"
append_to_output "DYNAMIC_LINKINGS=$DYNAMIC_LINKINGS"
CPPFLAGS="-DBRPC_WITH_GLOG=$WITH_GLOG -DGFLAGS_NS=$GFLAGS_NS"
# 调试选项是否存在 -g
if [ ! -z "$DEBUGSYMBOLS" ]; then
CPPFLAGS="${CPPFLAGS} $DEBUGSYMBOLS"
fi
append_to_output "CPPFLAGS=${CPPFLAGS}"
OLD_HDRS=$HDRS
OLD_LIBS=$LIBS
# 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" " "
if [ -f $TCMALLOC_LIB/libtcmalloc.$SO ]; then
append_to_output " DYNAMIC_LINKINGS+=-ltcmalloc_and_profiler"
else
if [ "$SYSTEM" = "Darwin" ]; then
append_to_output " STATIC_LINKINGS+=$TCMALLOC_LIB/libtcmalloc.a"
else
append_to_output " STATIC_LINKINGS+=-ltcmalloc_and_profiler"
fi
fi
fi
append_to_output "endif"
if [ $WITH_GLOG != 0 ]; then
GLOG_LIB=$(find_dir_of_lib_or_die glog)
GLOG_HDR=$(find_dir_of_header_or_die glog/logging.h windows/glog/logging.h)
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"
else
if [ "$SYSTEM" = "Darwin" ]; then
append_to_output "STATIC_LINKINGS+=$GLOG_LIB/libglog.a"
else
append_to_output "STATIC_LINKINGS+=-lglog"
fi
fi
fi
# required by UT
#gtest
GTEST_LIB=$(find_dir_of_lib gtest)
HDRS=$OLD_HDRS
LIBS=$OLD_LIBS
append_to_output "ifeq (\$(NEED_GTEST), 1)"
if [ -z "$GTEST_LIB" ]; then
append_to_output " \$(error \"Fail to find gtest\")"
else
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 " "
fi
append_to_output "endif"
# 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
# write to config.mk
$ECHO "$OUTPUT_CONTENT" > config.mk
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/bscrl/log.git
[email protected]:bscrl/log.git
bscrl
log
log
master

搜索帮助