红联Linux门户
Linux帮助

linux下protobuf下载并cmake编译安装全过程脚本

发布时间:2017-06-16 10:03:43来源:blog.csdn.net/10km作者:10km
protobuf源码下载
github上下载最新版本的protobuf源码zip包,并解压缩到本地
fetch_protobuf.sh
###########################
#!/bin/bash
shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
#如果指定的文件/文件夹存在则删除,删除失败则中止脚本
remove_if_exist(){
if [ $# -eq 1 ]
then
if [ -e $1 ]
then
rm $1 -fr
if [ ! $? -eq 0 ]
then
exit -1
fi
fi
return 0
else 
echo invalid argument:
echo $*
exit -1
fi
}
exit_on_error(){
if [ ! $? -eq 0 ]
then
exit -1
fi
}
# 如果文件存在且checksum与$2指定的md5相等则返回 1,否则返回0
# $1 待检查的文件路径
# $2 md5校验码
need_download(){
if [ $# -eq 2 ]
then
if [ -f $1 ]; then
echo "File already exists. Checking md5..."
os=`uname -s`
if [ "$os" = "Linux" ]; then
checksum=`md5sum $1 | awk '{ print $1 }'`
elif [ "$os" = "Darwin" ]; then
checksum=`cat $1 | md5`
fi
if [ "$checksum" = "$2" ]; then
echo "Checksum is correct. No need to download $1."
return 1
else
echo "Checksum is incorrect. Need to download again $1"                
fi
else
return 0
fi
else 
echo invalid argument:
echo $*
exit -1
fi
}
# 从github上下载源码,如果本地不存在指定的zip包,或md5校验码不匹配则从github下载
# $1 项目名称
# $2 版本号
# $3 zip文件的md5
# $4 项目所有者名称
# $5 zip包文件名前缀
download_from_github(){
if [ $# -eq 5 ]
then
project_folder="$1-$2"
project_package="$project_folder.zip"
if need_download $project_package $3
then
echo "(下载源码)downloading $1 $2 source"
wget --no-check-certificate https://github.com/$4/$1/archive/$5$2.zip -O $project_package
exit_on_error
fi
remove_if_exist $project_folder
unzip $project_package
exit_on_error
else
echo invalid argument:
echo $*
exit -1
fi
}
download_from_github "protobuf" "3.3.1" "9377e414994fa6165ecb58a41cca3b40" "google" "v"
popd
 
protobuf编译安装
对下载的protobuf源码编译并安装到release/protobuf_linux_x86_64文件夹下
build_protobuf.sh
###########################
#!/bin/bash
GXX_PATH="-DCMAKE_BUILD_TYPE:STRING=RELEASE"
INSTALL_FOLDER=$(dirname $(readlink -f $0))/release/protobuf_linux_x86_64
pushd protobuf-3.3.1
echo INSTALL_FOLDER:$INSTALL_FOLDER
if [ -d build.gcc ]
then 
rm -fr build.gcc/*
else 
mkdir build.gcc
fi
pushd build.gcc
cmake ../cmake $GXX_PATH -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$INSTALL_FOLDER  -Dprotobuf_BUILD_TESTS=off
# 编译并安装protobuf到指定的文件夹
make install
popd
rm -fr build.gcc
popd
###########################
上面的脚本中在调用cmake生成Makefile时定义了protobuf_BUILD_TESTS=off,这是因为protobuf的CMakeLists.txt中默认定义protobuf_BUILD_TESTS=on,
而当protobuf_BUILD_TESTS=on时,需要gmock支持(https://github.com/google/googlemock),否则cmake会报如下错误:
CMake Error at tests.cmake:2 (message):
Cannot find gmock directory.
gmock是google公司推出的一款开源的白盒测试工具,对于我们一般的应用来说,不需要gmock提供的功能,所以这里可以将protobuf_BUILD_TESTS=off,避免麻烦。 
 
本文永久更新地址:http://www.linuxdiyf.com/linux/31519.html