envsetup.sh 47.7 KB
Newer Older
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 7
- croot:   Changes directory to the top of the tree.
- m:       Makes from the top of the tree.
Ying Wang's avatar
Ying Wang committed
8 9
- mm:      Builds all of the modules in the current directory, but not their dependencies.
- mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
10
           To limit the modules being built use the syntax: mmm dir/:target1,target2.
Ying Wang's avatar
Ying Wang committed
11 12
- mma:     Builds all of the modules in the current directory, and their dependencies.
- mmma:    Builds all of the modules in the supplied directories, and their dependencies.
13
- cgrep:   Greps on all local C/C++ files.
Jon Boekenoogen's avatar
ggrep  
Jon Boekenoogen committed
14
- ggrep:   Greps on all local Gradle files.
15 16
- jgrep:   Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
17 18
- mangrep: Greps on all local AndroidManifest.xml files.
- sepgrep: Greps on all local sepolicy files.
19
- sgrep:   Greps on all local source files.
20
- godir:   Go to the directory containing a file.
21

Dan Albert's avatar
Dan Albert committed
22 23 24 25 26
Environemnt options:
- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
                 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
                 build is leak-check clean.

27 28 29 30 31
Look at the source to view more functions. The complete list is:
EOF
    T=$(gettop)
    local A
    A=""
32
    for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
33 34 35 36 37 38 39 40 41 42 43 44 45
      A="$A $i"
    done
    echo $A
}

# Get the value of a build variable as an absolute path.
function get_abs_build_var()
{
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
46
    (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
47
      command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
48 49 50 51 52 53 54 55 56 57
}

# Get the exact value of a build variable.
function get_build_var()
{
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
58
    (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
59
      command make --no-print-directory -f build/core/config.mk dumpvar-$1)
60 61 62 63 64 65 66 67 68 69
}

# check to see if the supplied product is one we can build
function check_product()
{
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
70 71 72
        TARGET_PRODUCT=$1 \
        TARGET_BUILD_VARIANT= \
        TARGET_BUILD_TYPE= \
Joe Onorato's avatar
Joe Onorato committed
73
        TARGET_BUILD_APPS= \
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        get_build_var TARGET_DEVICE > /dev/null
    # hide successful answers, but allow the errors to show
}

VARIANT_CHOICES=(user userdebug eng)

# check to see if the supplied variant is valid
function check_variant()
{
    for v in ${VARIANT_CHOICES[@]}
    do
        if [ "$v" = "$1" ]
        then
            return 0
        fi
    done
    return 1
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

function setpaths()
{
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP."
        return
    fi

    ##################################################################
    #                                                                #
    #              Read me before you modify this code               #
    #                                                                #
    #   This function sets ANDROID_BUILD_PATHS to what it is adding  #
    #   to PATH, and the next time it is run, it removes that from   #
    #   PATH.  This is required so lunch can be run more than once   #
    #   and still have working paths.                                #
    #                                                                #
    ##################################################################

112 113 114
    # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
    # due to "C:\Program Files" being in the path.

115
    # out with the old
116
    if [ -n "$ANDROID_BUILD_PATHS" ] ; then
117 118
        export PATH=${PATH/$ANDROID_BUILD_PATHS/}
    fi
119
    if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
120
        export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
121 122
        # strip leading ':', if any
        export PATH=${PATH/:%/}
123
    fi
124 125 126

    # and in with the new
    prebuiltdir=$(getprebuilt)
127
    gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
128

129 130
    # defined in core/config.mk
    targetgccversion=$(get_build_var TARGET_GCC_VERSION)
131
    targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
132
    export TARGET_GCC_VERSION=$targetgccversion
133

134
    # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
135 136
    export ANDROID_TOOLCHAIN=
    export ANDROID_TOOLCHAIN_2ND_ARCH=
137 138
    local ARCH=$(get_build_var TARGET_ARCH)
    case $ARCH in
139
        x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn's avatar
Mark D Horn committed
140
            ;;
Pavel Chupin's avatar
Pavel Chupin committed
141 142
        x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
            ;;
143
        arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
144
            ;;
145
        arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
146
               toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
147
            ;;
148
        mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu's avatar
Serban Constantinescu committed
149
            ;;
150 151 152
        *)
            echo "Can't find toolchain for unknown architecture: $ARCH"
            toolchaindir=xxxxxxxxx
Mark D Horn's avatar
Mark D Horn committed
153 154
            ;;
    esac
155
    if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
156
        export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
157
    fi
158

159 160 161 162 163
    if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
        export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
    fi

    unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang's avatar
Ying Wang committed
164
    case $ARCH in
Bruce Beare's avatar
Bruce Beare committed
165
        arm)
166
            # Legacy toolchain configuration used for ARM kernel compilation
167
            toolchaindir=arm/arm-eabi-$targetgccversion/bin
Bruce Beare's avatar
Bruce Beare committed
168
            if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
169 170
                 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
                 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare's avatar
Bruce Beare committed
171
            fi
Ying Wang's avatar
Ying Wang committed
172 173
            ;;
        *)
Bruce Beare's avatar
Bruce Beare committed
174
            # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang's avatar
Ying Wang committed
175 176 177
            ;;
    esac

178
    export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
179
    export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_KERNEL_TOOLCHAIN_PATH$ANDROID_DEV_SCRIPTS:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
    # to ensure that the corresponding 'emulator' binaries are used.
    case $(uname -s) in
        Darwin)
            ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
            ;;
        Linux)
            ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
            ;;
        *)
            ANDROID_EMULATOR_PREBUILTS=
            ;;
    esac
    if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
195
        ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
196 197 198
        export ANDROID_EMULATOR_PREBUILTS
    fi

199
    export PATH=$ANDROID_BUILD_PATHS$PATH
200

201
    unset ANDROID_JAVA_TOOLCHAIN
202
    unset ANDROID_PRE_BUILD_PATHS
203 204
    if [ -n "$JAVA_HOME" ]; then
        export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
205 206
        export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
        export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
207 208
    fi

209
    unset ANDROID_PRODUCT_OUT
210 211 212
    export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
    export OUT=$ANDROID_PRODUCT_OUT

213 214 215
    unset ANDROID_HOST_OUT
    export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)

216
    # needed for building linux on MacOS
