-
Notifications
You must be signed in to change notification settings - Fork 207
/
create_patched_sdk.sh
executable file
·231 lines (193 loc) · 7.81 KB
/
create_patched_sdk.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
#!/usr/bin/env bash
print_usage() {
cat << EOF
Usage: $(basename $0) {use_simulator} {sdks_output_path} {no_overwrite} {no_warnings} {tbd_tool} {xcode_installation_path} {sdk_platform}
Options:
- use_simulator (bool)
Dump binaries from simulator runtime as opposed to DeviceSupport files
- sdks_output_path (str)
Directory to install SDK to (default: '\$THEOS/sdks')
- no_overwrite (bool)
Disable overwrite of existing dumped binaries (default: 0)
- no_warnings (bool)
Disable 'tbd' warnings (default: 0)
- tbd_tool (str)
Path to 'tbd' tool from inoahdev (default: 'tbd')
- xcode_installation_path (str)
Path to target Xcode version (default: currently selected Xcode.app)
- sdk_platform (str)
Target platform for patched SDK (default: 'iOS'; alternatively 'tvOS')
Note: {} options are optional to provide, and can be ignored with a '-'
EOF
}
ignored() {
if [[ $1 == "-" ]]; then
return 0
else
return 1
fi
}
if [[ $# -lt 1 ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
print_usage
exit 0
fi
sdk_platform="$7"
if [[ $# -lt 7 ]] || ignored $sdk_platform; then
echo "No sdk_platform provided. Defaulting to iOS."
sdk_platform="iOS"
elif [[ $(echo $sdk_platform | tr '[A-Z]' '[a-z]') == ios ]]; then
sdk_platform="iOS"
elif [[ $(echo $sdk_platform | tr '[A-Z]' '[a-z]') == tvos ]]; then
sdk_platform="tvOS"
else
echo "Unsupported sdk_platform provided. Please choose either 'iOS' or 'tvOS'."
exit 1
fi
sdks_output_path="$2"
if [[ $# -lt 2 ]] || ignored $sdks_output_path; then
if [[ -z $THEOS ]]; then
printf 'No Theos installation found. Please either install Theos or provide a path to an sdks directory\n\n'
print_usage
exit 1
fi
sdks_output_path="$THEOS/sdks"
fi
if ! [[ -d $sdks_output_path ]]; then
mkdir -p "$sdks_output_path"
fi
# tbd info
tbd_version="v3"
if [[ $sdk_platform == iOS ]]; then
archs_option=("--replace-archs" armv7 armv7s arm64 arm64e)
else
archs_option=("--replace-archs" arm64 arm64e)
fi
tbd_options=("--ignore-clients" "--ignore-undefineds" "--allow-private-objc-symbols" "--ignore-missing-exports")
write_options=("--preserve-subdirs" "--replace-path-extension")
no_overwrite="--no-overwrite"
if [[ $# -gt 2 ]] && ! ignored $3; then
no_overwrite=""
fi
no_warnings=""
tbd_tool="$5"
if [[ $# -gt 3 ]] && ! ignored $4; then
no_warnings="--ignore-warnings"
fi
if [[ $# -lt 5 ]] || ignored $tbd_tool; then
tbd_tool="tbd"
if ! [[ -x $(which $tbd_tool) ]]; then
printf 'No installation of tbd found. Please install the latest release of tbd from here; https://github.com/inoahdev/tbd/releases or provide a path to a tbd installation\n\n'
print_usage
exit 1
fi
else
if ! [[ -x $(which $tbd_tool) ]]; then
echo "Provided tbd-tool ($tbd_tool) doesn't exist or isn't executable"
exit 1
fi
fi
use_simulator="$1"
if [[ $# -lt 1 ]]; then
use_simulator="-"
fi
xcode_developer_path="$6/Contents/Developer"
if [[ $# -lt 6 ]] || [[ -z $6 ]] || ignored $6; then
xcode_developer_path=$(xcode-select -p)
fi
if [[ -z $xcode_developer_path ]]; then
printf 'No Xcode installation found. Please either install Xcode or provide a path to an Xcode installation\n\n'
print_usage
exit 1
fi
if [[ $sdk_platform == iOS ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot"
if ! [[ -d $xcode_sim_runtime_path ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot"
fi
xcode_plat_sdks_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/SDKs"
xcode_default_sdk_path="$xcode_plat_sdks_path/iPhoneOS.sdk"
else
xcode_sim_runtime_path="$xcode_developer_path/Platforms/AppleTVOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/tvOS.simruntime/Contents/Resources/RuntimeRoot"
if ! [[ -d $xcode_sim_runtime_path ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/AppleTVOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/tvOS.simruntime/Contents/Resources/RuntimeRoot"
fi
xcode_plat_sdks_path="$xcode_developer_path/Platforms/AppleTVOS.platform/Developer/SDKs"
xcode_default_sdk_path="$xcode_plat_sdks_path/AppleTVOS.sdk"
fi
preferred_xcode_sdk_path=""
for xcode_sdk_path in "$xcode_plat_sdks_path/"*; do
xcode_sdk_real=$(readlink -f "$xcode_sdk_path")
if [[ $xcode_sdk_real == $xcode_default_sdk_path ]]; then
preferred_xcode_sdk_path=$xcode_sdk_path
fi
done
if [[ -z $preferred_xcode_sdk_path ]]; then
echo 'Failed to find sdk for simulator runtime'
exit 1
fi
preferred_xcode_sdk_name=$(basename $preferred_xcode_sdk_path)
if [[ $sdk_platform == iOS ]]; then
xcode_sdk_version=${preferred_xcode_sdk_name:8} # Remove 'iPhoneOS' in front of sdk name
else
xcode_sdk_version=${preferred_xcode_sdk_name:9} # Remove 'AppleTVOS' in front of sdk name
fi
xcode_sdk_version=${xcode_sdk_version%????} # Remove '.sdk' at back of sdk name
sdks_output_path_single_sdk_path=""
device_support_dir="$HOME/Library/Developer/Xcode/$sdk_platform DeviceSupport/"
if [[ -d $device_support_dir ]] && ignored $use_simulator; then
for symbols_path in "$device_support_dir"*; do
if ! [[ -d $symbols_path ]]; then
continue
fi
os_version="$(basename "$symbols_path" | grep -o "\d\+\(\.\d\+\)\{1,2\}")"
if [[ $sdk_platform == ios ]]; then
sdk_name=$(printf "iPhoneOS%s.sdk" "$os_version")
else
sdk_name=$(printf "AppleTVOS%s.sdk" "$os_version")
fi
symbols_actual_path="$symbols_path/Symbols/System"
if ! [[ -d $symbols_actual_path ]]; then
echo "Symbols for $sdk_platform $os_version don't exist"
continue
fi
sdks_output_path_single_sdk_path="$sdks_output_path/$sdk_name"
if [[ -d $sdks_output_path_single_sdk_path ]]; then
echo "SDK for $sdk_platform $os_version already exists"
continue
fi
echo "Creating SDK for $sdk_platform $os_version ..."
if [[ $xcode_sdk_version != $os_version ]]; then
echo "Warning: $sdk_platform $xcode_sdk_version Xcode SDK will be used as a base for sdk for $sdk_platform $os_version"
fi
mkdir -p "$sdks_output_path_single_sdk_path"
cp -R "$xcode_default_sdk_path/"* "$sdks_output_path_single_sdk_path"
"$tbd_tool" \
-p $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" -r all "$symbols_actual_path" \
-o "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/System"
if [[ $? -ne 0 ]]; then
echo "Failed to create tbds from Symbols directory for $sdk_platform $os_version"
fi
done
else
if ignored $use_simulator; then
echo 'No DeviceSupport binaries found, falling back to dumping from simulator runtime binaries'
fi
sdks_output_path_single_sdk_path="$sdks_output_path/$preferred_xcode_sdk_name"
if [[ -d $sdks_output_path_single_sdk_path ]]; then
echo "SDK for $sdk_platform $xcode_sdk_version already exists"
exit 1
fi
echo "Creating sdk for $sdk_platform $xcode_sdk_version ..."
mkdir -p "$sdks_output_path_single_sdk_path"
cp -R "$xcode_default_sdk_path/"* "$sdks_output_path_single_sdk_path"
parse_paths=("-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" "-r" "all" "$xcode_sim_runtime_path/Developer"
"-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" "-r" "all" "$xcode_sim_runtime_path/System"
"-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" "-r" "all" "$xcode_sim_runtime_path/Library")
write_paths=("-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/Developer"
"-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/System"
"-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/Library")
"$tbd_tool" "${parse_paths[@]}" "${write_paths[@]}"
if [[ $? -ne 0 ]]; then
echo "Failed to create tbds from simulator runtime for $sdk_platform $xcode_sdk_version"
fi
fi