File indexing completed on 2024-06-16 04:06:50

0001 #!/bin/bash
0002 
0003 # SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier  <caulier dot gilles at gmail dot com>
0004 #
0005 # SPDX-License-Identifier: BSD-3-Clause
0006 #
0007 
0008 ########################################################################
0009 # Check if run as root
0010 ChecksRunAsRoot()
0011 {
0012 
0013 if [[ $EUID -ne 0 ]]; then
0014     echo "This script should be run as root using sudo command."
0015     exit 1
0016 else
0017     echo "Check run as root passed..."
0018 fi
0019 
0020 }
0021 
0022 ########################################################################
0023 # Check CPU core available (Linux or MacOS)
0024 ChecksCPUCores()
0025 {
0026 
0027 CPU_CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)
0028 
0029 if [[ $CPU_CORES -gt 1 ]]; then
0030     CPU_CORES=$((CPU_CORES-1))
0031 fi
0032 
0033 echo "CPU Cores to use : $CPU_CORES"
0034 
0035 }
0036 
0037 ########################################################################
0038 # For time execution measurement ; startup
0039 StartScript()
0040 {
0041 
0042 BEGIN_SCRIPT=$(date +"%s")
0043 
0044 }
0045 
0046 ########################################################################
0047 # For time execution measurement : shutdown
0048 TerminateScript()
0049 {
0050 
0051 TERMIN_SCRIPT=$(date +"%s")
0052 difftimelps=$(($TERMIN_SCRIPT-$BEGIN_SCRIPT))
0053 echo "Elaspsed time for script execution : $(($difftimelps / 3600 )) hours $((($difftimelps % 3600) / 60)) minutes $(($difftimelps % 60)) seconds"
0054 
0055 }
0056 
0057 ########################################################################
0058 # Copy dependencies with dumpbin analysis
0059 # arg1 : dumpbin program full path.
0060 # arg2 : original file path to parse.
0061 # arg3 : target path to copy dependencies.
0062 # arg4 : dependencies path (only copy shared libs from this path).
0063 CopyReccursiveDependencies()
0064 {
0065 
0066 echo "Scan dependencies for $2"
0067 
0068 FILES=$("$1" -DEPENDENTS "$2" | grep ".dll" | awk '{print $1}')
0069 #echo "deps: $FILES"
0070 for FILE in $FILES ; do
0071     if [[ -f "$4/$FILE"  && ! -f  "$3/$FILE" ]] ; then
0072         cp -u "$4/$FILE" "$3" 2> /dev/null || true
0073 #        echo "   ==> $4/$FILE"
0074         CopyReccursiveDependencies "$1" "$4/$FILE" "$3" "$4"
0075     fi
0076 done
0077 
0078 }
0079 
0080 ########################################################################
0081 # Automatically register the remote servers has know hosts
0082 RegisterRemoteServers()
0083 {
0084 
0085 SERVER_LIST="\
0086 invent.kde.org \
0087 tinami.kde.org \
0088 "
0089 
0090 if [[ ! -f ~/.ssh/known_hosts ]] ; then
0091     touch ~/.ssh/known_hosts
0092 fi
0093 
0094 for server in $SERVER_LIST; do
0095 
0096     echo "Register $server"
0097 
0098     ssh-keygen -R $server
0099     ssh-keyscan -H $server >> ~/.ssh/known_hosts
0100 
0101 done
0102 
0103 }
0104 
0105 ########################################################################
0106 # Append paths to the wanted VCPKG binary tools
0107 AppendVCPKGPaths()
0108 {
0109 
0110 ORIG_PATH="$PATH"
0111 
0112 export PATH="\
0113 $PATH:\
0114 /c/bison:\
0115 /c/icoutils/bin:\
0116 $VCPKG_INSTALL_PREFIX/tools/gperf:\
0117 $VCPKG_INSTALL_PREFIX/tools/curl:\
0118 $VCPKG_INSTALL_PREFIX/tools/python3:\
0119 $VCPKG_INSTALL_PREFIX/tools/pkgconf:\
0120 $VCPKG_INSTALL_PREFIX/bin:\
0121 $VCPKG_INSTALL_PREFIX/tools/Qt6/bin\
0122 "
0123 
0124 echo "PATH=$PATH"
0125 
0126 export PKG_CONFIG_PATH=$VCPKG_INSTALL_PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH
0127 
0128 }
0129 
0130 ########################################################################
0131 # Upload a large file to a remote server with retry if failure
0132 # arg1 : local file path to upload.
0133 # arg2 : remote ssh url.
0134 # arg3 : remote directory.
0135 # arg4 : pause between retry.
0136 UploadWithRetry()
0137 {
0138 
0139 MAX_RETRIES=10
0140 i=0
0141 RC=1
0142 
0143 while [[ $RC -ne 0 ]] ; do
0144 
0145     i=$(($i+1))
0146 
0147     if [ $i -eq $MAX_RETRIES ] ; then
0148         echo -e "Hit maximum number of retries, giving up."
0149         exit -1
0150     fi
0151 
0152     sleep $4
0153     echo -e "Try $i/$MAX_RETRIES :: rsync -r -v --progress -e ssh $1 $2:$3"
0154     bash -c "rsync -r -v --progress -e ssh $1 $2:$3"
0155     RC=$?
0156     echo "rsync return code: $RC"
0157 
0158 done
0159 
0160 }