|  |  | 
 |  |  | #!/bin/bash | 
 |  |  |  | 
 |  |  | #+-------------------------------------------------------------------------------------------- | 
 |  |  | #|Description:  This shell script used download and compile MQTT C library mosquitto for ARM | 
 |  |  | #|     Author:  GuoWenxue <guowenxue@gmail.com> | 
 |  |  | #|  ChangeLog: | 
 |  |  | #|           1, Initialize 1.0.0 on 2011.04.12 | 
 |  |  | #+-------------------------------------------------------------------------------------------- | 
 |  |  | # library name and version | 
 |  |  | # Official: https://mosquitto.org/ | 
 |  |  | LIB_NAME=mosquitto-2.0.15 | 
 |  |  | PACK_SUFIX=tar.gz | 
 |  |  |  | 
 |  |  | PREFIX_PATH=`pwd`/../install | 
 |  |  |  | 
 |  |  | # LingYun source code FTP server | 
 |  |  | LYFTP_SRC=http://master.weike-iot.com:2211/src/ | 
 |  |  |  | 
 |  |  | # library download URL address | 
 |  |  | LIB_URL=$LYFTP_SRC | 
 |  |  |  | 
 |  |  | # Cross compiler for cross compile on Linux server | 
 |  |  | CROSS_COMPILE=/opt/gcc-aarch32-10.3-2021.07/bin/arm-none-linux-gnueabihf- | 
 |  |  |  | 
 |  |  | function msg_banner() | 
 |  |  | { | 
 |  |  |     echo "" | 
 |  |  |     echo "+-----------------------------------------------------------------------" | 
 |  |  |     echo "|  $1 " | 
 |  |  |     echo "+-----------------------------------------------------------------------" | 
 |  |  |     echo "" | 
 |  |  | # compile jobs | 
 |  |  | JOBS=`cat /proc/cpuinfo |grep "processor"|wc -l` | 
 |  |  |  | 
 |  |  | # this project absolute path | 
 |  |  | PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) | 
 |  |  |  | 
 |  |  | # top project absolute path | 
 |  |  | TOP_PATH=$(realpath $PRJ_PATH/..) | 
 |  |  |  | 
 |  |  | # binaries install path | 
 |  |  | PREFIX_PATH=$TOP_PATH/install | 
 |  |  | BIN_PATH=$PREFIX_PATH/bin | 
 |  |  | LIB_PATH=$PREFIX_PATH/lib | 
 |  |  | INC_PATH=$PREFIX_PATH/include | 
 |  |  |  | 
 |  |  | # check installed or not file | 
 |  |  | INST_FILE=$PREFIX_PATH/bin/mosquitto | 
 |  |  |  | 
 |  |  | # shell script will exit once get command error | 
 |  |  | set -e | 
 |  |  |  | 
 |  |  | #+-------------------------+ | 
 |  |  | #| Shell script functions  | | 
 |  |  | #+-------------------------+ | 
 |  |  |  | 
 |  |  | function pr_error() { | 
 |  |  |     echo -e "\033[40;31m $1 \033[0m" | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function pr_warn() { | 
 |  |  |     echo -e "\033[40;33m $1 \033[0m" | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function pr_info() { | 
 |  |  |     echo -e "\033[40;32m $1 \033[0m" | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function check_result() | 
 |  |  | { | 
 |  |  |     if [ $? != 0 ] ; then | 
 |  |  |        echo "" | 
 |  |  |        echo "+-----------------------------------------------------------------------" | 
 |  |  |        echo "|  $1 " | 
 |  |  |        echo "+-----------------------------------------------------------------------" | 
 |  |  |        echo "" | 
 |  |  |        exit ; | 
 |  |  |         pr_error "$1" | 
 |  |  |     fi | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function export_cross() | 
 |  |  | # decompress a packet to destination path | 
 |  |  | function do_unpack() | 
 |  |  | { | 
 |  |  |     tarball=$1 | 
 |  |  |     dstpath=`pwd` | 
 |  |  |  | 
 |  |  |     if [[ $# == 2 ]] ; then | 
 |  |  |         dstpath=$2 | 
 |  |  |     fi | 
 |  |  |  | 
 |  |  |     pr_info "decompress $tarball => $dstpath" | 
 |  |  |  | 
 |  |  |     mkdir -p $dstpath | 
 |  |  |     case $tarball in | 
 |  |  |         *.tar.gz) | 
 |  |  |             tar -xzf $tarball -C $dstpath | 
 |  |  |             ;; | 
 |  |  |  | 
 |  |  |         *.tar.bz2) | 
 |  |  |             tar -xjf $tarball -C $dstpath | 
 |  |  |             ;; | 
 |  |  |  | 
 |  |  |         *.tar.xz) | 
 |  |  |             tar -xJf $tarball -C $dstpath | 
 |  |  |             ;; | 
 |  |  |  | 
 |  |  |         *.tar.zst) | 
 |  |  |             tar -I zstd -xf $tarball -C $dstpath | 
 |  |  |             ;; | 
 |  |  |  | 
 |  |  |         *.tar) | 
 |  |  |             tar -xf $tarball -C $dstpath | 
 |  |  |             ;; | 
 |  |  |  | 
 |  |  |         *.zip) | 
 |  |  |             unzip -qo $tarball -d $dstpath | 
 |  |  |             ;; | 
 |  |  |  | 
 |  |  |         *) | 
 |  |  |             pr_error "decompress Unsupport packet: $tarball" | 
 |  |  |             return 1; | 
 |  |  |             ;; | 
 |  |  |     esac | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function do_export() | 
 |  |  | { | 
 |  |  |     BUILD_ARCH=$(uname -m) | 
 |  |  |     if [[ $BUILD_ARCH =~ "arm" ]] ; then | 
 |  |  |         pr_warn "local($BUILD_ARCH) compile $LIB_NAME" | 
 |  |  |         return ; | 
 |  |  |     fi | 
 |  |  |  | 
 |  |  |     pr_warn "cross(${CROSS_COMPILE}) compile $LIB_NAME" | 
 |  |  |  | 
 |  |  |     # export cross toolchain | 
 |  |  |     export CC=${CROSS_COMPILE}gcc | 
 |  |  |     export CXX=${CROSS_COMPILE}g++ | 
 |  |  | 
 |  |  |     export OBJDUMP=${CROSS_COMPILE}objdump | 
 |  |  |     export STRIP=${CROSS_COMPILE}strip | 
 |  |  |  | 
 |  |  |     # export cross configure  | 
 |  |  |     # export cross configure | 
 |  |  |     export CONFIG_CROSS=" --build=i686-pc-linux --host=arm-linux " | 
 |  |  |  | 
 |  |  |     # Clear LDFLAGS and CFLAGS | 
 |  |  | 
 |  |  |     export CFLAGS= | 
 |  |  | } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | function compile_mosquitto() | 
 |  |  | function do_fetch() | 
 |  |  | { | 
 |  |  |     SRC_NAME=mosquitto-1.6.4 | 
 |  |  |     PACK_SUFIX=tar.gz | 
 |  |  |  | 
 |  |  |     if [ -f ${PREFIX_PATH}/bin/mosquitto ] ; then | 
 |  |  |         msg_banner "$SRC_NAME already compile and installed" | 
 |  |  |         return 0; | 
 |  |  |     if [ -e ${INST_FILE} ] ; then | 
 |  |  |         pr_warn "$LIB_NAME compile and installed alredy" | 
 |  |  |         exit ; | 
 |  |  |     fi | 
 |  |  |  | 
 |  |  |     msg_banner "Start cross compile $SRC_NAME " | 
 |  |  |  | 
 |  |  |     if [ ! -f ${SRC_NAME}.${PACK_SUFIX} ] ; then | 
 |  |  |         #wget https://mosquitto.org/files/source/${SRC_NAME}.${PACK_SUFIX} | 
 |  |  |         wget ${LYFTP_SRC}/${SRC_NAME}.${PACK_SUFIX} | 
 |  |  |  | 
 |  |  |         check_result "ERROR: download ${SRC_NAME} failure" | 
 |  |  |     if [ -d $LIB_NAME ] ; then | 
 |  |  |         pr_warn "$LIB_NAME fetch already" | 
 |  |  |         return ; | 
 |  |  |     fi | 
 |  |  |  | 
 |  |  |     tar -xzf ${SRC_NAME}.${PACK_SUFIX} | 
 |  |  |     cd ${SRC_NAME} | 
 |  |  |     if [ ! -f ${LIB_NAME}.${PACK_SUFIX} ] ; then | 
 |  |  |         wget ${LIB_URL}/${LIB_NAME}.${PACK_SUFIX} | 
 |  |  |         check_result "ERROR: download ${LIB_NAME} failure" | 
 |  |  |     fi | 
 |  |  |  | 
 |  |  |     export CFLAGS=-I${PREFIX_PATH}/include | 
 |  |  |     export LDFLAGS="-L${PREFIX_PATH}/lib -lcrypto -lssl -ldl -lpthread" | 
 |  |  |     export DESTDIR=${PREFIX_PATH} | 
 |  |  |  | 
 |  |  |     make WITH_SRV=no WITH_UUID=no | 
 |  |  |     check_result "ERROR: compile ${SRC_NAME} failure" | 
 |  |  |  | 
 |  |  |     make DESTDIR=${PREFIX_PATH} prefix=/ install | 
 |  |  |     cp src/mosquitto ${PREFIX_PATH}/bin | 
 |  |  |     ${STRIP} ${PREFIX_PATH}/bin/mosquitto* | 
 |  |  |  | 
 |  |  |     cd - | 
 |  |  |     do_unpack ${LIB_NAME}.${PACK_SUFIX} | 
 |  |  | } | 
 |  |  |  | 
 |  |  | if [ ! -f ${PREFIX_PATH}//lib/libcrypto.a ] ; then | 
 |  |  |     cd ../openssl | 
 |  |  |     ./build.sh | 
 |  |  |     cd - | 
 |  |  | function do_build() | 
 |  |  | { | 
 |  |  |     cd $LIB_NAME | 
 |  |  |  | 
 |  |  |     do_export | 
 |  |  |  | 
 |  |  |     export CFLAGS=-I${INC_PATH} | 
 |  |  |     export LDFLAGS="-L${LIB_PATH} -lcrypto -lssl -ldl -lpthread" | 
 |  |  |     export DESTDIR=${PREFIX_PATH} | 
 |  |  |  | 
 |  |  |     make WITH_UUID=no WITH_STATIC_LIBRARIES=yes | 
 |  |  |     check_result "ERROR: compile ${LIB_NAME} failure" | 
 |  |  |  | 
 |  |  |     make DESTDIR=${PREFIX_PATH} prefix=/ install | 
 |  |  |     check_result "ERROR: compile ${LIB_NAME} failure" | 
 |  |  |  | 
 |  |  |     install -m 755 src/mosquitto $BIN_PATH | 
 |  |  |     install -m 644 lib/libmosquitto.a $LIB_PATH | 
 |  |  | } | 
 |  |  |  | 
 |  |  | function do_clean() | 
 |  |  | { | 
 |  |  |     rm -rf *${LIB_NAME}* | 
 |  |  | } | 
 |  |  |  | 
 |  |  | if [[ $# == 1 && $1 == -c ]] ;then | 
 |  |  |     pr_warn "start clean ${LIB_NAME}" | 
 |  |  |     do_clean | 
 |  |  |     exit; | 
 |  |  | fi | 
 |  |  |  | 
 |  |  | export_cross | 
 |  |  | cd ../openssl && ./build.sh && cd - | 
 |  |  | cd ../cjson && ./build.sh && cd - | 
 |  |  |  | 
 |  |  | compile_mosquitto | 
 |  |  | do_fetch | 
 |  |  |  | 
 |  |  | do_build | 
 |  |  |  |