blob: 509f6818381dcf7181261c07053e81d5cc069f4d [file] [log] [blame]
Michael Bestas3952f6c2016-08-26 01:12:08 +03001function __print_cm_functions_help() {
2cat <<EOF
Dan Pasanen03447712016-12-19 11:22:55 -06003Additional LineageOS functions:
Michael Bestas3952f6c2016-08-26 01:12:08 +03004- cout: Changes directory to out.
5- mmp: Builds all of the modules in the current directory and pushes them to the device.
6- mmap: Builds all of the modules in the current directory and its dependencies, then pushes the package to the device.
7- mmmp: Builds all of the modules in the supplied directories and pushes them to the device.
8- mms: Short circuit builder. Quickly re-build the kernel, rootfs, boot and system images
9 without deep dependencies. Requires the full build to have run before.
Dan Pasanen03447712016-12-19 11:22:55 -060010- cmgerrit: A Git wrapper that fetches/pushes patch from/to LineageOS Gerrit Review.
Michael Bestas3952f6c2016-08-26 01:12:08 +030011- cmrebase: Rebase a Gerrit change and push it again.
Dan Pasanen03447712016-12-19 11:22:55 -060012- cmremote: Add git remote for LineageOS Gerrit Review.
Michael Bestas3952f6c2016-08-26 01:12:08 +030013- aospremote: Add git remote for matching AOSP repository.
14- cafremote: Add git remote for matching CodeAurora repository.
15- mka: Builds using SCHED_BATCH on all processors.
16- mkap: Builds the module(s) using mka and pushes them to the device.
17- cmka: Cleans and builds using mka.
18- repodiff: Diff 2 different branches or tags within the same repo
19- repolastsync: Prints date and time of last repo sync.
20- reposync: Parallel repo sync using ionice and SCHED_BATCH.
21- repopick: Utility to fetch changes from Gerrit.
22- installboot: Installs a boot.img to the connected device.
23- installrecovery: Installs a recovery.img to the connected device.
24EOF
25}
26
27function brunch()
28{
29 breakfast $*
30 if [ $? -eq 0 ]; then
31 mka bacon
32 else
33 echo "No such item in brunch menu. Try 'breakfast'"
34 return 1
35 fi
36 return $?
37}
38
39function breakfast()
40{
41 target=$1
42 local variant=$2
43 CM_DEVICES_ONLY="true"
44 unset LUNCH_MENU_CHOICES
45 add_lunch_combo full-eng
46 for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
47 do
48 echo "including $f"
49 . $f
50 done
51 unset f
52
53 if [ $# -eq 0 ]; then
54 # No arguments, so let's have the full menu
55 lunch
56 else
57 echo "z$target" | grep -q "-"
58 if [ $? -eq 0 ]; then
59 # A buildtype was specified, assume a full device name
60 lunch $target
61 else
Simon Shields63ce74b2016-12-28 11:33:29 +110062 # This is probably just the Lineage model name
Michael Bestas3952f6c2016-08-26 01:12:08 +030063 if [ -z "$variant" ]; then
64 variant="userdebug"
65 fi
Matt Mowered8c2482017-01-02 02:26:01 -060066
67 if ! check_product lineage_$target && check_product cm_$target; then
Simon Shields63ce74b2016-12-28 11:33:29 +110068 echo "** Warning: '$target' is using CM-based makefiles. This will be deprecated in the next major release."
69 lunch cm_$target-$variant
Matt Mowered8c2482017-01-02 02:26:01 -060070 else
71 lunch lineage_$target-$variant
Simon Shields63ce74b2016-12-28 11:33:29 +110072 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +030073 fi
74 fi
75 return $?
76}
77
78alias bib=breakfast
79
80function eat()
81{
82 if [ "$OUT" ] ; then
Zhao Wei Liew321dde52017-01-01 15:24:38 +080083 MODVERSION=$(get_build_var LINEAGE_VERSION)
Dan Pasanen03447712016-12-19 11:22:55 -060084 ZIPFILE=lineage-$MODVERSION.zip
Michael Bestas3952f6c2016-08-26 01:12:08 +030085 ZIPPATH=$OUT/$ZIPFILE
86 if [ ! -f $ZIPPATH ] ; then
87 echo "Nothing to eat"
88 return 1
89 fi
90 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
91 if [ $(adb get-state) != device -a $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
92 echo "No device is online. Waiting for one..."
93 echo "Please connect USB and/or enable USB debugging"
94 until [ $(adb get-state) = device -o $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
95 sleep 1
96 done
97 echo "Device Found.."
98 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -080099 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD"); then
100 # if adbd isn't root we can't write to /cache/recovery/
101 adb root
102 sleep 1
103 adb wait-for-device
104 cat << EOF > /tmp/command
Michael Bestas3952f6c2016-08-26 01:12:08 +0300105--sideload_auto_reboot
106EOF
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800107 if adb push /tmp/command /cache/recovery/ ; then
108 echo "Rebooting into recovery for sideload installation"
109 adb reboot recovery
110 adb wait-for-sideload
111 adb sideload $ZIPPATH
112 fi
113 rm /tmp/command
114 else
115 echo "The connected device does not appear to be $CM_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300116 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800117 return $?
Michael Bestas3952f6c2016-08-26 01:12:08 +0300118 else
119 echo "Nothing to eat"
120 return 1
121 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300122}
123
124function omnom()
125{
126 brunch $*
127 eat
128}
129
130function cout()
131{
132 if [ "$OUT" ]; then
133 cd $OUT
134 else
135 echo "Couldn't locate out directory. Try setting OUT."
136 fi
137}
138
139function dddclient()
140{
141 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
142 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
143 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
144 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
145 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
146 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
147 local ARCH=$(get_build_var TARGET_ARCH)
148 local GDB
149 case "$ARCH" in
150 arm) GDB=arm-linux-androideabi-gdb;;
151 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
152 mips|mips64) GDB=mips64el-linux-android-gdb;;
153 x86) GDB=x86_64-linux-android-gdb;;
154 x86_64) GDB=x86_64-linux-android-gdb;;
155 *) echo "Unknown arch $ARCH"; return 1;;
156 esac
157
158 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
159 local EXE="$1"
160 if [ "$EXE" ] ; then
161 EXE=$1
162 if [[ $EXE =~ ^[^/].* ]] ; then
163 EXE="system/bin/"$EXE
164 fi
165 else
166 EXE="app_process"
167 fi
168
169 local PORT="$2"
170 if [ "$PORT" ] ; then
171 PORT=$2
172 else
173 PORT=":5039"
174 fi
175
176 local PID="$3"
177 if [ "$PID" ] ; then
178 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
179 PID=`pid $3`
180 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
181 # that likely didn't work because of returning multiple processes
182 # try again, filtering by root processes (don't contain colon)
183 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
184 if [[ ! "$PID" =~ ^[0-9]+$ ]]
185 then
186 echo "Couldn't resolve '$3' to single PID"
187 return 1
188 else
189 echo ""
190 echo "WARNING: multiple processes matching '$3' observed, using root process"
191 echo ""
192 fi
193 fi
194 fi
195 adb forward "tcp$PORT" "tcp$PORT"
196 local USE64BIT="$(is64bit $PID)"
197 adb shell gdbserver$USE64BIT $PORT --attach $PID &
198 sleep 2
199 else
200 echo ""
201 echo "If you haven't done so already, do this first on the device:"
202 echo " gdbserver $PORT /system/bin/$EXE"
203 echo " or"
204 echo " gdbserver $PORT --attach <PID>"
205 echo ""
206 fi
207
208 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
209 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
210
211 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
212 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
213 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
214 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
215 # Enable special debugging for ART processes.
216 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
217 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
218 fi
219 echo >>"$OUT_ROOT/gdbclient.cmds" ""
220
221 local WHICH_GDB=
222 # 64-bit exe found
223 if [ "$USE64BIT" != "" ] ; then
224 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
225 # 32-bit exe / 32-bit platform
226 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
227 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
228 # 32-bit exe / 64-bit platform
229 else
230 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
231 fi
232
233 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
234 else
235 echo "Unable to determine build system output dir."
236 fi
237}
238
239function cmremote()
240{
241 if ! git rev-parse --git-dir &> /dev/null
242 then
243 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
244 return 1
245 fi
246 git remote rm cmremote 2> /dev/null
247 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Dan Pasanen03447712016-12-19 11:22:55 -0600248 CMUSER=$(git config --get review.review.lineageos.org.username)
Michael Bestas3952f6c2016-08-26 01:12:08 +0300249 if [ -z "$CMUSER" ]
250 then
Dan Pasanen03447712016-12-19 11:22:55 -0600251 git remote add cmremote ssh://review.lineageos.org:29418/$GERRIT_REMOTE
Michael Bestas3952f6c2016-08-26 01:12:08 +0300252 else
Dan Pasanen03447712016-12-19 11:22:55 -0600253 git remote add cmremote ssh://$CMUSER@review.lineageos.org:29418/$GERRIT_REMOTE
Michael Bestas3952f6c2016-08-26 01:12:08 +0300254 fi
255 echo "Remote 'cmremote' created"
256}
257
258function aospremote()
259{
260 if ! git rev-parse --git-dir &> /dev/null
261 then
262 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
263 return 1
264 fi
265 git remote rm aosp 2> /dev/null
Michael Bestase724a1c2016-10-20 23:25:40 +0300266 PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Michael Bestas3952f6c2016-08-26 01:12:08 +0300267 if (echo $PROJECT | grep -qv "^device")
268 then
269 PFX="platform/"
270 fi
271 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
272 echo "Remote 'aosp' created"
273}
274
275function cafremote()
276{
277 if ! git rev-parse --git-dir &> /dev/null
278 then
279 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
280 return 1
281 fi
282 git remote rm caf 2> /dev/null
Michael Bestase724a1c2016-10-20 23:25:40 +0300283 PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Michael Bestas3952f6c2016-08-26 01:12:08 +0300284 if (echo $PROJECT | grep -qv "^device")
285 then
286 PFX="platform/"
287 fi
Zhao Wei Liewfb4b8c52017-01-08 09:03:01 +0800288 git remote add caf https://source.codeaurora.org/quic/la/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300289 echo "Remote 'caf' created"
290}
291
292function installboot()
293{
294 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
295 then
296 echo "No recovery.fstab found. Build recovery first."
297 return 1
298 fi
299 if [ ! -e "$OUT/boot.img" ];
300 then
301 echo "No boot.img found. Run make bootimage first."
302 return 1
303 fi
304 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
305 if [ -z "$PARTITION" ];
306 then
307 # Try for RECOVERY_FSTAB_VERSION = 2
308 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
309 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
310 if [ -z "$PARTITION" ];
311 then
312 echo "Unable to determine boot partition."
313 return 1
314 fi
315 fi
316 adb start-server
317 adb wait-for-online
318 adb root
319 sleep 1
320 adb wait-for-online shell mount /system 2>&1 > /dev/null
321 adb wait-for-online remount
322 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
323 then
324 adb push $OUT/boot.img /cache/
325 for i in $OUT/system/lib/modules/*;
326 do
327 adb push $i /system/lib/modules/
328 done
329 adb shell dd if=/cache/boot.img of=$PARTITION
330 adb shell chmod 644 /system/lib/modules/*
331 echo "Installation complete."
332 else
333 echo "The connected device does not appear to be $CM_BUILD, run away!"
334 fi
335}
336
337function installrecovery()
338{
339 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
340 then
341 echo "No recovery.fstab found. Build recovery first."
342 return 1
343 fi
344 if [ ! -e "$OUT/recovery.img" ];
345 then
346 echo "No recovery.img found. Run make recoveryimage first."
347 return 1
348 fi
349 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
350 if [ -z "$PARTITION" ];
351 then
352 # Try for RECOVERY_FSTAB_VERSION = 2
353 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
354 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
355 if [ -z "$PARTITION" ];
356 then
357 echo "Unable to determine recovery partition."
358 return 1
359 fi
360 fi
361 adb start-server
362 adb wait-for-online
363 adb root
364 sleep 1
365 adb wait-for-online shell mount /system 2>&1 >> /dev/null
366 adb wait-for-online remount
367 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
368 then
369 adb push $OUT/recovery.img /cache/
370 adb shell dd if=/cache/recovery.img of=$PARTITION
371 echo "Installation complete."
372 else
373 echo "The connected device does not appear to be $CM_BUILD, run away!"
374 fi
375}
376
377function makerecipe() {
378 if [ -z "$1" ]
379 then
380 echo "No branch name provided."
381 return 1
382 fi
383 cd android
384 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
385 git commit -a -m "$1"
386 cd ..
387
388 repo forall -c '
389
390 if [ "$REPO_REMOTE" = "github" ]
391 then
392 pwd
393 cmremote
394 git push cmremote HEAD:refs/heads/'$1'
395 fi
396 '
397}
398
399function cmgerrit() {
400 if [ "$(__detect_shell)" = "zsh" ]; then
401 # zsh does not define FUNCNAME, derive from funcstack
402 local FUNCNAME=$funcstack[1]
403 fi
404
405 if [ $# -eq 0 ]; then
406 $FUNCNAME help
407 return 1
408 fi
Dan Pasanen03447712016-12-19 11:22:55 -0600409 local user=`git config --get review.review.lineageos.org.username`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300410 local review=`git config --get remote.github.review`
411 local project=`git config --get remote.github.projectname`
412 local command=$1
413 shift
414 case $command in
415 help)
416 if [ $# -eq 0 ]; then
417 cat <<EOF
418Usage:
419 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
420
421Commands:
422 fetch Just fetch the change as FETCH_HEAD
423 help Show this help, or for a specific command
424 pull Pull a change into current branch
425 push Push HEAD or a local branch to Gerrit for a specific branch
426
427Any other Git commands that support refname would work as:
428 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
429
430See '$FUNCNAME help COMMAND' for more information on a specific command.
431
432Example:
433 $FUNCNAME checkout -b topic 1234/5
434works as:
435 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
436 && git checkout -b topic FETCH_HEAD
437will checkout a new branch 'topic' base on patch-set 5 of change 1234.
438Patch-set 1 will be fetched if omitted.
439EOF
440 return
441 fi
442 case $1 in
443 __cmg_*) echo "For internal use only." ;;
444 changes|for)
445 if [ "$FUNCNAME" = "cmgerrit" ]; then
446 echo "'$FUNCNAME $1' is deprecated."
447 fi
448 ;;
449 help) $FUNCNAME help ;;
450 fetch|pull) cat <<EOF
451usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
452
453works as:
454 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
455 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
456
457Example:
458 $FUNCNAME $1 1234
459will $1 patch-set 1 of change 1234
460EOF
461 ;;
462 push) cat <<EOF
463usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
464
465works as:
466 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
467 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
468
469Example:
470 $FUNCNAME push fix6789:gingerbread
471will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
472HEAD will be pushed from local if omitted.
473EOF
474 ;;
475 *)
476 $FUNCNAME __cmg_err_not_supported $1 && return
477 cat <<EOF
478usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
479
480works as:
481 git fetch http://DOMAIN/p/PROJECT \\
482 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
483 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
484EOF
485 ;;
486 esac
487 ;;
488 __cmg_get_ref)
489 $FUNCNAME __cmg_err_no_arg $command $# && return 1
490 local change_id patchset_id hash
491 case $1 in
492 */*)
493 change_id=${1%%/*}
494 patchset_id=${1#*/}
495 ;;
496 *)
497 change_id=$1
498 patchset_id=1
499 ;;
500 esac
501 hash=$(($change_id % 100))
502 case $hash in
503 [0-9]) hash="0$hash" ;;
504 esac
505 echo "refs/changes/$hash/$change_id/$patchset_id"
506 ;;
507 fetch|pull)
508 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
509 $FUNCNAME __cmg_err_not_repo && return 1
510 local change=$1
511 shift
512 git $command $@ http://$review/p/$project \
513 $($FUNCNAME __cmg_get_ref $change) || return 1
514 ;;
515 push)
516 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
517 $FUNCNAME __cmg_err_not_repo && return 1
518 if [ -z "$user" ]; then
519 echo >&2 "Gerrit username not found."
520 return 1
521 fi
522 local local_branch remote_branch
523 case $1 in
524 *:*)
525 local_branch=${1%:*}
526 remote_branch=${1##*:}
527 ;;
528 *)
529 local_branch=HEAD
530 remote_branch=$1
531 ;;
532 esac
533 shift
534 git push $@ ssh://$user@$review:29418/$project \
535 $local_branch:refs/for/$remote_branch || return 1
536 ;;
537 changes|for)
538 if [ "$FUNCNAME" = "cmgerrit" ]; then
539 echo >&2 "'$FUNCNAME $command' is deprecated."
540 fi
541 ;;
542 __cmg_err_no_arg)
543 if [ $# -lt 2 ]; then
544 echo >&2 "'$FUNCNAME $command' missing argument."
545 elif [ $2 -eq 0 ]; then
546 if [ -n "$3" ]; then
547 $FUNCNAME help $1
548 else
549 echo >&2 "'$FUNCNAME $1' missing argument."
550 fi
551 else
552 return 1
553 fi
554 ;;
555 __cmg_err_not_repo)
556 if [ -z "$review" -o -z "$project" ]; then
557 echo >&2 "Not currently in any reviewable repository."
558 else
559 return 1
560 fi
561 ;;
562 __cmg_err_not_supported)
563 $FUNCNAME __cmg_err_no_arg $command $# && return
564 case $1 in
565 #TODO: filter more git commands that don't use refname
566 init|add|rm|mv|status|clone|remote|bisect|config|stash)
567 echo >&2 "'$FUNCNAME $1' is not supported."
568 ;;
569 *) return 1 ;;
570 esac
571 ;;
572 #TODO: other special cases?
573 *)
574 $FUNCNAME __cmg_err_not_supported $command && return 1
575 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
576 $FUNCNAME __cmg_err_not_repo && return 1
577 local args="$@"
578 local change pre_args refs_arg post_args
579 case "$args" in
580 *--\ *)
581 pre_args=${args%%-- *}
582 post_args="-- ${args#*-- }"
583 ;;
584 *) pre_args="$args" ;;
585 esac
586 args=($pre_args)
587 pre_args=
588 if [ ${#args[@]} -gt 0 ]; then
589 change=${args[${#args[@]}-1]}
590 fi
591 if [ ${#args[@]} -gt 1 ]; then
592 pre_args=${args[0]}
593 for ((i=1; i<${#args[@]}-1; i++)); do
594 pre_args="$pre_args ${args[$i]}"
595 done
596 fi
597 while ((1)); do
598 case $change in
599 ""|--)
600 $FUNCNAME help $command
601 return 1
602 ;;
603 *@*)
604 if [ -z "$refs_arg" ]; then
605 refs_arg="@${change#*@}"
606 change=${change%%@*}
607 fi
608 ;;
609 *~*)
610 if [ -z "$refs_arg" ]; then
611 refs_arg="~${change#*~}"
612 change=${change%%~*}
613 fi
614 ;;
615 *^*)
616 if [ -z "$refs_arg" ]; then
617 refs_arg="^${change#*^}"
618 change=${change%%^*}
619 fi
620 ;;
621 *:*)
622 if [ -z "$refs_arg" ]; then
623 refs_arg=":${change#*:}"
624 change=${change%%:*}
625 fi
626 ;;
627 *) break ;;
628 esac
629 done
630 $FUNCNAME fetch $change \
631 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
632 || return 1
633 ;;
634 esac
635}
636
637function cmrebase() {
638 local repo=$1
639 local refs=$2
640 local pwd="$(pwd)"
641 local dir="$(gettop)/$repo"
642
643 if [ -z $repo ] || [ -z $refs ]; then
Dan Pasanen03447712016-12-19 11:22:55 -0600644 echo "LineageOS Gerrit Rebase Usage: "
Michael Bestas3952f6c2016-08-26 01:12:08 +0300645 echo " cmrebase <path to project> <patch IDs on Gerrit>"
646 echo " The patch IDs appear on the Gerrit commands that are offered."
647 echo " They consist on a series of numbers and slashes, after the text"
648 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
649 echo ""
650 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
651 echo ""
652 return
653 fi
654
655 if [ ! -d $dir ]; then
656 echo "Directory $dir doesn't exist in tree."
657 return
658 fi
659 cd $dir
660 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
661 echo "Starting branch..."
662 repo start tmprebase .
663 echo "Bringing it up to date..."
664 repo sync .
665 echo "Fetching change..."
Dan Pasanen03447712016-12-19 11:22:55 -0600666 git fetch "http://review.lineageos.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Michael Bestas3952f6c2016-08-26 01:12:08 +0300667 if [ "$?" != "0" ]; then
668 echo "Error cherry-picking. Not uploading!"
669 return
670 fi
671 echo "Uploading..."
672 repo upload .
673 echo "Cleaning up..."
674 repo abandon tmprebase .
675 cd $pwd
676}
677
678function mka() {
679 local T=$(gettop)
680 if [ "$T" ]; then
681 case `uname -s` in
682 Darwin)
683 make -C $T -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
684 ;;
685 *)
Steve Kondik36ae30b2016-10-26 17:37:40 -0700686 mk_timer schedtool -B -n 10 -e ionice -n 7 make -C $T -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300687 ;;
688 esac
689
690 else
691 echo "Couldn't locate the top of the tree. Try setting TOP."
692 fi
693}
694
695function cmka() {
696 if [ ! -z "$1" ]; then
697 for i in "$@"; do
698 case $i in
699 bacon|otapackage|systemimage)
700 mka installclean
701 mka $i
702 ;;
703 *)
704 mka clean-$i
705 mka $i
706 ;;
707 esac
708 done
709 else
710 mka clean
711 mka
712 fi
713}
714
715function mms() {
716 local T=$(gettop)
717 if [ -z "$T" ]
718 then
719 echo "Couldn't locate the top of the tree. Try setting TOP."
720 return 1
721 fi
722
723 case `uname -s` in
724 Darwin)
725 local NUM_CPUS=$(sysctl hw.ncpu|cut -d" " -f2)
726 ONE_SHOT_MAKEFILE="__none__" \
727 make -C $T -j $NUM_CPUS "$@"
728 ;;
729 *)
730 local NUM_CPUS=$(cat /proc/cpuinfo | grep "^processor" | wc -l)
731 ONE_SHOT_MAKEFILE="__none__" \
732 mk_timer schedtool -B -n 1 -e ionice -n 1 \
733 make -C $T -j $NUM_CPUS "$@"
734 ;;
735 esac
736}
737
738function repolastsync() {
739 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
740 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
741 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
742 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
743}
744
745function reposync() {
746 case `uname -s` in
747 Darwin)
748 repo sync -j 4 "$@"
749 ;;
750 *)
751 schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
752 ;;
753 esac
754}
755
756function repodiff() {
757 if [ -z "$*" ]; then
758 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
759 return
760 fi
761 diffopts=$* repo forall -c \
762 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
763}
764
765# Return success if adb is up and not in recovery
766function _adb_connected {
767 {
768 if [[ "$(adb get-state)" == device &&
Marc K299137f2016-10-20 10:27:10 +0200769 "$(adb shell test -e /sbin/recovery; echo $?)" != 0 ]]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300770 then
771 return 0
772 fi
773 } 2>/dev/null
774
775 return 1
776};
777
778# Credit for color strip sed: http://goo.gl/BoIcm
779function dopush()
780{
781 local func=$1
782 shift
783
784 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
785 if ! _adb_connected; then
786 echo "No device is online. Waiting for one..."
787 echo "Please connect USB and/or enable USB debugging"
788 until _adb_connected; do
789 sleep 1
790 done
791 echo "Device Found."
792 fi
793
794 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD") || [ "$FORCE_PUSH" = "true" ];
795 then
796 # retrieve IP and PORT info if we're using a TCP connection
Marc K2be9cac2016-10-20 10:27:35 +0200797 TCPIPPORT=$(adb devices \
798 | egrep '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]):[0-9]+[^0-9]+' \
Michael Bestas3952f6c2016-08-26 01:12:08 +0300799 | head -1 | awk '{print $1}')
800 adb root &> /dev/null
801 sleep 0.3
802 if [ -n "$TCPIPPORT" ]
803 then
804 # adb root just killed our connection
805 # so reconnect...
806 adb connect "$TCPIPPORT"
807 fi
808 adb wait-for-device &> /dev/null
809 sleep 0.3
810 adb remount &> /dev/null
811
812 mkdir -p $OUT
813 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
814 ret=$?;
815 if [ $ret -ne 0 ]; then
816 rm -f $OUT/.log;return $ret
817 fi
818
819 # Install: <file>
820 if [ `uname` = "Linux" ]; then
Marc K97b035d2016-10-20 10:27:44 +0200821 LOC="$(cat $OUT/.log | sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' -e 's/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//' \
822 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300823 else
Marc K97b035d2016-10-20 10:27:44 +0200824 LOC="$(cat $OUT/.log | sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" -E "s/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//" \
825 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300826 fi
827
828 # Copy: <file>
829 if [ `uname` = "Linux" ]; then
Marc K97b035d2016-10-20 10:27:44 +0200830 LOC="$LOC $(cat $OUT/.log | sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' -e 's/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//' \
831 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300832 else
Marc K97b035d2016-10-20 10:27:44 +0200833 LOC="$LOC $(cat $OUT/.log | sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" -E 's/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//' \
834 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300835 fi
836
837 # If any files are going to /data, push an octal file permissions reader to device
838 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
839 CHKPERM="/data/local/tmp/chkfileperm.sh"
840(
841cat <<'EOF'
842#!/system/xbin/sh
843FILE=$@
844if [ -e $FILE ]; then
845 ls -l $FILE | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}' | cut -d ' ' -f1
846fi
847EOF
848) > $OUT/.chkfileperm.sh
849 echo "Pushing file permissions checker to device"
850 adb push $OUT/.chkfileperm.sh $CHKPERM
851 adb shell chmod 755 $CHKPERM
852 rm -f $OUT/.chkfileperm.sh
853 fi
854
855 stop_n_start=false
856 for FILE in $(echo $LOC | tr " " "\n"); do
857 # Make sure file is in $OUT/system or $OUT/data
858 case $FILE in
859 $OUT/system/*|$OUT/data/*)
860 # Get target file name (i.e. /system/bin/adb)
861 TARGET=$(echo $FILE | sed "s#$OUT##")
862 ;;
863 *) continue ;;
864 esac
865
866 case $TARGET in
867 /data/*)
868 # fs_config only sets permissions and se labels for files pushed to /system
869 if [ -n "$CHKPERM" ]; then
870 OLDPERM=$(adb shell $CHKPERM $TARGET)
871 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
872 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
873 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
874 fi
875 echo "Pushing: $TARGET"
876 adb push $FILE $TARGET
877 if [ -n "$OLDPERM" ]; then
878 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
879 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
880 adb shell chmod "$OLDPERM" $TARGET
881 else
882 echo "$TARGET did not exist previously, you should set file permissions manually"
883 fi
884 adb shell restorecon "$TARGET"
885 ;;
886 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
887 # Only need to stop services once
888 if ! $stop_n_start; then
889 adb shell stop
890 stop_n_start=true
891 fi
892 echo "Pushing: $TARGET"
893 adb push $FILE $TARGET
894 ;;
895 *)
896 echo "Pushing: $TARGET"
897 adb push $FILE $TARGET
898 ;;
899 esac
900 done
901 if [ -n "$CHKPERM" ]; then
902 adb shell rm $CHKPERM
903 fi
904 if $stop_n_start; then
905 adb shell start
906 fi
907 rm -f $OUT/.log
908 return 0
909 else
910 echo "The connected device does not appear to be $CM_BUILD, run away!"
911 fi
912}
913
914alias mmp='dopush mm'
915alias mmmp='dopush mmm'
916alias mmap='dopush mma'
Zhao Wei Liew64fc5ae2016-12-10 16:48:27 +0800917alias mmmap='dopush mmma'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300918alias mkap='dopush mka'
919alias cmkap='dopush cmka'
920
921function repopick() {
922 T=$(gettop)
923 $T/vendor/cm/build/tools/repopick.py $@
924}
925
926function fixup_common_out_dir() {
927 common_out_dir=$(get_build_var OUT_DIR)/target/common
928 target_device=$(get_build_var TARGET_DEVICE)
929 if [ ! -z $CM_FIXUP_COMMON_OUT ]; then
930 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
931 mv ${common_out_dir} ${common_out_dir}-${target_device}
932 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
933 else
934 [ -L ${common_out_dir} ] && rm ${common_out_dir}
935 mkdir -p ${common_out_dir}-${target_device}
936 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
937 fi
938 else
939 [ -L ${common_out_dir} ] && rm ${common_out_dir}
940 mkdir -p ${common_out_dir}
941 fi
942}
Steve Kondik26e669b2016-11-04 12:05:19 -0700943
944# Enable SD-LLVM if available
945if [ -d $(gettop)/prebuilts/snapdragon-llvm/toolchains ]; then
Alexander Martinz1bbf3e92016-11-05 15:27:43 +0100946 case `uname -s` in
947 Darwin)
948 # Darwin is not supported yet
949 ;;
950 *)
951 export SDCLANG=true
952 export SDCLANG_PATH=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_3.8/prebuilt/linux-x86_64/bin
953 export SDCLANG_LTO_DEFS=$(gettop)/device/qcom/common/sdllvm-lto-defs.mk
954 ;;
955 esac
Steve Kondik26e669b2016-11-04 12:05:19 -0700956fi
Luca Stefani84fda602016-11-24 14:16:33 +0100957
958# Android specific JACK args
959if [ -n "$JACK_SERVER_VM_ARGUMENTS" ] && [ -z "$ANDROID_JACK_VM_ARGS" ]; then
960 export ANDROID_JACK_VM_ARGS=$JACK_SERVER_VM_ARGUMENTS
961fi