Relanding 148377 - [Android] Upstream additional changes from envsetup.

Now requires you to specifically change the GYP_GENERATORS to specify Ninja. The original change broke because gyp ran for both make and ninja and the webkit bot determines whether to use ninja by the presence of build.ninja (thanks to Peter for figuring this out)

BUG=137569,136693


Review URL: https://chromiumcodereview.appspot.com/10827073

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148790 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/build/android/adb_device_functions.sh b/build/android/adb_device_functions.sh
new file mode 100755
index 0000000..257d800
--- /dev/null
+++ b/build/android/adb_device_functions.sh
@@ -0,0 +1,133 @@
+#!/bin/bash
+#
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# A collection of functions useful for maintaining android devices
+
+
+# Run an adb command on all connected device in parallel.
+# Usage: adb_all command line to eval.  Quoting is optional.
+#
+# Examples:
+#  adb_all install Chrome.apk
+#  adb_all 'shell cat /path/to/file'
+#
+adb_all() {
+  if [[ $# == 0 ]]; then
+    echo "Usage: adb_all <adb command>.  Quoting is optional."
+    echo "Example: adb_all install Chrome.apk"
+    return 1
+  fi
+  local DEVICES=$(adb_blocking_get_devices)
+  local NUM_DEVICES=$(echo $DEVICES | wc -w)
+  if (( $NUM_DEVICES > 1 )); then
+    echo "Looping over $NUM_DEVICES devices"
+  fi
+  _adb_multi "$DEVICES" "$*"
+}
+
+
+# Run a command on each connected device.  Quoting the command is suggested but
+# not required.  The script setups up variable DEVICE to correspond to the
+# current serial number.  Intended for complex one_liners that don't work in
+# adb_all
+# Usage: adb_device_loop 'command line to eval'
+adb_device_loop() {
+  if [[ $# == 0 ]]; then
+    echo "Intended for more complex one-liners that cannot be done with" \
+        "adb_all."
+    echo 'Usage: adb_device_loop "echo $DEVICE: $(adb root &&' \
+        'adb shell cat /data/local.prop)"'
+    return 1
+  fi
+  local DEVICES=$(adb_blocking_get_devices)
+  # Do not change DEVICE variable name - part of api
+  for DEVICE in $DEVICES; do
+    DEV_TYPE=$(adb -s $DEVICE shell getprop ro.product.device | sed 's/\r//')
+    echo "Running on $DEVICE ($DEV_TYPE)"
+    ANDROID_SERIAL=$DEVICE eval "$*"
+  done
+}
+
+# Erases data from any devices visible on adb.  To preserve a device,
+# disconnect it or:
+#  1) Reboot it into fastboot with 'adb reboot bootloader'
+#  2) Run wipe_all_devices to wipe remaining devices
+#  3) Restore device it with 'fastboot reboot'
+#
+#  Usage: wipe_all_devices [-f]
+#
+wipe_all_devices() {
+  if [[ -z $(which adb) || -z $(which fastboot) ]]; then
+    echo "aborting: adb and fastboot not in path"
+    return 1
+  elif ! $(groups | grep -q 'plugdev'); then
+    echo "If fastboot fails, run: 'sudo adduser $(whoami) plugdev'"
+  fi
+
+  local DEVICES=$(adb_blocking_get_devices)
+
+  if [[ $1 != '-f' ]]; then
+    echo "This will ERASE ALL DATA from $(echo $DEVICES | wc -w) device."
+    read -p "Hit enter to continue"
+  fi
+
+  _adb_multi "$DEVICES" "root"
+  _adb_multi "$DEVICES" "wait-for-device"
+  _adb_multi "$DEVICES" "reboot bootloader"
+  for DEVICE in $DEVICES; do
+    fastboot_erase $DEVICE
+  done
+
+  # Reboot devices together
+  for DEVICE in $DEVICES; do
+    fastboot -s $DEVICE reboot
+  done
+}
+
+# Wipe a device in fastboot.
+# Usage fastboot_erase [serial]
+fastboot_erase() {
+  if [[ -n $1 ]]; then
+    echo "Wiping $1"
+    local SERIAL="-s $1"
+  else
+    if [ -z $(fastboot devices) ]; then
+      echo "No devices in fastboot, aborting."
+      echo "Check out wipe_all_devices to see if sufficient"
+      echo "You can put a device in fastboot using adb reboot bootloader"
+      return 1
+    fi
+    local SERIAL=""
+  fi
+  fastboot $SERIAL erase cache
+  fastboot $SERIAL erase userdata
+}
+
+# Block until adb detects a device, then return list of serials
+adb_blocking_get_devices() {
+  local DEVICES="$(adb devices | grep 'device$')"
+  if [[ -z $DEVICES ]]; then
+    echo '- waiting for device -' >&2
+    local DEVICES="$(adb wait-for-device devices | grep 'device$')"
+  fi
+  echo "$DEVICES" | awk -vORS=' ' '{print $1}' | sed 's/ $/\n/'
+}
+
+###################################################
+## HELPER FUNCTIONS
+###################################################
+
+# Run an adb command in parallel over a device list
+_adb_multi() {
+  local DEVICES=$1
+  local ADB_ARGS=$2
+  (
+    for DEVICE in $DEVICES; do
+      adb -s $DEVICE $ADB_ARGS &
+    done
+    wait
+  )
+}
diff --git a/build/android/envsetup.sh b/build/android/envsetup.sh
index 9bf4349..2c19dba 100755
--- a/build/android/envsetup.sh
+++ b/build/android/envsetup.sh
@@ -4,31 +4,25 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-# Sets up environment for building Chromium on Android. Only Android NDK,
-# Revision 6b on Linux or Mac is offically supported.
-#
-# To run this script, the system environment ANDROID_NDK_ROOT must be set
-# to Android NDK's root path.
-#
-# TODO(michaelbai): Develop a standard for NDK/SDK integration.
-#
-# If current path isn't the Chrome's src directory, CHROME_SRC must be set
-# to the Chrome's src directory.
+# Sets up environment for building Chromium on Android.  It can either be
+# compiled with the Android tree or using the Android SDK/NDK. To build with
+# NDK/SDK: ". build/android/envsetup.sh --sdk".  Environment variable
+# ANDROID_SDK_BUILD=1 will then be defined and used in the rest of the setup to
+# specifiy build type.
 
-if [ ! -d "${ANDROID_NDK_ROOT}" ]; then
-  echo "ANDROID_NDK_ROOT must be set to the path of Android NDK, Revision 6b." \
-    >& 2
-  echo "which could be installed by" >& 2
-  echo "<chromium_tree>/src/build/install-build-deps-android-sdk.sh" >& 2
-  return 1
-fi
+# NOTE(yfriedman): This looks unnecessary but downstream the default value
+# should be 0 until all builds switch to SDK/NDK.
+export ANDROID_SDK_BUILD=1
+# Loop over args in case we add more arguments in the future.
+while [ "$1" != "" ]; do
+  case $1 in
+    -s | --sdk  ) export ANDROID_SDK_BUILD=1 ; shift ;;
+    *  )          shift ; break ;;
+  esac
+done
 
-if [ ! -d "${ANDROID_SDK_ROOT}" ]; then
-  echo "ANDROID_SDK_ROOT must be set to the path of Android SDK, Android 3.2." \
-    >& 2
-  echo "which could be installed by" >& 2
-  echo "<chromium_tree>/src/build/install-build-deps-android-sdk.sh" >& 2
-  return 1
+if [[ "${ANDROID_SDK_BUILD}" -eq 1 ]]; then
+  echo "Using SDK build"
 fi
 
 host_os=$(uname -s | sed -e 's/Linux/linux/;s/Darwin/mac/')
@@ -45,53 +39,7 @@
     return 1
 esac
 
-# The following defines will affect ARM code generation of both C/C++ compiler
-# and V8 mksnapshot.
-case "${TARGET_PRODUCT-full}" in
-  "full")
-    DEFINES=" target_arch=arm"
-    DEFINES+=" arm_neon=0 armv7=1 arm_thumb=1 arm_fpu=vfpv3-d16"
-    toolchain_arch="arm-linux-androideabi-4.4.3"
-    ;;
-  *x86*)
-    DEFINES=" target_arch=ia32 use_libffmpeg=0"
-    toolchain_arch="x86-4.4.3"
-    ;;
-  *)
-    echo "TARGET_PRODUCT: ${TARGET_PRODUCT} is not supported." >& 2
-    return 1
-esac
-
-# If we are building NDK/SDK, and in the upstream (open source) tree,
-# define a special variable for bringup purposes.
-case "${ANDROID_BUILD_TOP-undefined}" in
-  "undefined")
-    DEFINES+=" android_upstream_bringup=1"
-    ;;
-esac
-
-toolchain_path="${ANDROID_NDK_ROOT}/toolchains/${toolchain_arch}/prebuilt/"
-export ANDROID_TOOLCHAIN="${toolchain_path}/${toolchain_dir}/bin/"
-
-if [ ! -d "${ANDROID_TOOLCHAIN}" ]; then
-  echo "Can not find Android toolchain in ${ANDROID_TOOLCHAIN}." >& 2
-  echo "The NDK version might be wrong." >& 2
-  return 1
-fi
-
-export ANDROID_SDK_VERSION="15"
-
-# Needed by android antfiles when creating apks.
-export ANDROID_SDK_HOME=${ANDROID_SDK_ROOT}
-
-# Add Android SDK/NDK tools to system path.
-export PATH=$PATH:${ANDROID_NDK_ROOT}
-export PATH=$PATH:${ANDROID_SDK_ROOT}/tools
-export PATH=$PATH:${ANDROID_SDK_ROOT}/platform-tools
-# Must have tools like arm-linux-androideabi-gcc on the path for ninja
-export PATH=$PATH:${ANDROID_TOOLCHAIN}
-
-CURRENT_DIR="$(readlink -f ${PWD})"
+CURRENT_DIR="$(readlink -f "$(dirname $BASH_SOURCE)/../../")"
 if [ -z "${CHROME_SRC}" ]; then
   # If $CHROME_SRC was not set, assume current directory is CHROME_SRC.
   export CHROME_SRC="${CURRENT_DIR}"