217 218 219 220 221 222
    # TODO: fix the path
    #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
}

function printconfig()
{
223 224 225 226 227 228
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
    get_build_var report_config
229 230 231 232
}

function set_stuff_for_environment()
{
233
    settitle
234
    set_java_home
235 236
    setpaths
    set_sequence_number
237

238
    export ANDROID_BUILD_TOP=$(gettop)
Ben Cheng's avatar
Ben Cheng committed
239 240
    # With this environment variable new GCC can apply colors to warnings/errors
    export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
Dan Albert's avatar
Dan Albert committed
241
    export ASAN_OPTIONS=detect_leaks=0
242 243 244 245
}

function set_sequence_number()
{
246
    export BUILD_ENV_SEQUENCE_NUMBER=10
247 248 249 250
}

function settitle()
{
251
    if [ "$STAY_OFF_MY_LAWN" = "" ]; then
252
        local arch=$(gettargetarch)
Joe Onorato's avatar
Joe Onorato committed
253 254 255 256
        local product=$TARGET_PRODUCT
        local variant=$TARGET_BUILD_VARIANT
        local apps=$TARGET_BUILD_APPS
        if [ -z "$apps" ]; then
257
            export PROMPT_COMMAND="echo -ne \"\033]0;[${arch}-${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
Joe Onorato's avatar
Joe Onorato committed
258
        else
259
            export PROMPT_COMMAND="echo -ne \"\033]0;[$arch $apps $variant] ${USER}@${HOSTNAME}: ${PWD}\007\""
Joe Onorato's avatar
Joe Onorato committed
260
        fi
261
    fi
262 263
}

Kenny Root's avatar
Kenny Root committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
function addcompletions()
{
    local T dir f

    # Keep us from trying to run in something that isn't bash.
    if [ -z "${BASH_VERSION}" ]; then
        return
    fi

    # Keep us from trying to run in bash that's too old.
    if [ ${BASH_VERSINFO[0]} -lt 3 ]; then
        return
    fi

    dir="sdk/bash_completion"
    if [ -d ${dir} ]; then
280
        for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
Kenny Root's avatar
Kenny Root committed
281 282 283 284 285 286
            echo "including $f"
            . $f
        done
    fi
}

287 288 289 290 291 292 293
function choosetype()
{
    echo "Build type choices are:"
    echo "     1. release"
    echo "     2. debug"
    echo

294
    local DEFAULT_NUM DEFAULT_VALUE
295 296
    DEFAULT_NUM=1
    DEFAULT_VALUE=release
297

298 299
    export TARGET_BUILD_TYPE=
    local ANSWER
300 301 302
    while [ -z $TARGET_BUILD_TYPE ]
    do
        echo -n "Which would you like? ["$DEFAULT_NUM"] "
303 304 305 306 307 308
        if [ -z "$1" ] ; then
            read ANSWER
        else
            echo $1
            ANSWER=$1
        fi
309 310 311 312 313 314 315
        case $ANSWER in
        "")
            export TARGET_BUILD_TYPE=$DEFAULT_VALUE
            ;;
        1)
            export TARGET_BUILD_TYPE=release
            ;;
316 317 318
        release)
            export TARGET_BUILD_TYPE=release
            ;;
319 320 321
        2)
            export TARGET_BUILD_TYPE=debug
            ;;
322 323 324
        debug)
            export TARGET_BUILD_TYPE=debug
            ;;
325 326 327 328 329 330
        *)
            echo
            echo "I didn't understand your response.  Please try again."
            echo
            ;;
        esac
331 332 333
        if [ -n "$1" ] ; then
            break
        fi
334 335 336 337 338
    done

    set_stuff_for_environment
}

