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

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 if Xcode Command Line tools are installed
0024 ChecksXCodeCLI()
0025 {
0026 
0027 xcode-select --print-path
0028 
0029 if [[ $? -ne 0 ]]; then
0030     echo "XCode CLI tools are not installed"
0031     echo "See http://www.macports.org/install.php for details."
0032     exit 1
0033 else
0034     echo "Check XCode CLI tools passed..."
0035 fi
0036 
0037 export MACOSX_DEPLOYMENT_TARGET=$OSX_MIN_TARGET
0038 echo "Target OSX minimal version: $MACOSX_DEPLOYMENT_TARGET"
0039 
0040 MACOS_MAJOR=`echo $MACOSX_DEPLOYMENT_TARGET | awk -F '.' '{print $1 "." $2}'| cut -d . -f 1`
0041 MACOS_MINOR=`echo $MACOSX_DEPLOYMENT_TARGET | awk -F '.' '{print $1 "." $2}'| cut -d . -f 2`
0042 
0043 if [[ $MACOS_MAJOR -lt 11 && $MACOS_MINOR -lt 9 ]]; then
0044     export CXXFLAGS=-stdlib=libc++
0045 fi
0046 
0047 if [[ ! -d /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX$MACOS_MAJOR.$MACOS_MINOR.sdk ]] ; then
0048     echo "XCode Target SDK $MACOS_MAJOR.$MACOS_MINOR as minimal version is not installed in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs"
0049 
0050     if [[ ! -d $DOWNLOAD_DIR/macos-sdk.git ]] ; then
0051 
0052         echo "Downloading archive of SDK from https://github.com/alexey-lysiuk/macos-sdk, please wait..."
0053         git clone https://github.com/alexey-lysiuk/macos-sdk.git $DOWNLOAD_DIR/macos-sdk.git
0054 
0055     fi
0056 
0057     echo "Copying SDK $MACOS_MAJOR.$MACOS_MINOR into XCode, please wait..."
0058     cp -R $DOWNLOAD_DIR/macos-sdk.git/MacOSX$MACOS_MAJOR.$MACOS_MINOR.sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/
0059 
0060 else
0061 
0062     echo "Check XCode Target SDK minimal version passed..."
0063 
0064 fi
0065 
0066 # Adjust the property "MinimumSDKVersion" from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Info.plist
0067 /usr/libexec/PlistBuddy -c "Set MinimumSDKVersion $MACOS_MAJOR.$MACOS_MINOR" /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Info.plist
0068 
0069 }
0070 
0071 ########################################################################
0072 # Check if Macports is installed
0073 ChecksMacports()
0074 {
0075 
0076 which port
0077 
0078 if [[ $? -ne 0 ]]; then
0079     echo "Macports is not installed"
0080     echo "See http://www.macports.org/install.php for details."
0081     exit 1
0082 else
0083     echo "Check Macports passed..."
0084 fi
0085 
0086 }
0087 
0088 ########################################################################
0089 # Check CPU core available (Linux or MacOS)
0090 ChecksCPUCores()
0091 {
0092 
0093 CPU_CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)
0094 
0095 if [[ $CPU_CORES -gt 1 ]]; then
0096     CPU_CORES=$((CPU_CORES-1))
0097 fi
0098 
0099 echo "CPU Cores to use : $CPU_CORES"
0100 
0101 }
0102 
0103 ########################################################################
0104 # Performs All Checks
0105 CommonChecks()
0106 {
0107 
0108 ChecksRunAsRoot
0109 ChecksXCodeCLI
0110 ChecksMacports
0111 
0112 }
0113 
0114 ########################################################################
0115 # For time execution measurement ; startup
0116 StartScript()
0117 {
0118 
0119 BEGIN_SCRIPT=$(date +"%s")
0120 
0121 }
0122 
0123 ########################################################################
0124 # For time execution measurement : shutdown
0125 TerminateScript()
0126 {
0127 
0128 TERMIN_SCRIPT=$(date +"%s")
0129 difftimelps=$(($TERMIN_SCRIPT-$BEGIN_SCRIPT))
0130 echo "Elaspsed time for script execution : $(($difftimelps / 3600 )) hours $((($difftimelps % 3600) / 60)) minutes $(($difftimelps % 60)) seconds"
0131 
0132 }
0133 
0134 ########################################################################
0135 # Set strings with detected MacOS info :
0136 #    $MAJOR_OSX_VERSION : detected MacOS major ID (as 10, 11, 12)
0137 #    $MINOR_OSX_VERSION : detected MacOS minor ID (as 7 for 10.7 or 10 for 10.10)
0138 #    $OSX_CODE_NAME     : detected MacOS code name
0139 OsxCodeName()
0140 {
0141 
0142 MAJOR_OSX_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $1 "." $2}'| cut -d . -f 1)
0143 MINOR_OSX_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $1 "." $2}'| cut -d . -f 2)
0144 
0145 if   [[ $MAJOR_OSX_VERSION == "10" ]] ; then
0146 
0147     if   [[ $MINOR_OSX_VERSION == "15" ]]
0148         then OSX_CODE_NAME="Catalina"
0149     elif [[ $MINOR_OSX_VERSION == "14" ]]
0150         then OSX_CODE_NAME="Mojave"
0151     elif [[ $MINOR_OSX_VERSION == "13" ]]
0152         then OSX_CODE_NAME="HighSierra"
0153     elif [[ $MINOR_OSX_VERSION == "12" ]]
0154         then OSX_CODE_NAME="Sierra"
0155     elif [[ $MINOR_OSX_VERSION == "11" ]]
0156         then OSX_CODE_NAME="ElCapitan"
0157     elif [[ $MINOR_OSX_VERSION == "10" ]]
0158         then OSX_CODE_NAME="Yosemite"
0159     elif [[ $MINOR_OSX_VERSION == "9" ]]
0160         then OSX_CODE_NAME="Mavericks"
0161     elif [[ $MINOR_OSX_VERSION == "8" ]]
0162         then OSX_CODE_NAME="MountainLion"
0163     elif [[ $MINOR_OSX_VERSION == "7" ]]
0164         then OSX_CODE_NAME="Lion"
0165     elif [[ $MINOR_OSX_VERSION == "6" ]]
0166         then OSX_CODE_NAME="SnowLeopard"
0167     elif [[ $MINOR_OSX_VERSION == "5" ]]
0168         then OSX_CODE_NAME="Leopard"
0169     elif [[ $MINOR_OSX_VERSION == "4" ]]
0170         then OSX_CODE_NAME="Tiger"
0171     elif [[ $MINOR_OSX_VERSION == "3" ]]
0172         then OSX_CODE_NAME="Panther"
0173     elif [[ $MINOR_OSX_VERSION == "2" ]]
0174         then OSX_CODE_NAME="Jaguar"
0175     elif [[ $MINOR_OSX_VERSION == "1" ]]
0176         then OSX_CODE_NAME="Puma"
0177     elif [[ $MINOR_OSX_VERSION == "0" ]]
0178         then OSX_CODE_NAME="Cheetah"
0179     fi
0180 
0181 elif [[ $MAJOR_OSX_VERSION == "11" ]]
0182     then OSX_CODE_NAME="BigSur"
0183 
0184 elif [[ $MAJOR_OSX_VERSION == "12" ]]
0185     then OSX_CODE_NAME="Monterey"
0186 fi
0187 
0188 echo -e "---------- Detected OSX version $MAJOR_OSX_VERSION.$MINOR_OSX_VERSION and code name $OSX_CODE_NAME"
0189 
0190 }
0191 
0192 #################################################################################################
0193 # Relocate list of binaries files.
0194 # Replace INSTALL_PREFIX by @rpath in library paths dependencies registered in bin file.
0195 # List of bin files to patch is passed as first argument.
0196 RelocatableBinaries()
0197 {
0198 
0199 RPATHSTR="@rpath"
0200 
0201 FILESLIST=("${!1}")
0202 
0203 #echo "Relocatable list: ${FILESLIST[@]}"
0204 
0205 for FILE in ${FILESLIST[@]} ; do
0206 
0207     ISMACHO=`file "$FILE" | grep "Mach-O" || true`
0208 
0209     # Do not touch debug extension
0210     ISDSYM=`file "$FILE" | grep "dSYM" || true`
0211 
0212     if [[ $ISMACHO ]] && [[ ! $ISDSYM ]] ; then
0213 
0214         # For each file from bin list, we replace the absolute path to external dependency with a relative path
0215         # NOTE: relative path must be resolved in main executable later.
0216 
0217         echo "Relocate $FILE"
0218 
0219         # List all external dependencies starting with INSTALL_PREFIX
0220 
0221         DEPS=$(otool -L $FILE | grep $INSTALL_PREFIX | awk -F ' \\\(' '{print $1}')
0222 
0223         for EXTLIB in $DEPS ; do
0224 
0225             RPATHLIB=${EXTLIB/$INSTALL_PREFIX/$RPATHSTR}
0226  #           echo "   $EXTLIB ==> $RPATHLIB"
0227             install_name_tool -change $EXTLIB $RPATHLIB $FILE
0228 
0229         done
0230 
0231     fi
0232 
0233 done
0234 
0235 }
0236 
0237 ########################################################################
0238 # Automatically register the remote servers has know hosts
0239 RegisterRemoteServers()
0240 {
0241 
0242 SERVER_LIST="\
0243 invent.kde.org \
0244 tinami.kde.org \
0245 "
0246 
0247 if [[ ! -f ~/.ssh/known_hosts ]] ; then
0248     touch ~/.ssh/known_hosts
0249 fi
0250 
0251 for server in $SERVER_LIST; do
0252 
0253     echo "Register $server"
0254 
0255     ssh-keygen -R $server
0256     ssh-keyscan -H $server >> ~/.ssh/known_hosts
0257 
0258 done
0259 
0260 }