@@ -107,14 +55,36 @@
   echo "${CHROME_SRC}"
 fi
 
-if [ ! -d "${CHROME_SRC}" ]; then
-  echo "CHROME_SRC must be set to the path of Chrome source code." >& 2
+# Source functions script.  The file is in the same directory as this script.
+. "$(dirname $BASH_SOURCE)"/envsetup_functions.sh
+
+if [ "${ANDROID_SDK_BUILD}" -eq 1 ]; then
+  sdk_build_init
+# Sets up environment for building Chromium for Android with source. Expects
+# android environment setup and lunch.
+elif [ -z "$ANDROID_BUILD_TOP" -o -z "$ANDROID_TOOLCHAIN" -o \
+  -z "$ANDROID_PRODUCT_OUT" ]; then
+  echo "Android build environment variables must be set."
+  echo "Please cd to the root of your Android tree and do: "
+  echo "  . build/envsetup.sh"
+  echo "  lunch"
+  echo "Then try this again."
+  echo "Or did you mean NDK/SDK build. Run envsetup.sh with --sdk argument."
   return 1
+else
+  non_sdk_build_init
 fi
 
-# Add Chromium Android development scripts to system path.
-# Must be after CHROME_SRC is set.
-export PATH=$PATH:${CHROME_SRC}/build/android
+# Workaround for valgrind build
+if [ -n "$CHROME_ANDROID_VALGRIND_BUILD" ]; then
+# arm_thumb=0 is a workaround for https://bugs.kde.org/show_bug.cgi?id=270709
+  DEFINES+=" arm_thumb=0 release_extra_cflags='-fno-inline\
+ -fno-omit-frame-pointer -fno-builtin' release_valgrind_build=1\
+ release_optimize=1"
+fi
+
+# Source a bunch of helper functions
+. ${CHROME_SRC}/build/android/adb_device_functions.sh
 
 ANDROID_GOMA_WRAPPER=""
 if [[ -d $GOMA_DIR ]]; then