339 340 341 342 343 344
#
# This function isn't really right:  It chooses a TARGET_PRODUCT
# based on the list of boards.  Usually, that gets you something
# that kinda works with a generic product, but really, you should
# pick a product by name.
#
345 346 347 348 349
function chooseproduct()
{
    if [ "x$TARGET_PRODUCT" != x ] ; then
        default_value=$TARGET_PRODUCT
    else
350
        default_value=full
351 352
    fi

353 354
    export TARGET_PRODUCT=
    local ANSWER
355 356
    while [ -z "$TARGET_PRODUCT" ]
    do
357
        echo -n "Which product would you like? [$default_value] "
358 359 360 361 362 363 364
        if [ -z "$1" ] ; then
            read ANSWER
        else
            echo $1
            ANSWER=$1
        fi

365 366
        if [ -z "$ANSWER" ] ; then
            export TARGET_PRODUCT=$default_value
367 368 369 370 371 372 373 374 375 376
        else
            if check_product $ANSWER
            then
                export TARGET_PRODUCT=$ANSWER
            else
                echo "** Not a valid product: $ANSWER"
            fi
        fi
        if [ -n "$1" ] ; then
            break
377 378 379 380 381 382
        fi
    done

    set_stuff_for_environment
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
function choosevariant()
{
    echo "Variant choices are:"
    local index=1
    local v
    for v in ${VARIANT_CHOICES[@]}
    do
        # The product name is the name of the directory containing
        # the makefile we found, above.
        echo "     $index. $v"
        index=$(($index+1))
    done

    local default_value=eng
    local ANSWER

    export TARGET_BUILD_VARIANT=
    while [ -z "$TARGET_BUILD_VARIANT" ]
    do
        echo -n "Which would you like? [$default_value] "
        if [ -z "$1" ] ; then
            read ANSWER
        else
            echo $1
            ANSWER=$1
        fi

        if [ -z "$ANSWER" ] ; then
            export TARGET_BUILD_VARIANT=$default_value
        elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
            if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
414
                export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
            fi
        else
            if check_variant $ANSWER
            then
                export TARGET_BUILD_VARIANT=$ANSWER
            else
                echo "** Not a valid variant: $ANSWER"
            fi
        fi
        if [ -n "$1" ] ; then
            break
        fi
    done
}

430 431
function choosecombo()
{
432
    choosetype $1
433 434 435

    echo
    echo
436
    chooseproduct $2
437 438 439

    echo
    echo
440
    choosevariant $3
441

442 443
    echo
    set_stuff_for_environment
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    printconfig
}

# Clear this variable.  It will be built up again when the vendorsetup.sh
# files are included at the end of this file.
unset LUNCH_MENU_CHOICES
function add_lunch_combo()
{
    local new_combo=$1
    local c
    for c in ${LUNCH_MENU_CHOICES[@]} ; do
        if [ "$new_combo" = "$c" ] ; then
            return
        fi
    done
    LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
460 461
}

462
# add the default one here
463
add_lunch_combo aosp_arm-eng
Serban Constantinescu's avatar
Serban Constantinescu committed
464
add_lunch_combo aosp_arm64-eng
465
add_lunch_combo aosp_mips-eng
Chris Dearman's avatar
Chris Dearman committed
466
add_lunch_combo aosp_mips64-eng
Serban Constantinescu's avatar
Serban Constantinescu committed
467
add_lunch_combo aosp_x86-eng
Pavel Chupin's avatar
Pavel Chupin committed
468
add_lunch_combo aosp_x86_64-eng
469

470 471 472 473 474 475 476
function print_lunch_menu()
{
    local uname=$(uname)
    echo
    echo "You're building on" $uname
    echo
    echo "Lunch menu... pick a combo:"
477 478 479 480 481 482 483 484 485

    local i=1
    local choice
    for choice in ${LUNCH_MENU_CHOICES[@]}
    do
        echo "     $i. $choice"
        i=$(($i+1))
    done

486 487 488 489 490
    echo
}

function lunch()
{
491 492
    local answer

493
    if [ "$1" ] ; then
494
        answer=$1
495 496
    else
        print_lunch_menu
497
        echo -n "Which would you like? [aosp_arm-eng] "
498
        read answer
499 500
    fi

501 502 503 504
    local selection=

    if [ -z "$answer" ]
    then
505
        selection=aosp_arm-eng
506 507 508 509
    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
    then
        if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
        then
510
            selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
511 512
        fi
    elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
513
    then
514
        selection=$answer
515 516
    fi

517 518 519 520 521 522 523
    if [ -z "$selection" ]
    then
        echo
        echo "Invalid lunch combo: $answer"
        return 1
    fi

Joe Onorato's avatar
Joe Onorato committed
524 525
    export TARGET_BUILD_APPS=

526 527 528
    local product=$(echo -n $selection | sed -e "s/-.*$//")
    check_product $product
    if [ $? -ne 0 ]
529
    then
530 531 532 533 534
        echo
        echo "** Don't have a product spec for: '$product'"
        echo "** Do you have the right repo manifest?"
        product=
    fi
535

536 537 538 539 540 541 542 543 544
    local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
    check_variant $variant
    if [ $? -ne 0 ]
    then
        echo
        echo "** Invalid variant: '$variant'"
        echo "** Must be one of ${VARIANT_CHOICES[@]}"
        variant=
    fi
545

546 547 548 549 550
    if [ -z "$product" -o -z "$variant" ]
    then
        echo
        return 1
    fi
551

552 553 554
    export TARGET_PRODUCT=$product
    export TARGET_BUILD_VARIANT=$variant
    export TARGET_BUILD_TYPE=release
555

556 557 558 559
    echo

    set_stuff_for_environment
    printconfig
560 561
}

Jeff Davidson's avatar
Jeff Davidson committed
562 563 564 565 566 567 568 569 570 571 572 573 574
# Tab completion for lunch.
function _lunch()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
    return 0
}
complete -F _lunch lunch

Joe Onorato's avatar
Joe Onorato committed
575
# Configures the build to build unbundled apps.
576
# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onorato's avatar
Joe Onorato committed
577 578
function tapas()
{
579
    local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
580
    local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
581
    local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
Joe Onorato's avatar
Joe Onorato committed
582

583 584 585 586
    if [ $(echo $arch | wc -w) -gt 1 ]; then
        echo "tapas: Error: Multiple build archs supplied: $arch"
        return
    fi
Joe Onorato's avatar
Joe Onorato committed
587 588 589 590
    if [ $(echo $variant | wc -w) -gt 1 ]; then
        echo "tapas: Error: Multiple build variants supplied: $variant"
        return
    fi
591 592 593

    local product=full
    case $arch in
594 595 596 597 598 599
      x86)    product=full_x86;;
      mips)   product=full_mips;;
      armv5)  product=generic_armv5;;
      arm64)  product=aosp_arm64;;
      x86_64) product=aosp_x86_64;;
      mips64)  product=aosp_mips64;;
600
    esac
Joe Onorato's avatar
Joe Onorato committed
601 602 603
    if [ -z "$variant" ]; then
        variant=eng
    fi
604 605 606
    if [ -z "$apps" ]; then
        apps=all
    fi
Joe Onorato's avatar
Joe Onorato committed
607

608
    export TARGET_PRODUCT=$product
Joe Onorato's avatar
Joe Onorato committed
609 610 611 612 613 614 615 616
    export TARGET_BUILD_VARIANT=$variant
    export TARGET_BUILD_TYPE=release
    export TARGET_BUILD_APPS=$apps

    set_stuff_for_environment
    printconfig
}

617 618
function gettop
{
619
    local TOPFILE=build/core/envsetup.mk
620
    if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
621 622
        # The following circumlocution ensures we remove symlinks from TOP.
        (cd $TOP; PWD= /bin/pwd)
623 624
    else
        if [ -f $TOPFILE ] ; then
625 626 627 628
            # The following circumlocution (repeated below as well) ensures
            # that we record the true directory name and not one that is
            # faked up with symlink names.
            PWD= /bin/pwd
629
        else
630
            local HERE=$PWD
631 632
            T=
            while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
633
                \cd ..
synergy's avatar
synergy committed
634
                T=`PWD= /bin/pwd -P`
635
            done
636
            \cd $HERE
637 638 639 640 641 642 643
            if [ -f "$T/$TOPFILE" ]; then
                echo $T
            fi
        fi
    fi
}

644 645 646 647 648 649 650
# Return driver for "make", if any (eg. static analyzer)
function getdriver()
{
    local T="$1"
    test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
    if [ -n "$WITH_STATIC_ANALYZER" ]; then
        echo "\
651 652
$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
653 654 655 656 657
--status-bugs \
--top=$T"
    fi
}

