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 brew
0077 
0078 if [[ $? -ne 0 ]]; then
0079     echo "HomeBrew is not installed"
0080     echo "See https://docs.brew.sh/Installation for details."
0081     exit 1
0082 else
0083     echo "Check HomeBrew 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 
0187 elif [[ $MAJOR_OSX_VERSION == "13" ]]
0188     then OSX_CODE_NAME="Ventura"
0189 
0190 elif [[ $MAJOR_OSX_VERSION == "14" ]]
0191     then OSX_CODE_NAME="Sonoma"
0192 fi
0193 
0194 echo -e "---------- Detected OSX version $MAJOR_OSX_VERSION.$MINOR_OSX_VERSION and code name $OSX_CODE_NAME"
0195 
0196 }
0197 
0198 #################################################################################################
0199 # Relocate list of binaries files.
0200 # Replace INSTALL_PREFIX by @rpath in library paths dependencies registered in bin file.
0201 # List of bin files to patch is passed as first argument.
0202 RelocatableBinaries()
0203 {
0204 
0205 RPATHSTR="@rpath"
0206 
0207 FILESLIST=("${!1}")
0208 
0209 #echo "Relocatable list: ${FILESLIST[@]}"
0210 
0211 for FILE in ${FILESLIST[@]} ; do
0212 
0213     ISMACHO=`file "$FILE" | grep "Mach-O" || true`
0214 
0215     # Do not touch debug extension
0216     ISDSYM=`file "$FILE" | grep "dSYM" || true`
0217 
0218     if [[ $ISMACHO ]] && [[ ! $ISDSYM ]] ; then
0219 
0220         # For each file from bin list, we replace the absolute path to external dependency with a relative path
0221         # NOTE: relative path must be resolved in main executable later.
0222 
0223         echo "Relocate $FILE"
0224 
0225         # List all external dependencies starting with INSTALL_PREFIX
0226 
0227         DEPS=$(otool -L $FILE | grep $INSTALL_PREFIX | awk -F ' \\\(' '{print $1}')
0228 
0229         for EXTLIB in $DEPS ; do
0230 
0231             RPATHLIB=${EXTLIB/$INSTALL_PREFIX/$RPATHSTR}
0232  #           echo "   $EXTLIB ==> $RPATHLIB"
0233             install_name_tool -change $EXTLIB $RPATHLIB $FILE
0234             codesign --force -s - $FILE
0235 
0236         done
0237 
0238     fi
0239 
0240 done
0241 
0242 }
0243 
0244 ########################################################################
0245 # Automatically register the remote servers has know hosts
0246 RegisterRemoteServers()
0247 {
0248 
0249 SERVER_LIST="\
0250 invent.kde.org \
0251 tinami.kde.org \
0252 "
0253 
0254 if [[ ! -f ~/.ssh/known_hosts ]] ; then
0255     touch ~/.ssh/known_hosts
0256 fi
0257 
0258 for server in $SERVER_LIST; do
0259 
0260     echo "Register $server"
0261 
0262     ssh-keygen -R $server
0263     ssh-keyscan -H $server >> ~/.ssh/known_hosts
0264 
0265 done
0266 
0267 }
0268 
0269 ########################################################################
0270 # Copy dependencies with otool analysis
0271 # arg1 : original file path to parse.
0272 # arg2 : target path to copy dependencies.
0273 
0274 # Cache in memory of already scanned files to speed-up operations.
0275 _already_scanned_libs=()
0276 
0277 CopyReccursiveDependencies()
0278 {
0279 
0280 # Check if file have not alredy scanned
0281 echo ${_already_scanned_libs[@]} | grep -q "$1" && return;
0282 
0283 DEPS=$(otool -L $INSTALL_PREFIX/$1 | grep $INSTALL_PREFIX | awk -F ' \\\(' '{print $1}')
0284 
0285 _already_scanned_libs+=("$1")
0286 
0287 for EXTLIB in $DEPS ; do
0288 
0289     if [[ $EXTLIB == $INSTALL_PREFIX/$1* ]] ; then
0290         continue
0291     fi
0292 
0293     _library="${EXTLIB/$INSTALL_PREFIX\//}"
0294 
0295     if [ ! -e "$TEMPROOT/$_library" ] ; then
0296         _directory="${_library%/*}"
0297 
0298         if [ ! -d "$TEMPROOT/$_directory" ] ; then
0299 #            echo "  Creating $TEMPROOT/$_directory"
0300             mkdir -p "$TEMPROOT/$_directory"
0301         fi
0302 
0303         echo "  Copying $_library"
0304         cp -aH "$INSTALL_PREFIX/$_library" "$TEMPROOT/$_directory/"
0305     fi
0306 
0307     CopyReccursiveDependencies "$_library" "$2"
0308 
0309 done
0310 
0311 }