@@ -130,38 +100,13 @@
 # Performs a gyp_chromium run to convert gyp->Makefile for android code.
 android_gyp() {
   echo "GYP_GENERATORS set to '$GYP_GENERATORS'"
-  "${CHROME_SRC}/build/gyp_chromium" --depth="${CHROME_SRC}" "$@"
+  if [[ $GYP_DEFINES =~ "clang=1" ]]; then
+    echo -n "NOTE: You have \`clang=1' in \$GYP_DEFINES; "
+    echo "did you mean to run \`android_clang_gyp'?"
+  fi
+
+  "${CHROME_SRC}/build/gyp_chromium" --depth="${CHROME_SRC}" --check "$@"
 }
 
-export OBJCOPY=$(echo ${ANDROID_TOOLCHAIN}/*-objcopy)
-export STRIP=$(echo ${ANDROID_TOOLCHAIN}/*-strip)
-
-# The set of GYP_DEFINES to pass to gyp. Use 'readlink -e' on directories
-# to canonicalize them (remove double '/', remove trailing '/', etc).
-DEFINES+=" OS=android"
-DEFINES+=" android_build_type=0"
-DEFINES+=" host_os=${host_os}"
-DEFINES+=" linux_fpic=1"
-DEFINES+=" release_optimize=s"
-DEFINES+=" linux_use_tcmalloc=0"
-DEFINES+=" disable_nacl=1"
-DEFINES+=" remoting=0"
-DEFINES+=" p2p_apis=0"
-DEFINES+=" enable_touch_events=1"
-DEFINES+=" build_ffmpegsumo=0"
-DEFINES+=" gtest_target_type=shared_library"
-DEFINES+=" branding=Chromium"
-DEFINES+=\
-" android_sdk=${ANDROID_SDK_ROOT}/platforms/android-${ANDROID_SDK_VERSION}"
-DEFINES+=" android_sdk_tools=${ANDROID_SDK_ROOT}/platform-tools"
-
-export GYP_DEFINES="${DEFINES}"
-
-# Use the "android" flavor of the Makefile generator for both Linux and OS X.
-export GYP_GENERATORS="make-android"
-
-# Use our All target as the default
-export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} default_target=All"
-
-# We want to use our version of "all" targets.
-export CHROMIUM_GYP_FILE="${CHROME_SRC}/build/all_android.gyp"
+# FLOCK needs to be null on system that has no flock
+which flock > /dev/null || export FLOCK=
diff --git a/build/android/envsetup_functions.sh b/build/android/envsetup_functions.sh
new file mode 100755
index 0000000..2b03505
--- /dev/null
+++ b/build/android/envsetup_functions.sh
@@ -0,0 +1,292 @@
+#!/bin/bash
+
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Defines functions for envsetup.sh which sets up environment for building
+# Chromium on Android.  The build can be either use the Android NDK/SDK or
+# android source tree.  Each has a unique init function which calls functions
+# prefixed with "common_" that is common for both environment setups.
+
+################################################################################
+# Check to make sure the toolchain exists for the NDK version.
+################################################################################
+common_check_toolchain() {
+  if [[ ! -d "${ANDROID_TOOLCHAIN}" ]]; then
+    echo "Can not find Android toolchain in ${ANDROID_TOOLCHAIN}." >& 2
+    echo "The NDK version might be wrong." >& 2
+    return 1
+  fi
+}
+
+################################################################################
+# Exports environment variables common to both sdk and non-sdk build (e.g. PATH)
+# based on CHROME_SRC and ANDROID_TOOLCHAIN, along with DEFINES for GYP_DEFINES.
+################################################################################
+common_vars_defines() {
+
+  # Set toolchain path according to product architecture.
+  toolchain_arch="arm"
+  if [[ "${TARGET_PRODUCT}" =~ .*x86.* ]]; then
+    toolchain_arch="x86"
+  fi
+
+  toolchain_target=$(basename \
+    ${ANDROID_NDK_ROOT}/toolchains/${toolchain_arch}-*)
+  toolchain_path="${ANDROID_NDK_ROOT}/toolchains/${toolchain_target}"\
+"/prebuilt/${toolchain_dir}/bin/"
+
+  # Set only if not already set.
+  # Don't override ANDROID_TOOLCHAIN if set by Android configuration env.
+  export ANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN:-${toolchain_path}}
+
+  common_check_toolchain
+
+  # Add Android SDK/NDK tools to system path.
+  export PATH=$PATH:${ANDROID_NDK_ROOT}
+  export PATH=$PATH:${ANDROID_SDK_ROOT}/tools
+  export PATH=$PATH:${ANDROID_SDK_ROOT}/platform-tools
+  # Must have tools like arm-linux-androideabi-gcc on the path for ninja
+  export PATH=$PATH:${ANDROID_TOOLCHAIN}
+
+  # Add Chromium Android development scripts to system path.
+  # Must be after CHROME_SRC is set.
+  export PATH=$PATH:${CHROME_SRC}/build/android
+
+  export OBJCOPY=$(echo ${ANDROID_TOOLCHAIN}/*-objcopy)
+  export STRIP=$(echo ${ANDROID_TOOLCHAIN}/*-strip)
+
+  # The set of GYP_DEFINES to pass to gyp. Use 'readlink -e' on directories
+  # to canonicalize them (remove double '/', remove trailing '/', etc).
+  DEFINES="OS=android"
+  DEFINES+=" host_os=${host_os}"
+  DEFINES+=" linux_fpic=1"
+  DEFINES+=" release_optimize=s"
+  DEFINES+=" linux_use_tcmalloc=0"
+  DEFINES+=" disable_nacl=1"
+  DEFINES+=" remoting=0"
+  DEFINES+=" p2p_apis=0"
+  DEFINES+=" enable_touch_events=1"
+  DEFINES+=" build_ffmpegsumo=0"
+
+  DEFINES+=" gtest_target_type=shared_library"
+  if [ -z "$CHROME_ANDROID_OFFICIAL_BUILD" ]; then
+    DEFINES+=" branding=Chromium"
+  else
+    DEFINES+=" branding=Chrome"
+    DEFINES+=" buildtype=Official"
+
+    # These defines are used by various chrome build scripts to tag the binary's
+    # version string as 'official' in linux builds (e.g. in
+    # chrome/trunk/src/chrome/tools/build/version.py).
+    export OFFICIAL_BUILD=1
+    export CHROMIUM_BUILD="_google_chrome"
+    export CHROME_BUILD_TYPE="_official"
+
+    # Used by chrome_version_info_posix.cc to display the channel name.
+    # Valid values: "unstable", "stable", "dev", "beta".
+    export CHROME_VERSION_EXTRA="beta"
+  fi
+
+  # The order file specifies the order of symbols in the .text section of the
+  # shared library, libchromeview.so.  The file is an order list of section
+  # names and the library is linked with option
+  # --section-ordering-file=<orderfile>. The order file is updated by profiling
+  # startup after compiling with the order_profiling=1 GYP_DEFINES flag.
+  ORDER_DEFINES="order_text_section=${CHROME_SRC}/orderfiles/orderfile.out"
+
+  # The following defines will affect ARM code generation of both C/C++ compiler
+  # and V8 mksnapshot.
+  case "${TARGET_PRODUCT}" in
+    "passion"|"soju"|"sojua"|"sojus"|"yakju"|"mysid"|"nakasi")
+      DEFINES+=" target_arch=arm"
+      DEFINES+=" arm_neon=1 armv7=1 arm_thumb=1"
+      DEFINES+=" ${ORDER_DEFINES}"
+      ;;
+    "trygon"|"tervigon")
+      DEFINES+=" target_arch=arm"
+      DEFINES+=" arm_neon=0 armv7=1 arm_thumb=1 arm_fpu=vfpv3-d16"
+      DEFINES+=" ${ORDER_DEFINES}"
+      ;;
+    "full")
+      DEFINES+=" target_arch=arm"
+      DEFINES+=" arm_neon=0 armv7=0 arm_thumb=1 arm_fpu=vfp"
+      ;;
+    *x86*)
+    # TODO(tedbo): The ia32 build fails on ffmpeg, so we disable it here.
+      DEFINES+=" target_arch=ia32 use_libffmpeg=0"
+
+      host_arch=$(uname -m | sed -e \
+        's/i.86/ia32/;s/x86_64/x64/;s/amd64/x64/;s/arm.*/arm/;s/i86pc/ia32/')
+      DEFINES+=" host_arch=${host_arch}"
+      ;;
+    *)
+      echo "TARGET_PRODUCT: ${TARGET_PRODUCT} is not supported." >& 2
+      return 1
+  esac
+}
+
+
+################################################################################
+# Exports common GYP variables based on variable DEFINES and CHROME_SRC.
+################################################################################
+common_gyp_vars() {
+  export GYP_DEFINES="${DEFINES}"
+  export GYP_GENERATORS="make-android"
+
+  # Use our All target as the default
+  export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} default_target=All"
+
+  # We want to use our version of "all" targets.
+  export CHROMIUM_GYP_FILE="${CHROME_SRC}/build/all_android.gyp"
+}
+
+
+################################################################################
+# Initializes environment variables for NDK/SDK build. Only Android NDK Revision
+# 7 on Linux or Mac is offically supported. To run this script, the system
+# environment ANDROID_NDK_ROOT must be set to Android NDK's root path as well as
+# ANDROID_SDK_ROOT to the SDK root.
+# To build Chromium for Android with NDK/SDK follow the steps below:
+#  > export ANDROID_NDK_ROOT=<android ndk root>
+#  > export ANDROID_SDK_ROOT=<android sdk root>
+#  > . build/android/envsetup.sh --sdk
+#  > make
+#############################################################################
+sdk_build_init() {
+  if [ ! -d "${ANDROID_NDK_ROOT}" ]; then
+    echo "ANDROID_NDK_ROOT must be set to the path of Android NDK." >& 2
+    echo "which could be installed by" >& 2
+    echo "<chromium_tree>/src/build/install-build-deps-android-sdk.sh" >& 2
+    return 1
+  fi
+
+  if [ ! -d "${ANDROID_SDK_ROOT}" ]; then
+    echo "ANDROID_SDK_ROOT must be set to the path of Android SDK." >& 2
+    echo "which could be installed by" >& 2
+    echo "<chromium_tree>/src/build/install-build-deps-android-sdk.sh" >& 2
+    return 1
+  fi
+
+  # Makes sure ANDROID_BUILD_TOP is unset if build has option --sdk
+  unset ANDROID_BUILD_TOP
+
+  # Set default target.
+  export TARGET_PRODUCT="${TARGET_PRODUCT:-trygon}"
+
+  # Android sdk platform version to use
+  export ANDROID_SDK_VERSION=15
+  # Unset toolchain so that it can be set based on TARGET_PRODUCT.
+  # This makes it easy to switch between architectures.
+  unset ANDROID_TOOLCHAIN
+
+  common_vars_defines
+
+  DEFINES+=" sdk_build=1"
+  # If we are building NDK/SDK, and in the upstream (open source) tree,
+  # define a special variable for bringup purposes.
+  case "${ANDROID_BUILD_TOP-undefined}" in
+    "undefined")
+      DEFINES+=" android_upstream_bringup=1"
+      ;;
+  esac
+
+  # Sets android specific directories to NOT_SDK_COMPLIANT.  This will allow
+  # android_gyp to generate make files, but will cause errors when (and only
+  # when) building targets that depend on these directories.
+  DEFINES+=" android_src='NOT_SDK_COMPLIANT'"
+  DEFINES+=" android_product_out=${CHROME_SRC}/out/android"
+  DEFINES+=" android_lib='NOT_SDK_COMPLIANT'"
+  DEFINES+=" android_static_lib='NOT_SDK_COMPLIANT'"
+  DEFINES+=\
+" android_sdk=${ANDROID_SDK_ROOT}/platforms/android-${ANDROID_SDK_VERSION}"
+  DEFINES+=" android_sdk_tools=${ANDROID_SDK_ROOT}/platform-tools"
+
+  common_gyp_vars
+
+  if [ -n "$CHROME_ANDROID_BUILD_WEBVIEW" ]; then
+    # Can not build WebView with NDK/SDK because it needs the Android build
+    # system and build inside an Android source tree.
+    echo "Can not build WebView with NDK/SDK.  Requires android source tree." \
+        >& 2
+    echo "Try . build/android/envsetup.sh instead." >& 2
+    return 1
+  fi
+
+}
+
+
+################################################################################
+# Initializes environment variables for build with android source.  This expects
+# android environment to be set up along with lunch.  To build:
+#  > . build/envsetup.sh
+#  >  lunch <lunch-type>
+#  > . build/android/envsetup.sh
+#  > make
+#############################################################################
+non_sdk_build_init() {
+  # We export "TOP" here so that "mmm" can be run to build Java code without
+  # having to cd to $ANDROID_BUILD_TOP.
+  export TOP="$ANDROID_BUILD_TOP"
+
+  # We export "ANDROID_NDK_ROOT" for building Chromium for Android by NDK.
+  export ANDROID_NDK_ROOT=${ANDROID_BUILD_TOP}/prebuilts/ndk/android-ndk-r7
+
+  # Android sdk platform version to use
+  export ANDROID_SDK_VERSION=15
+
+  # We export "ANDROID_SDK_ROOT" for building Java source with the SDK.
+  export ANDROID_SDK_ROOT=${ANDROID_BUILD_TOP}/prebuilts/sdk/\
+${ANDROID_SDK_VERSION}
+  # Needed by android antfiles when creating apks.
+  export ANDROID_SDK_HOME=${ANDROID_SDK_ROOT}
+
+  common_vars_defines
+
+  DEFINES+=" sdk_build=0"
+  DEFINES+=" android_product_out=${ANDROID_PRODUCT_OUT}"
+
+  if [ -n "$CHROME_ANDROID_BUILD_WEBVIEW" ]; then
+    webview_build_init
+    return
+  fi
+
+  # The non-SDK build currently requires the SDK path to build the framework
+  # Java aidl files. TODO(steveblock): Investigate avoiding this requirement.
+  DEFINES+=" android_sdk=${ANDROID_SDK_ROOT}"
+  DEFINES+=" android_sdk_tools=${ANDROID_SDK_ROOT}/../tools/linux"
+
+  common_gyp_vars
+}
+
+################################################################################
+# To build WebView, we use the Android build system and build inside an Android
+# source tree. This method is called from non_sdk_build_init() and adds to the
+# settings specified there.
+#############################################################################
+webview_build_init() {
+  # We need to supply SDK paths relative to the top of the Android tree to make
+  # sure the generated Android makefiles are portable, as they will be checked
+  # into the Android tree.
+  ANDROID_SDK=$(python -c \
+      "import os.path; print os.path.relpath('${ANDROID_SDK_ROOT}', '${TOP}')")
+  ANDROID_SDK_TOOLS=$(python -c \
+      "import os.path; \
+      print os.path.relpath('${ANDROID_SDK_ROOT}/../tools/linux', \
+      '${TOP}')")
+  DEFINES+=" android_build_type=1"
+  DEFINES+=" android_upstream_bringup=1"
+  DEFINES+=" android_sdk=\$(GYP_ABS_ANDROID_TOP_DIR)/${ANDROID_SDK}"
+  DEFINES+=" android_sdk_tools=\$(GYP_ABS_ANDROID_TOP_DIR)/${ANDROID_SDK_TOOLS}"
+  export GYP_DEFINES="${DEFINES}"
+
+  export GYP_GENERATORS="android"
+
+  export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} default_target=All"
+  export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} limit_to_target_all=1"
+  export GYP_GENERATOR_FLAGS="${GYP_GENERATOR_FLAGS} auto_regeneration=0"
+
+  # TODO(torne): This isn't upstream yet. Upstream it or remove this setting.
+  export CHROMIUM_GYP_FILE="${CHROME_SRC}/build/all_android_webview.gyp"
+}
diff --git a/build/common.gypi b/build/common.gypi
index c55f65e..97c896a3 100644
--- a/build/common.gypi
+++ b/build/common.gypi
@@ -70,6 +70,11 @@
         'android_upstream_bringup%': '<(android_upstream_bringup)',
         'buildtype%': '<(buildtype)',
 