658 659
function m()
{
660 661
    local T=$(gettop)
    local DRV=$(getdriver $T)
662
    if [ "$T" ]; then
663
        $DRV make -C $T -f build/core/main.mk $@
664 665 666 667 668 669 670 671
    else
        echo "Couldn't locate the top of the tree.  Try setting TOP."
    fi
}

function findmakefile()
{
    TOPFILE=build/core/envsetup.mk
672
    local HERE=$PWD
673 674
    T=
    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
675
        T=`PWD= /bin/pwd`
676 677
        if [ -f "$T/Android.mk" ]; then
            echo $T/Android.mk
678
            \cd $HERE
679 680
            return
        fi
681
        \cd ..
682
    done
683
    \cd $HERE
684 685 686 687
}

function mm()
{
688 689
    local T=$(gettop)
    local DRV=$(getdriver $T)
690 691 692
    # If we're sitting in the root of the build tree, just do a
    # normal make.
    if [ -f build/core/envsetup.mk -a -f Makefile ]; then
693
        $DRV make $@
694 695
    else
        # Find the closest Android.mk file.
696
        local M=$(findmakefile)
Ying Wang's avatar
Ying Wang committed
697 698 699
        local MODULES=
        local GET_INSTALL_PATH=
        local ARGS=
700 701
        # Remove the path to top as the makefilepath needs to be relative
        local M=`echo $M|sed 's:'$T'/::'`
702 703 704 705 706
        if [ ! "$T" ]; then
            echo "Couldn't locate the top of the tree.  Try setting TOP."
        elif [ ! "$M" ]; then
            echo "Couldn't locate a makefile from the current directory."
        else
Ying Wang's avatar
Ying Wang committed
707 708 709 710 711 712 713 714 715 716 717 718
            for ARG in $@; do
                case $ARG in
                  GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
                esac
            done
            if [ -n "$GET_INSTALL_PATH" ]; then
              MODULES=
              ARGS=GET-INSTALL-PATH
            else
              MODULES=all_modules
              ARGS=$@
            fi
719
            ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES $ARGS
720 721 722 723 724 725
        fi
    fi
}

function mmm()
{
726 727
    local T=$(gettop)
    local DRV=$(getdriver $T)
728
    if [ "$T" ]; then
729
        local MAKEFILE=
730
        local MODULES=
731 732
        local ARGS=
        local DIR TO_CHOP
Ying Wang's avatar
Ying Wang committed
733
        local GET_INSTALL_PATH=
734 735 736
        local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
        local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
        for DIR in $DIRS ; do
737 738 739 740 741
            MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
            if [ "$MODULES" = "" ]; then
                MODULES=all_modules
            fi
            DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
742
            if [ -f $DIR/Android.mk ]; then
Ying Wang's avatar
Ying Wang committed
743 744 745 746
                local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
                local TO_CHOP=`expr $TO_CHOP + 1`
                local START=`PWD= /bin/pwd`
                local MFILE=`echo $START | cut -c${TO_CHOP}-`
747 748 749 750 751 752 753
                if [ "$MFILE" = "" ] ; then
                    MFILE=$DIR/Android.mk
                else
                    MFILE=$MFILE/$DIR/Android.mk
                fi
                MAKEFILE="$MAKEFILE $MFILE"
            else
Ying Wang's avatar
Ying Wang committed
754 755 756 757 758
                case $DIR in
                  showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
                  GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
                  *) echo "No Android.mk in $DIR."; return 1;;
                esac
759 760
            fi
        done
Ying Wang's avatar
Ying Wang committed
761 762 763 764
        if [ -n "$GET_INSTALL_PATH" ]; then
          ARGS=$GET_INSTALL_PATH
          MODULES=
        fi
765
        ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
766 767 768 769 770
    else
        echo "Couldn't locate the top of the tree.  Try setting TOP."
    fi
}

Ying Wang's avatar
Ying Wang committed
771 772
function mma()
{
773 774
  local T=$(gettop)
  local DRV=$(getdriver $T)
Ying Wang's avatar
Ying Wang committed
775
  if [ -f build/core/envsetup.mk -a -f Makefile ]; then
776
    $DRV make $@
Ying Wang's avatar
Ying Wang committed
777 778 779 780 781
  else
    if [ ! "$T" ]; then
      echo "Couldn't locate the top of the tree.  Try setting TOP."
    fi
    local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
782
    $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wang's avatar
Ying Wang committed
783 784 785 786 787
  fi
}

function mmma()
{
788 789
  local T=$(gettop)
  local DRV=$(getdriver $T)
Ying Wang's avatar
Ying Wang committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
  if [ "$T" ]; then
    local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
    local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
    local MY_PWD=`PWD= /bin/pwd`
    if [ "$MY_PWD" = "$T" ]; then
      MY_PWD=
    else
      MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
    fi
    local DIR=
    local MODULE_PATHS=
    local ARGS=
    for DIR in $DIRS ; do
      if [ -d $DIR ]; then
        if [ "$MY_PWD" = "" ]; then
          MODULE_PATHS="$MODULE_PATHS $DIR"
        else
          MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
        fi
      else
        case $DIR in
          showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
          *) echo "Couldn't find directory $DIR"; return 1;;
        esac
      fi
    done
816
    $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS all_modules BUILD_MODULES_IN_PATHS="$MODULE_PATHS"
Ying Wang's avatar
Ying Wang committed
817 818 819 820 821
  else
    echo "Couldn't locate the top of the tree.  Try setting TOP."
  fi
}

822 823 824 825
function croot()
{
    T=$(gettop)
    if [ "$T" ]; then
826
        \cd $(gettop)
827 828 829 830 831
    else
        echo "Couldn't locate the top of the tree.  Try setting TOP."
    fi
}

832 833 834 835 836 837 838 839
function cproj()
{
    TOPFILE=build/core/envsetup.mk
    local HERE=$PWD
    T=
    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
        T=$PWD
        if [ -f "$T/Android.mk" ]; then
840
            \cd $T
841 842
            return
        fi
843
        \cd ..
844
    done
845
    \cd $HERE
846 847 848
    echo "can't find Android.mk"
}

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
# simplified version of ps; output in the form
# <pid> <procname>
function qpid() {
    local prepend=''
    local append=''
    if [ "$1" = "--exact" ]; then
        prepend=' '
        append='$'
        shift
    elif [ "$1" = "--help" -o "$1" = "-h" ]; then
		echo "usage: qpid [[--exact] <process name|pid>"
		return 255
	fi

    local EXE="$1"
    if [ "$EXE" ] ; then
865
		qpid | \grep "$prepend$EXE$append"
866 867 868 869 870 871 872
	else
		adb shell ps \
			| tr -d '\r' \
			| sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
	fi
}

