-
Notifications
You must be signed in to change notification settings - Fork 321
/
gen_sdk_package.sh
executable file
·257 lines (202 loc) · 5.32 KB
/
gen_sdk_package.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env bash
#
# Package the macOS SDKs into a tar file to be used by `build.sh`.
#
export LC_ALL=C
command -v gnutar &>/dev/null
if [ $? -eq 0 ]; then
TAR=gnutar
else
TAR=tar
fi
if [ -z "$SDK_COMPRESSOR" ]; then
command -v xz &>/dev/null
if [ $? -eq 0 ]; then
SDK_COMPRESSOR=xz
SDK_EXT="tar.xz"
else
SDK_COMPRESSOR=bzip2
SDK_EXT="tar.bz2"
fi
fi
case $SDK_COMPRESSOR in
"gz")
SDK_COMPRESSOR=gzip
SDK_EXT=".tar.gz"
;;
"bzip2")
SDK_EXT=".tar.bz2"
;;
"xz")
SDK_EXT=".tar.xz"
;;
"zip")
SDK_EXT=".zip"
;;
*)
echo "error: unknown compressor \"$SDK_COMPRESSOR\"" >&2
exit 1
esac
function compress()
{
case $SDK_COMPRESSOR in
"zip")
$SDK_COMPRESSOR -q -5 -r - $1 > $2 ;;
*)
tar cf - $1 | $SDK_COMPRESSOR -5 - > $2 ;;
esac
}
function rreadlink()
{
if [ ! -h "$1" ]; then
echo "$1"
else
local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"
cd $(dirname $1)
rreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"
fi
}
WDIR=$(pwd)
if [ -z "$XCODE_TOOLS" ]; then
# Using full fledged Xcode
function set_xcode_dir()
{
local tmp=$(ls $1 2>/dev/null | grep "^Xcode.*.app" | grep -v "beta" | head -n1)
if [ -z "$tmp" ]; then
tmp=$(ls $1 2>/dev/null | grep "^Xcode.*.app" | head -n1)
fi
if [ -n "$tmp" ]; then
XCODEDIR="$1/$tmp"
fi
}
if [ $(uname -s) != "Darwin" ]; then
if [ -z "$XCODEDIR" ]; then
echo "This script must be run on macOS" 1>&2
echo "... Or with XCODEDIR=... on Linux" 1>&2
exit 1
else
case "$XCODEDIR" in
/*) ;;
*) XCODEDIR="$PWD/$XCODEDIR" ;;
esac
set_xcode_dir "$XCODEDIR"
fi
else
set_xcode_dir $(echo /Volumes/Xcode* | tr ' ' '\n' | grep -v "beta" | head -n1)
if [ -z "$XCODEDIR" ]; then
set_xcode_dir /Applications
if [ -z "$XCODEDIR" ]; then
set_xcode_dir $(echo /Volumes/Xcode* | tr ' ' '\n' | head -n1)
if [ -z "$XCODEDIR" ]; then
echo "please mount Xcode.dmg" 1>&2
exit 1
fi
fi
fi
fi
if [ ! -d "$XCODEDIR" ]; then
echo "cannot find Xcode (XCODEDIR=$XCODEDIR)" 1>&2
exit 1
fi
echo -e "found Xcode: $XCODEDIR"
set -e
pushd "$XCODEDIR" &>/dev/null
if [ -d "Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs" ]; then
pushd "Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs" &>/dev/null
else
if [ -d "../Packages" ]; then
pushd "../Packages" &>/dev/null
elif [ -d "Packages" ]; then
pushd "Packages" &>/dev/null
else
if [ $? -ne 0 ]; then
echo "Xcode (or this script) is out of date" 1>&2
echo "trying some magic to find the SDKs anyway ..." 1>&2
SDKDIR=$(find . -name SDKs -type d | grep MacOSX | head -n1)
if [ -z "$SDKDIR" ]; then
echo "cannot find SDKs!" 1>&2
exit 1
fi
pushd "$SDKDIR" &>/dev/null
fi
fi
fi
else
# Using Xcode Command Line tools
if [ $(uname -s) != "Darwin" ]; then
if [ -z "$XCODE_TOOLS_DIR" ]; then
echo "This script must be run on macOS" 1>&2
echo "... Or with XCODE_TOOLS_DIR=... on Linux" 1>&2
exit 1
else
case "$XCODE_TOOLS_DIR" in
/*) ;;
*) XCODE_TOOLS_DIR="$PWD/$XCODE_TOOLS_DIR" ;;
esac
fi
else
XCODE_TOOLS_DIR="/Library/Developer/CommandLineTools"
fi
if [ ! -d "$XCODE_TOOLS_DIR" ]; then
echo "cannot find Xcode Command Line Tools (XCODE_TOOLS_DIR=$XCODE_TOOLS_DIR)" 1>&2
exit 1
fi
echo -e "found Xcode Command Line Tools: $XCODE_TOOLS_DIR"
pushd "$XCODE_TOOLS_DIR" &>/dev/null
if [ -d "SDKs" ]; then
pushd "SDKs" &>/dev/null
else
echo "$XCODE_TOOLS_DIR/SDKs does not exist" 1>&2
exit 1
fi
XCODEDIR=$XCODE_TOOLS_DIR
set -e
fi
SDKS=$(ls | grep -E "^MacOSX15.*|^MacOSX14.*|^MacOSX13.*|^MacOSX12.*|^MacOSX11.*|^MacOSX10.*" | grep -v "Patch")
if [ -z "$SDKS" ]; then
echo "No SDK found" 1>&2
exit 1
fi
# Xcode 5
LIBCXXDIR1="Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1"
# Xcode 6
LIBCXXDIR2="Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1"
# Xcode Command Line Tools
LIBCXXDIR3="usr/include/c++/v1"
# Manual directory
MANDIR="Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man"
for SDK in $SDKS; do
echo -n "packaging $(echo "$SDK" | sed -E "s/(.sdk|.pkg)//g") SDK "
echo "(this may take several minutes) ..."
if [[ $SDK == *.pkg ]]; then
cp $SDK $WDIR
continue
fi
TMP=$(mktemp -d /tmp/XXXXXXXXXXX)
cp -r $(rreadlink $SDK) $TMP/$SDK &>/dev/null || true
pushd "$XCODEDIR" &>/dev/null
mkdir -p $TMP/$SDK/usr/include/c++
# libc++ headers
if [ ! -f "$TMP/$SDK/usr/include/c++/v1/version" ]; then
if [ -d $LIBCXXDIR1 ]; then
cp -rf $LIBCXXDIR1 "$TMP/$SDK/usr/include/c++"
elif [ -d $LIBCXXDIR2 ]; then
cp -rf $LIBCXXDIR2 "$TMP/$SDK/usr/include/c++"
elif [ -d $LIBCXXDIR3 ]; then
cp -rf $LIBCXXDIR3 "$TMP/$SDK/usr/include/c++"
fi
fi
if [ -d $MANDIR ]; then
mkdir -p $TMP/$SDK/usr/share/man
cp -rf $MANDIR/* $TMP/$SDK/usr/share/man
fi
popd &>/dev/null
pushd $TMP &>/dev/null
compress "*" "$WDIR/$SDK$SDK_EXT"
popd &>/dev/null
rm -rf $TMP
done
popd &>/dev/null
popd &>/dev/null
echo ""
ls -lh | grep MacOSX