blob: a418d214e6fedd180de4595e9b386c5f8f5b96cd [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
Paul Keithb11d5732017-11-02 20:31:33 +0100105 ZIPPATH=`ls -tr "$OUT"/lineage-*.zip | tail -1`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300106 if [ ! -f $ZIPPATH ] ; then
107 echo "Nothing to eat"
108 return 1
109 fi
110 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
111 if [ $(adb get-state) != device -a $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
112 echo "No device is online. Waiting for one..."
113 echo "Please connect USB and/or enable USB debugging"
114 until [ $(adb get-state) = device -o $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
115 sleep 1
116 done
117 echo "Device Found.."
118 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200119 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD"); then
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800120 # if adbd isn't root we can't write to /cache/recovery/
121 adb root
122 sleep 1
123 adb wait-for-device
124 cat << EOF > /tmp/command
Michael Bestas3952f6c2016-08-26 01:12:08 +0300125--sideload_auto_reboot
126EOF
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800127 if adb push /tmp/command /cache/recovery/ ; then
128 echo "Rebooting into recovery for sideload installation"
129 adb reboot recovery
130 adb wait-for-sideload
131 adb sideload $ZIPPATH
132 fi
133 rm /tmp/command
134 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200135 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300136 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800137 return $?
Michael Bestas3952f6c2016-08-26 01:12:08 +0300138 else
139 echo "Nothing to eat"
140 return 1
141 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300142}
143
144function omnom()
145{
146 brunch $*
147 eat
148}
149
150function cout()
151{
152 if [ "$OUT" ]; then
153 cd $OUT
154 else
155 echo "Couldn't locate out directory. Try setting OUT."
156 fi
157}
158
159function dddclient()
160{
161 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
162 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
163 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
164 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
165 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
166 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
167 local ARCH=$(get_build_var TARGET_ARCH)
168 local GDB
169 case "$ARCH" in
170 arm) GDB=arm-linux-androideabi-gdb;;
171 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
172 mips|mips64) GDB=mips64el-linux-android-gdb;;
173 x86) GDB=x86_64-linux-android-gdb;;
174 x86_64) GDB=x86_64-linux-android-gdb;;
175 *) echo "Unknown arch $ARCH"; return 1;;
176 esac
177
178 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
179 local EXE="$1"
180 if [ "$EXE" ] ; then
181 EXE=$1
182 if [[ $EXE =~ ^[^/].* ]] ; then
183 EXE="system/bin/"$EXE
184 fi
185 else
186 EXE="app_process"
187 fi
188
189 local PORT="$2"
190 if [ "$PORT" ] ; then
191 PORT=$2
192 else
193 PORT=":5039"
194 fi
195
196 local PID="$3"
197 if [ "$PID" ] ; then
198 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
199 PID=`pid $3`
200 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
201 # that likely didn't work because of returning multiple processes
202 # try again, filtering by root processes (don't contain colon)
203 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
204 if [[ ! "$PID" =~ ^[0-9]+$ ]]
205 then
206 echo "Couldn't resolve '$3' to single PID"
207 return 1
208 else
209 echo ""
210 echo "WARNING: multiple processes matching '$3' observed, using root process"
211 echo ""
212 fi
213 fi
214 fi
215 adb forward "tcp$PORT" "tcp$PORT"
216 local USE64BIT="$(is64bit $PID)"
217 adb shell gdbserver$USE64BIT $PORT --attach $PID &
218 sleep 2
219 else
220 echo ""
221 echo "If you haven't done so already, do this first on the device:"
222 echo " gdbserver $PORT /system/bin/$EXE"
223 echo " or"
224 echo " gdbserver $PORT --attach <PID>"
225 echo ""
226 fi
227
228 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
229 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
230
231 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
232 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"
233 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
234 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
235 # Enable special debugging for ART processes.
236 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
237 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
238 fi
239 echo >>"$OUT_ROOT/gdbclient.cmds" ""
240
241 local WHICH_GDB=
242 # 64-bit exe found
243 if [ "$USE64BIT" != "" ] ; then
244 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
245 # 32-bit exe / 32-bit platform
246 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
247 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
248 # 32-bit exe / 64-bit platform
249 else
250 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
251 fi
252
253 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
254 else
255 echo "Unable to determine build system output dir."
256 fi
257}
258
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200259function lineageremote()
Michael Bestas3952f6c2016-08-26 01:12:08 +0300260{
261 if ! git rev-parse --git-dir &> /dev/null
262 then
263 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
264 return 1
265 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200266 git remote rm lineage 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300267 local GERRIT_REMOTE=$(git config --get remote.github.projectname)
Michael Bestasaf3532b2017-08-23 17:40:40 +0300268 if [ -z "$GERRIT_REMOTE" ]
269 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300270 local GERRIT_REMOTE=$(git config --get remote.aosp.projectname | sed s#platform/#android/#g | sed s#/#_#g)
271 local PFX="LineageOS/"
Michael Bestasaf3532b2017-08-23 17:40:40 +0300272 fi
Michael Bestasad3a5702017-08-24 00:00:48 +0300273 local LINEAGE_USER=$(git config --get review.review.lineageos.org.username)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200274 if [ -z "$LINEAGE_USER" ]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300275 then
Michael Bestasaf3532b2017-08-23 17:40:40 +0300276 git remote add lineage ssh://review.lineageos.org:29418/$PFX$GERRIT_REMOTE
Michael Bestas3952f6c2016-08-26 01:12:08 +0300277 else
Michael Bestasaf3532b2017-08-23 17:40:40 +0300278 git remote add lineage ssh://$LINEAGE_USER@review.lineageos.org:29418/$PFX$GERRIT_REMOTE
Michael Bestas3952f6c2016-08-26 01:12:08 +0300279 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200280 echo "Remote 'lineage' created"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300281}
282
283function aospremote()
284{
285 if ! git rev-parse --git-dir &> /dev/null
286 then
287 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
288 return 1
289 fi
290 git remote rm aosp 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300291 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400292 # Google moved the repo location in Oreo
293 if [ $PROJECT = "build/make" ]
294 then
295 PROJECT="build"
296 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300297 if (echo $PROJECT | grep -qv "^device")
298 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300299 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300300 fi
301 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
302 echo "Remote 'aosp' created"
303}
304
305function cafremote()
306{
307 if ! git rev-parse --git-dir &> /dev/null
308 then
309 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
310 return 1
311 fi
312 git remote rm caf 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300313 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400314 # Google moved the repo location in Oreo
315 if [ $PROJECT = "build/make" ]
316 then
317 PROJECT="build"
318 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300319 if (echo $PROJECT | grep -qv "^device")
320 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300321 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300322 fi
Zhao Wei Liewfb4b8c52017-01-08 09:03:01 +0800323 git remote add caf https://source.codeaurora.org/quic/la/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300324 echo "Remote 'caf' created"
325}
326
327function installboot()
328{
329 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
330 then
331 echo "No recovery.fstab found. Build recovery first."
332 return 1
333 fi
334 if [ ! -e "$OUT/boot.img" ];
335 then
336 echo "No boot.img found. Run make bootimage first."
337 return 1
338 fi
339 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
340 if [ -z "$PARTITION" ];
341 then
342 # Try for RECOVERY_FSTAB_VERSION = 2
343 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
344 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
345 if [ -z "$PARTITION" ];
346 then
347 echo "Unable to determine boot partition."
348 return 1
349 fi
350 fi
351 adb start-server
352 adb wait-for-online
353 adb root
354 sleep 1
355 adb wait-for-online shell mount /system 2>&1 > /dev/null
356 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200357 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300358 then
359 adb push $OUT/boot.img /cache/
Ashwin Rameshb0ea62a2017-08-13 12:00:15 +0530360 if [ -e "$OUT/system/lib/modules/*" ];
361 then
362 for i in $OUT/system/lib/modules/*;
363 do
364 adb push $i /system/lib/modules/
365 done
366 adb shell chmod 644 /system/lib/modules/*
367 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300368 adb shell dd if=/cache/boot.img of=$PARTITION
Michael Bestas3952f6c2016-08-26 01:12:08 +0300369 echo "Installation complete."
370 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200371 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300372 fi
373}
374
375function installrecovery()
376{
377 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
378 then
379 echo "No recovery.fstab found. Build recovery first."
380 return 1
381 fi
382 if [ ! -e "$OUT/recovery.img" ];
383 then
384 echo "No recovery.img found. Run make recoveryimage first."
385 return 1
386 fi
387 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
388 if [ -z "$PARTITION" ];
389 then
390 # Try for RECOVERY_FSTAB_VERSION = 2
391 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
392 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
393 if [ -z "$PARTITION" ];
394 then
395 echo "Unable to determine recovery partition."
396 return 1
397 fi
398 fi
399 adb start-server
400 adb wait-for-online
401 adb root
402 sleep 1
403 adb wait-for-online shell mount /system 2>&1 >> /dev/null
404 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200405 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300406 then
407 adb push $OUT/recovery.img /cache/
408 adb shell dd if=/cache/recovery.img of=$PARTITION
409 echo "Installation complete."
410 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200411 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300412 fi
413}
414
415function makerecipe() {
416 if [ -z "$1" ]
417 then
418 echo "No branch name provided."
419 return 1
420 fi
421 cd android
422 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
423 git commit -a -m "$1"
424 cd ..
425
426 repo forall -c '
427
428 if [ "$REPO_REMOTE" = "github" ]
429 then
430 pwd
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200431 lineageremote
432 git push lineage HEAD:refs/heads/'$1'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300433 fi
434 '
435}
436
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200437function lineagegerrit() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300438 if [ "$(__detect_shell)" = "zsh" ]; then
439 # zsh does not define FUNCNAME, derive from funcstack
440 local FUNCNAME=$funcstack[1]
441 fi
442
443 if [ $# -eq 0 ]; then
444 $FUNCNAME help
445 return 1
446 fi
Dan Pasanen03447712016-12-19 11:22:55 -0600447 local user=`git config --get review.review.lineageos.org.username`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300448 local review=`git config --get remote.github.review`
449 local project=`git config --get remote.github.projectname`
450 local command=$1
451 shift
452 case $command in
453 help)
454 if [ $# -eq 0 ]; then
455 cat <<EOF
456Usage:
457 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
458
459Commands:
460 fetch Just fetch the change as FETCH_HEAD
461 help Show this help, or for a specific command
462 pull Pull a change into current branch
463 push Push HEAD or a local branch to Gerrit for a specific branch
464
465Any other Git commands that support refname would work as:
466 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
467
468See '$FUNCNAME help COMMAND' for more information on a specific command.
469
470Example:
471 $FUNCNAME checkout -b topic 1234/5
472works as:
473 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
474 && git checkout -b topic FETCH_HEAD
475will checkout a new branch 'topic' base on patch-set 5 of change 1234.
476Patch-set 1 will be fetched if omitted.
477EOF
478 return
479 fi
480 case $1 in
481 __cmg_*) echo "For internal use only." ;;
482 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200483 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300484 echo "'$FUNCNAME $1' is deprecated."
485 fi
486 ;;
487 help) $FUNCNAME help ;;
488 fetch|pull) cat <<EOF
489usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
490
491works as:
492 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
493 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
494
495Example:
496 $FUNCNAME $1 1234
497will $1 patch-set 1 of change 1234
498EOF
499 ;;
500 push) cat <<EOF
501usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
502
503works as:
504 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
505 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
506
507Example:
508 $FUNCNAME push fix6789:gingerbread
509will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
510HEAD will be pushed from local if omitted.
511EOF
512 ;;
513 *)
514 $FUNCNAME __cmg_err_not_supported $1 && return
515 cat <<EOF
516usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
517
518works as:
519 git fetch http://DOMAIN/p/PROJECT \\
520 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
521 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
522EOF
523 ;;
524 esac
525 ;;
526 __cmg_get_ref)
527 $FUNCNAME __cmg_err_no_arg $command $# && return 1
528 local change_id patchset_id hash
529 case $1 in
530 */*)
531 change_id=${1%%/*}
532 patchset_id=${1#*/}
533 ;;
534 *)
535 change_id=$1
536 patchset_id=1
537 ;;
538 esac
539 hash=$(($change_id % 100))
540 case $hash in
541 [0-9]) hash="0$hash" ;;
542 esac
543 echo "refs/changes/$hash/$change_id/$patchset_id"
544 ;;
545 fetch|pull)
546 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
547 $FUNCNAME __cmg_err_not_repo && return 1
548 local change=$1
549 shift
550 git $command $@ http://$review/p/$project \
551 $($FUNCNAME __cmg_get_ref $change) || return 1
552 ;;
553 push)
554 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
555 $FUNCNAME __cmg_err_not_repo && return 1
556 if [ -z "$user" ]; then
557 echo >&2 "Gerrit username not found."
558 return 1
559 fi
560 local local_branch remote_branch
561 case $1 in
562 *:*)
563 local_branch=${1%:*}
564 remote_branch=${1##*:}
565 ;;
566 *)
567 local_branch=HEAD
568 remote_branch=$1
569 ;;
570 esac
571 shift
572 git push $@ ssh://$user@$review:29418/$project \
573 $local_branch:refs/for/$remote_branch || return 1
574 ;;
575 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200576 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300577 echo >&2 "'$FUNCNAME $command' is deprecated."
578 fi
579 ;;
580 __cmg_err_no_arg)
581 if [ $# -lt 2 ]; then
582 echo >&2 "'$FUNCNAME $command' missing argument."
583 elif [ $2 -eq 0 ]; then
584 if [ -n "$3" ]; then
585 $FUNCNAME help $1
586 else
587 echo >&2 "'$FUNCNAME $1' missing argument."
588 fi
589 else
590 return 1
591 fi
592 ;;
593 __cmg_err_not_repo)
594 if [ -z "$review" -o -z "$project" ]; then
595 echo >&2 "Not currently in any reviewable repository."
596 else
597 return 1
598 fi
599 ;;
600 __cmg_err_not_supported)
601 $FUNCNAME __cmg_err_no_arg $command $# && return
602 case $1 in
603 #TODO: filter more git commands that don't use refname
604 init|add|rm|mv|status|clone|remote|bisect|config|stash)
605 echo >&2 "'$FUNCNAME $1' is not supported."
606 ;;
607 *) return 1 ;;
608 esac
609 ;;
610 #TODO: other special cases?
611 *)
612 $FUNCNAME __cmg_err_not_supported $command && return 1
613 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
614 $FUNCNAME __cmg_err_not_repo && return 1
615 local args="$@"
616 local change pre_args refs_arg post_args
617 case "$args" in
618 *--\ *)
619 pre_args=${args%%-- *}
620 post_args="-- ${args#*-- }"
621 ;;
622 *) pre_args="$args" ;;
623 esac
624 args=($pre_args)
625 pre_args=
626 if [ ${#args[@]} -gt 0 ]; then
627 change=${args[${#args[@]}-1]}
628 fi
629 if [ ${#args[@]} -gt 1 ]; then
630 pre_args=${args[0]}
631 for ((i=1; i<${#args[@]}-1; i++)); do
632 pre_args="$pre_args ${args[$i]}"
633 done
634 fi
635 while ((1)); do
636 case $change in
637 ""|--)
638 $FUNCNAME help $command
639 return 1
640 ;;
641 *@*)
642 if [ -z "$refs_arg" ]; then
643 refs_arg="@${change#*@}"
644 change=${change%%@*}
645 fi
646 ;;
647 *~*)
648 if [ -z "$refs_arg" ]; then
649 refs_arg="~${change#*~}"
650 change=${change%%~*}
651 fi
652 ;;
653 *^*)
654 if [ -z "$refs_arg" ]; then
655 refs_arg="^${change#*^}"
656 change=${change%%^*}
657 fi
658 ;;
659 *:*)
660 if [ -z "$refs_arg" ]; then
661 refs_arg=":${change#*:}"
662 change=${change%%:*}
663 fi
664 ;;
665 *) break ;;
666 esac
667 done
668 $FUNCNAME fetch $change \
669 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
670 || return 1
671 ;;
672 esac
673}
674
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200675function lineagerebase() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300676 local repo=$1
677 local refs=$2
678 local pwd="$(pwd)"
679 local dir="$(gettop)/$repo"
680
681 if [ -z $repo ] || [ -z $refs ]; then
Dan Pasanen03447712016-12-19 11:22:55 -0600682 echo "LineageOS Gerrit Rebase Usage: "
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200683 echo " lineagerebase <path to project> <patch IDs on Gerrit>"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300684 echo " The patch IDs appear on the Gerrit commands that are offered."
685 echo " They consist on a series of numbers and slashes, after the text"
686 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
687 echo ""
688 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
689 echo ""
690 return
691 fi
692
693 if [ ! -d $dir ]; then
694 echo "Directory $dir doesn't exist in tree."
695 return
696 fi
697 cd $dir
698 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
699 echo "Starting branch..."
700 repo start tmprebase .
701 echo "Bringing it up to date..."
702 repo sync .
703 echo "Fetching change..."
Dan Pasanen03447712016-12-19 11:22:55 -0600704 git fetch "http://review.lineageos.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Michael Bestas3952f6c2016-08-26 01:12:08 +0300705 if [ "$?" != "0" ]; then
706 echo "Error cherry-picking. Not uploading!"
707 return
708 fi
709 echo "Uploading..."
710 repo upload .
711 echo "Cleaning up..."
712 repo abandon tmprebase .
713 cd $pwd
714}
715
716function mka() {
Luca Stefani085af722017-08-17 20:34:44 +0200717 m -j "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300718}
719
720function cmka() {
721 if [ ! -z "$1" ]; then
722 for i in "$@"; do
723 case $i in
724 bacon|otapackage|systemimage)
725 mka installclean
726 mka $i
727 ;;
728 *)
729 mka clean-$i
730 mka $i
731 ;;
732 esac
733 done
734 else
735 mka clean
736 mka
737 fi
738}
739
Michael Bestas3952f6c2016-08-26 01:12:08 +0300740function repolastsync() {
741 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
742 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
743 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
744 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
745}
746
747function reposync() {
Luca Stefani085af722017-08-17 20:34:44 +0200748 repo sync -j 4 "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300749}
750
751function repodiff() {
752 if [ -z "$*" ]; then
753 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
754 return
755 fi
756 diffopts=$* repo forall -c \
757 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
758}
759
760# Return success if adb is up and not in recovery
761function _adb_connected {
762 {
763 if [[ "$(adb get-state)" == device &&
Marc K299137f2016-10-20 10:27:10 +0200764 "$(adb shell test -e /sbin/recovery; echo $?)" != 0 ]]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300765 then
766 return 0
767 fi
768 } 2>/dev/null
769
770 return 1
771};
772
773# Credit for color strip sed: http://goo.gl/BoIcm
774function dopush()
775{
776 local func=$1
777 shift
778
779 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
780 if ! _adb_connected; then
781 echo "No device is online. Waiting for one..."
782 echo "Please connect USB and/or enable USB debugging"
783 until _adb_connected; do
784 sleep 1
785 done
786 echo "Device Found."
787 fi
788
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200789 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD") || [ "$FORCE_PUSH" = "true" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300790 then
791 # retrieve IP and PORT info if we're using a TCP connection
Marc K2be9cac2016-10-20 10:27:35 +0200792 TCPIPPORT=$(adb devices \
793 | 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 +0300794 | head -1 | awk '{print $1}')
795 adb root &> /dev/null
796 sleep 0.3
797 if [ -n "$TCPIPPORT" ]
798 then
799 # adb root just killed our connection
800 # so reconnect...
801 adb connect "$TCPIPPORT"
802 fi
803 adb wait-for-device &> /dev/null
804 sleep 0.3
805 adb remount &> /dev/null
806
807 mkdir -p $OUT
808 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
809 ret=$?;
810 if [ $ret -ne 0 ]; then
811 rm -f $OUT/.log;return $ret
812 fi
813
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500814 is_gnu_sed=`sed --version | head -1 | grep -c GNU`
815
Michael Bestas3952f6c2016-08-26 01:12:08 +0300816 # Install: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500817 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200818 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}\] +//' \
819 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300820 else
Marc K97b035d2016-10-20 10:27:44 +0200821 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}\] +//" \
822 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300823 fi
824
825 # Copy: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500826 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200827 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}\] +//' \
828 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300829 else
Marc K97b035d2016-10-20 10:27:44 +0200830 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}\] +//' \
831 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300832 fi
833
834 # If any files are going to /data, push an octal file permissions reader to device
835 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
836 CHKPERM="/data/local/tmp/chkfileperm.sh"
837(
838cat <<'EOF'
839#!/system/xbin/sh
840FILE=$@
841if [ -e $FILE ]; then
842 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
843fi
844EOF
845) > $OUT/.chkfileperm.sh
846 echo "Pushing file permissions checker to device"
847 adb push $OUT/.chkfileperm.sh $CHKPERM
848 adb shell chmod 755 $CHKPERM
849 rm -f $OUT/.chkfileperm.sh
850 fi
851
852 stop_n_start=false
853 for FILE in $(echo $LOC | tr " " "\n"); do
854 # Make sure file is in $OUT/system or $OUT/data
855 case $FILE in
856 $OUT/system/*|$OUT/data/*)
857 # Get target file name (i.e. /system/bin/adb)
858 TARGET=$(echo $FILE | sed "s#$OUT##")
859 ;;
860 *) continue ;;
861 esac
862
863 case $TARGET in
864 /data/*)
865 # fs_config only sets permissions and se labels for files pushed to /system
866 if [ -n "$CHKPERM" ]; then
867 OLDPERM=$(adb shell $CHKPERM $TARGET)
868 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
869 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
870 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
871 fi
872 echo "Pushing: $TARGET"
873 adb push $FILE $TARGET
874 if [ -n "$OLDPERM" ]; then
875 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
876 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
877 adb shell chmod "$OLDPERM" $TARGET
878 else
879 echo "$TARGET did not exist previously, you should set file permissions manually"
880 fi
881 adb shell restorecon "$TARGET"
882 ;;
883 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
884 # Only need to stop services once
885 if ! $stop_n_start; then
886 adb shell stop
887 stop_n_start=true
888 fi
889 echo "Pushing: $TARGET"
890 adb push $FILE $TARGET
891 ;;
892 *)
893 echo "Pushing: $TARGET"
894 adb push $FILE $TARGET
895 ;;
896 esac
897 done
898 if [ -n "$CHKPERM" ]; then
899 adb shell rm $CHKPERM
900 fi
901 if $stop_n_start; then
902 adb shell start
903 fi
904 rm -f $OUT/.log
905 return 0
906 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200907 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300908 fi
909}
910
911alias mmp='dopush mm'
912alias mmmp='dopush mmm'
913alias mmap='dopush mma'
Zhao Wei Liew64fc5ae2016-12-10 16:48:27 +0800914alias mmmap='dopush mmma'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300915alias mkap='dopush mka'
916alias cmkap='dopush cmka'
917
918function repopick() {
919 T=$(gettop)
Dan Pasanen91f76202017-07-06 08:21:30 -0500920 $T/vendor/lineage/build/tools/repopick.py $@
Michael Bestas3952f6c2016-08-26 01:12:08 +0300921}
922
923function fixup_common_out_dir() {
924 common_out_dir=$(get_build_var OUT_DIR)/target/common
925 target_device=$(get_build_var TARGET_DEVICE)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200926 if [ ! -z $LINEAGE_FIXUP_COMMON_OUT ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300927 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
928 mv ${common_out_dir} ${common_out_dir}-${target_device}
929 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
930 else
931 [ -L ${common_out_dir} ] && rm ${common_out_dir}
932 mkdir -p ${common_out_dir}-${target_device}
933 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
934 fi
935 else
936 [ -L ${common_out_dir} ] && rm ${common_out_dir}
937 mkdir -p ${common_out_dir}
938 fi
939}
Steve Kondik26e669b2016-11-04 12:05:19 -0700940
941# Enable SD-LLVM if available
942if [ -d $(gettop)/prebuilts/snapdragon-llvm/toolchains ]; then
Alexander Martinz1bbf3e92016-11-05 15:27:43 +0100943 case `uname -s` in
944 Darwin)
945 # Darwin is not supported yet
946 ;;
947 *)
948 export SDCLANG=true
949 export SDCLANG_PATH=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_3.8/prebuilt/linux-x86_64/bin
950 export SDCLANG_LTO_DEFS=$(gettop)/device/qcom/common/sdllvm-lto-defs.mk
951 ;;
952 esac
Steve Kondik26e669b2016-11-04 12:05:19 -0700953fi
Luca Stefani84fda602016-11-24 14:16:33 +0100954
955# Android specific JACK args
956if [ -n "$JACK_SERVER_VM_ARGUMENTS" ] && [ -z "$ANDROID_JACK_VM_ARGS" ]; then
957 export ANDROID_JACK_VM_ARGS=$JACK_SERVER_VM_ARGUMENTS
958fi