1 | #!/bin/bash
|
---|
2 | # $Id: sdk-extractor.sh 96416 2022-08-22 20:36:29Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # Extracts the SDKs from a commandline tools DMG.
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2014-2022 Oracle and/or its affiliates.
|
---|
9 | #
|
---|
10 | # This file is part of VirtualBox base platform packages, as
|
---|
11 | # available from https://www.virtualbox.org.
|
---|
12 | #
|
---|
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 | #
|
---|
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 | #
|
---|
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
|
---|
54 |
|
---|
55 | #
|
---|
56 | # Parse arguments.
|
---|
57 | #
|
---|
58 | MY_TMP_DIR=/var/tmp/sdkextractor
|
---|
59 | MY_DST_DIR="${MY_DARWIN_DIR}/sdk/incoming"
|
---|
60 | MY_DMG_FILE=
|
---|
61 | MY_PKG_FILE=
|
---|
62 | MY_DRY_RUN=
|
---|
63 |
|
---|
64 | my_usage()
|
---|
65 | {
|
---|
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."
|
---|
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 | ;;
|
---|
87 |
|
---|
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
|
---|
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
|
---|
116 | shift;
|
---|
117 | ;;
|
---|
118 |
|
---|
119 | --dry-run)
|
---|
120 | MY_DRY_RUN=1;
|
---|
121 | ;;
|
---|
122 |
|
---|
123 | --h*|-h*|-?|--?)
|
---|
124 | my_usage 0;
|
---|
125 | esac
|
---|
126 | done
|
---|
127 |
|
---|
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
|
---|
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
|
---|
139 | if ! mkdir -p "${MY_DST_DIR}"; then
|
---|
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
|
---|
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;
|
---|
152 | exit 1;
|
---|
153 | fi
|
---|
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
|
---|
159 |
|
---|
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;
|
---|
168 | exit 1;
|
---|
169 | fi
|
---|
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;
|
---|
175 | exit 1;
|
---|
176 | fi
|
---|
177 |
|
---|
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;
|
---|
180 | exit 1;
|
---|
181 | fi
|
---|
182 | for x in "${MY_TMP_DIR_DMG}/"*.pkg;
|
---|
183 | do
|
---|
184 | if [ -e "${x}" ]; then
|
---|
185 | MY_PKG_FILE=$x;
|
---|
186 | fi
|
---|
187 | done
|
---|
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}";
|
---|
193 | fi
|
---|
194 |
|
---|
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 |
|
---|
202 | #
|
---|
203 | # Find the SDK packages and extract them to the 'x' directory.
|
---|
204 | #
|
---|
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};
|
---|
226 | do
|
---|
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;
|
---|
230 | exit 1;
|
---|
231 | fi
|
---|
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;
|
---|
237 | exit 1;
|
---|
238 | fi
|
---|
239 | cd "${MY_CWD}"
|
---|
240 | done
|
---|
241 |
|
---|
242 | #
|
---|
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 | #
|
---|
258 | # Clean up.
|
---|
259 | #
|
---|
260 | echo "info: Successfully extracted. Cleaning up temporary files..."
|
---|
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}"
|
---|
266 | rmdir -- "${MY_TMP_DIR}"
|
---|
267 |
|
---|