linux下boost编译安装全过程脚本塈bzip2编译安装全过程脚本

boost编译安装

build_boost.sh

#!/bin/bash
shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
# 上一条命令执行出错则中止脚本执行
exit_on_error(){
    if [ ! $? -eq 0 ]
    then
        echo "exit for error"
        exit -1
    fi
}
# 如果文件/文件夹存在则删除
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
}
# 清除指定文件夹的内容,如果文件夹不存在则创建空文件夹
clearn_folder(){
    if [ $# -eq 1 ]
    then
        if [ -d "$1" ]
        then 
            rm -fr $1/*
        else 
            mkdir -p "$1"
        fi
        exit_on_error
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 生成安装路径名后缀
install_suffix(){
    if [ $# -eq 1 ]
    then
        typeset -l os processor
        os=`uname -s`
        processor=`uname -p`
        echo $1_${os}_${processor}
    else 
        echo invalid argument:
        echo $*
        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
}
###############下载boost
BOOST_VERSION="1.58.0"
BOOST_FOLDER="boost_${BOOST_VERSION//./_}"
BOOST_PACKAGE="$BOOST_FOLDER.tar.gz"
# 注意这里的md5是1.58.0版本tar.gz包的md5校验码,如果下载其他版本则该值要重新计算并更新,
# 计算md5方法参见need_download函数
if need_download $BOOST_PACKAGE "5a5d5614d9a07672e1ab2a250b5defc5"
then
    echo "(下载源码)downloading boost $BOOST_VERSION source"
    wget --no-check-certificate https://nchc.dl.sourceforge.net/project/boost/boost/$BOOST_VERSION/$BOOST_PACKAGE
    exit_on_error
fi
remove_if_exist $BOOST_FOLDER
tar zxvf $BOOST_PACKAGE
exit_on_error

###################编译boost
#安装路径
install_folder=$(dirname $(readlink -f $0))/release/$(install_suffix boost)
echo install_folder:$install_folder
bzip2_path=$(dirname $(readlink -f $0))/release/$(install_suffix bzip2)
pushd boost_1_58_0
# 指定bzip2位置,编译iostreams库时需要
# 如果不指定编译iostreams时会报错找不到:bzlib.h
export LIBRARY_PATH=$bzip2_path/lib:$LIBRARY_PATH
export CPLUS_INCLUDE_PATH=$bzip2_path/include:$CPLUS_INCLUDE_PATH
# 不编译python库
./bootstrap.sh --without-libraries=python
exit_on_error
./b2 --clean
# 删除原安装路径
remove_if_exist $INSTALL_FOLDER
# --prefix 指定安装位置
# --debug-configuration 编译时显示加载的配置信息
# -q 参数指示出错就停止编译
# link=static 只编译静态库
./b2 --prefix=$INSTALL_FOLDER -q --debug-configuration link=static install

popd

以上脚本完成boost源码下载编译安装全过程,注意,如果没有安装bzip2,则在编译过程中会报错

libs/iostreams/src/bzip2.cpp:20:56: fatal error: bzlib.h: No such file or directory

解决这个问题有很简单的办法,就是apt安装libbz2-dev

sudo apt-get install libbz2-dev

但因为项目需要,不能使用编译好的二进制代码 ,我得编译安装bzip2,所以先执行下面的脚本再执行 build_boost.sh,boost才能正常编译。

bzip2编译安装

下面的脚本完成bzip2下载编译安装全过程。
build_bzip2.sh

#!/bin/bash
shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
# 上一条命令执行出错则中止脚本执行
exit_on_error(){
    if [ ! $? -eq 0 ]
    then
        echo "exit for error"
        exit -1
    fi
}
# 如果文件/文件夹存在则删除
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
}
# 清除指定文件夹的内容,如果文件夹不存在则创建空文件夹
clearn_folder(){
    if [ $# -eq 1 ]
    then
        if [ -d "$1" ]
        then 
            rm -fr $1/*
        else 
            mkdir -p "$1"
        fi
        exit_on_error
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 生成安装路径名后缀
install_suffix(){
    if [ $# -eq 1 ]
    then
        typeset -l os processor
        os=`uname -s`
        processor=`uname -p`
        echo $1_${os}_${processor}
    else 
        echo invalid argument:
        echo $*
        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
}
###############下载bzip2
BZIP2_VERSION="1.0.6"
BZIP2_FOLDER="bzip2-$BZIP2_VERSION"
BZIP2_PACKAGE="$BZIP2_FOLDER.tar.gz"
if need_download $BZIP2_PACKAGE "00b516f4704d4a7cb50a1d97e6e8e15b"
then
    echo "(下载源码)downloading bzip2 $BZIP2_VERSION source"
    wget --no-check-certificate http://www.bzip.org/$BZIP2_VERSION/$BZIP2_PACKAGE
    exit_on_error
fi
remove_if_exist $BZIP2_FOLDER
tar zxvf $BZIP2_PACKAGE
exit_on_error
bzip2_makefile=$BZIP2_FOLDER/Makefile
# 使用sed编译器修改Makefile,在编译选项中增加-fPIC参数
# 判断CFLAGS中是否已经有-fPIC选项,如果没有就添加,没有则不修改
if [ -z "$(grep '^\s*CFLAGS\s*=' $bzip2_makefile | grep '\-fPIC')" ] 
then
    echo "add -fPIC to CFLAGS in $bzip2_makefile"
    sed -i -r 's/(^\s*CFLAGS\s*=)(.*$)/\1-fPIC \2/' $bzip2_makefile
    exit_on_error
else
    echo "found -fPIC in CFLAGS,no need modify $bzip2_makefile"
fi
###################编译bzip2
#安装路径
install_folder=$(dirname $(readlink -f $0))/release/$(install_suffix bzip2)
echo install_folder:$install_folder

pushd bzip2-1.0.6
clearn_folder $install_folder
make clean
exit_on_error
make -j8
make install PREFIX=$install_folder 
exit_on_error
popd

另外,在编译bzip2之前,要修改bzip2的Makefile.在CFLAGS定义中增加-fPIC选项,如下:

CFLAGS=-fPIC -Wall -Winline -O2 -g $(BIGFILES)

否则编译boost时会报错,在下载bzip2源码的脚本中有相关的代码用于自动在CFLAGS定义中增加-fPIC选项

上面两个脚本中有不少相同的函数,为方便维护,在实际工程中,我是把它合并放在一个库文件中的,本文为了让每个脚本都能独立运行,才特别将公用函数分别复制到每个脚本中。

你可能感兴趣的:(开发工具,script)