+        # Sets whether we're building with the Android SDK/NDK (and hence with
+        # Ant, value 0), or as part of the Android system (and hence with the
+        # Android build system, value 1).
+        'android_build_type%': 0,
+
         # Compute the architecture that we're building on.
         'conditions': [
           ['OS=="win" or OS=="mac" or OS=="ios"', {
@@ -130,6 +135,7 @@
       'enable_hidpi%': '<(enable_hidpi)',
       'enable_touch_ui%': '<(enable_touch_ui)',
       'android_upstream_bringup%': '<(android_upstream_bringup)',
+      'android_build_type%': '<(android_build_type)',
 
       # We used to provide a variable for changing how libraries were built.
       # This variable remains until we can clean up all the users.
@@ -599,6 +605,7 @@
     'wix_path%': '<(wix_path)',
     'android_upstream_bringup%': '<(android_upstream_bringup)',
     'use_system_libjpeg%': '<(use_system_libjpeg)',
+    'android_build_type%': '<(android_build_type)',
 
     # Use system yasm instead of bundled one.
     'use_system_yasm%': 0,
@@ -906,15 +913,9 @@
                 'android_app_abi%': 'armeabi-v7a',
               }],
             ],
-
-            # Sets whether we're building with the Android SDK/NDK (and hence
-            # with Ant, value 0), or as part of the Android system (and hence
-            # with the Android build system, value 1).
-            'android_build_type%': 0,
           },
           'android_ndk_root%': '<(android_ndk_root)',
           'android_ndk_sysroot%': '<(android_ndk_root)/platforms/android-9/arch-<(target_arch)',
-          'android_build_type%': '<(android_build_type)',
           'android_app_abi%': '<(android_app_abi)',
         },
         'android_ndk_root%': '<(android_ndk_root)',