blob: c854a96dc3532791d143177535eb578249642f03 [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.
Michael Bestasa504aa42018-08-10 22:51:37 +030013- githubremote: Add git remote for LineageOS Github.
Michael Bestas3952f6c2016-08-26 01:12:08 +030014- mka: Builds using SCHED_BATCH on all processors.
15- mkap: Builds the module(s) using mka and pushes them to the device.
16- cmka: Cleans and builds using mka.
17- repodiff: Diff 2 different branches or tags within the same repo
18- repolastsync: Prints date and time of last repo sync.
19- reposync: Parallel repo sync using ionice and SCHED_BATCH.
20- repopick: Utility to fetch changes from Gerrit.
21- installboot: Installs a boot.img to the connected device.
22- installrecovery: Installs a recovery.img to the connected device.
23EOF
24}
25
Luca Stefani076c27b2017-08-17 20:30:00 +020026function mk_timer()
27{
28 local start_time=$(date +"%s")
29 $@
30 local ret=$?
31 local end_time=$(date +"%s")
32 local tdiff=$(($end_time-$start_time))
33 local hours=$(($tdiff / 3600 ))
34 local mins=$((($tdiff % 3600) / 60))
35 local secs=$(($tdiff % 60))
36 local ncolors=$(tput colors 2>/dev/null)
37 echo
38 if [ $ret -eq 0 ] ; then
39 echo -n "#### make completed successfully "
40 else
41 echo -n "#### make failed to build some targets "
42 fi
43 if [ $hours -gt 0 ] ; then
44 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
45 elif [ $mins -gt 0 ] ; then
46 printf "(%02g:%02g (mm:ss))" $mins $secs
47 elif [ $secs -gt 0 ] ; then
48 printf "(%s seconds)" $secs
49 fi
50 echo " ####"
51 echo
52 return $ret
53}
54
Michael Bestas3952f6c2016-08-26 01:12:08 +030055function brunch()
56{
57 breakfast $*
58 if [ $? -eq 0 ]; then
59 mka bacon
60 else
61 echo "No such item in brunch menu. Try 'breakfast'"
62 return 1
63 fi
64 return $?
65}
66
67function breakfast()
68{
69 target=$1
70 local variant=$2
Luca Stefani5c60e4f2017-08-17 19:28:48 +020071 LINEAGE_DEVICES_ONLY="true"
Michael Bestas3952f6c2016-08-26 01:12:08 +030072 unset LUNCH_MENU_CHOICES
73 add_lunch_combo full-eng
Dan Pasanen91f76202017-07-06 08:21:30 -050074 for f in `/bin/ls vendor/lineage/vendorsetup.sh 2> /dev/null`
Michael Bestas3952f6c2016-08-26 01:12:08 +030075 do
76 echo "including $f"
77 . $f
78 done
79 unset f
80
81 if [ $# -eq 0 ]; then
82 # No arguments, so let's have the full menu
83 lunch
84 else
85 echo "z$target" | grep -q "-"
86 if [ $? -eq 0 ]; then
87 # A buildtype was specified, assume a full device name
88 lunch $target
89 else
Simon Shields63ce74b2016-12-28 11:33:29 +110090 # This is probably just the Lineage model name
Michael Bestas3952f6c2016-08-26 01:12:08 +030091 if [ -z "$variant" ]; then
92 variant="userdebug"
93 fi
Matt Mowered8c2482017-01-02 02:26:01 -060094
Luca Stefani5c60e4f2017-08-17 19:28:48 +020095 lunch lineage_$target-$variant
Michael Bestas3952f6c2016-08-26 01:12:08 +030096 fi
97 fi
98 return $?
99}
100
101alias bib=breakfast
102
103function eat()
104{
105 if [ "$OUT" ] ; then
Paul Keithb11d5732017-11-02 20:31:33 +0100106 ZIPPATH=`ls -tr "$OUT"/lineage-*.zip | tail -1`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300107 if [ ! -f $ZIPPATH ] ; then
108 echo "Nothing to eat"
109 return 1
110 fi
111 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Adrian DC6393a3f2018-06-16 19:56:43 +0200112 if [ $(adb get-state) != device -a $(adb shell 'test -e /sbin/recovery 2> /dev/null; echo $?') != 0 ] ; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300113 echo "No device is online. Waiting for one..."
114 echo "Please connect USB and/or enable USB debugging"
Adrian DC6393a3f2018-06-16 19:56:43 +0200115 until [ $(adb get-state) = device -o $(adb shell 'test -e /sbin/recovery 2> /dev/null; echo $?') = 0 ];do
Michael Bestas3952f6c2016-08-26 01:12:08 +0300116 sleep 1
117 done
118 echo "Device Found.."
119 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200120 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD"); then
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800121 # if adbd isn't root we can't write to /cache/recovery/
122 adb root
123 sleep 1
124 adb wait-for-device
125 cat << EOF > /tmp/command
Michael Bestas3952f6c2016-08-26 01:12:08 +0300126--sideload_auto_reboot
127EOF
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800128 if adb push /tmp/command /cache/recovery/ ; then
129 echo "Rebooting into recovery for sideload installation"
130 adb reboot recovery
131 adb wait-for-sideload
132 adb sideload $ZIPPATH
133 fi
134 rm /tmp/command
135 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200136 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300137 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800138 return $?
Michael Bestas3952f6c2016-08-26 01:12:08 +0300139 else
140 echo "Nothing to eat"
141 return 1
142 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300143}
144
145function omnom()
146{
147 brunch $*
148 eat
149}
150
151function cout()
152{
153 if [ "$OUT" ]; then
154 cd $OUT
155 else
156 echo "Couldn't locate out directory. Try setting OUT."
157 fi
158}
159
160function dddclient()
161{
162 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
163 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
164 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
165 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
166 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
167 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
168 local ARCH=$(get_build_var TARGET_ARCH)
169 local GDB
170 case "$ARCH" in
171 arm) GDB=arm-linux-androideabi-gdb;;
172 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
173 mips|mips64) GDB=mips64el-linux-android-gdb;;
174 x86) GDB=x86_64-linux-android-gdb;;
175 x86_64) GDB=x86_64-linux-android-gdb;;
176 *) echo "Unknown arch $ARCH"; return 1;;
177 esac
178
179 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
180 local EXE="$1"
181 if [ "$EXE" ] ; then
182 EXE=$1
183 if [[ $EXE =~ ^[^/].* ]] ; then
184 EXE="system/bin/"$EXE
185 fi
186 else
187 EXE="app_process"
188 fi
189
190 local PORT="$2"
191 if [ "$PORT" ] ; then
192 PORT=$2
193 else
194 PORT=":5039"
195 fi
196
197 local PID="$3"
198 if [ "$PID" ] ; then
199 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
200 PID=`pid $3`
201 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
202 # that likely didn't work because of returning multiple processes
203 # try again, filtering by root processes (don't contain colon)
204 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
205 if [[ ! "$PID" =~ ^[0-9]+$ ]]
206 then
207 echo "Couldn't resolve '$3' to single PID"
208 return 1
209 else
210 echo ""
211 echo "WARNING: multiple processes matching '$3' observed, using root process"
212 echo ""
213 fi
214 fi
215 fi
216 adb forward "tcp$PORT" "tcp$PORT"
217 local USE64BIT="$(is64bit $PID)"
218 adb shell gdbserver$USE64BIT $PORT --attach $PID &
219 sleep 2
220 else
221 echo ""
222 echo "If you haven't done so already, do this first on the device:"
223 echo " gdbserver $PORT /system/bin/$EXE"
224 echo " or"
225 echo " gdbserver $PORT --attach <PID>"
226 echo ""
227 fi
228
229 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
230 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
231
232 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
233 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"
234 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
235 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
236 # Enable special debugging for ART processes.
237 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
238 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
239 fi
240 echo >>"$OUT_ROOT/gdbclient.cmds" ""
241
242 local WHICH_GDB=
243 # 64-bit exe found
244 if [ "$USE64BIT" != "" ] ; then
245 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
246 # 32-bit exe / 32-bit platform
247 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
248 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
249 # 32-bit exe / 64-bit platform
250 else
251 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
252 fi
253
254 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
255 else
256 echo "Unable to determine build system output dir."
257 fi
258}
259
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200260function lineageremote()
Michael Bestas3952f6c2016-08-26 01:12:08 +0300261{
262 if ! git rev-parse --git-dir &> /dev/null
263 then
264 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
265 return 1
266 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200267 git remote rm lineage 2> /dev/null
Michael Bestas9e6bde52018-04-02 19:54:45 +0300268 local REMOTE=$(git config --get remote.github.projectname)
269 local LINEAGE="true"
270 if [ -z "$REMOTE" ]
Michael Bestasaf3532b2017-08-23 17:40:40 +0300271 then
Michael Bestas9e6bde52018-04-02 19:54:45 +0300272 REMOTE=$(git config --get remote.aosp.projectname)
273 LINEAGE="false"
Michael Bestasaf3532b2017-08-23 17:40:40 +0300274 fi
Michael Bestas9e6bde52018-04-02 19:54:45 +0300275 if [ -z "$REMOTE" ]
276 then
277 REMOTE=$(git config --get remote.caf.projectname)
278 LINEAGE="false"
279 fi
280
281 if [ $LINEAGE = "false" ]
282 then
283 local PROJECT=$(echo $REMOTE | sed -e "s#platform/#android/#g; s#/#_#g")
284 local PFX="LineageOS/"
285 else
286 local PROJECT=$REMOTE
287 fi
288
Michael Bestasad3a5702017-08-24 00:00:48 +0300289 local LINEAGE_USER=$(git config --get review.review.lineageos.org.username)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200290 if [ -z "$LINEAGE_USER" ]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300291 then
Michael Bestas9e6bde52018-04-02 19:54:45 +0300292 git remote add lineage ssh://review.lineageos.org:29418/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300293 else
Michael Bestas9e6bde52018-04-02 19:54:45 +0300294 git remote add lineage ssh://$LINEAGE_USER@review.lineageos.org:29418/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300295 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200296 echo "Remote 'lineage' created"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300297}
298
299function aospremote()
300{
301 if ! git rev-parse --git-dir &> /dev/null
302 then
303 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
304 return 1
305 fi
306 git remote rm aosp 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300307 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400308 # Google moved the repo location in Oreo
309 if [ $PROJECT = "build/make" ]
310 then
311 PROJECT="build"
312 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300313 if (echo $PROJECT | grep -qv "^device")
314 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300315 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300316 fi
317 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
318 echo "Remote 'aosp' created"
319}
320
321function cafremote()
322{
323 if ! git rev-parse --git-dir &> /dev/null
324 then
325 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
326 return 1
327 fi
328 git remote rm caf 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300329 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400330 # Google moved the repo location in Oreo
331 if [ $PROJECT = "build/make" ]
332 then
333 PROJECT="build"
334 fi
Rashed Abdel-Tawabb52c7082017-12-24 22:40:31 +0200335 if [[ $PROJECT =~ "qcom/opensource" ]];
336 then
337 PROJECT=$(echo $PROJECT | sed -e "s#qcom\/opensource#qcom-opensource#")
338 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300339 if (echo $PROJECT | grep -qv "^device")
340 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300341 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300342 fi
Zhao Wei Liewfb4b8c52017-01-08 09:03:01 +0800343 git remote add caf https://source.codeaurora.org/quic/la/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300344 echo "Remote 'caf' created"
345}
346
Michael Bestasa504aa42018-08-10 22:51:37 +0300347function githubremote()
348{
349 if ! git rev-parse --git-dir &> /dev/null
350 then
351 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
352 return 1
353 fi
354 git remote rm github 2> /dev/null
355 local REMOTE=$(git config --get remote.aosp.projectname)
356
357 if [ -z "$REMOTE" ]
358 then
359 REMOTE=$(git config --get remote.caf.projectname)
360 fi
361
362 local PROJECT=$(echo $REMOTE | sed -e "s#platform/#android/#g; s#/#_#g")
363
364 git remote add github https://github.com/LineageOS/$PROJECT
365 echo "Remote 'github' created"
366}
367
Michael Bestas3952f6c2016-08-26 01:12:08 +0300368function installboot()
369{
370 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
371 then
372 echo "No recovery.fstab found. Build recovery first."
373 return 1
374 fi
375 if [ ! -e "$OUT/boot.img" ];
376 then
377 echo "No boot.img found. Run make bootimage first."
378 return 1
379 fi
380 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
381 if [ -z "$PARTITION" ];
382 then
383 # Try for RECOVERY_FSTAB_VERSION = 2
384 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
385 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
386 if [ -z "$PARTITION" ];
387 then
388 echo "Unable to determine boot partition."
389 return 1
390 fi
391 fi
392 adb start-server
393 adb wait-for-online
394 adb root
395 sleep 1
396 adb wait-for-online shell mount /system 2>&1 > /dev/null
397 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200398 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300399 then
400 adb push $OUT/boot.img /cache/
Ashwin Rameshb0ea62a2017-08-13 12:00:15 +0530401 if [ -e "$OUT/system/lib/modules/*" ];
402 then
403 for i in $OUT/system/lib/modules/*;
404 do
405 adb push $i /system/lib/modules/
406 done
407 adb shell chmod 644 /system/lib/modules/*
408 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300409 adb shell dd if=/cache/boot.img of=$PARTITION
Michael W2e203942018-01-14 18:46:02 +0100410 adb shell rm -rf /cache/boot.img
Michael Bestas3952f6c2016-08-26 01:12:08 +0300411 echo "Installation complete."
412 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200413 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300414 fi
415}
416
417function installrecovery()
418{
419 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
420 then
421 echo "No recovery.fstab found. Build recovery first."
422 return 1
423 fi
424 if [ ! -e "$OUT/recovery.img" ];
425 then
426 echo "No recovery.img found. Run make recoveryimage first."
427 return 1
428 fi
429 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
430 if [ -z "$PARTITION" ];
431 then
432 # Try for RECOVERY_FSTAB_VERSION = 2
433 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
434 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
435 if [ -z "$PARTITION" ];
436 then
437 echo "Unable to determine recovery partition."
438 return 1
439 fi
440 fi
441 adb start-server
442 adb wait-for-online
443 adb root
444 sleep 1
445 adb wait-for-online shell mount /system 2>&1 >> /dev/null
446 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200447 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300448 then
449 adb push $OUT/recovery.img /cache/
450 adb shell dd if=/cache/recovery.img of=$PARTITION
Michael W2e203942018-01-14 18:46:02 +0100451 adb shell rm -rf /cache/recovery.img
Michael Bestas3952f6c2016-08-26 01:12:08 +0300452 echo "Installation complete."
453 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200454 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300455 fi
456}
457
458function makerecipe() {
459 if [ -z "$1" ]
460 then
461 echo "No branch name provided."
462 return 1
463 fi
464 cd android
465 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
466 git commit -a -m "$1"
467 cd ..
468
469 repo forall -c '
470
471 if [ "$REPO_REMOTE" = "github" ]
472 then
473 pwd
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200474 lineageremote
475 git push lineage HEAD:refs/heads/'$1'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300476 fi
477 '
478}
479
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200480function lineagegerrit() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300481 if [ "$(__detect_shell)" = "zsh" ]; then
482 # zsh does not define FUNCNAME, derive from funcstack
483 local FUNCNAME=$funcstack[1]
484 fi
485
486 if [ $# -eq 0 ]; then
487 $FUNCNAME help
488 return 1
489 fi
Dan Pasanen03447712016-12-19 11:22:55 -0600490 local user=`git config --get review.review.lineageos.org.username`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300491 local review=`git config --get remote.github.review`
492 local project=`git config --get remote.github.projectname`
493 local command=$1
494 shift
495 case $command in
496 help)
497 if [ $# -eq 0 ]; then
498 cat <<EOF
499Usage:
500 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
501
502Commands:
503 fetch Just fetch the change as FETCH_HEAD
504 help Show this help, or for a specific command
505 pull Pull a change into current branch
506 push Push HEAD or a local branch to Gerrit for a specific branch
507
508Any other Git commands that support refname would work as:
509 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
510
511See '$FUNCNAME help COMMAND' for more information on a specific command.
512
513Example:
514 $FUNCNAME checkout -b topic 1234/5
515works as:
516 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
517 && git checkout -b topic FETCH_HEAD
518will checkout a new branch 'topic' base on patch-set 5 of change 1234.
519Patch-set 1 will be fetched if omitted.
520EOF
521 return
522 fi
523 case $1 in
524 __cmg_*) echo "For internal use only." ;;
525 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200526 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300527 echo "'$FUNCNAME $1' is deprecated."
528 fi
529 ;;
530 help) $FUNCNAME help ;;
531 fetch|pull) cat <<EOF
532usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
533
534works as:
535 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
536 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
537
538Example:
539 $FUNCNAME $1 1234
540will $1 patch-set 1 of change 1234
541EOF
542 ;;
543 push) cat <<EOF
544usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
545
546works as:
547 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
548 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
549
550Example:
551 $FUNCNAME push fix6789:gingerbread
552will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
553HEAD will be pushed from local if omitted.
554EOF
555 ;;
556 *)
557 $FUNCNAME __cmg_err_not_supported $1 && return
558 cat <<EOF
559usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
560
561works as:
562 git fetch http://DOMAIN/p/PROJECT \\
563 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
564 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
565EOF
566 ;;
567 esac
568 ;;
569 __cmg_get_ref)
570 $FUNCNAME __cmg_err_no_arg $command $# && return 1
571 local change_id patchset_id hash
572 case $1 in
573 */*)
574 change_id=${1%%/*}
575 patchset_id=${1#*/}
576 ;;
577 *)
578 change_id=$1
579 patchset_id=1
580 ;;
581 esac
582 hash=$(($change_id % 100))
583 case $hash in
584 [0-9]) hash="0$hash" ;;
585 esac
586 echo "refs/changes/$hash/$change_id/$patchset_id"
587 ;;
588 fetch|pull)
589 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
590 $FUNCNAME __cmg_err_not_repo && return 1
591 local change=$1
592 shift
593 git $command $@ http://$review/p/$project \
594 $($FUNCNAME __cmg_get_ref $change) || return 1
595 ;;
596 push)
597 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
598 $FUNCNAME __cmg_err_not_repo && return 1
599 if [ -z "$user" ]; then
600 echo >&2 "Gerrit username not found."
601 return 1
602 fi
603 local local_branch remote_branch
604 case $1 in
605 *:*)
606 local_branch=${1%:*}
607 remote_branch=${1##*:}
608 ;;
609 *)
610 local_branch=HEAD
611 remote_branch=$1
612 ;;
613 esac
614 shift
615 git push $@ ssh://$user@$review:29418/$project \
616 $local_branch:refs/for/$remote_branch || return 1
617 ;;
618 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200619 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300620 echo >&2 "'$FUNCNAME $command' is deprecated."
621 fi
622 ;;
623 __cmg_err_no_arg)
624 if [ $# -lt 2 ]; then
625 echo >&2 "'$FUNCNAME $command' missing argument."
626 elif [ $2 -eq 0 ]; then
627 if [ -n "$3" ]; then
628 $FUNCNAME help $1
629 else
630 echo >&2 "'$FUNCNAME $1' missing argument."
631 fi
632 else
633 return 1
634 fi
635 ;;
636 __cmg_err_not_repo)
637 if [ -z "$review" -o -z "$project" ]; then
638 echo >&2 "Not currently in any reviewable repository."
639 else
640 return 1
641 fi
642 ;;
643 __cmg_err_not_supported)
644 $FUNCNAME __cmg_err_no_arg $command $# && return
645 case $1 in
646 #TODO: filter more git commands that don't use refname
647 init|add|rm|mv|status|clone|remote|bisect|config|stash)
648 echo >&2 "'$FUNCNAME $1' is not supported."
649 ;;
650 *) return 1 ;;
651 esac
652 ;;
653 #TODO: other special cases?
654 *)
655 $FUNCNAME __cmg_err_not_supported $command && return 1
656 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
657 $FUNCNAME __cmg_err_not_repo && return 1
658 local args="$@"
659 local change pre_args refs_arg post_args
660 case "$args" in
661 *--\ *)
662 pre_args=${args%%-- *}
663 post_args="-- ${args#*-- }"
664 ;;
665 *) pre_args="$args" ;;
666 esac
667 args=($pre_args)
668 pre_args=
669 if [ ${#args[@]} -gt 0 ]; then
670 change=${args[${#args[@]}-1]}
671 fi
672 if [ ${#args[@]} -gt 1 ]; then
673 pre_args=${args[0]}
674 for ((i=1; i<${#args[@]}-1; i++)); do
675 pre_args="$pre_args ${args[$i]}"
676 done
677 fi
678 while ((1)); do
679 case $change in
680 ""|--)
681 $FUNCNAME help $command
682 return 1
683 ;;
684 *@*)
685 if [ -z "$refs_arg" ]; then
686 refs_arg="@${change#*@}"
687 change=${change%%@*}
688 fi
689 ;;
690 *~*)
691 if [ -z "$refs_arg" ]; then
692 refs_arg="~${change#*~}"
693 change=${change%%~*}
694 fi
695 ;;
696 *^*)
697 if [ -z "$refs_arg" ]; then
698 refs_arg="^${change#*^}"
699 change=${change%%^*}
700 fi
701 ;;
702 *:*)
703 if [ -z "$refs_arg" ]; then
704 refs_arg=":${change#*:}"
705 change=${change%%:*}
706 fi
707 ;;
708 *) break ;;
709 esac
710 done
711 $FUNCNAME fetch $change \
712 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
713 || return 1
714 ;;
715 esac
716}
717
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200718function lineagerebase() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300719 local repo=$1
720 local refs=$2
721 local pwd="$(pwd)"
722 local dir="$(gettop)/$repo"
723
724 if [ -z $repo ] || [ -z $refs ]; then
Dan Pasanen03447712016-12-19 11:22:55 -0600725 echo "LineageOS Gerrit Rebase Usage: "
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200726 echo " lineagerebase <path to project> <patch IDs on Gerrit>"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300727 echo " The patch IDs appear on the Gerrit commands that are offered."
728 echo " They consist on a series of numbers and slashes, after the text"
729 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
730 echo ""
731 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
732 echo ""
733 return
734 fi
735
736 if [ ! -d $dir ]; then
737 echo "Directory $dir doesn't exist in tree."
738 return
739 fi
740 cd $dir
741 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
742 echo "Starting branch..."
743 repo start tmprebase .
744 echo "Bringing it up to date..."
745 repo sync .
746 echo "Fetching change..."
Dan Pasanen03447712016-12-19 11:22:55 -0600747 git fetch "http://review.lineageos.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Michael Bestas3952f6c2016-08-26 01:12:08 +0300748 if [ "$?" != "0" ]; then
749 echo "Error cherry-picking. Not uploading!"
750 return
751 fi
752 echo "Uploading..."
753 repo upload .
754 echo "Cleaning up..."
755 repo abandon tmprebase .
756 cd $pwd
757}
758
759function mka() {
Luca Stefani085af722017-08-17 20:34:44 +0200760 m -j "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300761}
762
763function cmka() {
764 if [ ! -z "$1" ]; then
765 for i in "$@"; do
766 case $i in
767 bacon|otapackage|systemimage)
768 mka installclean
769 mka $i
770 ;;
771 *)
772 mka clean-$i
773 mka $i
774 ;;
775 esac
776 done
777 else
778 mka clean
779 mka
780 fi
781}
782
Michael Bestas3952f6c2016-08-26 01:12:08 +0300783function repolastsync() {
784 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
785 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
786 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
787 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
788}
789
790function reposync() {
Luca Stefani085af722017-08-17 20:34:44 +0200791 repo sync -j 4 "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300792}
793
794function repodiff() {
795 if [ -z "$*" ]; then
796 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
797 return
798 fi
799 diffopts=$* repo forall -c \
800 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
801}
802
803# Return success if adb is up and not in recovery
804function _adb_connected {
805 {
806 if [[ "$(adb get-state)" == device &&
Adrian DC6393a3f2018-06-16 19:56:43 +0200807 "$(adb shell 'test -e /sbin/recovery; echo $?')" != 0 ]]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300808 then
809 return 0
810 fi
811 } 2>/dev/null
812
813 return 1
814};
815
816# Credit for color strip sed: http://goo.gl/BoIcm
817function dopush()
818{
819 local func=$1
820 shift
821
822 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
823 if ! _adb_connected; then
824 echo "No device is online. Waiting for one..."
825 echo "Please connect USB and/or enable USB debugging"
826 until _adb_connected; do
827 sleep 1
828 done
829 echo "Device Found."
830 fi
831
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200832 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD") || [ "$FORCE_PUSH" = "true" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300833 then
834 # retrieve IP and PORT info if we're using a TCP connection
Marc K2be9cac2016-10-20 10:27:35 +0200835 TCPIPPORT=$(adb devices \
836 | 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 +0300837 | head -1 | awk '{print $1}')
838 adb root &> /dev/null
839 sleep 0.3
840 if [ -n "$TCPIPPORT" ]
841 then
842 # adb root just killed our connection
843 # so reconnect...
844 adb connect "$TCPIPPORT"
845 fi
846 adb wait-for-device &> /dev/null
847 sleep 0.3
848 adb remount &> /dev/null
849
850 mkdir -p $OUT
851 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
852 ret=$?;
853 if [ $ret -ne 0 ]; then
854 rm -f $OUT/.log;return $ret
855 fi
856
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500857 is_gnu_sed=`sed --version | head -1 | grep -c GNU`
858
Michael Bestas3952f6c2016-08-26 01:12:08 +0300859 # Install: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500860 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200861 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}\] +//' \
862 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300863 else
Marc K97b035d2016-10-20 10:27:44 +0200864 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}\] +//" \
865 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300866 fi
867
868 # Copy: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500869 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200870 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}\] +//' \
871 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300872 else
Marc K97b035d2016-10-20 10:27:44 +0200873 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}\] +//' \
874 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300875 fi
876
877 # If any files are going to /data, push an octal file permissions reader to device
878 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
879 CHKPERM="/data/local/tmp/chkfileperm.sh"
880(
881cat <<'EOF'
882#!/system/xbin/sh
883FILE=$@
884if [ -e $FILE ]; then
885 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
886fi
887EOF
888) > $OUT/.chkfileperm.sh
889 echo "Pushing file permissions checker to device"
890 adb push $OUT/.chkfileperm.sh $CHKPERM
891 adb shell chmod 755 $CHKPERM
892 rm -f $OUT/.chkfileperm.sh
893 fi
894
895 stop_n_start=false
Adrian DCa8e06d32018-06-17 00:15:00 +0200896 for TARGET in $(echo $LOC | tr " " "\n" | sed "s#.*$OUT##" | sort | uniq); do
Michael Bestas3952f6c2016-08-26 01:12:08 +0300897 # Make sure file is in $OUT/system or $OUT/data
Adrian DCa8e06d32018-06-17 00:15:00 +0200898 case $TARGET in
899 /system/*|/data/*)
900 # Get out file from target (i.e. /system/bin/adb)
901 FILE=$OUT$TARGET
Michael Bestas3952f6c2016-08-26 01:12:08 +0300902 ;;
903 *) continue ;;
904 esac
905
906 case $TARGET in
907 /data/*)
908 # fs_config only sets permissions and se labels for files pushed to /system
909 if [ -n "$CHKPERM" ]; then
910 OLDPERM=$(adb shell $CHKPERM $TARGET)
911 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
912 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
913 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
914 fi
915 echo "Pushing: $TARGET"
916 adb push $FILE $TARGET
917 if [ -n "$OLDPERM" ]; then
918 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
919 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
920 adb shell chmod "$OLDPERM" $TARGET
921 else
922 echo "$TARGET did not exist previously, you should set file permissions manually"
923 fi
924 adb shell restorecon "$TARGET"
925 ;;
926 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
927 # Only need to stop services once
928 if ! $stop_n_start; then
929 adb shell stop
930 stop_n_start=true
931 fi
932 echo "Pushing: $TARGET"
933 adb push $FILE $TARGET
934 ;;
935 *)
936 echo "Pushing: $TARGET"
937 adb push $FILE $TARGET
938 ;;
939 esac
940 done
941 if [ -n "$CHKPERM" ]; then
942 adb shell rm $CHKPERM
943 fi
944 if $stop_n_start; then
945 adb shell start
946 fi
947 rm -f $OUT/.log
948 return 0
949 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200950 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300951 fi
952}
953
954alias mmp='dopush mm'
955alias mmmp='dopush mmm'
956alias mmap='dopush mma'
Zhao Wei Liew64fc5ae2016-12-10 16:48:27 +0800957alias mmmap='dopush mmma'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300958alias mkap='dopush mka'
959alias cmkap='dopush cmka'
960
961function repopick() {
962 T=$(gettop)
Dan Pasanen91f76202017-07-06 08:21:30 -0500963 $T/vendor/lineage/build/tools/repopick.py $@
Michael Bestas3952f6c2016-08-26 01:12:08 +0300964}
965
966function fixup_common_out_dir() {
967 common_out_dir=$(get_build_var OUT_DIR)/target/common
968 target_device=$(get_build_var TARGET_DEVICE)
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700969 common_target_out=common-${target_device}
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200970 if [ ! -z $LINEAGE_FIXUP_COMMON_OUT ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300971 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
972 mv ${common_out_dir} ${common_out_dir}-${target_device}
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700973 ln -s ${common_target_out} ${common_out_dir}
Michael Bestas3952f6c2016-08-26 01:12:08 +0300974 else
975 [ -L ${common_out_dir} ] && rm ${common_out_dir}
976 mkdir -p ${common_out_dir}-${target_device}
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700977 ln -s ${common_target_out} ${common_out_dir}
Michael Bestas3952f6c2016-08-26 01:12:08 +0300978 fi
979 else
980 [ -L ${common_out_dir} ] && rm ${common_out_dir}
981 mkdir -p ${common_out_dir}
982 fi
983}
Steve Kondik26e669b2016-11-04 12:05:19 -0700984
985# Enable SD-LLVM if available
986if [ -d $(gettop)/prebuilts/snapdragon-llvm/toolchains ]; then
Alexander Martinz1bbf3e92016-11-05 15:27:43 +0100987 case `uname -s` in
988 Darwin)
989 # Darwin is not supported yet
990 ;;
991 *)
992 export SDCLANG=true
nicknitewolf0bb793e2017-09-28 18:38:55 +0800993 export SDCLANG_PATH=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_4.0/prebuilt/linux-x86_64/bin
994 export SDCLANG_PATH_2=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_4.0/prebuilt/linux-x86_64/bin
Mandeep Singh Grangdc8df222016-05-05 13:09:41 -0700995 export SDCLANG_LTO_DEFS=$(gettop)/vendor/lineage/build/core/sdllvm-lto-defs.mk
Alexander Martinz1bbf3e92016-11-05 15:27:43 +0100996 ;;
997 esac
Steve Kondik26e669b2016-11-04 12:05:19 -0700998fi
Luca Stefani84fda602016-11-24 14:16:33 +0100999
1000# Android specific JACK args
1001if [ -n "$JACK_SERVER_VM_ARGUMENTS" ] && [ -z "$ANDROID_JACK_VM_ARGS" ]; then
1002 export ANDROID_JACK_VM_ARGS=$JACK_SERVER_VM_ARGUMENTS
1003fi