873 874
function pid()
{
875 876 877 878 879 880 881 882 883 884 885
    local prepend=''
    local append=''
    if [ "$1" = "--exact" ]; then
        prepend=' '
        append='$'
        shift
    fi
    local EXE="$1"
    if [ "$EXE" ] ; then
        local PID=`adb shell ps \
            | tr -d '\r' \
886
            | \grep "$prepend$EXE$append" \
887 888 889 890 891 892
            | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
        echo "$PID"
    else
        echo "usage: pid [--exact] <process name>"
		return 255
    fi
893 894
}

895 896 897 898
# systemstack - dump the current stack trace of all threads in the system process
# to the usual ANR traces file
function systemstack()
{
899 900 901 902 903 904 905 906
    stacks system_server
}

function stacks()
{
    if [[ $1 =~ ^[0-9]+$ ]] ; then
        local PID="$1"
    elif [ "$1" ] ; then
907 908 909 910 911 912 913 914
        local PIDLIST="$(pid $1)"
        if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
            local PID="$PIDLIST"
        elif [ "$PIDLIST" ] ; then
            echo "more than one process: $1"
        else
            echo "no such process: $1"
        fi
915 916 917 918 919
    else
        echo "usage: stacks [pid|process name]"
    fi

    if [ "$PID" ] ; then
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
        # Determine whether the process is native
        if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
            # Dump stacks of Dalvik process
            local TRACES=/data/anr/traces.txt
            local ORIG=/data/anr/traces.orig
            local TMP=/data/anr/traces.tmp

            # Keep original traces to avoid clobbering
            adb shell mv $TRACES $ORIG

            # Make sure we have a usable file
            adb shell touch $TRACES
            adb shell chmod 666 $TRACES

            # Dump stacks and wait for dump to finish
            adb shell kill -3 $PID
            adb shell notify $TRACES >/dev/null

            # Restore original stacks, and show current output
            adb shell mv $TRACES $TMP
            adb shell mv $ORIG $TRACES
            adb shell cat $TMP
        else
            # Dump stacks of native process
944 945
            local USE64BIT="$(is64bit $PID)"
            adb shell debuggerd$USE64BIT -b $PID
946
        fi
947
    fi
948 949
}

John Michelau's avatar
John Michelau committed
950 951
function gdbwrapper()
{
952 953 954 955 956
    local GDB_CMD="$1"
    shift 1
    $GDB_CMD -x "$@"
}

957 958 959 960 961
function get_symbols_directory()
{
    echo $(get_abs_build_var TARGET_OUT_UNSTRIPPED)
}

962 963
# Read the ELF header from /proc/$PID/exe to determine if the process is
# 64-bit.
964 965 966 967
function is64bit()
{
    local PID="$1"
    if [ "$PID" ] ; then
968
        if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
969 970 971 972 973 974 975
            echo "64"
        else
            echo ""
        fi
    else
        echo ""
    fi
John Michelau's avatar
John Michelau committed
976 977
}

Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
978
function adb_get_product_device() {
979 980 981 982 983
  local candidate=`adb shell getprop ro.product.device | sed s/.$//`
  if [ -z $candidate ]; then
    candidate=`adb shell getprop ro.hardware | sed s/.$//`
  fi
  echo $candidate
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
}

# returns 0 when process is not traced
function adb_get_traced_by() {
  echo `adb shell cat /proc/$1/status | grep -e "^TracerPid:" | sed "s/^TracerPid:\t//" | sed s/.$//`
}

function gdbclient() {
  # TODO:
  # 1. Check for ANDROID_SERIAL/multiple devices
  local PROCESS_NAME="n/a"
  local PID=$1
  local PORT=5039
  if [ -z "$PID" ]; then
    echo "Usage: gdbclient <pid|processname> [port number]"
    return -1
  fi
  local DEVICE=$(adb_get_product_device)

  if [ -z "$DEVICE" ]; then
    echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
    return -2
  fi

  if [ -n "$2" ]; then
    PORT=$2
  fi

  local ROOT=$(gettop)
  if [ -z "$ROOT" ]; then
    # This is for the situation with downloaded symbols (from the build server)
    # we check if they are available.
    ROOT=`realpath .`
  fi

  local OUT_ROOT="$ROOT/out/target/product/$DEVICE"
  local SYMBOLS_DIR="$OUT_ROOT/symbols"
1021 1022 1023 1024 1025 1026
  local IS_TAPAS_USER="$(get_build_var TARGET_BUILD_APPS)"
  local TAPAS_SYMBOLS_DIR=

  if [ $IS_TAPAS_USER ]; then
    TAPAS_SYMBOLS_DIR=$(get_symbols_directory)
  fi
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1027 1028

  if [ ! -d $SYMBOLS_DIR ]; then
1029 1030 1031 1032 1033 1034
    if [ $IS_TAPAS_USER ]; then
      mkdir -p $SYMBOLS_DIR/system/bin
    else
      echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
      return -3
    fi
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  fi

  # let's figure out which executable we are about to debug

  # check if user specified a name -> resolve to pid
  if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
    PROCESS_NAME=$PID
    PID=$(pid --exact $PROCESS_NAME)
    if [ -z "$PID" ]; then
      echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
      return -4
1046 1047
    else
      echo "Resolved pid for $PROCESS_NAME is $PID"
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1048 1049 1050 1051
    fi
  fi

  local EXE=`adb shell readlink /proc/$PID/exe | sed s/.$//`
1052 1053 1054 1055 1056 1057

  if [ -z "$EXE" ]; then
    echo "Error: no such pid=$PID - is process still alive?"
    return -4
  fi

Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1058 1059 1060
  local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE

  if [ ! -f $LOCAL_EXE_PATH ]; then
1061 1062 1063 1064 1065 1066
    if [ $IS_TAPAS_USER ]; then
      adb pull $EXE $LOCAL_EXE_PATH
    else
      echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
      return -5
    fi
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1067 1068 1069 1070 1071 1072 1073 1074
  fi

  local USE64BIT=""

  if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
    USE64BIT="64"
  fi

1075 1076 1077 1078 1079
  # and now linker for tapas users...
  if [ -n "$IS_TAPAS_USER" -a ! -f "$SYMBOLS_DIR/system/bin/linker$USE64BIT" ]; then
    adb pull /system/bin/linker$USE64BIT $SYMBOLS_DIR/system/bin/linker$USE64BIT
  fi

Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
  local GDB=
  local GDB64=
  local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | sed s/.$//`
  # TODO: we assume these are available via $PATH
  if [[ $CPU_ABI =~ (^|,)arm64 ]]; then
    GDB=arm-linux-androideabi-gdb
    GDB64=aarch64-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)arm ]]; then
    GDB=arm-linux-androideabi-gdb
  elif [[ $CPU_ABI =~ (^|,)x86_64 ]]; then
1090
    GDB=x86_64-linux-android-gdb
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1091
  elif [[ $CPU_ABI =~ (^|,)x86 ]]; then
1092
    GDB=x86_64-linux-android-gdb
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
  elif [[ $CPU_ABI =~ (^|,)mips64 ]]; then
    GDB=mipsel-linux-android-gdb
    GDB64=mips64el-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)mips ]]; then
    GDB=mipsel-linux-android-gdb
  else
    echo "Error: unrecognized cpu.abilist: $CPU_ABI"
    return -6
  fi

  # TODO: check if tracing process is gdbserver and not some random strace...
1104
  if [ "$(adb_get_traced_by $PID)" -eq 0 ]; then
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    # start gdbserver
    echo "Starting gdbserver..."
    # TODO: check if adb is already listening $PORT
    # to avoid unnecessary calls
    echo ". adb forward for port=$PORT..."
    adb forward tcp:$PORT tcp:$PORT
    echo ". starting gdbserver to attach to pid=$PID..."
    adb shell gdbserver$USE64BIT :$PORT --attach $PID &
    echo ". give it couple of seconds to start..."
    sleep 2
    echo ". done"
  else
    echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
1118
    adb forward tcp:$PORT tcp:$PORT
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1119 1120 1121
  fi

  local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
1122
  local TAPAS_OUT_SO_SYMBOLS=$TAPAS_SYMBOLS_DIR/system/lib$USE64BIT
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1123 1124 1125
  local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
  local ART_CMD=""

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  local SOLIB_SYSROOT=$SYMBOLS_DIR
  local SOLIB_SEARCHPATH=$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

  if [ $IS_TAPAS_USER ]; then
    SOLIB_SYSROOT=$TAPAS_SYMBOLS_DIR:$SOLIB_SYSROOT
    SOLIB_SEARCHPATH=$TAPAS_OUT_SO_SYMBOLS:$SOLIB_SEARCHPATH
  fi

  echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SOLIB_SYSROOT"
  echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $SOLIB_SEARCHPATH"
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
  local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
  if [ -f $DALVIK_GDB_SCRIPT ]; then
    echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
    ART_CMD="art-on"
  else
    echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
  fi
  echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
  if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
    echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
  fi

  echo >>"$OUT_ROOT/gdbclient.cmds" ""

  local WHICH_GDB=$GDB

  if [ -n "$USE64BIT" -a -n "$GDB64" ]; then
    WHICH_GDB=$GDB64
  fi

  gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
}

1159 1160 1161
# gdbclient now determines whether the user wants to debug a 32-bit or 64-bit
# executable, set up the approriate gdbserver, then invokes the proper host
# gdb.
Dmitriy Ivanov's avatar
Dmitriy Ivanov committed
1162
function gdbclient_old()
1163
{
1164 1165 1166
   local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
   local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
   local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
1167
   local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
1168
   local OUT_EXE_SYMBOLS=$(get_symbols_directory)
1169
   local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
Nick Kralevich's avatar
Nick Kralevich committed
1170 1171 1172 1173
   local ARCH=$(get_build_var TARGET_ARCH)
   local GDB
   case "$ARCH" in
       arm) GDB=arm-linux-androideabi-gdb;;
1174
       arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
1175
       mips|mips64) GDB=mips64el-linux-android-gdb;;
Serban Constantinescu's avatar
Serban Constantinescu committed
1176 1177
       x86) GDB=x86_64-linux-android-gdb;;
       x86_64) GDB=x86_64-linux-android-gdb;;
Nick Kralevich's avatar
Nick Kralevich committed
1178 1179 1180
       *) echo "Unknown arch $ARCH"; return 1;;
   esac

1181 1182 1183 1184
   if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
       local EXE="$1"
       if [ "$EXE" ] ; then
           EXE=$1
Brigid Smith's avatar
Brigid Smith committed
1185 1186 1187
           if [[ $EXE =~ ^[^/].* ]] ; then
               EXE="system/bin/"$EXE
           fi
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
       else
           EXE="app_process"
       fi

       local PORT="$2"
       if [ "$PORT" ] ; then
           PORT=$2
       else
           PORT=":5039"
       fi

Chris Craik's avatar
Chris Craik committed
1199 1200 1201
       local PID="$3"
       if [ "$PID" ] ; then
           if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1202
               PID=`pid $3`
Chris Craik's avatar
Chris Craik committed
1203 1204 1205
               if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
                   # that likely didn't work because of returning multiple processes
                   # try again, filtering by root processes (don't contain colon)
1206
                   PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
Chris Craik's avatar
Chris Craik committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
                   if [[ ! "$PID" =~ ^[0-9]+$ ]]
                   then
                       echo "Couldn't resolve '$3' to single PID"
                       return 1
                   else
                       echo ""
                       echo "WARNING: multiple processes matching '$3' observed, using root process"
                       echo ""
                   fi
               fi
1217
           fi
1218
           adb forward "tcp$PORT" "tcp$PORT"
1219 1220
           local USE64BIT="$(is64bit $PID)"
           adb shell gdbserver$USE64BIT $PORT --attach $PID &
1221 1222 1223 1224 1225 1226
           sleep 2
       else
               echo ""
               echo "If you haven't done so already, do this first on the device:"
               echo "    gdbserver $PORT /system/bin/$EXE"
                   echo " or"
Chris Craik's avatar
Chris Craik committed
1227
               echo "    gdbserver $PORT --attach <PID>"
1228 1229 1230
               echo ""
       fi

1231
       OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
Colin Cross's avatar
Colin Cross committed
1232
       OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
1233

1234
       echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1235
       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"
1236
       echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1237
       echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1238 1239 1240 1241
       # Enable special debugging for ART processes.
       if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
          echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
       fi
1242 1243
       echo >>"$OUT_ROOT/gdbclient.cmds" ""

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
       local WHICH_GDB=
       # 64-bit exe found
       if [ "$USE64BIT" != "" ] ; then
           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
       # 32-bit exe / 32-bit platform
       elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
       # 32-bit exe / 64-bit platform
       else
           WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
       fi
1255

1256
       gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
  else
       echo "Unable to determine build system output dir."
   fi

}

case `uname -s` in
    Darwin)
        function sgrep()
        {
1267
            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 "$@"
1268 1269 1270 1271 1272 1273
        }

        ;;
    *)
        function sgrep()
        {
1274
            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 "$@"
1275 1276 1277 1278
        }
        ;;
esac

1279 1280 1281 1282 1283
function gettargetarch
{
    get_build_var TARGET_ARCH
}

Jon Boekenoogen's avatar
ggrep  
Jon Boekenoogen committed
1284 1285 1286 1287 1288
function ggrep()
{
    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
}

1289 1290
function jgrep()
{
1291
    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
1292 1293 1294 1295
}

function cgrep()
{
1296
    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' -o -name '*.hpp' \) -print0 | xargs -0 grep --color -n "$@"
1297 1298 1299 1300
}

function resgrep()
{
1301
    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;
1302 1303
}

1304 1305 1306 1307 1308
function mangrep()
{
    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
}

1309 1310 1311 1312 1313
function sepgrep()
{
    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 "$@"
}

1314 1315 1316 1317
case `uname -s` in
    Darwin)
        function mgrep()
        {
1318
            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 "$@"
1319 1320 1321 1322
        }

        function treegrep()
        {
1323
            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 "$@"
1324 1325 1326 1327 1328 1329
        }

        ;;
    *)
        function mgrep()
        {
1330
            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 "$@"
1331 1332 1333 1334
        }

        function treegrep()
        {
1335
            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 "$@"
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
        }

        ;;
esac

function getprebuilt
{
    get_abs_build_var ANDROID_PREBUILTS
}

function tracedmdump()
{
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP."
        return
    fi
1353
    local prebuiltdir=$(getprebuilt)
1354 1355
    local arch=$(gettargetarch)
    local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
1356

1357
    local TRACE=$1
1358 1359 1360 1361 1362
    if [ ! "$TRACE" ] ; then
        echo "usage:  tracedmdump  tracename"
        return
    fi

1363 1364 1365 1366 1367
    if [ ! -r "$KERNEL" ] ; then
        echo "Error: cannot find kernel: '$KERNEL'"
        return
    fi

1368
    local BASETRACE=$(basename $TRACE)
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
    if [ "$BASETRACE" = "$TRACE" ] ; then
        TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
    fi

    echo "post-processing traces..."
    rm -f $TRACE/qtrace.dexlist
    post_trace $TRACE
    if [ $? -ne 0 ]; then
        echo "***"
        echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
        echo "***"
        return
    fi
    echo "generating dexlist output..."
    /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
    echo "generating dmtrace data..."
    q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
    echo "generating html file..."
    dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
    echo "done, see $TRACE/dmtrace.html for details"
    echo "or run:"
    echo "    traceview $TRACE/dmtrace"
}

1393 1394
# communicate with a running device or emulator, set up necessary state,
# and run the hat command.
1395 1396
function runhat()
{
1397 1398
    # process standard adb options
    local adbTarget=""
1399
    if [ "$1" = "-d" -o "$1" = "-e" ]; then
1400 1401
        adbTarget=$1
        shift 1
1402
    elif [ "$1" = "-s" ]; then
1403 1404 1405 1406
        adbTarget="$1 $2"
        shift 2
    fi
    local adbOptions=${adbTarget}
1407
    #echo adbOptions = ${adbOptions}
1408 1409 1410

    # runhat options
    local targetPid=$1
1411 1412

    if [ "$targetPid" = "" ]; then
1413
        echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
1414 1415 1416
        return
    fi

1417 1418 1419 1420 1421 1422
    # confirm hat is available
    if [ -z $(which hat) ]; then
        echo "hat is not available in this configuration."
        return
    fi

1423
    # issue "am" command to cause the hprof dump
Nick Kralevich's avatar
Nick Kralevich committed
1424
    local devFile=/data/local/tmp/hprof-$targetPid
1425
    echo "Poking $targetPid and waiting for data..."
1426
    echo "Storing data at $devFile"
1427
    adb ${adbOptions} shell am dumpheap $targetPid $devFile
1428
    echo "Press enter when logcat shows \"hprof: heap dump completed\""
1429 1430 1431
    echo -n "> "
    read

1432
    local localFile=/tmp/$$-hprof
1433

1434 1435
    echo "Retrieving file $devFile..."
    adb ${adbOptions} pull $devFile $localFile
1436

1437
    adb ${adbOptions} shell rm $devFile
1438

1439
    echo "Running hat on $localFile"
1440 1441
    echo "View the output by pointing your browser at http://localhost:7000/"
    echo ""
Dianne Hackborn's avatar
Dianne Hackborn committed
1442
    hat -JXmx512m $localFile
1443 1444 1445 1446
}

function getbugreports()
{
1447
    local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
1448 1449 1450 1451 1452 1453

    if [ ! "$reports" ]; then
        echo "Could not locate any bugreports."
        return
    fi

1454 1455
    local report
    for report in ${reports[@]}
1456
    do
1457 1458 1459
        echo "/sdcard/bugreports/${report}"
        adb pull /sdcard/bugreports/${report} ${report}
        gunzip ${report}
1460 1461 1462
    done
}

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
function getsdcardpath()
{
    adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
}

function getscreenshotpath()
{
    echo "$(getsdcardpath)/Pictures/Screenshots"
}

