#!/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.

# This script runs gyp with the configuration required to build WebView in the
# Android build system. It is not necessary to source build/android/envsetup.sh
# before running this script.

set -e

PLATFORM=${1:-linux-arm}
echo "Generating makefiles for $PLATFORM"

export PYTHONDONTWRITEBYTECODE=1

CHROME_SRC="$(readlink -f "$(dirname "$0")/../..")"
GYP="${CHROME_SRC}/build/gyp_chromium"

# Use the latest API in the AOSP prebuilts directory (change with AOSP roll).
android_sdk_version=18

# For the WebView build we always use the SDK in the Android tree.
export ANDROID_SDK_ROOT=${ANDROID_BUILD_TOP}/prebuilts/sdk/\
${android_sdk_version}

DEFINES="OS=android"
DEFINES+=" android_webview_build=1"

# 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}', \
    '${ANDROID_BUILD_TOP}')")
DEFINES+=" android_src=\$(PWD)"
DEFINES+=" android_ndk_root=ndk_root_unused_in_webview_build"
DEFINES+=" android_sdk=\$(PWD)/${android_sdk}"
DEFINES+=" android_sdk_root=\$(PWD)/${android_sdk}"
DEFINES+=" android_sdk_version=sdk_version_unused_in_webview_build"
DEFINES+=" android_toolchain=${ANDROID_TOOLCHAIN}"

# TODO: Get rid of this block, crbug.com/334021
if [[ -n "$CHROME_ANDROID_WEBVIEW_OFFICIAL_BUILD" ]]; then
  DEFINES+=" logging_like_official_build=1"
  DEFINES+=" tracing_like_official_build=1"
fi

export GYP_DEFINES="${GYP_DEFINES} ${DEFINES}"

FLAGS="-f android -Gdefault_target=libwebviewchromium -Glimit_to_target_all=1 "\
"--depth=${CHROME_SRC} ${CHROME_SRC}/android_webview/android_webview.gyp"

for host_os in linux mac; do
  host_platform=$(echo $host_os | sed -e 's/mac/darwin/')
  android_sdk_tools=$(python -c \
      "import os.path, sys; \
      print os.path.relpath( \
      '${ANDROID_SDK_ROOT}/../tools/${host_platform}', \
      '${ANDROID_BUILD_TOP}')")
  EFLAGS=\
"${FLAGS} -Dhost_os=${host_os} -Dandroid_sdk_tools=\$(PWD)/${android_sdk_tools}"

  if [ "$PLATFORM" == "${host_platform}-arm" -o "$PLATFORM" == "all" ]; then
    ${GYP} --suffix .${host_platform}-arm ${EFLAGS} -Dtarget_arch=arm
  fi
  if [ "$PLATFORM" == "${host_platform}-arm64" -o "$PLATFORM" == "all" ]; then
    ${GYP} --suffix .${host_platform}-arm64 ${EFLAGS} -Dtarget_arch=arm64
  fi
  if [ "$PLATFORM" == "${host_platform}-x86" -o "$PLATFORM" == "all" ]; then
    ${GYP} --suffix .${host_platform}-x86 ${EFLAGS} -Dtarget_arch=ia32
  fi
  if [ "$PLATFORM" == "${host_platform}-x86_64" -o "$PLATFORM" == "all" ]; then
    ${GYP} --suffix .${host_platform}-x86_64 ${EFLAGS} -Dtarget_arch=x64
  fi
  if [ "$PLATFORM" == "${host_platform}-mips" -o "$PLATFORM" == "all" ]; then
    ${GYP} --suffix .${host_platform}-mips ${EFLAGS} -Dtarget_arch=mipsel
  fi
  if [ "$PLATFORM" == "${host_platform}-mips64" -o "$PLATFORM" == "all" ]; then
    ${GYP} --suffix .${host_platform}-mips64 ${EFLAGS} -Dtarget_arch=mips64el
  fi
done