VirtualBox

Changeset 85060 in vbox for trunk


Ignore:
Timestamp:
Jul 5, 2020 9:25:42 PM (4 years ago)
Author:
vboxsync
Message:

tools/darwin.amd64: Simple SDK extractor tool.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/tools/darwin.amd64/bin/sdk-extractor.sh

    r85039 r85060  
    22# $Id$
    33## @file
    4 # Extracts the necessary bits from the Xcode 4.1 lion package (inside installercode_41_lion.dmg).
    5 
    6 #
    7 # Copyright (C) 2014 Oracle Corporation
     4# Extracts the SDKs from a commandline tools DMG.
     5#
     6
     7#
     8# Copyright (C) 2014-2020 Oracle Corporation
    89#
    910# This file is part of VirtualBox Open Source Edition (OSE), as
     
    3233# Constants.
    3334#
    34 MY_PKGS="gcc4.2.pkg llvm-gcc4.2.pkg DeveloperToolsCLI.pkg xcrun.pkg JavaSDK.pkg MacOSX10.6.pkg MacOSX10.7.pkg"
    35 MY_LAST_PKG="MacOSX10.7.pkg"
    36 MY_PKGS="clang.pkg"
    37 MY_LAST_PKG="clang.pkg"
    38 declare -a MY_FULL_PKGS
    39 for i in $MY_PKGS;
    40 do
    41     MY_FULL_PKGS[$((${#MY_FULL_PKGS[*]}))]="./Applications/Install Xcode.app/Contents/Resources/Packages/${i}"
    42 done
     35#MY_PKGS="gcc4.2.pkg llvm-gcc4.2.pkg DeveloperToolsCLI.pkg xcrun.pkg JavaSDK.pkg MacOSX10.6.pkg MacOSX10.7.pkg"
     36#MY_LAST_PKG="MacOSX10.7.pkg"
     37#MY_PKGS="clang.pkg"
     38#MY_LAST_PKG="clang.pkg"
     39#declare -a MY_FULL_PKGS
     40#for i in $MY_PKGS;
     41#do
     42#    MY_FULL_PKGS[$((${#MY_FULL_PKGS[*]}))]="./Applications/Install Xcode.app/Contents/Resources/Packages/${i}"
     43#done
    4344
    4445#
    4546# Parse arguments.
    4647#
    47 MY_TMP_DIR=/var/tmp/xcode41extractor
    48 MY_DST_DIR="${MY_DARWIN_DIR}/xcode/v41"
     48MY_TMP_DIR=/var/tmp/sdkextractor
     49MY_DST_DIR="${MY_DARWIN_DIR}/sdk/incoming"
     50MY_DMG_FILE=
    4951MY_PKG_FILE=
     52MY_DRY_RUN=
    5053
    5154my_usage()
    5255{
    53     echo "usage: $0 [--tmpdir|-t <tmpdir>]  <--destination|-d> <dstdir>  <--filename|-f> <dir/InstallXcodeLion.pkg>";
     56    echo "usage: $0 [--dry-run] [--tmpdir|-t <tmpdir>]  <--destination|-d> <dstdir>  <--filename|-f> <dir/Command_Line_Tools*.dmg|pkg>";
     57    echo ""
     58    echo "Works for command lines tools for Xcode 8.2 and later."
     59    echo "For older versions, the SDK must be extraced from Xcode itself, it seems."
    5460    exit $1;
    5561}
     
    8490                exit 1;
    8591            fi
    86             MY_PKG_FILE="$1";
     92            case "$1" in
     93                *.[dD][mM][gG])
     94                    MY_DMG_FILE="$1";
     95                    MY_PKG_FILE=;
     96                    ;;
     97                *.[pP][kK][gG])
     98                    MY_PKG_FILE="$1";
     99                    MY_DMG_FILE=;
     100                    ;;
     101                *)
     102                    echo "error: filename does not end with .dmg or .pkg." 1>&2;
     103                    exit 1;
     104                    ;;
     105            esac
    87106            shift;
     107            ;;
     108
     109        --dry-run)
     110            MY_DRY_RUN=1;
    88111            ;;
    89112
     
    93116done
    94117
    95 # Check the package file.
    96 if [ -z "${MY_PKG_FILE}" ]; then
    97     echo "error: missing --filename <dir/InstallXcodeLion.pkg>." 1>&2l
     118# We must have something to work with.
     119if [ -z "${MY_PKG_FILE}" -a -z "${MY_DMG_FILE}" ]; then
     120    echo "error: missing --filename <dir/Command_Line_Tools*.dmg|pkg>." 1>&2l
    98121    my_usage 1;
    99 fi
    100 if ! xar -tf "${MY_PKG_FILE}" > /dev/null ; then
    101     echo "error: xar has trouble with '${MY_PKG_FILE}'." 1>&2;
    102     exit 1;
    103122fi
    104123
     
    118137    my_usage 1;
    119138fi
    120 if ! mkdir -p "${MY_TMP_DIR}/x"; then
    121     echo "error: error creating '${MY_TMP_DIR}/x'." 1>&2;
    122     exit 1;
    123 fi
    124 
    125 #
    126 # Extract the "Applications/Install Xcode.app" payload, calling it MainPayload.tar.
    127 #
    128 if [ ! -f "${MY_TMP_DIR}/x/MainPayload.tar" ]; then
    129     echo "info: Extracting '${MY_PKG_FILE}'..."
    130     if ! xar -xvf "${MY_PKG_FILE}" -C "${MY_TMP_DIR}/x"; then
    131         echo "error: extraction error." 1>&2;
    132         exit 1;
    133     fi
    134     if ! mv -f "${MY_TMP_DIR}/x/InstallXcodeLion.pkg/Payload" "${MY_TMP_DIR}/x/MainPayload.tar"; then
    135         echo "error: Failed to move the package payload. Did you get the right package file?" 1>&2;
    136         exit 1;
    137     fi
    138 fi
    139 
    140 #
    141 # Extract the sub-packages from MainPayload.tar.
    142 #
    143 if [ ! -f "${MY_TMP_DIR}/x/${MY_LAST_PKG}" ]; then
    144     echo "info: Extracting packages from 'MainPayload.tar'..."
    145     if ! tar xvf "${MY_TMP_DIR}/x/MainPayload.tar" -C "${MY_TMP_DIR}/x" "${MY_FULL_PKGS[@]}"; then
    146         echo "error: Failure extracting sub-packages from MainPayload.tar (see above)." 1>&2;
    147         exit 1;
    148     fi
    149 
    150     for i in $MY_PKGS;
     139MY_TMP_DIR_X="${MY_TMP_DIR}/x$$";
     140if ! mkdir -p "${MY_TMP_DIR_X}"; then
     141    echo "error: error creating '${MY_TMP_DIR_X}'." 1>&2;
     142    exit 1;
     143fi
     144MY_TMP_DIR_Y="${MY_TMP_DIR}/y$$";
     145if ! mkdir -p "${MY_TMP_DIR_Y}"; then
     146    echo "error: error creating '${MY_TMP_DIR_Y}'." 1>&2;
     147    exit 1;
     148fi
     149
     150# Attach the DMG if one is given, then find the PKG file inside it.
     151MY_TMP_DIR_DMG="${MY_TMP_DIR}/dmg";
     152rmdir -- "${MY_TMP_DIR_DMG}" 2>/dev/null;
     153if [ -d "${MY_TMP_DIR_DMG}" ]; then
     154    echo "info: Unmount '${MY_TMP_DIR_DMG}'...";
     155    hdiutil detach -force "${MY_TMP_DIR_DMG}";
     156    if ! rmdir -- "${MY_TMP_DIR_DMG}"; then
     157        echo "error: failed to detach DMG from previous run (mountpoint='${MY_TMP_DIR_DMG}')." 1>&2;
     158        exit 1;
     159    fi
     160fi
     161if [ -n "${MY_DMG_FILE}" ]; then
     162    echo "info: Mounting '${MY_DMG_FILE}' at '${MY_TMP_DIR_DMG}'...";
     163    if ! mkdir -p -- "${MY_TMP_DIR_DMG}"; then
     164        echo "error: error creating '${MY_TMP_DIR_DMG}'." 1>&2;
     165        exit 1;
     166    fi
     167
     168    if ! hdiutil attach -mountpoint "${MY_TMP_DIR_DMG}" -noautoopen -nobrowse -noverify "${MY_DMG_FILE}"; then
     169        echo "error: hdiutil attach failed for '${MY_DMG_FILE}'" 1>&2;
     170        exit 1;
     171    fi
     172    for x in "${MY_TMP_DIR_DMG}/"*.pkg;
    151173    do
    152         if ! mv -f "${MY_TMP_DIR}/x/Applications/Install Xcode.app/Contents/Resources/Packages/${i}" "${MY_TMP_DIR}/x/${i}"; then
    153             echo "error: Failed to move the package ${i}." 1>&2;
     174        if [ -e "${x}" ]; then
     175            MY_PKG_FILE=$x;
     176        fi
     177    done
     178    if [ -z "${MY_PKG_FILE}" ]; then
     179        echo "error: Found no .pkg file inside the DMG attached at '${MY_TMP_DIR_DMG}'." 1>&2;
     180        exit 1;
     181    fi
     182    echo "info: MY_PKG_FILE=${MY_PKG_FILE}";
     183fi
     184
     185# Check the package file.
     186echo "info: Checking '${MY_PKG_FILE}'...";
     187if ! xar -tf "${MY_PKG_FILE}" > /dev/null ; then
     188    echo "error: xar has trouble with '${MY_PKG_FILE}'." 1>&2;
     189    exit 1;
     190fi
     191
     192#
     193# Find the SDK packages and extract them to the 'x' directory.
     194#
     195# Command_Line_Tools_macOS_10.11_for_Xcode_8.2.dmg contains two packages with SDK
     196# in the name: CLTools_SDK_OSX1012.pkg and DevSDK_OSX1011.pkg.  The former ends up
     197# in /Library/Developer/ and the latter in root (/).  So, only pick the first one.
     198#
     199MY_SDK_PKGS=$(xar -tf "${MY_PKG_FILE}" | grep "SDK.*\.pkg$" | grep -v DevSDK_ | tr '\n\r' '  ')
     200if [ -z "${MY_SDK_PKGS}" ]; then
     201    echo "error: Found no SDK packages in '${MY_PKG_FILE}'." 1>&2;
     202    xar -tf "${MY_PKG_FILE}" 1>&2
     203    exit 1;
     204fi
     205echo "info: Extracking SDK packages: ${MY_SDK_PKGS}"
     206
     207if ! xar -xf "${MY_PKG_FILE}" -C "${MY_TMP_DIR_X}" ${MY_SDK_PKGS}; then
     208    echo "error: Failed to extract ${MY_SDK_PKGS} from '${MY_PKG_FILE}'." 1>&2;
     209    exit 1;
     210fi
     211
     212#
     213# Expand the unpacked packages into the Y directory.
     214#
     215for pkg in ${MY_SDK_PKGS};
     216do
     217    echo "info: Expanding '${MY_TMP_DIR_X}/${pkg}' using pbzx and cpio...";
     218    if ! pbzx "${MY_TMP_DIR_X}/${pkg}/Payload" > "${MY_TMP_DIR_X}/${pkg}/Payload.unpacked" ; then
     219        echo "error: Failed to unpack '${MY_TMP_DIR_X}/${pkg}/Payload' using pbzx." 1>&2;
     220        exit 1;
     221    fi
     222
     223    MY_CWD=`pwd`
     224    cd "${MY_TMP_DIR_Y}" || exit 1;
     225    if ! cpio -i < "${MY_TMP_DIR_X}/${pkg}/Payload.unpacked"; then
     226        echo "error: Failed to expand '${MY_TMP_DIR_X}/${pkg}/Payload.unpacked' using cpio." 1>&2;
     227        exit 1;
     228    fi
     229    cd "${MY_CWD}"
     230done
     231
     232#
     233# Now, pick the bits we want from the Y directory and move it over to the destination directory.
     234#
     235for sdk in "${MY_TMP_DIR_Y}/Library/Developer/CommandLineTools/SDKs/MacOSX10"*.sdk;
     236do
     237    MY_BASENAME=`basename "${sdk}"`
     238    echo "info: Moving '${sdk}/' to '${MY_DST_DIR}/${MY_BASENAME}'...";
     239    if [ -z "${MY_DRY_RUN}" ]; then
     240        if ! mv "${sdk}/" "${MY_DST_DIR}/${MY_BASENAME}"; then
     241            echo "error: Failed to move '${sdk}/' to '${MY_DST_DIR}/${MY_BASENAME}'." 1>&2;
    154242            exit 1;
    155243        fi
    156     done
    157 fi
    158 
    159 #
    160 # Work the sub-packages, extracting their payload content into the destination directory.
    161 #
    162 for i in $MY_PKGS;
    163 do
    164     rm -f -- "${MY_TMP_DIR}/x/Payload";
    165     echo "info: Extracting payload of sub-package ${i}...";
    166     if ! xar -xvf "${MY_TMP_DIR}/x/${i}" -C "${MY_TMP_DIR}/x" Payload; then
    167         echo "error: Failed to extract the payload of sub-package ${i}." 1>&2;
    168         exit 1;
    169     fi
    170     if ! tar xvf "${MY_TMP_DIR}/x/Payload" -C "${MY_DST_DIR}"; then
    171         echo "error: Failed to extract the payload content of sub-package ${i}." 1>&2;
    172         exit 1;
    173244    fi
    174245done
     
    178249#
    179250echo "info: Successfully extracted. Cleaning up temporary files..."
    180 rm -Rf -- "${MY_TMP_DIR}/x/"
     251if [ -d "${MY_TMP_DIR_DMG}" ]; then
     252    hdiutil detach -force "${MY_TMP_DIR_DMG}";
     253fi
     254rm -Rf -- "${MY_TMP_DIR_X}/" "${MY_TMP_DIR_Y}/"
     255rmdir -- "${MY_TMP_DIR_DMG}"
    181256rmdir -- "${MY_TMP_DIR}"
    182257
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette