From cafc37823fac451035e065fa469338fab3a5641a Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Fri, 23 Aug 2024 17:59:05 +0800
Subject: [PATCH] update openlibs build shell script

---
 openlibs/openssh/build.sh |  214 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 144 insertions(+), 70 deletions(-)

diff --git a/openlibs/openssh/build.sh b/openlibs/openssh/build.sh
index 96afb7c..cb33f5f 100755
--- a/openlibs/openssh/build.sh
+++ b/openlibs/openssh/build.sh
@@ -1,45 +1,120 @@
 #!/bin/bash
 
-#+--------------------------------------------------------------------------------------------
-#|Description:  This shell script used download and compile openssh for ARM
-#|     Author:  GuoWenxue <guowenxue@gmail.com>
-#|  ChangeLog:
-#|           1, Initialize 1.0.0 on 2011.04.12
-#+--------------------------------------------------------------------------------------------
+# library name and version
+# Official: https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/${SRC_NAME}.${PACK_SUFIX}
+LIB_NAME=openssh-8.0p1
+PACK_SUFIX=tar.gz
 
-PRJ_PATH=`pwd`
-PREFIX_PATH=${PRJ_PATH}/../install/
-#PREFIX_PATH=${PRJ_PATH}/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/sbin/sshd
+
+# 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 AS=${CROSS_COMPILE}as
     export AR=${CROSS_COMPILE}ar
     export LD=${CROSS_COMPILE}ld
@@ -48,12 +123,32 @@
     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 LDFLAGS=
     export CFLAGS=
+}
+
+function do_fetch()
+{
+    if [ -e ${INST_FILE} ] ; then
+        pr_warn "$LIB_NAME compile and installed alredy"
+        exit ;
+    fi
+
+    if [ -d $LIB_NAME ] ; then
+        pr_warn "$LIB_NAME fetch already"
+        return ;
+    fi
+
+    if [ ! -f ${LIB_NAME}.${PACK_SUFIX} ] ; then
+        wget ${LIB_URL}/${LIB_NAME}.${PACK_SUFIX}
+        check_result "ERROR: download ${LIB_NAME} failure"
+    fi
+
+    do_unpack ${LIB_NAME}.${PACK_SUFIX}
 }
 
 function modify_config()
@@ -63,72 +158,51 @@
     #sed -i -e "s|.*HAS_SHADOW_EXPIRE.*|/* #undef HAS_SHADOW_EXPIRE */|g" config.h
     #sed -i -e "s|.*USE_LINUX_AUDIT.*|/* #undef USE_LINUX_AUDIT */|g" config.h
     sed -i -e "s|.*HAVE___PROGNAME.*|/* #undef HAVE___PROGNAME */|g" config.h
-    echo "modify done"
+    sed -i -e "s|^prefix=.*|prefix=|g" Makefile
+    sed -i -e "s|^DESTDIR=.*|DESTDIR=${PREFIX_PATH}|g" Makefile
+    pr_warn "modify done"
 }
 
-function compile_openssh()
+function do_build()
 {
-    SRC_NAME=openssh-8.0p1
-    PACK_SUFIX=tar.gz
+    cd $LIB_NAME
 
-    if [ -f ${PREFIX_PATH}/sbin/sshd ] ; then
-        msg_banner "$SRC_NAME already compile and installed"
-        return 0;
-    fi
-
-    msg_banner "Start donwload $SRC_NAME "
-    if [ ! -f ${SRC_NAME}.${PACK_SUFIX} ] ; then
-        #wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/${SRC_NAME}.${PACK_SUFIX}
-        wget ${LYFTP_SRC}/${SRC_NAME}.${PACK_SUFIX}
-        check_result "ERROR: download ${SRC_NAME} failure"
-    fi 
-
-    rm -rf ${SRC_NAME}
-
-    tar -xzf ${SRC_NAME}.${PACK_SUFIX}
-
-    cd ${SRC_NAME}
-
-    msg_banner "Start cross compile $SRC_NAME "
+    do_export
 
     # must --disable-utmpx for lost these  files
     # must --with-audit=debug can not use linux
     # must --without-pie or environ is NULL will case SIGSEGV
     export ac_cv_func_realloc_0_nonnull=yes
     export ac_cv_func_malloc_0_nonnull=yes
-    ./configure --prefix=${PREFIX_PATH} ${CONFIG_CROSS} --disable-largefile --disable-pkcs11 --disable-strip \
+    ./configure ${CONFIG_CROSS} --disable-largefile --disable-pkcs11 --disable-strip \
         --disable-etc-default-login --disable-lastlog --disable-utmp --disable-utmpx --disable-wtmp --without-stackprotect \
         --disable-wtmpx --disable-pututline --disable-pututxline --without-hardening --without-rpath \
         --with-audit=debug --without-pie --without-pam --with-privsep-user=sshd --without-selinux \
-        --with-libs --with-zlib=${PREFIX_PATH} --with-ssl-dir=${PREFIX_PATH} --with-ssl-engine 
-
-    check_result "ERROR: configure ${SRC_NAME} failure"
+        --with-libs --with-zlib=${PREFIX_PATH} --with-ssl-dir=${PREFIX_PATH} --with-ssl-engine
+    check_result "ERROR: configure ${LIB_NAME} failure"
 
     modify_config
 
-    make && make install
-
-    check_result "ERROR: compile ${SRC_NAME} failure"
-
-
-    cd -
+    make && make install-nosysconf
+    check_result "ERROR: compile ${LIB_NAME} failure"
 }
 
+function do_clean()
+{
+    rm -rf *${LIB_NAME}*
+}
 
-if [ ! -f ${PREFIX_PATH}/lib/libz.a ] ; then
-    cd ../zlib
-       ./build.sh
-    cd -
+if [[ $# == 1 && $1 == -c ]] ;then
+    pr_warn "start clean ${LIB_NAME}"
+    do_clean
+    exit;
 fi
 
+cd ../zlib/ && ./build.sh && cd -
 
-if [ ! -f ${PREFIX_PATH}/lib/libcrypto.a ] ; then
-    cd ../openssl
-       ./build.sh
-    cd -
-fi
+cd ../openssl/ && ./build.sh && cd -
 
-export_cross
-compile_openssh
+do_fetch
 
+do_build
 

--
Gitblit v1.9.1