[53376] | 1 | #!/bin/bash
|
---|
| 2 | # $Id: sdk-extractor.sh 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
| 3 | ## @file
|
---|
[85060] | 4 | # Extracts the SDKs from a commandline tools DMG.
|
---|
| 5 | #
|
---|
[53376] | 6 |
|
---|
| 7 | #
|
---|
[98103] | 8 | # Copyright (C) 2014-2023 Oracle and/or its affiliates.
|
---|
[53376] | 9 | #
|
---|
[96416] | 10 | # This file is part of VirtualBox base platform packages, as
|
---|
| 11 | # available from https://www.virtualbox.org.
|
---|
[53376] | 12 | #
|
---|
[96416] | 13 | # This program is free software; you can redistribute it and/or
|
---|
| 14 | # modify it under the terms of the GNU General Public License
|
---|
| 15 | # as published by the Free Software Foundation, in version 3 of the
|
---|
| 16 | # License.
|
---|
| 17 | #
|
---|
| 18 | # This program is distributed in the hope that it will be useful, but
|
---|
| 19 | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 21 | # General Public License for more details.
|
---|
| 22 | #
|
---|
| 23 | # You should have received a copy of the GNU General Public License
|
---|
| 24 | # along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 25 | #
|
---|
| 26 | # SPDX-License-Identifier: GPL-3.0-only
|
---|
| 27 | #
|
---|
[53376] | 28 |
|
---|
| 29 | #
|
---|
| 30 | # Make sure we're talking the same language.
|
---|
| 31 | #
|
---|
| 32 | LC_ALL=C
|
---|
| 33 | export LC_ALL
|
---|
| 34 |
|
---|
| 35 | #
|
---|
| 36 | # Figure the tools/darwin.x86 location.
|
---|
| 37 | #
|
---|
| 38 | MY_DARWIN_DIR=`dirname "$0"`
|
---|
| 39 | MY_DARWIN_DIR=`(cd "${MY_DARWIN_DIR}" ; pwd)`
|
---|
| 40 | MY_DARWIN_DIR=`dirname "${MY_DARWIN_DIR}"`
|
---|
| 41 |
|
---|
| 42 | #
|
---|
| 43 | # Constants.
|
---|
| 44 | #
|
---|
[85060] | 45 | #MY_PKGS="gcc4.2.pkg llvm-gcc4.2.pkg DeveloperToolsCLI.pkg xcrun.pkg JavaSDK.pkg MacOSX10.6.pkg MacOSX10.7.pkg"
|
---|
| 46 | #MY_LAST_PKG="MacOSX10.7.pkg"
|
---|
| 47 | #MY_PKGS="clang.pkg"
|
---|
| 48 | #MY_LAST_PKG="clang.pkg"
|
---|
| 49 | #declare -a MY_FULL_PKGS
|
---|
| 50 | #for i in $MY_PKGS;
|
---|
| 51 | #do
|
---|
| 52 | # MY_FULL_PKGS[$((${#MY_FULL_PKGS[*]}))]="./Applications/Install Xcode.app/Contents/Resources/Packages/${i}"
|
---|
| 53 | #done
|
---|
[53376] | 54 |
|
---|
| 55 | #
|
---|
| 56 | # Parse arguments.
|
---|
| 57 | #
|
---|
[85060] | 58 | MY_TMP_DIR=/var/tmp/sdkextractor
|
---|
| 59 | MY_DST_DIR="${MY_DARWIN_DIR}/sdk/incoming"
|
---|
| 60 | MY_DMG_FILE=
|
---|
[53376] | 61 | MY_PKG_FILE=
|
---|
[85060] | 62 | MY_DRY_RUN=
|
---|
[53376] | 63 |
|
---|
| 64 | my_usage()
|
---|
| 65 | {
|
---|
[85060] | 66 | echo "usage: $0 [--dry-run] [--tmpdir|-t <tmpdir>] <--destination|-d> <dstdir> <--filename|-f> <dir/Command_Line_Tools*.dmg|pkg>";
|
---|
| 67 | echo ""
|
---|
| 68 | echo "Works for command lines tools for Xcode 8.2 and later."
|
---|
| 69 | echo "For older versions, the SDK must be extraced from Xcode itself, it seems."
|
---|
[53376] | 70 | exit $1;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | while test $# -ge 1;
|
---|
| 74 | do
|
---|
| 75 | ARG=$1;
|
---|
| 76 | shift;
|
---|
| 77 | case "$ARG" in
|
---|
| 78 |
|
---|
| 79 | --tmpdir|-t)
|
---|
| 80 | if test $# -eq 0; then
|
---|
| 81 | echo "error: missing --tmpdir argument." 1>&2;
|
---|
| 82 | exit 1;
|
---|
| 83 | fi
|
---|
| 84 | MY_TMP_DIR="$1";
|
---|
| 85 | shift;
|
---|
| 86 | ;;
|
---|
[69210] | 87 |
|
---|
[53376] | 88 | --destination|-d)
|
---|
| 89 | if test $# -eq 0; then
|
---|
| 90 | echo "error: missing --tmpdir argument." 1>&2;
|
---|
| 91 | exit 1;
|
---|
| 92 | fi
|
---|
| 93 | MY_DST_DIR="$1";
|
---|
| 94 | shift;
|
---|
| 95 | ;;
|
---|
| 96 |
|
---|
| 97 | --filename|-f)
|
---|
| 98 | if test $# -eq 0; then
|
---|
| 99 | echo "error: missing --filename argument." 1>&2;
|
---|
| 100 | exit 1;
|
---|
| 101 | fi
|
---|
[85060] | 102 | case "$1" in
|
---|
| 103 | *.[dD][mM][gG])
|
---|
| 104 | MY_DMG_FILE="$1";
|
---|
| 105 | MY_PKG_FILE=;
|
---|
| 106 | ;;
|
---|
| 107 | *.[pP][kK][gG])
|
---|
| 108 | MY_PKG_FILE="$1";
|
---|
| 109 | MY_DMG_FILE=;
|
---|
| 110 | ;;
|
---|
| 111 | *)
|
---|
| 112 | echo "error: filename does not end with .dmg or .pkg." 1>&2;
|
---|
| 113 | exit 1;
|
---|
| 114 | ;;
|
---|
| 115 | esac
|
---|
[53376] | 116 | shift;
|
---|
| 117 | ;;
|
---|
| 118 |
|
---|
[85060] | 119 | --dry-run)
|
---|
| 120 | MY_DRY_RUN=1;
|
---|
| 121 | ;;
|
---|
| 122 |
|
---|
[53376] | 123 | --h*|-h*|-?|--?)
|
---|
| 124 | my_usage 0;
|
---|
| 125 | esac
|
---|
| 126 | done
|
---|
| 127 |
|
---|
[85060] | 128 | # We must have something to work with.
|
---|
| 129 | if [ -z "${MY_PKG_FILE}" -a -z "${MY_DMG_FILE}" ]; then
|
---|
| 130 | echo "error: missing --filename <dir/Command_Line_Tools*.dmg|pkg>." 1>&2l
|
---|
[53376] | 131 | my_usage 1;
|
---|
| 132 | fi
|
---|
| 133 |
|
---|
| 134 | # Check the destination directory.
|
---|
| 135 | if [ -z "${MY_DST_DIR}" ]; then
|
---|
| 136 | echo "error: missing --destination <dstdir>." 1>&2;
|
---|
| 137 | my_usage 1;
|
---|
| 138 | fi
|
---|
[69210] | 139 | if ! mkdir -p "${MY_DST_DIR}"; then
|
---|
[53376] | 140 | echo "error: error creating '${MY_DST_DIR}'." 1>&2;
|
---|
| 141 | exit 1;
|
---|
| 142 | fi
|
---|
| 143 |
|
---|
| 144 | # Check the temporary directory.
|
---|
| 145 | if [ -z "${MY_TMP_DIR}" ]; then
|
---|
| 146 | echo "error: empty --tmpdir <tmpdir>." 1>&2;
|
---|
| 147 | my_usage 1;
|
---|
| 148 | fi
|
---|
[85060] | 149 | MY_TMP_DIR_X="${MY_TMP_DIR}/x$$";
|
---|
| 150 | if ! mkdir -p "${MY_TMP_DIR_X}"; then
|
---|
| 151 | echo "error: error creating '${MY_TMP_DIR_X}'." 1>&2;
|
---|
[53376] | 152 | exit 1;
|
---|
| 153 | fi
|
---|
[85060] | 154 | MY_TMP_DIR_Y="${MY_TMP_DIR}/y$$";
|
---|
| 155 | if ! mkdir -p "${MY_TMP_DIR_Y}"; then
|
---|
| 156 | echo "error: error creating '${MY_TMP_DIR_Y}'." 1>&2;
|
---|
| 157 | exit 1;
|
---|
| 158 | fi
|
---|
[53376] | 159 |
|
---|
[85060] | 160 | # Attach the DMG if one is given, then find the PKG file inside it.
|
---|
| 161 | MY_TMP_DIR_DMG="${MY_TMP_DIR}/dmg";
|
---|
| 162 | rmdir -- "${MY_TMP_DIR_DMG}" 2>/dev/null;
|
---|
| 163 | if [ -d "${MY_TMP_DIR_DMG}" ]; then
|
---|
| 164 | echo "info: Unmount '${MY_TMP_DIR_DMG}'...";
|
---|
| 165 | hdiutil detach -force "${MY_TMP_DIR_DMG}";
|
---|
| 166 | if ! rmdir -- "${MY_TMP_DIR_DMG}"; then
|
---|
| 167 | echo "error: failed to detach DMG from previous run (mountpoint='${MY_TMP_DIR_DMG}')." 1>&2;
|
---|
[53376] | 168 | exit 1;
|
---|
| 169 | fi
|
---|
[85060] | 170 | fi
|
---|
| 171 | if [ -n "${MY_DMG_FILE}" ]; then
|
---|
| 172 | echo "info: Mounting '${MY_DMG_FILE}' at '${MY_TMP_DIR_DMG}'...";
|
---|
| 173 | if ! mkdir -p -- "${MY_TMP_DIR_DMG}"; then
|
---|
| 174 | echo "error: error creating '${MY_TMP_DIR_DMG}'." 1>&2;
|
---|
[53376] | 175 | exit 1;
|
---|
| 176 | fi
|
---|
| 177 |
|
---|
[85060] | 178 | if ! hdiutil attach -mountpoint "${MY_TMP_DIR_DMG}" -noautoopen -nobrowse -noverify "${MY_DMG_FILE}"; then
|
---|
| 179 | echo "error: hdiutil attach failed for '${MY_DMG_FILE}'" 1>&2;
|
---|
[53376] | 180 | exit 1;
|
---|
| 181 | fi
|
---|
[85060] | 182 | for x in "${MY_TMP_DIR_DMG}/"*.pkg;
|
---|
[53376] | 183 | do
|
---|
[85060] | 184 | if [ -e "${x}" ]; then
|
---|
| 185 | MY_PKG_FILE=$x;
|
---|
[53376] | 186 | fi
|
---|
[69210] | 187 | done
|
---|
[85060] | 188 | if [ -z "${MY_PKG_FILE}" ]; then
|
---|
| 189 | echo "error: Found no .pkg file inside the DMG attached at '${MY_TMP_DIR_DMG}'." 1>&2;
|
---|
| 190 | exit 1;
|
---|
| 191 | fi
|
---|
| 192 | echo "info: MY_PKG_FILE=${MY_PKG_FILE}";
|
---|
[53376] | 193 | fi
|
---|
| 194 |
|
---|
[85060] | 195 | # Check the package file.
|
---|
| 196 | echo "info: Checking '${MY_PKG_FILE}'...";
|
---|
| 197 | if ! xar -tf "${MY_PKG_FILE}" > /dev/null ; then
|
---|
| 198 | echo "error: xar has trouble with '${MY_PKG_FILE}'." 1>&2;
|
---|
| 199 | exit 1;
|
---|
| 200 | fi
|
---|
| 201 |
|
---|
[53376] | 202 | #
|
---|
[85060] | 203 | # Find the SDK packages and extract them to the 'x' directory.
|
---|
[53376] | 204 | #
|
---|
[85060] | 205 | # Command_Line_Tools_macOS_10.11_for_Xcode_8.2.dmg contains two packages with SDK
|
---|
| 206 | # in the name: CLTools_SDK_OSX1012.pkg and DevSDK_OSX1011.pkg. The former ends up
|
---|
| 207 | # in /Library/Developer/ and the latter in root (/). So, only pick the first one.
|
---|
| 208 | #
|
---|
| 209 | MY_SDK_PKGS=$(xar -tf "${MY_PKG_FILE}" | grep "SDK.*\.pkg$" | grep -v DevSDK_ | tr '\n\r' ' ')
|
---|
| 210 | if [ -z "${MY_SDK_PKGS}" ]; then
|
---|
| 211 | echo "error: Found no SDK packages in '${MY_PKG_FILE}'." 1>&2;
|
---|
| 212 | xar -tf "${MY_PKG_FILE}" 1>&2
|
---|
| 213 | exit 1;
|
---|
| 214 | fi
|
---|
| 215 | echo "info: Extracking SDK packages: ${MY_SDK_PKGS}"
|
---|
| 216 |
|
---|
| 217 | if ! xar -xf "${MY_PKG_FILE}" -C "${MY_TMP_DIR_X}" ${MY_SDK_PKGS}; then
|
---|
| 218 | echo "error: Failed to extract ${MY_SDK_PKGS} from '${MY_PKG_FILE}'." 1>&2;
|
---|
| 219 | exit 1;
|
---|
| 220 | fi
|
---|
| 221 |
|
---|
| 222 | #
|
---|
| 223 | # Expand the unpacked packages into the Y directory.
|
---|
| 224 | #
|
---|
| 225 | for pkg in ${MY_SDK_PKGS};
|
---|
[53376] | 226 | do
|
---|
[85060] | 227 | echo "info: Expanding '${MY_TMP_DIR_X}/${pkg}' using pbzx and cpio...";
|
---|
| 228 | if ! pbzx "${MY_TMP_DIR_X}/${pkg}/Payload" > "${MY_TMP_DIR_X}/${pkg}/Payload.unpacked" ; then
|
---|
| 229 | echo "error: Failed to unpack '${MY_TMP_DIR_X}/${pkg}/Payload' using pbzx." 1>&2;
|
---|
[53376] | 230 | exit 1;
|
---|
| 231 | fi
|
---|
[85060] | 232 |
|
---|
| 233 | MY_CWD=`pwd`
|
---|
| 234 | cd "${MY_TMP_DIR_Y}" || exit 1;
|
---|
| 235 | if ! cpio -i < "${MY_TMP_DIR_X}/${pkg}/Payload.unpacked"; then
|
---|
| 236 | echo "error: Failed to expand '${MY_TMP_DIR_X}/${pkg}/Payload.unpacked' using cpio." 1>&2;
|
---|
[53376] | 237 | exit 1;
|
---|
| 238 | fi
|
---|
[85060] | 239 | cd "${MY_CWD}"
|
---|
[69210] | 240 | done
|
---|
[53376] | 241 |
|
---|
| 242 | #
|
---|
[85060] | 243 | # Now, pick the bits we want from the Y directory and move it over to the destination directory.
|
---|
| 244 | #
|
---|
| 245 | for sdk in "${MY_TMP_DIR_Y}/Library/Developer/CommandLineTools/SDKs/MacOSX10"*.sdk;
|
---|
| 246 | do
|
---|
| 247 | MY_BASENAME=`basename "${sdk}"`
|
---|
| 248 | echo "info: Moving '${sdk}/' to '${MY_DST_DIR}/${MY_BASENAME}'...";
|
---|
| 249 | if [ -z "${MY_DRY_RUN}" ]; then
|
---|
| 250 | if ! mv "${sdk}/" "${MY_DST_DIR}/${MY_BASENAME}"; then
|
---|
| 251 | echo "error: Failed to move '${sdk}/' to '${MY_DST_DIR}/${MY_BASENAME}'." 1>&2;
|
---|
| 252 | exit 1;
|
---|
| 253 | fi
|
---|
| 254 | fi
|
---|
| 255 | done
|
---|
| 256 |
|
---|
| 257 | #
|
---|
[53376] | 258 | # Clean up.
|
---|
| 259 | #
|
---|
| 260 | echo "info: Successfully extracted. Cleaning up temporary files..."
|
---|
[85060] | 261 | if [ -d "${MY_TMP_DIR_DMG}" ]; then
|
---|
| 262 | hdiutil detach -force "${MY_TMP_DIR_DMG}";
|
---|
| 263 | fi
|
---|
| 264 | rm -Rf -- "${MY_TMP_DIR_X}/" "${MY_TMP_DIR_Y}/"
|
---|
| 265 | rmdir -- "${MY_TMP_DIR_DMG}"
|
---|
[53376] | 266 | rmdir -- "${MY_TMP_DIR}"
|
---|
| 267 |
|
---|