blob: f236cab73311fe760ed57af90111b77f644bff7b [file] [log] [blame]
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001function __print_lineage_functions_help() {
Michael Bestas3952f6c2016-08-26 01:12:08 +03002cat <<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.
Luca Stefani5c60e4f2017-08-17 19:28:48 +02008- lineagegerrit: A Git wrapper that fetches/pushes patch from/to LineageOS Gerrit Review.
9- lineagerebase: Rebase a Gerrit change and push it again.
10- lineageremote: Add git remote for LineageOS Gerrit Review.
Michael Bestas3952f6c2016-08-26 01:12:08 +030011- aospremote: Add git remote for matching AOSP repository.
12- cafremote: Add git remote for matching CodeAurora repository.
13- mka: Builds using SCHED_BATCH on all processors.
14- mkap: Builds the module(s) using mka and pushes them to the device.
15- cmka: Cleans and builds using mka.
16- repodiff: Diff 2 different branches or tags within the same repo
17- repolastsync: Prints date and time of last repo sync.
18- reposync: Parallel repo sync using ionice and SCHED_BATCH.
19- repopick: Utility to fetch changes from Gerrit.
20- installboot: Installs a boot.img to the connected device.
21- installrecovery: Installs a recovery.img to the connected device.
22EOF
23}
24
Luca Stefani076c27b2017-08-17 20:30:00 +020025function mk_timer()
26{
27 local start_time=$(date +"%s")
28 $@
29 local ret=$?
30 local end_time=$(date +"%s")
31 local tdiff=$(($end_time-$start_time))
32 local hours=$(($tdiff / 3600 ))
33 local mins=$((($tdiff % 3600) / 60))
34 local secs=$(($tdiff % 60))
35 local ncolors=$(tput colors 2>/dev/null)
36 echo
37 if [ $ret -eq 0 ] ; then
38 echo -n "#### make completed successfully "
39 else
40 echo -n "#### make failed to build some targets "
41 fi
42 if [ $hours -gt 0 ] ; then
43 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
44 elif [ $mins -gt 0 ] ; then
45 printf "(%02g:%02g (mm:ss))" $mins $secs
46 elif [ $secs -gt 0 ] ; then
47 printf "(%s seconds)" $secs
48 fi
49 echo " ####"
50 echo
51 return $ret
52}
53
Michael Bestas3952f6c2016-08-26 01:12:08 +030054function brunch()
55{
56 breakfast $*
57 if [ $? -eq 0 ]; then
58 mka bacon
59 else
60 echo "No such item in brunch menu. Try 'breakfast'"
61 return 1
62 fi
63 return $?
64}
65
66function breakfast()
67{
68 target=$1
69 local variant=$2
Luca Stefani5c60e4f2017-08-17 19:28:48 +020070 LINEAGE_DEVICES_ONLY="true"
Michael Bestas3952f6c2016-08-26 01:12:08 +030071 unset LUNCH_MENU_CHOICES
72 add_lunch_combo full-eng
Dan Pasanen91f76202017-07-06 08:21:30 -050073 for f in `/bin/ls vendor/lineage/vendorsetup.sh 2> /dev/null`
Michael Bestas3952f6c2016-08-26 01:12:08 +030074 do
75 echo "including $f"
76 . $f
77 done
78 unset f
79
80 if [ $# -eq 0 ]; then
81 # No arguments, so let's have the full menu
82 lunch
83 else
84 echo "z$target" | grep -q "-"
85 if [ $? -eq 0 ]; then
86 # A buildtype was specified, assume a full device name
87 lunch $target
88 else
Simon Shields63ce74b2016-12-28 11:33:29 +110089 # This is probably just the Lineage model name
Michael Bestas3952f6c2016-08-26 01:12:08 +030090 if [ -z "$variant" ]; then
91 variant="userdebug"
92 fi
Matt Mowered8c2482017-01-02 02:26:01 -060093
Luca Stefani5c60e4f2017-08-17 19:28:48 +020094 lunch lineage_$target-$variant
Michael Bestas3952f6c2016-08-26 01:12:08 +030095 fi
96 fi
97 return $?
98}
99
100alias bib=breakfast
101
102function eat()
103{
104 if [ "$OUT" ] ; then
Zhao Wei Liew321dde52017-01-01 15:24:38 +0800105 MODVERSION=$(get_build_var LINEAGE_VERSION)
Dan Pasanen03447712016-12-19 11:22:55 -0600106 ZIPFILE=lineage-$MODVERSION.zip
Michael Bestas3952f6c2016-08-26 01:12:08 +0300107 ZIPPATH=$OUT/$ZIPFILE
108 if [ ! -f $ZIPPATH ] ; then
109 echo "Nothing to eat"
110 return 1
111 fi
112 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
113 if [ $(adb get-state) != device -a $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
114 echo "No device is online. Waiting for one..."
115 echo "Please connect USB and/or enable USB debugging"
116 until [ $(adb get-state) = device -o $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
117 sleep 1
118 done
119 echo "Device Found.."
120 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200121 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD"); then
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800122 # if adbd isn't root we can't write to /cache/recovery/
123 adb root
124 sleep 1
125 adb wait-for-device
126 cat << EOF > /tmp/command
Michael Bestas3952f6c2016-08-26 01:12:08 +0300127--sideload_auto_reboot
128EOF
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800129 if adb push /tmp/command /cache/recovery/ ; then
130 echo "Rebooting into recovery for sideload installation"
131 adb reboot recovery
132 adb wait-for-sideload
133 adb sideload $ZIPPATH
134 fi
135 rm /tmp/command
136 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200137 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300138 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800139 return $?
Michael Bestas3952f6c2016-08-26 01:12:08 +0300140 else
141 echo "Nothing to eat"
142 return 1
143 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300144}
145
146function omnom()
147{
148 brunch $*
149 eat
150}
151
152function cout()
153{
154 if [ "$OUT" ]; then
155 cd $OUT
156 else
157 echo "Couldn't locate out directory. Try setting OUT."
158 fi
159}
160
161function dddclient()
162{
163 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
164 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
165 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
166 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
167 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
168 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
169 local ARCH=$(get_build_var TARGET_ARCH)
170 local GDB
171 case "$ARCH" in
172 arm) GDB=arm-linux-androideabi-gdb;;
173 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
174 mips|mips64) GDB=mips64el-linux-android-gdb;;
175 x86) GDB=x86_64-linux-android-gdb;;
176 x86_64) GDB=x86_64-linux-android-gdb;;
177 *) echo "Unknown arch $ARCH"; return 1;;
178 esac
179
180 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
181 local EXE="$1"
182 if [ "$EXE" ] ; then
183 EXE=$1
184 if [[ $EXE =~ ^[^/].* ]] ; then
185 EXE="system/bin/"$EXE
186 fi
187 else
188 EXE="app_process"
189 fi
190
191 local PORT="$2"
192 if [ "$PORT" ] ; then
193 PORT=$2
194 else
195 PORT=":5039"
196 fi
197
198 local PID="$3"
199 if [ "$PID" ] ; then
200 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
201 PID=`pid $3`
202 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
203 # that likely didn't work because of returning multiple processes
204 # try again, filtering by root processes (don't contain colon)
205 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
206 if [[ ! "$PID" =~ ^[0-9]+$ ]]
207 then
208 echo "Couldn't resolve '$3' to single PID"
209 return 1
210 else
211 echo ""
212 echo "WARNING: multiple processes matching '$3' observed, using root process"
213 echo ""
214 fi
215 fi
216 fi
217 adb forward "tcp$PORT" "tcp$PORT"
218 local USE64BIT="$(is64bit $PID)"
219 adb shell gdbserver$USE64BIT $PORT --attach $PID &
220 sleep 2
221 else
222 echo ""
223 echo "If you haven't done so already, do this first on the device:"
224 echo " gdbserver $PORT /system/bin/$EXE"
225 echo " or"
226 echo " gdbserver $PORT --attach <PID>"
227 echo ""
228 fi
229
230 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
231 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
232
233 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
234 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"
235 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
236 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
237 # Enable special debugging for ART processes.
238 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
239 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
240 fi
241 echo >>"$OUT_ROOT/gdbclient.cmds" ""
242
243 local WHICH_GDB=
244 # 64-bit exe found
245 if [ "$USE64BIT" != "" ] ; then
246 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
247 # 32-bit exe / 32-bit platform
248 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
249 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
250 # 32-bit exe / 64-bit platform
251 else
252 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
253 fi
254
255 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
256 else
257 echo "Unable to determine build system output dir."
258 fi
259}
260
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200261function lineageremote()
Michael Bestas3952f6c2016-08-26 01:12:08 +0300262{
263 if ! git rev-parse --git-dir &> /dev/null
264 then
265 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
266 return 1
267 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200268 git remote rm lineage 2> /dev/null
Michael Bestas3952f6c2016-08-26 01:12:08 +0300269 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200270 LINEAGE_USER=$(git config --get review.review.lineageos.org.username)
271 if [ -z "$LINEAGE_USER" ]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300272 then
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200273 git remote add lineage ssh://review.lineageos.org:29418/$GERRIT_REMOTE
Michael Bestas3952f6c2016-08-26 01:12:08 +0300274 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200275 git remote add lineage ssh://$LINEAGE_USER@review.lineageos.org:29418/$GERRIT_REMOTE
Michael Bestas3952f6c2016-08-26 01:12:08 +0300276 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200277 echo "Remote 'lineage' created"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300278}
279
280function aospremote()
281{
282 if ! git rev-parse --git-dir &> /dev/null
283 then
284 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
285 return 1
286 fi
287 git remote rm aosp 2> /dev/null
Michael Bestase724a1c2016-10-20 23:25:40 +0300288 PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Michael Bestas3952f6c2016-08-26 01:12:08 +0300289 if (echo $PROJECT | grep -qv "^device")
290 then
291 PFX="platform/"
292 fi
293 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
294 echo "Remote 'aosp' created"
295}
296
297function cafremote()
298{
299 if ! git rev-parse --git-dir &> /dev/null
300 then
301 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
302 return 1
303 fi
304 git remote rm caf 2> /dev/null
Michael Bestase724a1c2016-10-20 23:25:40 +0300305 PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Michael Bestas3952f6c2016-08-26 01:12:08 +0300306 if (echo $PROJECT | grep -qv "^device")
307 then
308 PFX="platform/"
309 fi
Zhao Wei Liewfb4b8c52017-01-08 09:03:01 +0800310 git remote add caf https://source.codeaurora.org/quic/la/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300311 echo "Remote 'caf' created"
312}
313
314function installboot()
315{
316 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
317 then
318 echo "No recovery.fstab found. Build recovery first."
319 return 1
320 fi
321 if [ ! -e "$OUT/boot.img" ];
322 then
323 echo "No boot.img found. Run make bootimage first."
324 return 1
325 fi
326 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
327 if [ -z "$PARTITION" ];
328 then
329 # Try for RECOVERY_FSTAB_VERSION = 2
330 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
331 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
332 if [ -z "$PARTITION" ];
333 then
334 echo "Unable to determine boot partition."
335 return 1
336 fi
337 fi
338 adb start-server
339 adb wait-for-online
340 adb root
341 sleep 1
342 adb wait-for-online shell mount /system 2>&1 > /dev/null
343 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200344 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300345 then
346 adb push $OUT/boot.img /cache/
Ashwin Rameshb0ea62a2017-08-13 12:00:15 +0530347 if [ -e "$OUT/system/lib/modules/*" ];
348 then
349 for i in $OUT/system/lib/modules/*;
350 do
351 adb push $i /system/lib/modules/
352 done
353 adb shell chmod 644 /system/lib/modules/*
354 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300355 adb shell dd if=/cache/boot.img of=$PARTITION
Michael Bestas3952f6c2016-08-26 01:12:08 +0300356 echo "Installation complete."
357 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200358 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300359 fi
360}
361
362function installrecovery()
363{
364 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
365 then
366 echo "No recovery.fstab found. Build recovery first."
367 return 1
368 fi
369 if [ ! -e "$OUT/recovery.img" ];
370 then
371 echo "No recovery.img found. Run make recoveryimage first."
372 return 1
373 fi
374 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
375 if [ -z "$PARTITION" ];
376 then
377 # Try for RECOVERY_FSTAB_VERSION = 2
378 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
379 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
380 if [ -z "$PARTITION" ];
381 then
382 echo "Unable to determine recovery partition."
383 return 1
384 fi
385 fi
386 adb start-server
387 adb wait-for-online
388 adb root
389 sleep 1
390 adb wait-for-online shell mount /system 2>&1 >> /dev/null
391 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200392 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300393 then
394 adb push $OUT/recovery.img /cache/
395 adb shell dd if=/cache/recovery.img of=$PARTITION
396 echo "Installation complete."
397 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200398 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300399 fi
400}
401
402function makerecipe() {
403 if [ -z "$1" ]
404 then
405 echo "No branch name provided."
406 return 1
407 fi
408 cd android
409 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
410 git commit -a -m "$1"
411 cd ..
412
413 repo forall -c '
414
415 if [ "$REPO_REMOTE" = "github" ]
416 then
417 pwd
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200418 lineageremote
419 git push lineage HEAD:refs/heads/'$1'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300420 fi
421 '
422}
423
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200424function lineagegerrit() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300425 if [ "$(__detect_shell)" = "zsh" ]; then
426 # zsh does not define FUNCNAME, derive from funcstack
427 local FUNCNAME=$funcstack[1]
428 fi
429
430 if [ $# -eq 0 ]; then
431 $FUNCNAME help
432 return 1
433 fi
Dan Pasanen03447712016-12-19 11:22:55 -0600434 local user=`git config --get review.review.lineageos.org.username`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300435 local review=`git config --get remote.github.review`
436 local project=`git config --get remote.github.projectname`
437 local command=$1
438 shift
439 case $command in
440 help)
441 if [ $# -eq 0 ]; then
442 cat <<EOF
443Usage:
444 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
445
446Commands:
447 fetch Just fetch the change as FETCH_HEAD
448 help Show this help, or for a specific command
449 pull Pull a change into current branch
450 push Push HEAD or a local branch to Gerrit for a specific branch
451
452Any other Git commands that support refname would work as:
453 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
454
455See '$FUNCNAME help COMMAND' for more information on a specific command.
456
457Example:
458 $FUNCNAME checkout -b topic 1234/5
459works as:
460 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
461 && git checkout -b topic FETCH_HEAD
462will checkout a new branch 'topic' base on patch-set 5 of change 1234.
463Patch-set 1 will be fetched if omitted.
464EOF
465 return
466 fi
467 case $1 in
468 __cmg_*) echo "For internal use only." ;;
469 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200470 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300471 echo "'$FUNCNAME $1' is deprecated."
472 fi
473 ;;
474 help) $FUNCNAME help ;;
475 fetch|pull) cat <<EOF
476usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
477
478works as:
479 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
480 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
481
482Example:
483 $FUNCNAME $1 1234
484will $1 patch-set 1 of change 1234
485EOF
486 ;;
487 push) cat <<EOF
488usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
489
490works as:
491 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
492 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
493
494Example:
495 $FUNCNAME push fix6789:gingerbread
496will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
497HEAD will be pushed from local if omitted.
498EOF
499 ;;
500 *)
501 $FUNCNAME __cmg_err_not_supported $1 && return
502 cat <<EOF
503usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
504
505works as:
506 git fetch http://DOMAIN/p/PROJECT \\
507 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
508 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
509EOF
510 ;;
511 esac
512 ;;
513 __cmg_get_ref)
514 $FUNCNAME __cmg_err_no_arg $command $# && return 1
515 local change_id patchset_id hash
516 case $1 in
517 */*)
518 change_id=${1%%/*}
519 patchset_id=${1#*/}
520 ;;
521 *)
522 change_id=$1
523 patchset_id=1
524 ;;
525 esac
526 hash=$(($change_id % 100))
527 case $hash in
528 [0-9]) hash="0$hash" ;;
529 esac
530 echo "refs/changes/$hash/$change_id/$patchset_id"
531 ;;
532 fetch|pull)
533 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
534 $FUNCNAME __cmg_err_not_repo && return 1
535 local change=$1
536 shift
537 git $command $@ http://$review/p/$project \
538 $($FUNCNAME __cmg_get_ref $change) || return 1
539 ;;
540 push)
541 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
542 $FUNCNAME __cmg_err_not_repo && return 1
543 if [ -z "$user" ]; then
544 echo >&2 "Gerrit username not found."
545 return 1
546 fi
547 local local_branch remote_branch
548 case $1 in
549 *:*)
550 local_branch=${1%:*}
551 remote_branch=${1##*:}
552 ;;
553 *)
554 local_branch=HEAD
555 remote_branch=$1
556 ;;
557 esac
558 shift
559 git push $@ ssh://$user@$review:29418/$project \
560 $local_branch:refs/for/$remote_branch || return 1
561 ;;
562 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200563 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300564 echo >&2 "'$FUNCNAME $command' is deprecated."
565 fi
566 ;;
567 __cmg_err_no_arg)
568 if [ $# -lt 2 ]; then
569 echo >&2 "'$FUNCNAME $command' missing argument."
570 elif [ $2 -eq 0 ]; then
571 if [ -n "$3" ]; then
572 $FUNCNAME help $1
573 else
574 echo >&2 "'$FUNCNAME $1' missing argument."
575 fi
576 else
577 return 1
578 fi
579 ;;
580 __cmg_err_not_repo)
581 if [ -z "$review" -o -z "$project" ]; then
582 echo >&2 "Not currently in any reviewable repository."
583 else
584 return 1
585 fi
586 ;;
587 __cmg_err_not_supported)
588 $FUNCNAME __cmg_err_no_arg $command $# && return
589 case $1 in
590 #TODO: filter more git commands that don't use refname
591 init|add|rm|mv|status|clone|remote|bisect|config|stash)
592 echo >&2 "'$FUNCNAME $1' is not supported."
593 ;;
594 *) return 1 ;;
595 esac
596 ;;
597 #TODO: other special cases?
598 *)
599 $FUNCNAME __cmg_err_not_supported $command && return 1
600 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
601 $FUNCNAME __cmg_err_not_repo && return 1
602 local args="$@"
603 local change pre_args refs_arg post_args
604 case "$args" in
605 *--\ *)
606 pre_args=${args%%-- *}
607 post_args="-- ${args#*-- }"
608 ;;
609 *) pre_args="$args" ;;
610 esac
611 args=($pre_args)
612 pre_args=
613 if [ ${#args[@]} -gt 0 ]; then
614 change=${args[${#args[@]}-1]}
615 fi
616 if [ ${#args[@]} -gt 1 ]; then
617 pre_args=${args[0]}
618 for ((i=1; i<${#args[@]}-1; i++)); do
619 pre_args="$pre_args ${args[$i]}"
620 done
621 fi
622 while ((1)); do
623 case $change in
624 ""|--)
625 $FUNCNAME help $command
626 return 1
627 ;;
628 *@*)
629 if [ -z "$refs_arg" ]; then
630 refs_arg="@${change#*@}"
631 change=${change%%@*}
632 fi
633 ;;
634 *~*)
635 if [ -z "$refs_arg" ]; then
636 refs_arg="~${change#*~}"
637 change=${change%%~*}
638 fi
639 ;;
640 *^*)
641 if [ -z "$refs_arg" ]; then
642 refs_arg="^${change#*^}"
643 change=${change%%^*}
644 fi
645 ;;
646 *:*)
647 if [ -z "$refs_arg" ]; then
648 refs_arg=":${change#*:}"
649 change=${change%%:*}
650 fi
651 ;;
652 *) break ;;
653 esac
654 done
655 $FUNCNAME fetch $change \
656 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
657 || return 1
658 ;;
659 esac
660}
661
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200662function lineagerebase() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300663 local repo=$1
664 local refs=$2
665 local pwd="$(pwd)"
666 local dir="$(gettop)/$repo"
667
668 if [ -z $repo ] || [ -z $refs ]; then
Dan Pasanen03447712016-12-19 11:22:55 -0600669 echo "LineageOS Gerrit Rebase Usage: "
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200670 echo " lineagerebase <path to project> <patch IDs on Gerrit>"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300671 echo " The patch IDs appear on the Gerrit commands that are offered."
672 echo " They consist on a series of numbers and slashes, after the text"
673 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
674 echo ""
675 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
676 echo ""
677 return
678 fi
679
680 if [ ! -d $dir ]; then
681 echo "Directory $dir doesn't exist in tree."
682 return
683 fi
684 cd $dir
685 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
686 echo "Starting branch..."
687 repo start tmprebase .
688 echo "Bringing it up to date..."
689 repo sync .
690 echo "Fetching change..."
Dan Pasanen03447712016-12-19 11:22:55 -0600691 git fetch "http://review.lineageos.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Michael Bestas3952f6c2016-08-26 01:12:08 +0300692 if [ "$?" != "0" ]; then
693 echo "Error cherry-picking. Not uploading!"
694 return
695 fi
696 echo "Uploading..."
697 repo upload .
698 echo "Cleaning up..."
699 repo abandon tmprebase .
700 cd $pwd
701}
702
703function mka() {
Luca Stefani085af722017-08-17 20:34:44 +0200704 m -j "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300705}
706
707function cmka() {
708 if [ ! -z "$1" ]; then
709 for i in "$@"; do
710 case $i in
711 bacon|otapackage|systemimage)
712 mka installclean
713 mka $i
714 ;;
715 *)
716 mka clean-$i
717 mka $i
718 ;;
719 esac
720 done
721 else
722 mka clean
723 mka
724 fi
725}
726
Michael Bestas3952f6c2016-08-26 01:12:08 +0300727function repolastsync() {
728 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
729 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
730 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
731 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
732}
733
734function reposync() {
Luca Stefani085af722017-08-17 20:34:44 +0200735 repo sync -j 4 "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300736}
737
738function repodiff() {
739 if [ -z "$*" ]; then
740 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
741 return
742 fi
743 diffopts=$* repo forall -c \
744 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
745}
746
747# Return success if adb is up and not in recovery
748function _adb_connected {
749 {
750 if [[ "$(adb get-state)" == device &&
Marc K299137f2016-10-20 10:27:10 +0200751 "$(adb shell test -e /sbin/recovery; echo $?)" != 0 ]]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300752 then
753 return 0
754 fi
755 } 2>/dev/null
756
757 return 1
758};
759
760# Credit for color strip sed: http://goo.gl/BoIcm
761function dopush()
762{
763 local func=$1
764 shift
765
766 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
767 if ! _adb_connected; then
768 echo "No device is online. Waiting for one..."
769 echo "Please connect USB and/or enable USB debugging"
770 until _adb_connected; do
771 sleep 1
772 done
773 echo "Device Found."
774 fi
775
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200776 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD") || [ "$FORCE_PUSH" = "true" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300777 then
778 # retrieve IP and PORT info if we're using a TCP connection
Marc K2be9cac2016-10-20 10:27:35 +0200779 TCPIPPORT=$(adb devices \
780 | 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 +0300781 | head -1 | awk '{print $1}')
782 adb root &> /dev/null
783 sleep 0.3
784 if [ -n "$TCPIPPORT" ]
785 then
786 # adb root just killed our connection
787 # so reconnect...
788 adb connect "$TCPIPPORT"
789 fi
790 adb wait-for-device &> /dev/null
791 sleep 0.3
792 adb remount &> /dev/null
793
794 mkdir -p $OUT
795 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
796 ret=$?;
797 if [ $ret -ne 0 ]; then
798 rm -f $OUT/.log;return $ret
799 fi
800
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500801 is_gnu_sed=`sed --version | head -1 | grep -c GNU`
802
Michael Bestas3952f6c2016-08-26 01:12:08 +0300803 # Install: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500804 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200805 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}\] +//' \
806 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300807 else
Marc K97b035d2016-10-20 10:27:44 +0200808 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}\] +//" \
809 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300810 fi
811
812 # Copy: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500813 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200814 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}\] +//' \
815 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300816 else
Marc K97b035d2016-10-20 10:27:44 +0200817 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}\] +//' \
818 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300819 fi
820
821 # If any files are going to /data, push an octal file permissions reader to device
822 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
823 CHKPERM="/data/local/tmp/chkfileperm.sh"
824(
825cat <<'EOF'
826#!/system/xbin/sh
827FILE=$@
828if [ -e $FILE ]; then
829 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
830fi
831EOF
832) > $OUT/.chkfileperm.sh
833 echo "Pushing file permissions checker to device"
834 adb push $OUT/.chkfileperm.sh $CHKPERM
835 adb shell chmod 755 $CHKPERM
836 rm -f $OUT/.chkfileperm.sh
837 fi
838
839 stop_n_start=false
840 for FILE in $(echo $LOC | tr " " "\n"); do
841 # Make sure file is in $OUT/system or $OUT/data
842 case $FILE in
843 $OUT/system/*|$OUT/data/*)
844 # Get target file name (i.e. /system/bin/adb)
845 TARGET=$(echo $FILE | sed "s#$OUT##")
846 ;;
847 *) continue ;;
848 esac
849
850 case $TARGET in
851 /data/*)
852 # fs_config only sets permissions and se labels for files pushed to /system
853 if [ -n "$CHKPERM" ]; then
854 OLDPERM=$(adb shell $CHKPERM $TARGET)
855 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
856 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
857 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
858 fi
859 echo "Pushing: $TARGET"
860 adb push $FILE $TARGET
861 if [ -n "$OLDPERM" ]; then
862 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
863 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
864 adb shell chmod "$OLDPERM" $TARGET
865 else
866 echo "$TARGET did not exist previously, you should set file permissions manually"
867 fi
868 adb shell restorecon "$TARGET"
869 ;;
870 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
871 # Only need to stop services once
872 if ! $stop_n_start; then
873 adb shell stop
874 stop_n_start=true
875 fi
876 echo "Pushing: $TARGET"
877 adb push $FILE $TARGET
878 ;;
879 *)
880 echo "Pushing: $TARGET"
881 adb push $FILE $TARGET
882 ;;
883 esac
884 done
885 if [ -n "$CHKPERM" ]; then
886 adb shell rm $CHKPERM
887 fi
888 if $stop_n_start; then
889 adb shell start
890 fi
891 rm -f $OUT/.log
892 return 0
893 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200894 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300895 fi
896}
897
898alias mmp='dopush mm'
899alias mmmp='dopush mmm'
900alias mmap='dopush mma'
Zhao Wei Liew64fc5ae2016-12-10 16:48:27 +0800901alias mmmap='dopush mmma'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300902alias mkap='dopush mka'
903alias cmkap='dopush cmka'
904
905function repopick() {
906 T=$(gettop)
Dan Pasanen91f76202017-07-06 08:21:30 -0500907 $T/vendor/lineage/build/tools/repopick.py $@
Michael Bestas3952f6c2016-08-26 01:12:08 +0300908}
909
910function fixup_common_out_dir() {
911 common_out_dir=$(get_build_var OUT_DIR)/target/common
912 target_device=$(get_build_var TARGET_DEVICE)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200913 if [ ! -z $LINEAGE_FIXUP_COMMON_OUT ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300914 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
915 mv ${common_out_dir} ${common_out_dir}-${target_device}
916 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
917 else
918 [ -L ${common_out_dir} ] && rm ${common_out_dir}
919 mkdir -p ${common_out_dir}-${target_device}
920 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
921 fi
922 else
923 [ -L ${common_out_dir} ] && rm ${common_out_dir}
924 mkdir -p ${common_out_dir}
925 fi
926}
Steve Kondik26e669b2016-11-04 12:05:19 -0700927
928# Enable SD-LLVM if available
929if [ -d $(gettop)/prebuilts/snapdragon-llvm/toolchains ]; then
Alexander Martinz1bbf3e92016-11-05 15:27:43 +0100930 case `uname -s` in
931 Darwin)
932 # Darwin is not supported yet
933 ;;
934 *)
935 export SDCLANG=true
936 export SDCLANG_PATH=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_3.8/prebuilt/linux-x86_64/bin
937 export SDCLANG_LTO_DEFS=$(gettop)/device/qcom/common/sdllvm-lto-defs.mk
938 ;;
939 esac
Steve Kondik26e669b2016-11-04 12:05:19 -0700940fi
Luca Stefani84fda602016-11-24 14:16:33 +0100941
942# Android specific JACK args
943if [ -n "$JACK_SERVER_VM_ARGUMENTS" ] && [ -z "$ANDROID_JACK_VM_ARGS" ]; then
944 export ANDROID_JACK_VM_ARGS=$JACK_SERVER_VM_ARGUMENTS
945fi