1 | #!/bin/bash
|
---|
2 | # $Id: sdk-extractor.sh 85060 2020-07-05 21:25:42Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # Extracts the SDKs from a commandline tools DMG.
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2014-2020 Oracle Corporation
|
---|
9 | #
|
---|
10 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | # available from http://www.virtualbox.org. This file is free software;
|
---|
12 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | # General Public License (GPL) as published by the Free Software
|
---|
14 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | #
|
---|
18 |
|
---|
19 | #
|
---|
20 | # Make sure we're talking the same language.
|
---|
21 | #
|
---|
22 | LC_ALL=C
|
---|
23 | export LC_ALL
|
---|
24 |
|
---|
25 | #
|
---|
26 | # Figure the tools/darwin.x86 location.
|
---|
27 | #
|
---|
28 | MY_DARWIN_DIR=`dirname "$0"`
|
---|
29 | MY_DARWIN_DIR=`(cd "${MY_DARWIN_DIR}" ; pwd)`
|
---|
30 | MY_DARWIN_DIR=`dirname "${MY_DARWIN_DIR}"`
|
---|
31 |
|
---|
32 | #
|
---|
33 | # Constants.
|
---|
34 | #
|
---|
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
|
---|
44 |
|
---|
45 | #
|
---|
46 | # Parse arguments.
|
---|
47 | #
|
---|
48 | MY_TMP_DIR=/var/tmp/sdkextractor
|
---|
49 | MY_DST_DIR="${MY_DARWIN_DIR}/sdk/incoming"
|
---|
50 | MY_DMG_FILE=
|
---|
51 | MY_PKG_FILE=
|
---|
52 | MY_DRY_RUN=
|
---|
53 |
|
---|
54 | my_usage()
|
---|
55 | {
|
---|
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."
|
---|
60 | exit $1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | while test $# -ge 1;
|
---|
64 | do
|
---|
65 | ARG=$1;
|
---|
66 | shift;
|
---|
67 | case "$ARG" in
|
---|
68 |
|
---|
69 | --tmpdir|-t)
|
---|
70 | if test $# -eq 0; then
|
---|
71 | echo "error: missing --tmpdir argument." 1>&2;
|
---|
72 | exit 1;
|
---|
73 | fi
|
---|
74 | MY_TMP_DIR="$1";
|
---|
75 | shift;
|
---|
76 | ;;
|
---|
77 |
|
---|
78 | --destination|-d)
|
---|
79 | if test $# -eq 0; then
|
---|
80 | echo "error: missing --tmpdir argument." 1>&2;
|
---|
81 | exit 1;
|
---|
82 | fi
|
---|
83 | MY_DST_DIR="$1";
|
---|
84 | shift;
|
---|
85 | ;;
|
---|
86 |
|
---|
87 | --filename|-f)
|
---|
88 | if test $# -eq 0; then
|
---|
89 | echo "error: missing --filename argument." 1>&2;
|
---|
90 | exit 1;
|
---|
91 | fi
|
---|
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
|
---|
106 | shift;
|
---|
107 | ;;
|
---|
108 |
|
---|
109 | --dry-run)
|
---|
110 | MY_DRY_RUN=1;
|
---|
111 | ;;
|
---|
112 |
|
---|
113 | --h*|-h*|-?|--?)
|
---|
114 | my_usage 0;
|
---|
115 | esac
|
---|
116 | done
|
---|
117 |
|
---|
118 | # We must have something to work with.
|
---|
119 | if [ -z "${MY_PKG_FILE}" -a -z "${MY_DMG_FILE}" ]; then
|
---|
120 | echo "error: missing --filename <dir/Command_Line_Tools*.dmg|pkg>." 1>&2l
|
---|
121 | my_usage 1;
|
---|
122 | fi
|
---|
123 |
|
---|
124 | # Check the destination directory.
|
---|
125 | if [ -z "${MY_DST_DIR}" ]; then
|
---|
126 | echo "error: missing --destination <dstdir>." 1>&2;
|
---|
127 | my_usage 1;
|
---|
128 | fi
|
---|
129 | if ! mkdir -p "${MY_DST_DIR}"; then
|
---|
130 | echo "error: error creating '${MY_DST_DIR}'." 1>&2;
|
---|
131 | exit 1;
|
---|
132 | fi
|
---|
133 |
|
---|
134 | # Check the temporary directory.
|
---|
135 | if [ -z "${MY_TMP_DIR}" ]; then
|
---|
136 | echo "error: empty --tmpdir <tmpdir>." 1>&2;
|
---|
137 | my_usage 1;
|
---|
138 | fi
|
---|
139 | MY_TMP_DIR_X="${MY_TMP_DIR}/x$$";
|
---|
140 | if ! mkdir -p "${MY_TMP_DIR_X}"; then
|
---|
141 | echo "error: error creating '${MY_TMP_DIR_X}'." 1>&2;
|
---|
142 | exit 1;
|
---|
143 | fi
|
---|
144 | MY_TMP_DIR_Y="${MY_TMP_DIR}/y$$";
|
---|
145 | if ! mkdir -p "${MY_TMP_DIR_Y}"; then
|
---|
146 | echo "error: error creating '${MY_TMP_DIR_Y}'." 1>&2;
|
---|
147 | exit 1;
|
---|
148 | fi
|
---|
149 |
|
---|
150 | # Attach the DMG if one is given, then find the PKG file inside it.
|
---|
151 | MY_TMP_DIR_DMG="${MY_TMP_DIR}/dmg";
|
---|
152 | rmdir -- "${MY_TMP_DIR_DMG}" 2>/dev/null;
|
---|
153 | if [ -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
|
---|
160 | fi
|
---|
161 | if [ -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;
|
---|
173 | do
|
---|
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}";
|
---|
183 | fi
|
---|
184 |
|
---|
185 | # Check the package file.
|
---|
186 | echo "info: Checking '${MY_PKG_FILE}'...";
|
---|
187 | if ! xar -tf "${MY_PKG_FILE}" > /dev/null ; then
|
---|
188 | echo "error: xar has trouble with '${MY_PKG_FILE}'." 1>&2;
|
---|
189 | exit 1;
|
---|
190 | fi
|
---|
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 | #
|
---|
199 | MY_SDK_PKGS=$(xar -tf "${MY_PKG_FILE}" | grep "SDK.*\.pkg$" | grep -v DevSDK_ | tr '\n\r' ' ')
|
---|
200 | if [ -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;
|
---|
204 | fi
|
---|
205 | echo "info: Extracking SDK packages: ${MY_SDK_PKGS}"
|
---|
206 |
|
---|
207 | if ! 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;
|
---|
210 | fi
|
---|
211 |
|
---|
212 | #
|
---|
213 | # Expand the unpacked packages into the Y directory.
|
---|
214 | #
|
---|
215 | for pkg in ${MY_SDK_PKGS};
|
---|
216 | do
|
---|
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}"
|
---|
230 | done
|
---|
231 |
|
---|
232 | #
|
---|
233 | # Now, pick the bits we want from the Y directory and move it over to the destination directory.
|
---|
234 | #
|
---|
235 | for sdk in "${MY_TMP_DIR_Y}/Library/Developer/CommandLineTools/SDKs/MacOSX10"*.sdk;
|
---|
236 | do
|
---|
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;
|
---|
242 | exit 1;
|
---|
243 | fi
|
---|
244 | fi
|
---|
245 | done
|
---|
246 |
|
---|
247 | #
|
---|
248 | # Clean up.
|
---|
249 | #
|
---|
250 | echo "info: Successfully extracted. Cleaning up temporary files..."
|
---|
251 | if [ -d "${MY_TMP_DIR_DMG}" ]; then
|
---|
252 | hdiutil detach -force "${MY_TMP_DIR_DMG}";
|
---|
253 | fi
|
---|
254 | rm -Rf -- "${MY_TMP_DIR_X}/" "${MY_TMP_DIR_Y}/"
|
---|
255 | rmdir -- "${MY_TMP_DIR_DMG}"
|
---|
256 | rmdir -- "${MY_TMP_DIR}"
|
---|
257 |
|
---|