function getlastscreenshot()
{
    local screenshot_path=$(getscreenshotpath)
    local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
    if [ "$screenshot" = "" ]; then
        echo "No screenshots found."
        return
    fi
    echo "${screenshot}"
    adb ${adbOptions} pull ${screenshot_path}/${screenshot}
}

1485 1486
function startviewserver()
{
1487
    local port=4939
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
    if [ $# -gt 0 ]; then
            port=$1
    fi
    adb shell service call window 1 i32 $port
}

function stopviewserver()
{
    adb shell service call window 2
}

function isviewserverstarted()
{
    adb shell service call window 3
}

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
function key_home()
{
    adb shell input keyevent 3
}

function key_back()
{
    adb shell input keyevent 4
}

function key_menu()
{
    adb shell input keyevent 82
}

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
function smoketest()
{
    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
        return
    fi
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi

1531
    (\cd "$T" && mmm tests/SmokeTest) &&
1532 1533 1534 1535 1536 1537 1538
      adb uninstall com.android.smoketest > /dev/null &&
      adb uninstall com.android.smoketest.tests > /dev/null &&
      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
      adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
}

1539 1540 1541 1542 1543 1544 1545 1546
# simple shortcut to the runtest command
function runtest()
{
    T=$(gettop)
    if [ ! "$T" ]; then
        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
        return
    fi
1547
    ("$T"/development/testrunner/runtest.py $@)
1548 1549
}

1550 1551 1552 1553 1554
function godir () {
    if [[ -z "$1" ]]; then
        echo "Usage: godir <regex>"
        return
    fi
1555
    T=$(gettop)
1556 1557
    if [[ ! -f $T/filelist ]]; then
        echo -n "Creating index..."
1558
        (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1559 1560 1561 1562
        echo " Done"
        echo ""
    fi
    local lines
1563
    lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
    if [[ ${#lines[@]} = 0 ]]; then
        echo "Not found"
        return
    fi
    local pathname
    local choice
    if [[ ${#lines[@]} > 1 ]]; then
        while [[ -z "$pathname" ]]; do
            local index=1
            local line
            for line in ${lines[@]}; do
                printf "%6s %s\n" "[$index]" $line
1576
                index=$(($index + 1))
1577 1578 1579 1580 1581 1582 1583 1584 1585
            done
            echo
            echo -n "Select one: "
            unset choice
            read choice
            if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
                echo "Invalid choice"
                continue
            fi
1586
            pathname=${lines[$(($choice-1))]}
1587 1588 1589 1590
        done
    else
        pathname=${lines[0]}
    fi
1591
    \cd $T/$pathname
1592 1593
}

Neil Fuller's avatar
Neil Fuller committed
1594
# Force JAVA_HOME to point to java 1.7 if it isn't already set.
1595 1596 1597 1598
#
# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
# For some reason, installing the JDK doesn't make it show up in the
# JavaVM.framework/Versions/1.7/ folder.
1599
function set_java_home() {
1600
    # Clear the existing JAVA_HOME value if we set it ourselves, so that
Narayan Kamath's avatar
Narayan Kamath committed
1601 1602
    # we can reset it later, depending on the version of java the build
    # system needs.
1603 1604 1605 1606 1607 1608 1609
    #
    # If we don't do this, the JAVA_HOME value set by the first call to
    # build/envsetup.sh will persist forever.
    if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
      export JAVA_HOME=""
    fi

1610
    if [ ! "$JAVA_HOME" ]; then
Neil Fuller's avatar
Neil Fuller committed
1611 1612 1613 1614 1615 1616 1617 1618
      case `uname -s` in
          Darwin)
              export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
              ;;
          *)
              export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
              ;;
      esac
1619 1620 1621 1622

      # Keep track of the fact that we set JAVA_HOME ourselves, so that
      # we can change it on the next envsetup.sh, if required.
      export ANDROID_SET_JAVA_HOME=true
1623
    fi
1624
}
1625

1626 1627
# Print colored exit condition
function pez {
1628 1629 1630 1631 1632 1633 1634 1635 1636
    "$@"
    local retval=$?
    if [ $retval -ne 0 ]
    then
        echo -e "\e[0;31mFAILURE\e[00m"
    else
        echo -e "\e[0;32mSUCCESS\e[00m"
    fi
    return $retval
1637 1638
}

1639 1640 1641 1642 1643
function get_make_command()
{
  echo command make
}

1644 1645 1646
function make()
{
    local start_time=$(date +"%s")
1647
    $(get_make_command) "$@"
1648 1649 1650 1651 1652 1653
    local ret=$?
    local end_time=$(date +"%s")
    local tdiff=$(($end_time-$start_time))
    local hours=$(($tdiff / 3600 ))
    local mins=$((($tdiff % 3600) / 60))
    local secs=$(($tdiff % 60))
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
    local ncolors=$(tput colors 2>/dev/null)
    if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
        color_failed="\e[0;31m"
        color_success="\e[0;32m"
        color_reset="\e[00m"
    else
        color_failed=""
        color_success=""
        color_reset=""
    fi
1664 1665
    echo
    if [ $ret -eq 0 ] ; then
1666
        echo -n -e "${color_success}#### make completed successfully "
1667
    else
1668
        echo -n -e "${color_failed}#### make failed to build some targets "
1669 1670 1671 1672 1673 1674 1675 1676
    fi
    if [ $hours -gt 0 ] ; then
        printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
    elif [ $mins -gt 0 ] ; then
        printf "(%02g:%02g (mm:ss))" $mins $secs
    elif [ $secs -gt 0 ] ; then
        printf "(%s seconds)" $secs
    fi
1677
    echo -e " ####${color_reset}"
1678 1679 1680 1681
    echo
    return $ret
}

1682 1683 1684 1685 1686 1687 1688 1689 1690
if [ "x$SHELL" != "x/bin/bash" ]; then
    case `ps -o command -p $$` in
        *bash*)
            ;;
        *)
            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
            ;;
    esac
fi
1691 1692

# Execute the contents of any vendorsetup.sh files we can find.
1693 1694
for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \
         `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null`
1695 1696 1697 1698 1699
do
    echo "including $f"
    . $f
done
unset f
Kenny Root's avatar
Kenny Root committed
1700 1701

addcompletions