-
Notifications
You must be signed in to change notification settings - Fork 166
/
cycc
executable file
·501 lines (413 loc) · 11.6 KB
/
cycc
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env bash
c=(
$'\e[0m' # 0: neutral
$'\e[0;30m' # 1: black
$'\e[1;30m' # 2: dark gray
$'\e[0;31m' # 3: red
$'\e[1;31m' # 4: light red
$'\e[0;32m' # 5: green
$'\e[1;32m' # 6: light green
$'\e[0;33m' # 7: brown
$'\e[1;33m' # 8: yellow
$'\e[0;34m' # 9: blue
$'\e[1;34m' # 10: light blue
$'\e[0;35m' # 11: purple
$'\e[1;35m' # 12: light purple
$'\e[0;36m' # 13: cyan
$'\e[1;36m' # 14: light cyan
$'\e[0;37m' # 15: light gray
$'\e[1;37m' # 16: white
)
shopt -s nullglob
shopt -s extglob
# XXX: detect or something?
# XXX: gcc?
otool=otool
lipo=lipo
ldid=ldid
function has() {
value=$1
shift 1
while [[ $# != 0 ]]; do
if [[ $1 == ${value} ]]; then
return 0
fi
shift 1
done
return 1
}
function fatal() {
echo "$(basename "$0"): $1" 1>&2
exit 1
}
function usage() {
echo "Usage: $(basename "$0") [<option | code>*] [-- <flag>*]"
echo " option <- configuration for cycc itself"
echo " -i#.#: build for iOS version #.#"
echo " -m#.#: build for Mac OS X version #.#"
echo " -oXXX: use output filename XXX"
echo " -r#.#: build with gcc version #.#"
echo " -s : compile substrate extension"
echo " -q : don't print extra garbage"
echo " -v : also dump pre-processed code"
echo " code <- set of source files to preprocess"
echo " flag <- pass-through to gcc execution"
}
if [[ $# == 0 ]]; then
usage
exit 0
fi
gcc=g++
ios=
mac=
output=
declare -a modes
declare -a codes
while [[ $# != 0 ]]; do
if [[ ${1:0:1} != - ]]; then
codes+=("$1")
else case "${1:1:1}" in
# XXX: long arguments
(-) shift 1; break;;
(i) ios=${1:2};;
(m) mac=${1:2};;
(o) output=${1:2};;
(p) modes+=(package);;
(P) modes+=(package install);;
(q) modes+=(quiet);;
(r) gcc=${gcc}-${1:2};;
(s) modes+=(substrate);;
(v) modes+=(verbose);;
(V) modes+=(silly);;
(\?) usage; exit 0;;
(*) usage 1>&2; exit 1;;
esac fi
shift 1
done
if [[ ${#codes[@]} == 0 ]]; then
code=
ext=
name=
elif [[ ${#codes[@]} == 1 ]]; then
code=${codes[0]}
ext=mm
name=${code%.${ext}}
else
fatal 'more than one source code file unsupported'
fi
for code in "${codes[@]}"; do
if [[ ! -e "${code}" ]]; then
fatal "${code} does not exist"
fi
done
declare -a archs
declare -a flags
if has substrate "${modes[@]}" && [[ -z ${mac} && -z ${ios} ]]; then
# XXX: this should be based on the system you are currently running
mac=10.6
fi
if [[ -n ${mac} ]]; then
archs+=(i386 x86_64)
fi
if [[ -n ${ios} ]]; then
# XXX: if you are currently on an iphone, this will be incorrect
gcc=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/${gcc}${gvr}
archs+=(armv6)
sdks=(/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS*.sdk)
declare -a armv6
armv6+=(-mcpu=arm1176jzf-s -mthumb)
armv6+=(-miphoneos-version-min="${ios}")
armv6+=(-isysroot "${sdks[${#sdks[@]}-1]}")
armv6+=(-idirafter /usr/include)
armv6+=(-F/Library/Frameworks)
for flag in "${armv6[@]}"; do
flags+=(-Xarch_armv6 "${flag}")
done
fi
flags+=("$@")
flags+=(-Wall) #XXX:-Werror
flags+=(-fmessage-length=0)
if [[ ${#codes[@]} != 0 ]]; then
declare -a extra
# XXX: this is duplicated (obviously)
if [[ ${#archs[@]} == 0 ]]; then
while IFS= read -r line; do
# XXX: deduplicate flags
for flag in ${line#%flag *}; do
extra+=("${flag}")
done
done < <("${gcc}" -E "${flags[@]}" "${codes[@]}" | grep '^%flag ')
else
for arch in "${archs[@]}"; do
while IFS= read -r line; do
# XXX: deduplicate flags
for flag in ${line#%flag *}; do
extra+=("-Xarch_${arch}" "${flag}")
done
done < <("${gcc}" -arch "${arch}" -E "${flags[@]}" "${codes[@]}" | grep '^%flag ')
done
fi
fi
flags+=("${extra[@]}")
if has substrate "${modes[@]}"; then
flags+=(-framework CydiaSubstrate)
flags+=(-dynamiclib)
if [[ -z ${output} ]]; then
output=${name}.dylib
fi
fi
function lower() {
tr '[:upper:]' '[:lower:]'
}
# XXX: this should ready some config file
developer='Jay Freeman (saurik) <saurik@saurik.com>'
namespace="com.saurik"
function filter() {
sed -e '
/^'"$1"' / {
s/^'"$1"' *//;
p;
};
d;' "${codes[@]}"
}
function control() {
# XXX: use local
unset apt_architecture
unset apt_author
unset apt_depends
unset apt_description
unset apt_maintainer
unset apt_package
unset apt_priority
unset apt_section
unset apt_version
local line
while IFS= read -r line; do
if [[ ${line} =~ ^%apt\ *([a-zA-Z_]*)\ *:\ *(.*) ]]; then
local field value
field=${BASH_REMATCH[1]}
field=$(lower <<<${field})
value=${BASH_REMATCH[2]}
if [[ ${field} == depends && ${value} != *${substrate}* ]]; then
value="${value}, ${substrate}"
fi
echo "${field}: ${value}"
# XXX: escaping is wrong
eval "apt_${field}='${value}'"
fi
done < <(cat "${codes[@]}")
if [[ -z ${apt_architecture} && -n ${architecture} ]]; then
echo "architecture: ${architecture}"
fi
if [[ -z ${apt_author} && -n ${developer} ]]; then
echo "author: ${developer}"
fi
if [[ -z ${apt_depends} && -n ${substrate} ]]; then
echo "depends: ${substrate}"
fi
if [[ -z ${apt_maintainer} && -n ${developer} ]]; then
echo "maintainer: ${developer}"
fi
if [[ -z ${apt_package} && -n ${namespace} && -n ${name} ]]; then
echo "package: ${namespace}.$(lower <<<${name})"
fi
if [[ -z ${apt_priority} ]]; then
echo "priority: optional"
fi
if [[ -z ${apt_section} ]]; then
echo "section: Tweaks"
fi
if [[ -z ${apt_version} ]]; then
echo "version: ${CYCC_APT_VERSION:-0.9-1}"
fi
}
function process() {
local code=$1
shift 1
cat <<EOF
#line 1 "${code}"
EOF
/usr/bin/sed -e '
s/^%hook \(.*[^a-zA-Z0-9_]\)\([a-zA-Z_][a-zA-Z_0-9]*\)(/#undef MSOldCall_'$'\\\n''#define MSOldCall_ _\2'$'\\\n''MSHook(\1, \2, /;
s/^%class \(.*\)/MSHookClass(\1)/;
s/%original/MSOldCall_/g;
/^%/ s/^.*//;
/'$'\\n''/ {
i\
%line
=;
s/$/'$'\\\n''%enil/;
};
' "${code}" | sed -ne '
/^%line$/ { n; s/^/#line /; h; p; n; p; d; };
/^%enil$/ { x; s/^.*$//; x; d; };
x; /./ p; x; p;
'
}
if has verbose "${modes[@]}"; then
for code in "${codes[@]}"; do
process "${code}" | grep -Ev '^$|^#'
echo
done
fi
temp=$(mktemp ".cyc.XXX")
declare -a temps
temps+=("${temp}")
declare -a posts
for code in "${codes[@]}"; do
post=${temp}.${code}
process "${code}" >"${post}"
temps+=("${post}")
posts+=("${post}")
done
function clean() {
rm -rf "${temps[@]}"
temps=()
}
function try_() {
"$@"
exit=$?
if [[ ${exit} != 0 ]]; then
clean
exit "${exit}"
fi
}
function try() {
local first=$1
shift 1
if ! has quiet "${modes[@]}"; then
echo "${first##*/} ${c[2]}$@${c[0]}"
fi
try_ "${first}" "$@"
}
function tool() {
thin=$1
type=$("${otool}" -vh "${thin}" | sed -e 's/ */ /g; s/^ *//; /^MH_/ p; d;' | cut -d ' ' -f 5)
if [[ ${type} == @(EXECUTE|DYLIB|BUNDLE) ]]; then
try "${ldid}" -S "${thin}"
fi
}
if [[ -n ${output} ]] || has silly "${modes[@]}"; then
if ! has quiet "${modes[@]}"; then
echo "${gcc##*/}" "${c[2]}${flags[@]}${c[0]}"
fi
declare -a thins
for arch in "${archs[@]}"; do
unset extra
declare -a extra
if [[ -n ${output} ]]; then
thin="${temp}.${arch}"
thins+=("${thin}")
extra+=(-o "${thin}")
fi
if ! has quiet "${modes[@]}"; then
echo " ${c[16]}${arch}${c[0]}: ${c[2]}-arch ${arch} ${extra[@]}${c[0]}"
fi
if has silly "${modes[@]}"; then
echo -n "${arch} "
fi
try_ "${gcc}" -arch "${arch}" "${flags[@]}" "${posts[@]}" "${extra[@]}"
if [[ -n ${output} ]]; then
temps+=("${thin}")
tool "${thin}"
fi
done
if [[ -n ${output} ]]; then
if [[ ${#thins[@]} == 1 ]]; then
cp -a "${thins[0]}" "${output}"
else
try "${lipo}" -create "${thins[@]}" -output "${output}"
fi
fi
else
declare -a extra
for arch in "${archs[@]}"; do
extra+=(-arch "${arch}")
done
try "${gcc}" "${extra[@]}" "${flags[@]}" "${posts[@]}"
fi
if has package "${modes[@]}"; then
host=$(dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null)
if has verbose "${modes[@]}"; then
substrate=mobilesubstrate
echo
control
fi
function field() {
sed -e '
/^'"$1"' *:/ {
s/^[^:]*: *//;
p;
};
d;' "${control}"
}
function package() {
substrate=$1
architecture=$2
rm -rf "${temp}"/*
mkdir -p "${temp}"/DEBIAN
control="${temp}/DEBIAN/control"
control >"${control}"
target=${temp}/Library/MobileSubstrate/DynamicLibraries
mkdir -p "${target}"
cp -a "${output}" "${target}"
plist=${target}/${name}.plist
{
echo '<?xml version="1.0" encoding="UTF-8"?>'
echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
echo '<plist version="1.0">'
echo '<dict>'
echo ' <key>Filter</key>'
echo ' <dict>'
fflags=0
for fflag in $(filter %fflag); do
fflags=$((${fflags} | ${fflag}))
done
if [[ -n ${fflags} ]]; then
echo ' <key>Flags</key>'
echo ' <integer>'"${fflags}"'</integer>'
echo
fi
echo ' <key>Bundles</key>'
echo ' <array>'
for bundle in $(filter %bundle); do
echo ' <string>'"${bundle}"'</string>'
done
echo ' </array>'
echo
echo ' <key>Mode</key>'
echo ' <string>Any</string>'
echo ' </dict>'
echo '</dict>'
echo '</plist>'
} >"${plist}"
echo
cat "${plist}"
plutil -convert binary1 "${plist}"
package=$(field package)
version=$(field version)
echo
if has verbose "${modes[@]}"; then
(cd "${temp}" && find . -type f)
fi
deb=${package}_${version}_${architecture}.deb
{ try dpkg-deb -b "${temp}" "${deb}" 2>&1 1>&3 | sed -e "
/^warning, \`[^\']*\' contains user-defined field \`[^\']*\'$/ d;
/^dpkg-deb: ignoring [0-9]* warnings about the control file(s)$/ d;
"; } 3>/dev/stdout
if has install "${modes[@]}" && [[ ${architecture} == ${host} ]]; then
echo
sudo dpkg -i "${deb}"
fi
}
# XXX: reuse earlier temp
temp=$(mktemp -d ".${name}.XXX")
temps+=("${temp}")
if has armv6 "${archs[@]}"; then
package mobilesubstrate iphoneos-arm
fi
if has i386 "${archs[@]}"; then
package cydiasubstrate darwin-i386
fi
fi
clean