Advertisement
Guest User

envsetup.sh

a guest
Oct 7th, 2015
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 80.55 KB | None | 0 0
  1. function hmm() {
  2. cat <<EOF
  3. Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
  4. - lunch:   lunch <product_name>-<build_variant>
  5. - tapas:   tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
  6. - croot:   Changes directory to the top of the tree.
  7. - cout:    Changes directory to out.
  8. - m:       Makes from the top of the tree.
  9. - mm:      Builds all of the modules in the current directory, but not their dependencies.
  10. - mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
  11.            To limit the modules being built use the syntax: mmm dir/:target1,target2.
  12. - mma:     Builds all of the modules in the current directory, and their dependencies.
  13. - mmp:     Builds all of the modules in the current directory and pushes them to the device.
  14. - mmmp:    Builds all of the modules in the supplied directories and pushes them to the device.
  15. - mmma:    Builds all of the modules in the supplied directories, and their dependencies.
  16. - cgrep:   Greps on all local C/C++ files.
  17. - ggrep:   Greps on all local Gradle files.
  18. - jgrep:   Greps on all local Java files.
  19. - resgrep: Greps on all local res/*.xml files.
  20. - sgrep:   Greps on all local source files.
  21. - godir:   Go to the directory containing a file.
  22. - cmremote: Add git remote for CM Gerrit Review
  23. - cmgerrit: A Git wrapper that fetches/pushes patch from/to CM Gerrit Review
  24. - cmrebase: Rebase a Gerrit change and push it again
  25. - aospremote: Add git remote for matching AOSP repository
  26. - cafremote: Add git remote for matching CodeAurora repository.
  27. - mka:      Builds using SCHED_BATCH on all processors
  28. - mkap:     Builds the module(s) using mka and pushes them to the device.
  29. - cmka:     Cleans and builds using mka.
  30. - repolastsync: Prints date and time of last repo sync.
  31. - reposync: Parallel repo sync using ionice and SCHED_BATCH
  32. - repopick: Utility to fetch changes from Gerrit.
  33. - installboot: Installs a boot.img to the connected device.
  34. - installrecovery: Installs a recovery.img to the connected device.
  35.  
  36. Look at the source to view more functions. The complete list is:
  37. EOF
  38.     T=$(gettop)
  39.     for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
  40.       echo "$i"
  41.     done | column
  42. }
  43.  
  44. # Get the value of a build variable as an absolute path.
  45. function get_abs_build_var()
  46. {
  47.     T=$(gettop)
  48.     if [ ! "$T" ]; then
  49.         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
  50.         return
  51.     fi
  52.     (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core_ \
  53.       command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
  54. }
  55.  
  56. # Get the exact value of a build variable.
  57. function get_build_var()
  58. {
  59.     T=$(gettop)
  60.     if [ ! "$T" ]; then
  61.         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
  62.         return
  63.     fi
  64.     (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
  65.       command make --no-print-directory -f build/core/config.mk dumpvar-$1)
  66. }
  67.  
  68. # check to see if the supplied product is one we can build
  69. function check_product()
  70. {
  71.     T=$(gettop)
  72.     if [ ! "$T" ]; then
  73.         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
  74.         return
  75.     fi
  76.  
  77.     if (echo -n $1 | grep -q -e "^cm_") ; then
  78.        CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
  79.        export BUILD_NUMBER=$((date +%s%N ; echo $CM_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10)
  80.     else
  81.        CM_BUILD=
  82.     fi
  83.     export CM_BUILD
  84.  
  85.         TARGET_PRODUCT=$1 \
  86.         TARGET_BUILD_VARIANT= \
  87.         TARGET_BUILD_TYPE= \
  88.         TARGET_BUILD_APPS= \
  89.         get_build_var TARGET_DEVICE > /dev/null
  90.     # hide successful answers, but allow the errors to show
  91. }
  92.  
  93. VARIANT_CHOICES=(user userdebug eng)
  94.  
  95. # check to see if the supplied variant is valid
  96. function check_variant()
  97. {
  98.     for v in ${VARIANT_CHOICES[@]}
  99.     do
  100.         if [ "$v" = "$1" ]
  101.         then
  102.             return 0
  103.         fi
  104.     done
  105.     return 1
  106. }
  107.  
  108. function setpaths()
  109. {
  110.     T=$(gettop)
  111.     if [ ! "$T" ]; then
  112.         echo "Couldn't locate the top of the tree.  Try setting TOP."
  113.         return
  114.     fi
  115.  
  116.     ##################################################################
  117.     #                                                                #
  118.     #              Read me before you modify this code               #
  119.     #                                                                #
  120.     #   This function sets ANDROID_BUILD_PATHS to what it is adding  #
  121.     #   to PATH, and the next time it is run, it removes that from   #
  122.     #   PATH.  This is required so lunch can be run more than once   #
  123.     #   and still have working paths.                                #
  124.     #                                                                #
  125.     ##################################################################
  126.  
  127.     # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
  128.     # due to "C:\Program Files" being in the path.
  129.  
  130.     # out with the old
  131.     if [ -n "$ANDROID_BUILD_PATHS" ] ; then
  132.         export PATH=${PATH/$ANDROID_BUILD_PATHS/}
  133.     fi
  134.     if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
  135.         export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
  136.         # strip leading ':', if any
  137.         export PATH=${PATH/:%/}
  138.     fi
  139.  
  140.     # and in with the new
  141.     prebuiltdir=$(getprebuilt)
  142.     gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
  143.  
  144.     # defined in core/config.mk
  145.     targetgccversion=$(get_build_var TARGET_GCC_VERSION)
  146.     targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
  147.     export TARGET_GCC_VERSION=$targetgccversion
  148.  
  149.     # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
  150.     export ANDROID_TOOLCHAIN=
  151.     export ANDROID_TOOLCHAIN_2ND_ARCH=
  152.     local ARCH=$(get_build_var TARGET_ARCH)
  153.     case $ARCH in
  154.         x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
  155.             ;;
  156.         x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
  157.             ;;
  158.         arm) toolchaindir=arm/sm_arm-linux-androideabi-4.9.x/bin
  159.             ;;
  160.         arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
  161.                toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
  162.             ;;
  163.         mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
  164.             ;;
  165.         *)
  166.             echo "Can't find toolchain for unknown architecture: $ARCH"
  167.             toolchaindir=xxxxxxxxx
  168.             ;;
  169.     esac
  170.     if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
  171.         export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
  172.     fi
  173.  
  174.     if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
  175.         export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
  176.     fi
  177.  
  178.     unset ANDROID_KERNEL_TOOLCHAIN_PATH
  179.     case $ARCH in
  180.         arm)
  181.             # Legacy toolchain configuration used for ARM kernel compilation
  182.             toolchaindir=arm/sm_arm-eabi-4.9.x/bin
  183.             if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
  184.                  export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
  185.                  ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
  186.             fi
  187.             ;;
  188.         *)
  189.             # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
  190.             ;;
  191.     esac
  192.  
  193.     export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
  194.     export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_KERNEL_TOOLCHAIN_PATH$ANDROID_DEV_SCRIPTS:
  195.  
  196.     # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
  197.     # to ensure that the corresponding 'emulator' binaries are used.
  198.     case $(uname -s) in
  199.         Darwin)
  200.             ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
  201.             ;;
  202.         Linux)
  203.             ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
  204.             ;;
  205.         *)
  206.             ANDROID_EMULATOR_PREBUILTS=
  207.             ;;
  208.     esac
  209.     if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
  210.         ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
  211.         export ANDROID_EMULATOR_PREBUILTS
  212.     fi
  213.  
  214.     export PATH=$ANDROID_BUILD_PATHS$PATH
  215.  
  216.     unset ANDROID_JAVA_TOOLCHAIN
  217.     unset ANDROID_PRE_BUILD_PATHS
  218.     if [ -n "$JAVA_HOME" ]; then
  219.         export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
  220.         export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
  221.         export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
  222.     fi
  223.  
  224.     unset ANDROID_PRODUCT_OUT
  225.     export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
  226.     export OUT=$ANDROID_PRODUCT_OUT
  227.  
  228.     unset ANDROID_HOST_OUT
  229.     export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
  230.  
  231.     if [ -n "$ANDROID_CCACHE_DIR" ]; then
  232.         export CCACHE_DIR=$ANDROID_CCACHE_DIR
  233.     fi
  234.  
  235.     # needed for building linux on MacOS
  236.     # TODO: fix the path
  237.     #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
  238. }
  239.  
  240. function printconfig()
  241. {
  242.     T=$(gettop)
  243.     if [ ! "$T" ]; then
  244.         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
  245.         return
  246.     fi
  247.     get_build_var report_config
  248. }
  249.  
  250. function set_stuff_for_environment()
  251. {
  252.     settitle
  253.     set_java_home
  254.     setpaths
  255.     set_sequence_number
  256.  
  257.     # With this environment variable new GCC can apply colors to warnings/errors
  258.     export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
  259. }
  260.  
  261. function set_sequence_number()
  262. {
  263.     export BUILD_ENV_SEQUENCE_NUMBER=10
  264. }
  265.  
  266. function settitle()
  267. {
  268.     if [ "$STAY_OFF_MY_LAWN" = "" ]; then
  269.         local arch=$(gettargetarch)
  270.         local product=$TARGET_PRODUCT
  271.         local variant=$TARGET_BUILD_VARIANT
  272.         local apps=$TARGET_BUILD_APPS
  273.         if [ -z "$PROMPT_COMMAND"  ]; then
  274.             # No prompts
  275.             PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
  276.         elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
  277.             # Prompts exist, but no hardstatus
  278.             PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
  279.         fi
  280.         if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
  281.             PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
  282.         fi
  283.  
  284.         if [ -z "$apps" ]; then
  285.             ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
  286.         else
  287.             ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
  288.         fi
  289.         export ANDROID_PROMPT_PREFIX
  290.  
  291.         # Inject build data into hardstatus
  292.         export PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/\\033]0;\(.*\)\\007/\\033]0;$ANDROID_PROMPT_PREFIX \1\\007/g')"
  293.     fi
  294. }
  295.  
  296. function check_bash_version()
  297. {
  298.     # Keep us from trying to run in something that isn't bash.
  299.     if [ -z "${BASH_VERSION}" ]; then
  300.         return 1
  301.     fi
  302.  
  303.     # Keep us from trying to run in bash that's too old.
  304.     if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
  305.         return 2
  306.     fi
  307.  
  308.     return 0
  309. }
  310.  
  311. function choosetype()
  312. {
  313.     echo "Build type choices are:"
  314.     echo "     1. release"
  315.     echo "     2. debug"
  316.     echo
  317.  
  318.     local DEFAULT_NUM DEFAULT_VALUE
  319.     DEFAULT_NUM=1
  320.     DEFAULT_VALUE=release
  321.  
  322.     export TARGET_BUILD_TYPE=
  323.     local ANSWER
  324.     while [ -z $TARGET_BUILD_TYPE ]
  325.     do
  326.         echo -n "Which would you like? ["$DEFAULT_NUM"] "
  327.         if [ -z "$1" ] ; then
  328.             read ANSWER
  329.         else
  330.             echo $1
  331.             ANSWER=$1
  332.         fi
  333.         case $ANSWER in
  334.         "")
  335.             export TARGET_BUILD_TYPE=$DEFAULT_VALUE
  336.             ;;
  337.         1)
  338.             export TARGET_BUILD_TYPE=release
  339.             ;;
  340.         release)
  341.             export TARGET_BUILD_TYPE=release
  342.             ;;
  343.         2)
  344.             export TARGET_BUILD_TYPE=debug
  345.             ;;
  346.         debug)
  347.             export TARGET_BUILD_TYPE=debug
  348.             ;;
  349.         *)
  350.             echo
  351.             echo "I didn't understand your response.  Please try again."
  352.             echo
  353.             ;;
  354.         esac
  355.         if [ -n "$1" ] ; then
  356.             break
  357.         fi
  358.     done
  359.  
  360.     set_stuff_for_environment
  361. }
  362.  
  363. #
  364. # This function isn't really right:  It chooses a TARGET_PRODUCT
  365. # based on the list of boards.  Usually, that gets you something
  366. # that kinda works with a generic product, but really, you should
  367. # pick a product by name.
  368. #
  369. function chooseproduct()
  370. {
  371.     if [ "x$TARGET_PRODUCT" != x ] ; then
  372.         default_value=$TARGET_PRODUCT
  373.     else
  374.         default_value=full
  375.     fi
  376.  
  377.     export TARGET_PRODUCT=
  378.     local ANSWER
  379.     while [ -z "$TARGET_PRODUCT" ]
  380.     do
  381.         echo -n "Which product would you like? [$default_value] "
  382.         if [ -z "$1" ] ; then
  383.             read ANSWER
  384.         else
  385.             echo $1
  386.             ANSWER=$1
  387.         fi
  388.  
  389.         if [ -z "$ANSWER" ] ; then
  390.             export TARGET_PRODUCT=$default_value
  391.         else
  392.             if check_product $ANSWER
  393.             then
  394.                 export TARGET_PRODUCT=$ANSWER
  395.             else
  396.                 echo "** Not a valid product: $ANSWER"
  397.             fi
  398.         fi
  399.         if [ -n "$1" ] ; then
  400.             break
  401.         fi
  402.     done
  403.  
  404.     set_stuff_for_environment
  405. }
  406.  
  407. function choosevariant()
  408. {
  409.     echo "Variant choices are:"
  410.     local index=1
  411.     local v
  412.     for v in ${VARIANT_CHOICES[@]}
  413.     do
  414.         # The product name is the name of the directory containing
  415.         # the makefile we found, above.
  416.         echo "     $index. $v"
  417.         index=$(($index+1))
  418.     done
  419.  
  420.     local default_value=eng
  421.     local ANSWER
  422.  
  423.     export TARGET_BUILD_VARIANT=
  424.     while [ -z "$TARGET_BUILD_VARIANT" ]
  425.     do
  426.         echo -n "Which would you like? [$default_value] "
  427.         if [ -z "$1" ] ; then
  428.             read ANSWER
  429.         else
  430.             echo $1
  431.             ANSWER=$1
  432.         fi
  433.  
  434.         if [ -z "$ANSWER" ] ; then
  435.             export TARGET_BUILD_VARIANT=$default_value
  436.         elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
  437.             if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
  438.                 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
  439.             fi
  440.         else
  441.             if check_variant $ANSWER
  442.             then
  443.                 export TARGET_BUILD_VARIANT=$ANSWER
  444.             else
  445.                 echo "** Not a valid variant: $ANSWER"
  446.             fi
  447.         fi
  448.         if [ -n "$1" ] ; then
  449.             break
  450.         fi
  451.     done
  452. }
  453.  
  454. function choosecombo()
  455. {
  456.     choosetype $1
  457.  
  458.     echo
  459.     echo
  460.     chooseproduct $2
  461.  
  462.     echo
  463.     echo
  464.     choosevariant $3
  465.  
  466.     echo
  467.     set_stuff_for_environment
  468.     printconfig
  469. }
  470.  
  471. # Clear this variable.  It will be built up again when the vendorsetup.sh
  472. # files are included at the end of this file.
  473. unset LUNCH_MENU_CHOICES
  474. function add_lunch_combo()
  475. {
  476.     local new_combo=$1
  477.     local c
  478.     for c in ${LUNCH_MENU_CHOICES[@]} ; do
  479.         if [ "$new_combo" = "$c" ] ; then
  480.             return
  481.         fi
  482.     done
  483.     LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
  484. }
  485.  
  486. # add the default one here
  487. add_lunch_combo aosp_arm-eng
  488. add_lunch_combo aosp_arm64-eng
  489. add_lunch_combo aosp_mips-eng
  490. add_lunch_combo aosp_mips64-eng
  491. add_lunch_combo aosp_x86-eng
  492. add_lunch_combo aosp_x86_64-eng
  493.  
  494. function print_lunch_menu()
  495. {
  496.     local uname=$(uname)
  497.     echo
  498.     echo "You're building on" $uname
  499.     if [ "$(uname)" = "Darwin" ] ; then
  500.        echo "  (ohai, koush!)"
  501.     fi
  502.     echo
  503.     if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
  504.        echo "Breakfast menu... pick a combo:"
  505.     else
  506.        echo "Lunch menu... pick a combo:"
  507.     fi
  508.  
  509.     local i=1
  510.     local choice
  511.     for choice in ${LUNCH_MENU_CHOICES[@]}
  512.     do
  513.         echo " $i. $choice "
  514.         i=$(($i+1))
  515.     done | column
  516.  
  517.     if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
  518.        echo "... and don't forget the bacon!"
  519.     fi
  520.  
  521.     echo
  522. }
  523.  
  524. function brunch()
  525. {
  526.     breakfast $*
  527.     if [ $? -eq 0 ]; then
  528.         mka bacon
  529.     else
  530.         echo "No such item in brunch menu. Try 'breakfast'"
  531.         return 1
  532.     fi
  533.     return $?
  534. }
  535.  
  536. function breakfast()
  537. {
  538.     target=$1
  539.     local variant=$2
  540.     CM_DEVICES_ONLY="true"
  541.     unset LUNCH_MENU_CHOICES
  542.     add_lunch_combo full-eng
  543.     for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
  544.         do
  545.             echo "including $f"
  546.             . $f
  547.         done
  548.     unset f
  549.  
  550.     if [ $# -eq 0 ]; then
  551.         # No arguments, so let's have the full menu
  552.         lunch
  553.     else
  554.         echo "z$target" | grep -q "-"
  555.         if [ $? -eq 0 ]; then
  556.             # A buildtype was specified, assume a full device name
  557.             lunch $target
  558.         else
  559.             # This is probably just the CM model name
  560.             if [ -z "$variant" ]; then
  561.                 variant="userdebug"
  562.             fi
  563.             lunch cm_$target-$variant
  564.         fi
  565.     fi
  566.     return $?
  567. }
  568.  
  569. alias bib=breakfast
  570.  
  571. function lunch()
  572. {
  573.     local answer
  574.     LUNCH_MENU_CHOICES=($(for l in ${LUNCH_MENU_CHOICES[@]}; do echo "$l"; done | sort))
  575.  
  576.     if [ "$1" ] ; then
  577.         answer=$1
  578.     else
  579.         print_lunch_menu
  580.         echo -n "Which would you like? [aosp_arm-eng] "
  581.         read answer
  582.     fi
  583.  
  584.     local selection=
  585.  
  586.     if [ -z "$answer" ]
  587.     then
  588.         selection=aosp_arm-eng
  589.     elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
  590.     then
  591.         if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
  592.         then
  593.             selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
  594.         fi
  595.     elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
  596.     then
  597.         selection=$answer
  598.     fi
  599.  
  600.     if [ -z "$selection" ]
  601.     then
  602.         echo
  603.         echo "Invalid lunch combo: $answer"
  604.         return 1
  605.     fi
  606.  
  607.     export TARGET_BUILD_APPS=
  608.  
  609.     local product=$(echo -n $selection | sed -e "s/-.*$//")
  610.     check_product $product
  611.     if [ $? -ne 0 ]
  612.     then
  613.         # if we can't find a product, try to grab it off the CM github
  614.         T=$(gettop)
  615.         pushd $T > /dev/null
  616.         build/tools/roomservice.py $product
  617.         popd > /dev/null
  618.         check_product $product
  619.     else
  620.         build/tools/roomservice.py $product true
  621.     fi
  622.     if [ $? -ne 0 ]
  623.     then
  624.         echo
  625.         echo "** Don't have a product spec for: '$product'"
  626.         echo "** Do you have the right repo manifest?"
  627.         product=
  628.     fi
  629.  
  630.     local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
  631.     check_variant $variant
  632.     if [ $? -ne 0 ]
  633.     then
  634.         echo
  635.         echo "** Invalid variant: '$variant'"
  636.         echo "** Must be one of ${VARIANT_CHOICES[@]}"
  637.         variant=
  638.     fi
  639.  
  640.     if [ -z "$product" -o -z "$variant" ]
  641.     then
  642.         echo
  643.         return 1
  644.     fi
  645.  
  646.     export TARGET_PRODUCT=$product
  647.     export TARGET_BUILD_VARIANT=$variant
  648.     export TARGET_BUILD_TYPE=release
  649.  
  650.     echo
  651.  
  652.     fixup_common_out_dir
  653.  
  654.     set_stuff_for_environment
  655.     printconfig
  656. }
  657.  
  658. # Tab completion for lunch.
  659. function _lunch()
  660. {
  661.     local cur prev opts
  662.     COMPREPLY=()
  663.     cur="${COMP_WORDS[COMP_CWORD]}"
  664.     prev="${COMP_WORDS[COMP_CWORD-1]}"
  665.  
  666.     COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
  667.     return 0
  668. }
  669. complete -F _lunch lunch 2>/dev/null
  670.  
  671. # Configures the build to build unbundled apps.
  672. # Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
  673. function tapas()
  674. {
  675.     local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
  676.     local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
  677.     local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
  678.     local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
  679.  
  680.     if [ $(echo $arch | wc -w) -gt 1 ]; then
  681.         echo "tapas: Error: Multiple build archs supplied: $arch"
  682.         return
  683.     fi
  684.     if [ $(echo $variant | wc -w) -gt 1 ]; then
  685.         echo "tapas: Error: Multiple build variants supplied: $variant"
  686.         return
  687.     fi
  688.     if [ $(echo $density | wc -w) -gt 1 ]; then
  689.         echo "tapas: Error: Multiple densities supplied: $density"
  690.         return
  691.     fi
  692.  
  693.     local product=full
  694.     case $arch in
  695.       x86)    product=full_x86;;
  696.       mips)   product=full_mips;;
  697.       armv5)  product=generic_armv5;;
  698.       arm64)  product=aosp_arm64;;
  699.       x86_64) product=aosp_x86_64;;
  700.       mips64)  product=aosp_mips64;;
  701.     esac
  702.     if [ -z "$variant" ]; then
  703.         variant=eng
  704.     fi
  705.     if [ -z "$apps" ]; then
  706.         apps=all
  707.     fi
  708.     if [ -z "$density" ]; then
  709.         density=alldpi
  710.     fi
  711.  
  712.     export TARGET_PRODUCT=$product
  713.     export TARGET_BUILD_VARIANT=$variant
  714.     export TARGET_BUILD_DENSITY=$density
  715.     export TARGET_BUILD_TYPE=release
  716.     export TARGET_BUILD_APPS=$apps
  717.  
  718.     set_stuff_for_environment
  719.     printconfig
  720. }
  721.  
  722. function eat()
  723. {
  724.     if [ "$OUT" ] ; then
  725.         MODVERSION=$(get_build_var CM_VERSION)
  726.         ZIPFILE=cm-$MODVERSION.zip
  727.         ZIPPATH=$OUT/$ZIPFILE
  728.         if [ ! -f $ZIPPATH ] ; then
  729.             echo "Nothing to eat"
  730.             return 1
  731.         fi
  732.         adb start-server # Prevent unexpected starting server message from adb get-state in the next line
  733.         if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
  734.             echo "No device is online. Waiting for one..."
  735.             echo "Please connect USB and/or enable USB debugging"
  736.             until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
  737.                 sleep 1
  738.             done
  739.             echo "Device Found.."
  740.         fi
  741.     if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
  742.     then
  743.         # if adbd isn't root we can't write to /cache/recovery/
  744.         adb root
  745.         sleep 1
  746.         adb wait-for-device
  747.         cat << EOF > /tmp/command
  748. --sideload
  749. EOF
  750.         if adb push /tmp/command /cache/recovery/ ; then
  751.             echo "Rebooting into recovery for sideload installation"
  752.             adb reboot recovery
  753.             adb wait-for-sideload
  754.             adb sideload $ZIPPATH
  755.         fi
  756.         rm /tmp/command
  757.     else
  758.         echo "Nothing to eat"
  759.         return 1
  760.     fi
  761.     return $?
  762.     else
  763.         echo "The connected device does not appear to be $CM_BUILD, run away!"
  764.     fi
  765. }
  766.  
  767. function omnom
  768. {
  769.     brunch $*
  770.     eat
  771. }
  772.  
  773. function gettop
  774. {
  775.     local TOPFILE=build/core/envsetup.mk
  776.     if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
  777.         # The following circumlocution ensures we remove symlinks from TOP.
  778.         (cd $TOP; PWD= /bin/pwd)
  779.     else
  780.         if [ -f $TOPFILE ] ; then
  781.             # The following circumlocution (repeated below as well) ensures
  782.             # that we record the true directory name and not one that is
  783.             # faked up with symlink names.
  784.             PWD= /bin/pwd
  785.         else
  786.             local HERE=$PWD
  787.             T=
  788.             while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
  789.                 \cd ..
  790.                 T=`PWD= /bin/pwd -P`
  791.             done
  792.             \cd $HERE
  793.             if [ -f "$T/$TOPFILE" ]; then
  794.                 echo $T
  795.             fi
  796.         fi
  797.     fi
  798. }
  799.  
  800. # Return driver for "make", if any (eg. static analyzer)
  801. function getdriver()
  802. {
  803.     local T="$1"
  804.     test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
  805.     if [ -n "$WITH_STATIC_ANALYZER" ]; then
  806.         echo "\
  807. $T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
  808. --use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
  809. --status-bugs \
  810. --top=$T"
  811.     fi
  812. }
  813.  
  814. function m()
  815. {
  816.     local T=$(gettop)
  817.     local DRV=$(getdriver $T)
  818.     if [ "$T" ]; then
  819.         $DRV make -C $T -f build/core/main.mk $@
  820.     else
  821.         echo "Couldn't locate the top of the tree.  Try setting TOP."
  822.     fi
  823. }
  824.  
  825. function findmakefile()
  826. {
  827.     TOPFILE=build/core/envsetup.mk
  828.     local HERE=$PWD
  829.     T=
  830.     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
  831.         T=`PWD= /bin/pwd`
  832.         if [ -f "$T/Android.mk" ]; then
  833.             echo $T/Android.mk
  834.             \cd $HERE
  835.             return
  836.         fi
  837.         \cd ..
  838.     done
  839.     \cd $HERE
  840. }
  841.  
  842. function mm()
  843. {
  844.     local T=$(gettop)
  845.     local DRV=$(getdriver $T)
  846.     # If we're sitting in the root of the build tree, just do a
  847.     # normal make.
  848.     if [ -f build/core/envsetup.mk -a -f Makefile ]; then
  849.         $DRV make $@
  850.     else
  851.         # Find the closest Android.mk file.
  852.         local M=$(findmakefile)
  853.         local MODULES=
  854.         local GET_INSTALL_PATH=
  855.         local ARGS=
  856.         # Remove the path to top as the makefilepath needs to be relative
  857.         local M=`echo $M|sed 's:'$T'/::'`
  858.         if [ ! "$T" ]; then
  859.             echo "Couldn't locate the top of the tree.  Try setting TOP."
  860.         elif [ ! "$M" ]; then
  861.             echo "Couldn't locate a makefile from the current directory."
  862.         else
  863.             for ARG in $@; do
  864.                 case $ARG in
  865.                   GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
  866.                 esac
  867.             done
  868.             if [ -n "$GET_INSTALL_PATH" ]; then
  869.               MODULES=
  870.               ARGS=GET-INSTALL-PATH
  871.             else
  872.               MODULES=all_modules
  873.               ARGS=$@
  874.             fi
  875.             ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES $ARGS
  876.         fi
  877.     fi
  878. }
  879.  
  880. function mmm()
  881. {
  882.     local T=$(gettop)
  883.     local DRV=$(getdriver $T)
  884.     if [ "$T" ]; then
  885.         local MAKEFILE=
  886.         local MODULES=
  887.         local ARGS=
  888.         local DIR TO_CHOP
  889.         local GET_INSTALL_PATH=
  890.         local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
  891.         local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
  892.         for DIR in $DIRS ; do
  893.             MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
  894.             if [ "$MODULES" = "" ]; then
  895.                 MODULES=all_modules
  896.             fi
  897.             DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
  898.             if [ -f $DIR/Android.mk ]; then
  899.                 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
  900.                 local TO_CHOP=`expr $TO_CHOP + 1`
  901.                 local START=`PWD= /bin/pwd`
  902.                 local MFILE=`echo $START | cut -c${TO_CHOP}-`
  903.                 if [ "$MFILE" = "" ] ; then
  904.                     MFILE=$DIR/Android.mk
  905.                 else
  906.                     MFILE=$MFILE/$DIR/Android.mk
  907.                 fi
  908.                 MAKEFILE="$MAKEFILE $MFILE"
  909.             else
  910.                 case $DIR in
  911.                   showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
  912.                   GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
  913.                   *) echo "No Android.mk in $DIR."; return 1;;
  914.                 esac
  915.             fi
  916.         done
  917.         if [ -n "$GET_INSTALL_PATH" ]; then
  918.           ARGS=$GET_INSTALL_PATH
  919.           MODULES=
  920.         fi
  921.         ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
  922.     else
  923.         echo "Couldn't locate the top of the tree.  Try setting TOP."
  924.     fi
  925. }
  926.  
  927. function mma()
  928. {
  929.   local T=$(gettop)
  930.   local DRV=$(getdriver $T)
  931.   if [ -f build/core/envsetup.mk -a -f Makefile ]; then
  932.     $DRV make $@
  933.   else
  934.     if [ ! "$T" ]; then
  935.       echo "Couldn't locate the top of the tree.  Try setting TOP."
  936.     fi
  937.     local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
  938.     $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
  939.   fi
  940. }
  941.  
  942. function mmma()
  943. {
  944.   local T=$(gettop)
  945.   local DRV=$(getdriver $T)
  946.   if [ "$T" ]; then
  947.     local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
  948.     local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
  949.     local MY_PWD=`PWD= /bin/pwd`
  950.     if [ "$MY_PWD" = "$T" ]; then
  951.       MY_PWD=
  952.     else
  953.       MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
  954.     fi
  955.     local DIR=
  956.     local MODULE_PATHS=
  957.     local ARGS=
  958.     for DIR in $DIRS ; do
  959.       if [ -d $DIR ]; then
  960.         if [ "$MY_PWD" = "" ]; then
  961.           MODULE_PATHS="$MODULE_PATHS $DIR"
  962.         else
  963.           MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
  964.         fi
  965.       else
  966.         case $DIR in
  967.           showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
  968.           *) echo "Couldn't find directory $DIR"; return 1;;
  969.         esac
  970.       fi
  971.     done
  972.     $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS all_modules BUILD_MODULES_IN_PATHS="$MODULE_PATHS"
  973.   else
  974.     echo "Couldn't locate the top of the tree.  Try setting TOP."
  975.   fi
  976. }
  977.  
  978. function croot()
  979. {
  980.     T=$(gettop)
  981.     if [ "$T" ]; then
  982.         \cd $(gettop)
  983.     else
  984.         echo "Couldn't locate the top of the tree.  Try setting TOP."
  985.     fi
  986. }
  987.  
  988. function cout()
  989. {
  990.     if [  "$OUT" ]; then
  991.         cd $OUT
  992.     else
  993.         echo "Couldn't locate out directory.  Try setting OUT."
  994.     fi
  995. }
  996.  
  997. function cproj()
  998. {
  999.     TOPFILE=build/core/envsetup.mk
  1000.     local HERE=$PWD
  1001.     T=
  1002.     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
  1003.         T=$PWD
  1004.         if [ -f "$T/Android.mk" ]; then
  1005.             \cd $T
  1006.             return
  1007.         fi
  1008.         \cd ..
  1009.     done
  1010.     \cd $HERE
  1011.     echo "can't find Android.mk"
  1012. }
  1013.  
  1014. # simplified version of ps; output in the form
  1015. # <pid> <procname>
  1016. function qpid() {
  1017.     local prepend=''
  1018.     local append=''
  1019.     if [ "$1" = "--exact" ]; then
  1020.         prepend=' '
  1021.         append='$'
  1022.         shift
  1023.     elif [ "$1" = "--help" -o "$1" = "-h" ]; then
  1024.         echo "usage: qpid [[--exact] <process name|pid>"
  1025.         return 255
  1026.     fi
  1027.  
  1028.     local EXE="$1"
  1029.     if [ "$EXE" ] ; then
  1030.         qpid | \grep "$prepend$EXE$append"
  1031.     else
  1032.         adb shell ps \
  1033.             | tr -d '\r' \
  1034.             | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
  1035.     fi
  1036. }
  1037.  
  1038. function pid()
  1039. {
  1040.     local prepend=''
  1041.     local append=''
  1042.     if [ "$1" = "--exact" ]; then
  1043.         prepend=' '
  1044.         append='$'
  1045.         shift
  1046.     fi
  1047.     local EXE="$1"
  1048.     if [ "$EXE" ] ; then
  1049.         local PID=`adb shell ps \
  1050.             | tr -d '\r' \
  1051.             | \grep "$prepend$EXE$append" \
  1052.             | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
  1053.         echo "$PID"
  1054.     else
  1055.         echo "usage: pid [--exact] <process name>"
  1056.         return 255
  1057.     fi
  1058. }
  1059.  
  1060. # coredump_setup - enable core dumps globally for any process
  1061. #                  that has the core-file-size limit set correctly
  1062. #
  1063. # NOTE: You must call also coredump_enable for a specific process
  1064. #       if its core-file-size limit is not set already.
  1065. # NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
  1066.  
  1067. function coredump_setup()
  1068. {
  1069.     echo "Getting root...";
  1070.     adb root;
  1071.     adb wait-for-device;
  1072.  
  1073.     echo "Remounting root parition read-write...";
  1074.     adb shell mount -w -o remount -t rootfs rootfs;
  1075.     sleep 1;
  1076.     adb wait-for-device;
  1077.     adb shell mkdir -p /cores;
  1078.     adb shell mount -t tmpfs tmpfs /cores;
  1079.     adb shell chmod 0777 /cores;
  1080.  
  1081.     echo "Granting SELinux permission to dump in /cores...";
  1082.     adb shell restorecon -R /cores;
  1083.  
  1084.     echo "Set core pattern.";
  1085.     adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
  1086.  
  1087.     echo "Done."
  1088. }
  1089.  
  1090. # coredump_enable - enable core dumps for the specified process
  1091. # $1 = PID of process (e.g., $(pid mediaserver))
  1092. #
  1093. # NOTE: coredump_setup must have been called as well for a core
  1094. #       dump to actually be generated.
  1095.  
  1096. function coredump_enable()
  1097. {
  1098.     local PID=$1;
  1099.     if [ -z "$PID" ]; then
  1100.         printf "Expecting a PID!\n";
  1101.         return;
  1102.     fi;
  1103.     echo "Setting core limit for $PID to infinite...";
  1104.     adb shell prlimit $PID 4 -1 -1
  1105. }
  1106.  
  1107. # core - send SIGV and pull the core for process
  1108. # $1 = PID of process (e.g., $(pid mediaserver))
  1109. #
  1110. # NOTE: coredump_setup must be called once per boot for core dumps to be
  1111. #       enabled globally.
  1112.  
  1113. function core()
  1114. {
  1115.     local PID=$1;
  1116.  
  1117.     if [ -z "$PID" ]; then
  1118.         printf "Expecting a PID!\n";
  1119.         return;
  1120.     fi;
  1121.  
  1122.     local CORENAME=core.$PID;
  1123.     local COREPATH=/cores/$CORENAME;
  1124.     local SIG=SEGV;
  1125.  
  1126.     coredump_enable $1;
  1127.  
  1128.     local done=0;
  1129.     while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
  1130.         printf "\tSending SIG%s to %d...\n" $SIG $PID;
  1131.         adb shell kill -$SIG $PID;
  1132.         sleep 1;
  1133.     done;
  1134.  
  1135.     adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
  1136.     echo "Done: core is under $COREPATH on device.";
  1137. }
  1138.  
  1139. # systemstack - dump the current stack trace of all threads in the system process
  1140. # to the usual ANR traces file
  1141. function systemstack()
  1142. {
  1143.     stacks system_server
  1144. }
  1145.  
  1146. function stacks()
  1147. {
  1148.     if [[ $1 =~ ^[0-9]+$ ]] ; then
  1149.         local PID="$1"
  1150.     elif [ "$1" ] ; then
  1151.         local PIDLIST="$(pid $1)"
  1152.         if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
  1153.             local PID="$PIDLIST"
  1154.         elif [ "$PIDLIST" ] ; then
  1155.             echo "more than one process: $1"
  1156.         else
  1157.             echo "no such process: $1"
  1158.         fi
  1159.     else
  1160.         echo "usage: stacks [pid|process name]"
  1161.     fi
  1162.  
  1163.     if [ "$PID" ] ; then
  1164.         # Determine whether the process is native
  1165.         if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
  1166.             # Dump stacks of Dalvik process
  1167.             local TRACES=/data/anr/traces.txt
  1168.             local ORIG=/data/anr/traces.orig
  1169.             local TMP=/data/anr/traces.tmp
  1170.  
  1171.             # Keep original traces to avoid clobbering
  1172.             adb shell mv $TRACES $ORIG
  1173.  
  1174.             # Make sure we have a usable file
  1175.             adb shell touch $TRACES
  1176.             adb shell chmod 666 $TRACES
  1177.  
  1178.             # Dump stacks and wait for dump to finish
  1179.             adb shell kill -3 $PID
  1180.             adb shell notify $TRACES >/dev/null
  1181.  
  1182.             # Restore original stacks, and show current output
  1183.             adb shell mv $TRACES $TMP
  1184.             adb shell mv $ORIG $TRACES
  1185.             adb shell cat $TMP
  1186.         else
  1187.             # Dump stacks of native process
  1188.             local USE64BIT="$(is64bit $PID)"
  1189.             adb shell debuggerd$USE64BIT -b $PID
  1190.         fi
  1191.     fi
  1192. }
  1193.  
  1194. function gdbwrapper()
  1195. {
  1196.     local GDB_CMD="$1"
  1197.     shift 1
  1198.     $GDB_CMD -x "$@"
  1199. }
  1200.  
  1201. function get_symbols_directory()
  1202. {
  1203.     echo $(get_abs_build_var TARGET_OUT_UNSTRIPPED)
  1204. }
  1205.  
  1206. # Read the ELF header from /proc/$PID/exe to determine if the process is
  1207. # 64-bit.
  1208. function is64bit()
  1209. {
  1210.     local PID="$1"
  1211.     if [ "$PID" ] ; then
  1212.         if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
  1213.             echo "64"
  1214.         else
  1215.             echo ""
  1216.         fi
  1217.     else
  1218.         echo ""
  1219.     fi
  1220. }
  1221.  
  1222. function adb_get_product_device() {
  1223.   echo `adb shell getprop ro.product.device | sed s/.$//`
  1224. }
  1225.  
  1226. # returns 0 when process is not traced
  1227. function adb_get_traced_by() {
  1228.   echo `adb shell cat /proc/$1/status | grep -e "^TracerPid:" | sed "s/^TracerPid:\t//" | sed s/.$//`
  1229. }
  1230.  
  1231. function gdbclient() {
  1232.   # TODO:
  1233.   # 1. Check for ANDROID_SERIAL/multiple devices
  1234.   local PROCESS_NAME="n/a"
  1235.   local PID=$1
  1236.   local PORT=5039
  1237.   if [ -z "$PID" ]; then
  1238.     echo "Usage: gdbclient <pid|processname> [port number]"
  1239.     return -1
  1240.   fi
  1241.   local DEVICE=$(adb_get_product_device)
  1242.  
  1243.   if [ -z "$DEVICE" ]; then
  1244.     echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
  1245.     return -2
  1246.   fi
  1247.  
  1248.   if [ -n "$2" ]; then
  1249.     PORT=$2
  1250.   fi
  1251.  
  1252.   local ROOT=$(gettop)
  1253.   if [ -z "$ROOT" ]; then
  1254.     # This is for the situation with downloaded symbols (from the build server)
  1255.     # we check if they are available.
  1256.     ROOT=`realpath .`
  1257.   fi
  1258.  
  1259.   local OUT_ROOT="$ROOT/out/target/product/$DEVICE"
  1260.   local SYMBOLS_DIR="$OUT_ROOT/symbols"
  1261.  
  1262.   if [ ! -d $SYMBOLS_DIR ]; then
  1263.     echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
  1264.     return -3
  1265.   fi
  1266.  
  1267.   # let's figure out which executable we are about to debug
  1268.  
  1269.   # check if user specified a name -> resolve to pid
  1270.   if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
  1271.     PROCESS_NAME=$PID
  1272.     PID=$(pid --exact $PROCESS_NAME)
  1273.     if [ -z "$PID" ]; then
  1274.       echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
  1275.       return -4
  1276.     fi
  1277.   fi
  1278.  
  1279.   local EXE=`adb shell readlink /proc/$PID/exe | sed s/.$//`
  1280.   # TODO: print error in case there is no such pid
  1281.   local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE
  1282.  
  1283.   if [ ! -f $LOCAL_EXE_PATH ]; then
  1284.     echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
  1285.     return -5
  1286.   fi
  1287.  
  1288.   local USE64BIT=""
  1289.  
  1290.   if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
  1291.     USE64BIT="64"
  1292.   fi
  1293.  
  1294.   local GDB=
  1295.   local GDB64=
  1296.   local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | sed s/.$//`
  1297.   # TODO: we assume these are available via $PATH
  1298.   if [[ $CPU_ABI =~ (^|,)arm64 ]]; then
  1299.     GDB=arm-linux-androideabi-gdb
  1300.     GDB64=aarch64-linux-android-gdb
  1301.   elif [[ $CPU_ABI =~ (^|,)arm ]]; then
  1302.     GDB=arm-linux-androideabi-gdb
  1303.   elif [[ $CPU_ABI =~ (^|,)x86_64 ]]; then
  1304.     GDB=x86_64-linux-androideabi-gdb
  1305.   elif [[ $CPU_ABI =~ (^|,)x86 ]]; then
  1306.     GDB=x86_64-linux-androideabi-gdb
  1307.   elif [[ $CPU_ABI =~ (^|,)mips64 ]]; then
  1308.     GDB=mipsel-linux-android-gdb
  1309.     GDB64=mips64el-linux-android-gdb
  1310.   elif [[ $CPU_ABI =~ (^|,)mips ]]; then
  1311.     GDB=mipsel-linux-android-gdb
  1312.   else
  1313.     echo "Error: unrecognized cpu.abilist: $CPU_ABI"
  1314.     return -6
  1315.   fi
  1316.  
  1317.   # TODO: check if tracing process is gdbserver and not some random strace...
  1318.   if [ $(adb_get_traced_by $PID) -eq 0 ]; then
  1319.     # start gdbserver
  1320.     echo "Starting gdbserver..."
  1321.     # TODO: check if adb is already listening $PORT
  1322.     # to avoid unnecessary calls
  1323.     echo ". adb forward for port=$PORT..."
  1324.     adb forward tcp:$PORT tcp:$PORT
  1325.     echo ". starting gdbserver to attach to pid=$PID..."
  1326.     adb shell gdbserver$USE64BIT :$PORT --attach $PID &
  1327.     echo ". give it couple of seconds to start..."
  1328.     sleep 2
  1329.     echo ". done"
  1330.   else
  1331.     echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
  1332.   fi
  1333.  
  1334.   local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
  1335.   local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
  1336.   local ART_CMD=""
  1337.  
  1338.   echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SYMBOLS_DIR"
  1339.   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"
  1340.   local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
  1341.   if [ -f $DALVIK_GDB_SCRIPT ]; then
  1342.     echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
  1343.     ART_CMD="art-on"
  1344.   else
  1345.     echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
  1346.   fi
  1347.   echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
  1348.   if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
  1349.     echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
  1350.   fi
  1351.  
  1352.   echo >>"$OUT_ROOT/gdbclient.cmds" ""
  1353.  
  1354.   local WHICH_GDB=$GDB
  1355.  
  1356.   if [ -n "$USE64BIT" -a -n "$GDB64" ]; then
  1357.     WHICH_GDB=$GDB64
  1358.   fi
  1359.  
  1360.   gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
  1361. }
  1362.  
  1363. # gdbclient now determines whether the user wants to debug a 32-bit or 64-bit
  1364. # executable, set up the approriate gdbserver, then invokes the proper host
  1365. # gdb.
  1366. function gdbclient_old()
  1367. {
  1368.    local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
  1369.    local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
  1370.    local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
  1371.    local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
  1372.    local OUT_EXE_SYMBOLS=$(get_symbols_directory)
  1373.    local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
  1374.    local ARCH=$(get_build_var TARGET_ARCH)
  1375.    local GDB
  1376.    case "$ARCH" in
  1377.        arm) GDB=arm-linux-androideabi-gdb;;
  1378.        arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
  1379.        mips|mips64) GDB=mips64el-linux-android-gdb;;
  1380.        x86) GDB=x86_64-linux-android-gdb;;
  1381.        x86_64) GDB=x86_64-linux-android-gdb;;
  1382.        *) echo "Unknown arch $ARCH"; return 1;;
  1383.    esac
  1384.  
  1385.    if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
  1386.        local EXE="$1"
  1387.        if [ "$EXE" ] ; then
  1388.            EXE=$1
  1389.            if [[ $EXE =~ ^[^/].* ]] ; then
  1390.                EXE="system/bin/"$EXE
  1391.            fi
  1392.        else
  1393.            EXE="app_process"
  1394.        fi
  1395.  
  1396.        local PORT="$2"
  1397.        if [ "$PORT" ] ; then
  1398.            PORT=$2
  1399.        else
  1400.            PORT=":5039"
  1401.        fi
  1402.  
  1403.        local PID="$3"
  1404.        if [ "$PID" ] ; then
  1405.            if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
  1406.                PID=`pid $3`
  1407.                if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
  1408.                    # that likely didn't work because of returning multiple processes
  1409.                    # try again, filtering by root processes (don't contain colon)
  1410.                    PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
  1411.                    if [[ ! "$PID" =~ ^[0-9]+$ ]]
  1412.                    then
  1413.                        echo "Couldn't resolve '$3' to single PID"
  1414.                        return 1
  1415.                    else
  1416.                        echo ""
  1417.                        echo "WARNING: multiple processes matching '$3' observed, using root process"
  1418.                        echo ""
  1419.                    fi
  1420.                fi
  1421.            fi
  1422.            adb forward "tcp$PORT" "tcp$PORT"
  1423.            local USE64BIT="$(is64bit $PID)"
  1424.            adb shell gdbserver$USE64BIT $PORT --attach $PID &
  1425.            sleep 2
  1426.        else
  1427.                echo ""
  1428.                echo "If you haven't done so already, do this first on the device:"
  1429.                echo "    gdbserver $PORT /system/bin/$EXE"
  1430.                    echo " or"
  1431.                echo "    gdbserver $PORT --attach <PID>"
  1432.                echo ""
  1433.        fi
  1434.  
  1435.        OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
  1436.        OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
  1437.  
  1438.        echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
  1439.        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"
  1440.        echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
  1441.        echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
  1442.        # Enable special debugging for ART processes.
  1443.        if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
  1444.           echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
  1445.        fi
  1446.        echo >>"$OUT_ROOT/gdbclient.cmds" ""
  1447.  
  1448.        local WHICH_GDB=
  1449.        # 64-bit exe found
  1450.        if [ "$USE64BIT" != "" ] ; then
  1451.            WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
  1452.        # 32-bit exe / 32-bit platform
  1453.        elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
  1454.            WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
  1455.        # 32-bit exe / 64-bit platform
  1456.        else
  1457.            WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
  1458.        fi
  1459.  
  1460.        gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
  1461.   else
  1462.        echo "Unable to determine build system output dir."
  1463.    fi
  1464.  
  1465. }
  1466.  
  1467. function dddclient()
  1468. {
  1469.    local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
  1470.    local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
  1471.    local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
  1472.    local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
  1473.    local OUT_EXE_SYMBOLS=$(get_symbols_directory)
  1474.    local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
  1475.    local ARCH=$(get_build_var TARGET_ARCH)
  1476.    local GDB
  1477.    case "$ARCH" in
  1478.        arm) GDB=arm-linux-androideabi-gdb;;
  1479.        arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
  1480.        mips|mips64) GDB=mips64el-linux-android-gdb;;
  1481.        x86) GDB=x86_64-linux-android-gdb;;
  1482.        x86_64) GDB=x86_64-linux-android-gdb;;
  1483.        *) echo "Unknown arch $ARCH"; return 1;;
  1484.    esac
  1485.  
  1486.    if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
  1487.        local EXE="$1"
  1488.        if [ "$EXE" ] ; then
  1489.            EXE=$1
  1490.            if [[ $EXE =~ ^[^/].* ]] ; then
  1491.                EXE="system/bin/"$EXE
  1492.            fi
  1493.        else
  1494.            EXE="app_process"
  1495.        fi
  1496.  
  1497.        local PORT="$2"
  1498.        if [ "$PORT" ] ; then
  1499.            PORT=$2
  1500.        else
  1501.            PORT=":5039"
  1502.        fi
  1503.  
  1504.        local PID="$3"
  1505.        if [ "$PID" ] ; then
  1506.            if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
  1507.                PID=`pid $3`
  1508.                if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
  1509.                    # that likely didn't work because of returning multiple processes
  1510.                    # try again, filtering by root processes (don't contain colon)
  1511.                    PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
  1512.                    if [[ ! "$PID" =~ ^[0-9]+$ ]]
  1513.                    then
  1514.                        echo "Couldn't resolve '$3' to single PID"
  1515.                        return 1
  1516.                    else
  1517.                        echo ""
  1518.                        echo "WARNING: multiple processes matching '$3' observed, using root process"
  1519.                        echo ""
  1520.                    fi
  1521.                fi
  1522.            fi
  1523.            adb forward "tcp$PORT" "tcp$PORT"
  1524.            local USE64BIT="$(is64bit $PID)"
  1525.            adb shell gdbserver$USE64BIT $PORT --attach $PID &
  1526.            sleep 2
  1527.        else
  1528.                echo ""
  1529.                echo "If you haven't done so already, do this first on the device:"
  1530.                echo "    gdbserver $PORT /system/bin/$EXE"
  1531.                    echo " or"
  1532.                echo "    gdbserver $PORT --attach <PID>"
  1533.                echo ""
  1534.        fi
  1535.  
  1536.        OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
  1537.        OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
  1538.  
  1539.        echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
  1540.        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"
  1541.        echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
  1542.        echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
  1543.        # Enable special debugging for ART processes.
  1544.        if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
  1545.           echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
  1546.        fi
  1547.        echo >>"$OUT_ROOT/gdbclient.cmds" ""
  1548.  
  1549.        local WHICH_GDB=
  1550.        # 64-bit exe found
  1551.        if [ "$USE64BIT" != "" ] ; then
  1552.            WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
  1553.        # 32-bit exe / 32-bit platform
  1554.        elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
  1555.            WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
  1556.        # 32-bit exe / 64-bit platform
  1557.        else
  1558.            WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
  1559.        fi
  1560.  
  1561.        ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
  1562.   else
  1563.        echo "Unable to determine build system output dir."
  1564.    fi
  1565. }
  1566.  
  1567.  
  1568. case `uname -s` in
  1569.     Darwin)
  1570.         function sgrep()
  1571.         {
  1572.             find -E . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*\.(c|h|cc|cpp|S|java|xml|sh|mk|aidl)' -print0 | xargs -0 grep --color -n "$@"
  1573.         }
  1574.  
  1575.         ;;
  1576.     *)
  1577.         function sgrep()
  1578.         {
  1579.             find . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*\.\(c\|h\|cc\|cpp\|S\|java\|xml\|sh\|mk\|aidl\)' -print0 | xargs -0 grep --color -n "$@"
  1580.         }
  1581.         ;;
  1582. esac
  1583.  
  1584. function gettargetarch
  1585. {
  1586.     get_build_var TARGET_ARCH
  1587. }
  1588.  
  1589. function ggrep()
  1590. {
  1591.     find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
  1592. }
  1593.  
  1594. function jgrep()
  1595. {
  1596.     find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
  1597. }
  1598.  
  1599. function cgrep()
  1600. {
  1601.     find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
  1602. }
  1603.  
  1604. function resgrep()
  1605. {
  1606.     for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
  1607. }
  1608.  
  1609. function mangrep()
  1610. {
  1611.     find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
  1612. }
  1613.  
  1614. function sepgrep()
  1615. {
  1616.     find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d -print0 | xargs -0 grep --color -n -r --exclude-dir=\.git "$@"
  1617. }
  1618.  
  1619. case `uname -s` in
  1620.     Darwin)
  1621.         function mgrep()
  1622.         {
  1623.             find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
  1624.         }
  1625.  
  1626.         function treegrep()
  1627.         {
  1628.             find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
  1629.         }
  1630.  
  1631.         ;;
  1632.     *)
  1633.         function mgrep()
  1634.         {
  1635.             find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
  1636.         }
  1637.  
  1638.         function treegrep()
  1639.         {
  1640.             find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
  1641.         }
  1642.  
  1643.         ;;
  1644. esac
  1645.  
  1646. function getprebuilt
  1647. {
  1648.     get_abs_build_var ANDROID_PREBUILTS
  1649. }
  1650.  
  1651. function tracedmdump()
  1652. {
  1653.     T=$(gettop)
  1654.     if [ ! "$T" ]; then
  1655.         echo "Couldn't locate the top of the tree.  Try setting TOP."
  1656.         return
  1657.     fi
  1658.     local prebuiltdir=$(getprebuilt)
  1659.     local arch=$(gettargetarch)
  1660.     local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
  1661.  
  1662.     local TRACE=$1
  1663.     if [ ! "$TRACE" ] ; then
  1664.         echo "usage:  tracedmdump  tracename"
  1665.         return
  1666.     fi
  1667.  
  1668.     if [ ! -r "$KERNEL" ] ; then
  1669.         echo "Error: cannot find kernel: '$KERNEL'"
  1670.         return
  1671.     fi
  1672.  
  1673.     local BASETRACE=$(basename $TRACE)
  1674.     if [ "$BASETRACE" = "$TRACE" ] ; then
  1675.         TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
  1676.     fi
  1677.  
  1678.     echo "post-processing traces..."
  1679.     rm -f $TRACE/qtrace.dexlist
  1680.     post_trace $TRACE
  1681.     if [ $? -ne 0 ]; then
  1682.         echo "***"
  1683.         echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
  1684.         echo "***"
  1685.         return
  1686.     fi
  1687.     echo "generating dexlist output..."
  1688.     /bin/ls $ANDROID_PRODUCT_OUT/system/framework/*.jar $ANDROID_PRODUCT_OUT/system/app/*.apk $ANDROID_PRODUCT_OUT/data/app/*.apk 2>/dev/null | xargs dexlist > $TRACE/qtrace.dexlist
  1689.     echo "generating dmtrace data..."
  1690.     q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
  1691.     echo "generating html file..."
  1692.     dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
  1693.     echo "done, see $TRACE/dmtrace.html for details"
  1694.     echo "or run:"
  1695.     echo "    traceview $TRACE/dmtrace"
  1696. }
  1697.  
  1698. # communicate with a running device or emulator, set up necessary state,
  1699. # and run the hat command.
  1700. function runhat()
  1701. {
  1702.     # process standard adb options
  1703.     local adbTarget=""
  1704.     if [ "$1" = "-d" -o "$1" = "-e" ]; then
  1705.         adbTarget=$1
  1706.         shift 1
  1707.     elif [ "$1" = "-s" ]; then
  1708.         adbTarget="$1 $2"
  1709.         shift 2
  1710.     fi
  1711.     local adbOptions=${adbTarget}
  1712.     #echo adbOptions = ${adbOptions}
  1713.  
  1714.     # runhat options
  1715.     local targetPid=$1
  1716.  
  1717.     if [ "$targetPid" = "" ]; then
  1718.         echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
  1719.         return
  1720.     fi
  1721.  
  1722.     # confirm hat is available
  1723.     if [ -z $(which hat) ]; then
  1724.         echo "hat is not available in this configuration."
  1725.         return
  1726.     fi
  1727.  
  1728.     # issue "am" command to cause the hprof dump
  1729.     local devFile=/data/local/tmp/hprof-$targetPid
  1730.     echo "Poking $targetPid and waiting for data..."
  1731.     echo "Storing data at $devFile"
  1732.     adb ${adbOptions} shell am dumpheap $targetPid $devFile
  1733.     echo "Press enter when logcat shows \"hprof: heap dump completed\""
  1734.     echo -n "> "
  1735.     read
  1736.  
  1737.     local localFile=/tmp/$$-hprof
  1738.  
  1739.     echo "Retrieving file $devFile..."
  1740.     adb ${adbOptions} pull $devFile $localFile
  1741.  
  1742.     adb ${adbOptions} shell rm $devFile
  1743.  
  1744.     echo "Running hat on $localFile"
  1745.     echo "View the output by pointing your browser at http://localhost:7000/"
  1746.     echo ""
  1747.     hat -JXmx512m $localFile
  1748. }
  1749.  
  1750. function getbugreports()
  1751. {
  1752.     local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
  1753.  
  1754.     if [ ! "$reports" ]; then
  1755.         echo "Could not locate any bugreports."
  1756.         return
  1757.     fi
  1758.  
  1759.     local report
  1760.     for report in ${reports[@]}
  1761.     do
  1762.         echo "/sdcard/bugreports/${report}"
  1763.         adb pull /sdcard/bugreports/${report} ${report}
  1764.         gunzip ${report}
  1765.     done
  1766. }
  1767.  
  1768. function getsdcardpath()
  1769. {
  1770.     adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
  1771. }
  1772.  
  1773. function getscreenshotpath()
  1774. {
  1775.     echo "$(getsdcardpath)/Pictures/Screenshots"
  1776. }
  1777.  
  1778. function getlastscreenshot()
  1779. {
  1780.     local screenshot_path=$(getscreenshotpath)
  1781.     local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
  1782.     if [ "$screenshot" = "" ]; then
  1783.         echo "No screenshots found."
  1784.         return
  1785.     fi
  1786.     echo "${screenshot}"
  1787.     adb ${adbOptions} pull ${screenshot_path}/${screenshot}
  1788. }
  1789.  
  1790. function startviewserver()
  1791. {
  1792.     local port=4939
  1793.     if [ $# -gt 0 ]; then
  1794.             port=$1
  1795.     fi
  1796.     adb shell service call window 1 i32 $port
  1797. }
  1798.  
  1799. function stopviewserver()
  1800. {
  1801.     adb shell service call window 2
  1802. }
  1803.  
  1804. function isviewserverstarted()
  1805. {
  1806.     adb shell service call window 3
  1807. }
  1808.  
  1809. function key_home()
  1810. {
  1811.     adb shell input keyevent 3
  1812. }
  1813.  
  1814. function key_back()
  1815. {
  1816.     adb shell input keyevent 4
  1817. }
  1818.  
  1819. function key_menu()
  1820. {
  1821.     adb shell input keyevent 82
  1822. }
  1823.  
  1824. function smoketest()
  1825. {
  1826.     if [ ! "$ANDROID_PRODUCT_OUT" ]; then
  1827.         echo "Couldn't locate output files.  Try running 'lunch' first." >&2
  1828.         return
  1829.     fi
  1830.     T=$(gettop)
  1831.     if [ ! "$T" ]; then
  1832.         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
  1833.         return
  1834.     fi
  1835.  
  1836.     (\cd "$T" && mmm tests/SmokeTest) &&
  1837.       adb uninstall com.android.smoketest > /dev/null &&
  1838.       adb uninstall com.android.smoketest.tests > /dev/null &&
  1839.       adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
  1840.       adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
  1841.       adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
  1842. }
  1843.  
  1844. # simple shortcut to the runtest command
  1845. function runtest()
  1846. {
  1847.     T=$(gettop)
  1848.     if [ ! "$T" ]; then
  1849.         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
  1850.         return
  1851.     fi
  1852.     ("$T"/development/testrunner/runtest.py $@)
  1853. }
  1854.  
  1855. function godir () {
  1856.     if [[ -z "$1" ]]; then
  1857.         echo "Usage: godir <regex>"
  1858.         return
  1859.     fi
  1860.     T=$(gettop)
  1861.     if [[ ! -f $T/filelist ]]; then
  1862.         echo -n "Creating index..."
  1863.         (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
  1864.         echo " Done"
  1865.         echo ""
  1866.     fi
  1867.     local lines
  1868.     lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
  1869.     if [[ ${#lines[@]} = 0 ]]; then
  1870.         echo "Not found"
  1871.         return
  1872.     fi
  1873.     local pathname
  1874.     local choice
  1875.     if [[ ${#lines[@]} > 1 ]]; then
  1876.         while [[ -z "$pathname" ]]; do
  1877.             local index=1
  1878.             local line
  1879.             for line in ${lines[@]}; do
  1880.                 printf "%6s %s\n" "[$index]" $line
  1881.                 index=$(($index + 1))
  1882.             done
  1883.             echo
  1884.             echo -n "Select one: "
  1885.             unset choice
  1886.             read choice
  1887.             if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
  1888.                 echo "Invalid choice"
  1889.                 continue
  1890.             fi
  1891.             pathname=${lines[$(($choice-1))]}
  1892.         done
  1893.     else
  1894.         pathname=${lines[0]}
  1895.     fi
  1896.     \cd $T/$pathname
  1897. }
  1898.  
  1899. function cmremote()
  1900. {
  1901.     git remote rm cmremote 2> /dev/null
  1902.     GERRIT_REMOTE=$(git config --get remote.github.projectname)
  1903.     if [ -z "$GERRIT_REMOTE" ]
  1904.     then
  1905.         echo Unable to set up the git remote, are you under a git repo?
  1906.         return 0
  1907.     fi
  1908.     CMUSER=$(git config --get review.review.cyanogenmod.org.username)
  1909.     if [ -z "$CMUSER" ]
  1910.     then
  1911.         git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
  1912.     else
  1913.         git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
  1914.     fi
  1915.     echo You can now push to "cmremote".
  1916. }
  1917.  
  1918. function aospremote()
  1919. {
  1920.     git remote rm aosp 2> /dev/null
  1921.     if [ ! -d .git ]
  1922.     then
  1923.         echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
  1924.     fi
  1925.     PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
  1926.     if (echo $PROJECT | grep -qv "^device")
  1927.     then
  1928.         PFX="platform/"
  1929.     fi
  1930.     git remote add aosp https://android.googlesource.com/$PFX$PROJECT
  1931.     echo "Remote 'aosp' created"
  1932. }
  1933.  
  1934. function cafremote()
  1935. {
  1936.     git remote rm caf 2> /dev/null
  1937.     if [ ! -d .git ]
  1938.     then
  1939.         echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
  1940.     fi
  1941.     PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
  1942.     if (echo $PROJECT | grep -qv "^device")
  1943.     then
  1944.         PFX="platform/"
  1945.     fi
  1946.     git remote add caf git://codeaurora.org/$PFX$PROJECT
  1947.     echo "Remote 'caf' created"
  1948. }
  1949.  
  1950. function installboot()
  1951. {
  1952.     if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
  1953.     then
  1954.         echo "No recovery.fstab found. Build recovery first."
  1955.         return 1
  1956.     fi
  1957.     if [ ! -e "$OUT/boot.img" ];
  1958.     then
  1959.         echo "No boot.img found. Run make bootimage first."
  1960.         return 1
  1961.     fi
  1962.     PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
  1963.     if [ -z "$PARTITION" ];
  1964.     then
  1965.         # Try for RECOVERY_FSTAB_VERSION = 2
  1966.         PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
  1967.         PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
  1968.         if [ -z "$PARTITION" ];
  1969.         then
  1970.             echo "Unable to determine boot partition."
  1971.             return 1
  1972.         fi
  1973.     fi
  1974.     adb start-server
  1975.     adb wait-for-online
  1976.     adb root
  1977.     sleep 1
  1978.     adb wait-for-online shell mount /system 2>&1 > /dev/null
  1979.     adb wait-for-online remount
  1980.     if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
  1981.     then
  1982.         adb push $OUT/boot.img /cache/
  1983.         for i in $OUT/system/lib/modules/*;
  1984.         do
  1985.             adb push $i /system/lib/modules/
  1986.         done
  1987.         adb shell dd if=/cache/boot.img of=$PARTITION
  1988.         adb shell chmod 644 /system/lib/modules/*
  1989.         echo "Installation complete."
  1990.     else
  1991.         echo "The connected device does not appear to be $CM_BUILD, run away!"
  1992.     fi
  1993. }
  1994.  
  1995. function installrecovery()
  1996. {
  1997.     if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
  1998.     then
  1999.         echo "No recovery.fstab found. Build recovery first."
  2000.         return 1
  2001.     fi
  2002.     if [ ! -e "$OUT/recovery.img" ];
  2003.     then
  2004.         echo "No recovery.img found. Run make recoveryimage first."
  2005.         return 1
  2006.     fi
  2007.     PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
  2008.     if [ -z "$PARTITION" ];
  2009.     then
  2010.         # Try for RECOVERY_FSTAB_VERSION = 2
  2011.         PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
  2012.         PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
  2013.         if [ -z "$PARTITION" ];
  2014.         then
  2015.             echo "Unable to determine recovery partition."
  2016.             return 1
  2017.         fi
  2018.     fi
  2019.     adb start-server
  2020.     adb wait-for-online
  2021.     adb root
  2022.     sleep 1
  2023.     adb wait-for-online shell mount /system 2>&1 >> /dev/null
  2024.     adb wait-for-online remount
  2025.     if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
  2026.     then
  2027.         adb push $OUT/recovery.img /cache/
  2028.         adb shell dd if=/cache/recovery.img of=$PARTITION
  2029.         echo "Installation complete."
  2030.     else
  2031.         echo "The connected device does not appear to be $CM_BUILD, run away!"
  2032.     fi
  2033. }
  2034.  
  2035. function makerecipe() {
  2036.   if [ -z "$1" ]
  2037.   then
  2038.     echo "No branch name provided."
  2039.     return 1
  2040.   fi
  2041.   cd android
  2042.   sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
  2043.   git commit -a -m "$1"
  2044.   cd ..
  2045.  
  2046.   repo forall -c '
  2047.  
  2048.  if [ "$REPO_REMOTE" == "github" ]
  2049.  then
  2050.    pwd
  2051.    cmremote
  2052.    git push cmremote HEAD:refs/heads/'$1'
  2053.  fi
  2054.  '
  2055. }
  2056.  
  2057. function cmgerrit() {
  2058.     if [ $# -eq 0 ]; then
  2059.         $FUNCNAME help
  2060.         return 1
  2061.     fi
  2062.     local user=`git config --get review.review.cyanogenmod.org.username`
  2063.     local review=`git config --get remote.github.review`
  2064.     local project=`git config --get remote.github.projectname`
  2065.     local command=$1
  2066.     shift
  2067.     case $command in
  2068.         help)
  2069.             if [ $# -eq 0 ]; then
  2070.                 cat <<EOF
  2071. Usage:
  2072.     $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
  2073.  
  2074. Commands:
  2075.     fetch   Just fetch the change as FETCH_HEAD
  2076.     help    Show this help, or for a specific command
  2077.     pull    Pull a change into current branch
  2078.     push    Push HEAD or a local branch to Gerrit for a specific branch
  2079.  
  2080. Any other Git commands that support refname would work as:
  2081.     git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
  2082.  
  2083. See '$FUNCNAME help COMMAND' for more information on a specific command.
  2084.  
  2085. Example:
  2086.     $FUNCNAME checkout -b topic 1234/5
  2087. works as:
  2088.     git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
  2089.       && git checkout -b topic FETCH_HEAD
  2090. will checkout a new branch 'topic' base on patch-set 5 of change 1234.
  2091. Patch-set 1 will be fetched if omitted.
  2092. EOF
  2093.                 return
  2094.             fi
  2095.             case $1 in
  2096.                 __cmg_*) echo "For internal use only." ;;
  2097.                 changes|for)
  2098.                     if [ "$FUNCNAME" = "cmgerrit" ]; then
  2099.                         echo "'$FUNCNAME $1' is deprecated."
  2100.                     fi
  2101.                     ;;
  2102.                 help) $FUNCNAME help ;;
  2103.                 fetch|pull) cat <<EOF
  2104. usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
  2105.  
  2106. works as:
  2107.     git $1 OPTIONS http://DOMAIN/p/PROJECT \\
  2108.       refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
  2109.  
  2110. Example:
  2111.     $FUNCNAME $1 1234
  2112. will $1 patch-set 1 of change 1234
  2113. EOF
  2114.                     ;;
  2115.                 push) cat <<EOF
  2116. usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
  2117.  
  2118. works as:
  2119.     git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
  2120.       {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
  2121.  
  2122. Example:
  2123.     $FUNCNAME push fix6789:gingerbread
  2124. will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
  2125. HEAD will be pushed from local if omitted.
  2126. EOF
  2127.                     ;;
  2128.                 *)
  2129.                     $FUNCNAME __cmg_err_not_supported $1 && return
  2130.                     cat <<EOF
  2131. usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
  2132.  
  2133. works as:
  2134.     git fetch http://DOMAIN/p/PROJECT \\
  2135.       refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
  2136.       && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
  2137. EOF
  2138.                     ;;
  2139.             esac
  2140.             ;;
  2141.         __cmg_get_ref)
  2142.             $FUNCNAME __cmg_err_no_arg $command $# && return 1
  2143.             local change_id patchset_id hash
  2144.             case $1 in
  2145.                 */*)
  2146.                     change_id=${1%%/*}
  2147.                     patchset_id=${1#*/}
  2148.                     ;;
  2149.                 *)
  2150.                     change_id=$1
  2151.                     patchset_id=1
  2152.                     ;;
  2153.             esac
  2154.             hash=$(($change_id % 100))
  2155.             case $hash in
  2156.                 [0-9]) hash="0$hash" ;;
  2157.             esac
  2158.             echo "refs/changes/$hash/$change_id/$patchset_id"
  2159.             ;;
  2160.         fetch|pull)
  2161.             $FUNCNAME __cmg_err_no_arg $command $# help && return 1
  2162.             $FUNCNAME __cmg_err_not_repo && return 1
  2163.             local change=$1
  2164.             shift
  2165.             git $command $@ http://$review/p/$project \
  2166.                 $($FUNCNAME __cmg_get_ref $change) || return 1
  2167.             ;;
  2168.         push)
  2169.             $FUNCNAME __cmg_err_no_arg $command $# help && return 1
  2170.             $FUNCNAME __cmg_err_not_repo && return 1
  2171.             if [ -z "$user" ]; then
  2172.                 echo >&2 "Gerrit username not found."
  2173.                 return 1
  2174.             fi
  2175.             local local_branch remote_branch
  2176.             case $1 in
  2177.                 *:*)
  2178.                     local_branch=${1%:*}
  2179.                     remote_branch=${1##*:}
  2180.                     ;;
  2181.                 *)
  2182.                     local_branch=HEAD
  2183.                     remote_branch=$1
  2184.                     ;;
  2185.             esac
  2186.             shift
  2187.             git push $@ ssh://$user@$review:29418/$project \
  2188.                 $local_branch:refs/for/$remote_branch || return 1
  2189.             ;;
  2190.         changes|for)
  2191.             if [ "$FUNCNAME" = "cmgerrit" ]; then
  2192.                 echo >&2 "'$FUNCNAME $command' is deprecated."
  2193.             fi
  2194.             ;;
  2195.         __cmg_err_no_arg)
  2196.             if [ $# -lt 2 ]; then
  2197.                 echo >&2 "'$FUNCNAME $command' missing argument."
  2198.             elif [ $2 -eq 0 ]; then
  2199.                 if [ -n "$3" ]; then
  2200.                     $FUNCNAME help $1
  2201.                 else
  2202.                     echo >&2 "'$FUNCNAME $1' missing argument."
  2203.                 fi
  2204.             else
  2205.                 return 1
  2206.             fi
  2207.             ;;
  2208.         __cmg_err_not_repo)
  2209.             if [ -z "$review" -o -z "$project" ]; then
  2210.                 echo >&2 "Not currently in any reviewable repository."
  2211.             else
  2212.                 return 1
  2213.             fi
  2214.             ;;
  2215.         __cmg_err_not_supported)
  2216.             $FUNCNAME __cmg_err_no_arg $command $# && return
  2217.             case $1 in
  2218.                 #TODO: filter more git commands that don't use refname
  2219.                 init|add|rm|mv|status|clone|remote|bisect|config|stash)
  2220.                     echo >&2 "'$FUNCNAME $1' is not supported."
  2221.                     ;;
  2222.                 *) return 1 ;;
  2223.             esac
  2224.             ;;
  2225.     #TODO: other special cases?
  2226.         *)
  2227.             $FUNCNAME __cmg_err_not_supported $command && return 1
  2228.             $FUNCNAME __cmg_err_no_arg $command $# help && return 1
  2229.             $FUNCNAME __cmg_err_not_repo && return 1
  2230.             local args="$@"
  2231.             local change pre_args refs_arg post_args
  2232.             case "$args" in
  2233.                 *--\ *)
  2234.                     pre_args=${args%%-- *}
  2235.                     post_args="-- ${args#*-- }"
  2236.                     ;;
  2237.                 *) pre_args="$args" ;;
  2238.             esac
  2239.             args=($pre_args)
  2240.             pre_args=
  2241.             if [ ${#args[@]} -gt 0 ]; then
  2242.                 change=${args[${#args[@]}-1]}
  2243.             fi
  2244.             if [ ${#args[@]} -gt 1 ]; then
  2245.                 pre_args=${args[0]}
  2246.                 for ((i=1; i<${#args[@]}-1; i++)); do
  2247.                     pre_args="$pre_args ${args[$i]}"
  2248.                 done
  2249.             fi
  2250.             while ((1)); do
  2251.                 case $change in
  2252.                     ""|--)
  2253.                         $FUNCNAME help $command
  2254.                         return 1
  2255.                         ;;
  2256.                     *@*)
  2257.                         if [ -z "$refs_arg" ]; then
  2258.                             refs_arg="@${change#*@}"
  2259.                             change=${change%%@*}
  2260.                         fi
  2261.                         ;;
  2262.                     *~*)
  2263.                         if [ -z "$refs_arg" ]; then
  2264.                             refs_arg="~${change#*~}"
  2265.                             change=${change%%~*}
  2266.                         fi
  2267.                         ;;
  2268.                     *^*)
  2269.                         if [ -z "$refs_arg" ]; then
  2270.                             refs_arg="^${change#*^}"
  2271.                             change=${change%%^*}
  2272.                         fi
  2273.                         ;;
  2274.                     *:*)
  2275.                         if [ -z "$refs_arg" ]; then
  2276.                             refs_arg=":${change#*:}"
  2277.                             change=${change%%:*}
  2278.                         fi
  2279.                         ;;
  2280.                     *) break ;;
  2281.                 esac
  2282.             done
  2283.             $FUNCNAME fetch $change \
  2284.                 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
  2285.                 || return 1
  2286.             ;;
  2287.     esac
  2288. }
  2289.  
  2290. function cmrebase() {
  2291.     local repo=$1
  2292.     local refs=$2
  2293.     local pwd="$(pwd)"
  2294.     local dir="$(gettop)/$repo"
  2295.  
  2296.     if [ -z $repo ] || [ -z $refs ]; then
  2297.         echo "CyanogenMod Gerrit Rebase Usage: "
  2298.         echo "      cmrebase <path to project> <patch IDs on Gerrit>"
  2299.         echo "      The patch IDs appear on the Gerrit commands that are offered."
  2300.         echo "      They consist on a series of numbers and slashes, after the text"
  2301.         echo "      refs/changes. For example, the ID in the following command is 26/8126/2"
  2302.         echo ""
  2303.         echo "      git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
  2304.         echo ""
  2305.         return
  2306.     fi
  2307.  
  2308.     if [ ! -d $dir ]; then
  2309.         echo "Directory $dir doesn't exist in tree."
  2310.         return
  2311.     fi
  2312.     cd $dir
  2313.     repo=$(cat .git/config  | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
  2314.     echo "Starting branch..."
  2315.     repo start tmprebase .
  2316.     echo "Bringing it up to date..."
  2317.     repo sync .
  2318.     echo "Fetching change..."
  2319.     git fetch "http://review.cyanogenmod.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
  2320.     if [ "$?" != "0" ]; then
  2321.         echo "Error cherry-picking. Not uploading!"
  2322.         return
  2323.     fi
  2324.     echo "Uploading..."
  2325.     repo upload .
  2326.     echo "Cleaning up..."
  2327.     repo abandon tmprebase .
  2328.     cd $pwd
  2329. }
  2330.  
  2331. function mka() {
  2332.     case `uname -s` in
  2333.         Darwin)
  2334.             make -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
  2335.             ;;
  2336.         *)
  2337.             mk_timer schedtool -B -n 1 -e ionice -n 1 make -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
  2338.             ;;
  2339.     esac
  2340. }
  2341.  
  2342. function cmka() {
  2343.     if [ ! -z "$1" ]; then
  2344.         for i in "$@"; do
  2345.             case $i in
  2346.                 bacon|otapackage|systemimage)
  2347.                     mka installclean
  2348.                     mka $i
  2349.                     ;;
  2350.                 *)
  2351.                     mka clean-$i
  2352.                     mka $i
  2353.                     ;;
  2354.             esac
  2355.         done
  2356.     else
  2357.         mka clean
  2358.         mka
  2359.     fi
  2360. }
  2361.  
  2362. function repolastsync() {
  2363.     RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
  2364.     RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
  2365.     RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
  2366.     echo "Last repo sync: $RLSLOCAL / $RLSUTC"
  2367. }
  2368.  
  2369. function reposync() {
  2370.     case `uname -s` in
  2371.         Darwin)
  2372.             repo sync -j 4 "$@"
  2373.             ;;
  2374.         *)
  2375.             schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
  2376.             ;;
  2377.     esac
  2378. }
  2379.  
  2380. function repodiff() {
  2381.     if [ -z "$*" ]; then
  2382.         echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
  2383.         return
  2384.     fi
  2385.     diffopts=$* repo forall -c \
  2386.       'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
  2387. }
  2388.  
  2389. # Credit for color strip sed: http://goo.gl/BoIcm
  2390. function dopush()
  2391. {
  2392.     local func=$1
  2393.     shift
  2394.  
  2395.     adb start-server # Prevent unexpected starting server message from adb get-state in the next line
  2396.     if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
  2397.         echo "No device is online. Waiting for one..."
  2398.         echo "Please connect USB and/or enable USB debugging"
  2399.         until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
  2400.             sleep 1
  2401.         done
  2402.         echo "Device Found."
  2403.     fi
  2404.  
  2405.     if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD") || [ "$FORCE_PUSH" == "true" ];
  2406.     then
  2407.     # retrieve IP and PORT info if we're using a TCP connection
  2408.     TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
  2409.         | head -1 | awk '{print $1}')
  2410.     adb root &> /dev/null
  2411.     sleep 0.3
  2412.     if [ -n "$TCPIPPORT" ]
  2413.     then
  2414.         # adb root just killed our connection
  2415.         # so reconnect...
  2416.         adb connect "$TCPIPPORT"
  2417.     fi
  2418.     adb wait-for-device &> /dev/null
  2419.     sleep 0.3
  2420.     adb remount &> /dev/null
  2421.  
  2422.     mkdir -p $OUT
  2423.     ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
  2424.     ret=$?;
  2425.     if [ $ret -ne 0 ]; then
  2426.         rm -f $OUT/.log;return $ret
  2427.     fi
  2428.  
  2429.     # Install: <file>
  2430.     LOC="$(cat $OUT/.log | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | grep '^Install: ' | cut -d ':' -f 2)"
  2431.  
  2432.     # Copy: <file>
  2433.     LOC="$LOC $(cat $OUT/.log | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | grep '^Copy: ' | cut -d ':' -f 2)"
  2434.  
  2435.     # If any files are going to /data, push an octal file permissions reader to device
  2436.     if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
  2437.         CHKPERM="/data/local/tmp/chkfileperm.sh"
  2438. (
  2439. cat <<'EOF'
  2440. #!/system/xbin/sh
  2441. FILE=$@
  2442. if [ -e $FILE ]; then
  2443.     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
  2444. fi
  2445. EOF
  2446. ) > $OUT/.chkfileperm.sh
  2447.         echo "Pushing file permissions checker to device"
  2448.         adb push $OUT/.chkfileperm.sh $CHKPERM
  2449.         adb shell chmod 755 $CHKPERM
  2450.         rm -f $OUT/.chkfileperm.sh
  2451.     fi
  2452.  
  2453.     stop_n_start=false
  2454.     for FILE in $LOC; do
  2455.         # Make sure file is in $OUT/system or $OUT/data
  2456.         case $FILE in
  2457.             $OUT/system/*|$OUT/data/*)
  2458.                 # Get target file name (i.e. /system/bin/adb)
  2459.                 TARGET=$(echo $FILE | sed "s#$OUT##")
  2460.             ;;
  2461.             *) continue ;;
  2462.         esac
  2463.  
  2464.         case $TARGET in
  2465.             /data/*)
  2466.                 # fs_config only sets permissions and se labels for files pushed to /system
  2467.                 if [ -n "$CHKPERM" ]; then
  2468.                     OLDPERM=$(adb shell $CHKPERM $TARGET)
  2469.                     OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
  2470.                     OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
  2471.                     OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
  2472.                 fi
  2473.                 echo "Pushing: $TARGET"
  2474.                 adb push $FILE $TARGET
  2475.                 if [ -n "$OLDPERM" ]; then
  2476.                     echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
  2477.                     adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
  2478.                     adb shell chmod "$OLDPERM" $TARGET
  2479.                 else
  2480.                     echo "$TARGET did not exist previously, you should set file permissions manually"
  2481.                 fi
  2482.                 adb shell restorecon "$TARGET"
  2483.             ;;
  2484.             /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
  2485.                 # Only need to stop services once
  2486.                 if ! $stop_n_start; then
  2487.                     adb shell stop
  2488.                     stop_n_start=true
  2489.                 fi
  2490.                 echo "Pushing: $TARGET"
  2491.                 adb push $FILE $TARGET
  2492.             ;;
  2493.             *)
  2494.                 echo "Pushing: $TARGET"
  2495.                 adb push $FILE $TARGET
  2496.             ;;
  2497.         esac
  2498.     done
  2499.     if [ -n "$CHKPERM" ]; then
  2500.         adb shell rm $CHKPERM
  2501.     fi
  2502.     if $stop_n_start; then
  2503.         adb shell start
  2504.     fi
  2505.     rm -f $OUT/.log
  2506.     return 0
  2507.     else
  2508.         echo "The connected device does not appear to be $CM_BUILD, run away!"
  2509.     fi
  2510. }
  2511.  
  2512. alias mmp='dopush mm'
  2513. alias mmmp='dopush mmm'
  2514. alias mkap='dopush mka'
  2515. alias cmkap='dopush cmka'
  2516.  
  2517. function repopick() {
  2518.     T=$(gettop)
  2519.     $T/build/tools/repopick.py $@
  2520. }
  2521.  
  2522. function fixup_common_out_dir() {
  2523.     common_out_dir=$(get_build_var OUT_DIR)/target/common
  2524.     target_device=$(get_build_var TARGET_DEVICE)
  2525.     if [ ! -z $CM_FIXUP_COMMON_OUT ]; then
  2526.         if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
  2527.             mv ${common_out_dir} ${common_out_dir}-${target_device}
  2528.             ln -s ${common_out_dir}-${target_device} ${common_out_dir}
  2529.         else
  2530.             [ -L ${common_out_dir} ] && rm ${common_out_dir}
  2531.             mkdir -p ${common_out_dir}-${target_device}
  2532.             ln -s ${common_out_dir}-${target_device} ${common_out_dir}
  2533.         fi
  2534.     else
  2535.         [ -L ${common_out_dir} ] && rm ${common_out_dir}
  2536.         mkdir -p ${common_out_dir}
  2537.     fi
  2538. }
  2539.  
  2540. # Force JAVA_HOME to point to java 1.7 or java 1.6  if it isn't already set.
  2541. #
  2542. # Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
  2543. # For some reason, installing the JDK doesn't make it show up in the
  2544. # JavaVM.framework/Versions/1.7/ folder.
  2545. function set_java_home() {
  2546.     # Clear the existing JAVA_HOME value if we set it ourselves, so that
  2547.     # we can reset it later, depending on the version of java the build
  2548.     # system needs.
  2549.     #
  2550.     # If we don't do this, the JAVA_HOME value set by the first call to
  2551.     # build/envsetup.sh will persist forever.
  2552.     if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
  2553.       export JAVA_HOME=""
  2554.     fi
  2555.  
  2556.     if [ ! "$JAVA_HOME" ]; then
  2557.       if [ -n "$LEGACY_USE_JAVA6" ]; then
  2558.         case `uname -s` in
  2559.             Darwin)
  2560.                 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
  2561.                 ;;
  2562.             *)
  2563.                 export JAVA_HOME=/usr/lib/jvm/java-6-sun
  2564.                 ;;
  2565.         esac
  2566.       else
  2567.         case `uname -s` in
  2568.             Darwin)
  2569.                 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
  2570.                 ;;
  2571.             *)
  2572.                 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  2573.                 ;;
  2574.         esac
  2575.       fi
  2576.  
  2577.       # Keep track of the fact that we set JAVA_HOME ourselves, so that
  2578.       # we can change it on the next envsetup.sh, if required.
  2579.       export ANDROID_SET_JAVA_HOME=true
  2580.     fi
  2581. }
  2582.  
  2583. # Print colored exit condition
  2584. function pez {
  2585.     "$@"
  2586.     local retval=$?
  2587.     if [ $retval -ne 0 ]
  2588.     then
  2589.         echo -e "\e[0;31mFAILURE\e[00m"
  2590.     else
  2591.         echo -e "\e[0;32mSUCCESS\e[00m"
  2592.     fi
  2593.     return $retval
  2594. }
  2595.  
  2596. function get_make_command()
  2597. {
  2598.   echo command make
  2599. }
  2600.  
  2601. function mk_timer()
  2602. {
  2603.     local start_time=$(date +"%s")
  2604.     $@
  2605.     local ret=$?
  2606.     local end_time=$(date +"%s")
  2607.     local tdiff=$(($end_time-$start_time))
  2608.     local hours=$(($tdiff / 3600 ))
  2609.     local mins=$((($tdiff % 3600) / 60))
  2610.     local secs=$(($tdiff % 60))
  2611.     echo
  2612.     if [ $ret -eq 0 ] ; then
  2613.         echo -n -e "#### make completed successfully "
  2614.     else
  2615.         echo -n -e "#### make failed to build some targets "
  2616.     fi
  2617.     if [ $hours -gt 0 ] ; then
  2618.         printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
  2619.     elif [ $mins -gt 0 ] ; then
  2620.         printf "(%02g:%02g (mm:ss))" $mins $secs
  2621.     elif [ $secs -gt 0 ] ; then
  2622.         printf "(%s seconds)" $secs
  2623.     fi
  2624.     echo -e " ####"
  2625.     echo
  2626.     return $ret
  2627. }
  2628.  
  2629. function make()
  2630. {
  2631.     mk_timer $(get_make_command) "$@"
  2632. }
  2633.  
  2634.  
  2635.  
  2636. if [ "x$SHELL" != "x/bin/bash" ]; then
  2637.     case `ps -o command -p $$` in
  2638.         *bash*)
  2639.             ;;
  2640.         *zsh*)
  2641.             ;;
  2642.         *)
  2643.             echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
  2644.             ;;
  2645.     esac
  2646. fi
  2647.  
  2648. # Execute the contents of any vendorsetup.sh files we can find.
  2649. for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \
  2650.          `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null`
  2651. do
  2652.     echo "including $f"
  2653.     . $f
  2654. done
  2655. unset f
  2656.  
  2657. # Add completions
  2658. check_bash_version && {
  2659.     dirs="sdk/bash_completion vendor/cm/bash_completion"
  2660.     for dir in $dirs; do
  2661.     if [ -d ${dir} ]; then
  2662.         for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
  2663.             echo "including $f"
  2664.             . $f
  2665.         done
  2666.     fi
  2667.     done
  2668. }
  2669.  
  2670. export ANDROID_BUILD_TOP=$(gettop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement