chromium/build/android/BUILD.gn

# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/android/build_vars.gni")
import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
import("//build/config/clang/clang.gni")
import("//build/config/coverage/coverage.gni")
import("//build/config/gclient_args.gni")
import("//build/config/python.gni")
import("//build_overrides/build.gni")

if (build_with_chromium) {
  import("//base/allocator/partition_allocator/partition_alloc.gni")
}

if (enable_java_templates) {
  # Create or update the API versions cache if necessary by running a
  # functionally empty lint task. This prevents racy creation of the
  # cache while linting java targets in android_lint.
  android_lint("prepare_android_lint_cache") {
    create_cache = true
  }

  generate_build_config_srcjar("build_config_gen") {
    use_final_fields = false
  }

  generate_build_config_srcjar("build_config_for_testing_gen") {
    use_final_fields = false
    testonly = true
  }

  write_native_libraries_java("native_libraries_gen") {
    use_final_fields = false
  }

  java_library("build_java") {
    supports_android = true
    srcjar_deps = [
      ":build_config_gen",
      ":native_libraries_gen",
    ]
    sources = [
      "java/src/org/chromium/build/annotations/AlwaysInline.java",
      "java/src/org/chromium/build/annotations/AssumeNonNull.java",
      "java/src/org/chromium/build/annotations/CheckDiscard.java",
      "java/src/org/chromium/build/annotations/DoNotClassMerge.java",
      "java/src/org/chromium/build/annotations/DoNotInline.java",
      "java/src/org/chromium/build/annotations/DoNotStripLogs.java",
      "java/src/org/chromium/build/annotations/IdentifierNameString.java",
      "java/src/org/chromium/build/annotations/MockedInTests.java",
      "java/src/org/chromium/build/annotations/UsedByReflection.java",
    ]

    jar_excluded_patterns = [ "*/build/BuildConfig.class" ]

    # New version of NativeLibraries.java (with the actual correct values) will
    # be created when creating an apk.
    jar_excluded_patterns += [ "*/NativeLibraries.class" ]

    proguard_configs = [ "chromium_annotations.flags" ]
  }

  # Not all //build embedders pull in junit_binary deps that live in //third_party.
  if (build_with_chromium) {
    android_assets("junit_test_assets") {
      testonly = true

      # We just need any file here, so use the test itself.
      sources = [ "junit/src/org/chromium/build/AndroidAssetsTest.java" ]
    }
    android_resources("junit_test_resources") {
      testonly = true
      sources = [ "junit/res/values/strings.xml" ]
      mergeable_android_manifests = [ "junit/AndroidManifest_mergetest.xml" ]
    }
    robolectric_binary("build_junit_tests") {
      # Test has no JNI, so skip JNI Generator step.
      resources_package = "org.chromium.build"
      sources = [
        "junit/src/org/chromium/build/AndroidAssetsTest.java",
        "junit/src/org/chromium/build/IncrementalJavacTest.java",
      ]
      deps = [
        ":junit_test_assets",
        ":junit_test_resources",
        "//build/android/test/incremental_javac_gn:no_signature_change_prebuilt_java",
        "//third_party/junit",
      ]
    }
  }
}

if (defined(sanitizer_arch)) {
  action("generate_wrap_sh") {
    script = "generate_wrap_sh.py"
    outputs = [ "$target_gen_dir/$target_name/wrap.sh" ]
    args = [
      "--arch=$sanitizer_arch",
      "--output",
      rebase_path(outputs[0], root_build_dir),
    ]
  }
}

# TODO(go/turn-down-test-results): Remove once we turn down
# test-results.appspot.com
python_library("test_result_presentations_py") {
  pydeps_file = "pylib/results/presentation/test_results_presentation.pydeps"
  data = [
    "//build/android/pylib/results/presentation/template",
    "//build/android/pylib/results/presentation/javascript/main_html.js",
    "//third_party/catapult/third_party/gsutil/",
    "//third_party/jinja2/debug.py",
    "//third_party/six",
  ]
}

python_library("devil_chromium_py") {
  pydeps_file = "devil_chromium.pydeps"
  data = [
    "devil_chromium.py",
    "devil_chromium.json",
    "//third_party/catapult/third_party/gsutil/",
    "//third_party/catapult/devil/devil/devil_dependencies.json",

    # Read by gn_helpers.BuildWithChromium()
    "//build/config/gclient_args.gni",
  ]
}

# Contains runtime deps for installing apks.
# E.g. from test_runner.py or from apk_operations.py.
group("apk_installer_data") {
  # Other //build users let devil library fetch these from Google Storage.
  if (build_with_chromium) {
    if (is_component_build) {
      data_deps = [
        "//build/android/pylib/device/commands",
        "//tools/android/md5sum",
      ]
    }
    data = [ "//third_party/android_build_tools/bundletool/cipd/bundletool.jar" ]
  }
}

python_library("apk_operations_py") {
  pydeps_file = "apk_operations.pydeps"
  deps = [ ":apk_installer_data" ]
}

group("test_runner_py") {
  testonly = true
  deps = [
    ":test_runner_core_py",
    ":test_runner_device_support",
  ]
}

python_library("test_runner_core_py") {
  testonly = true
  pydeps_file = "test_runner.pydeps"
  data = [
    "pylib/gtest/filter/",
    "pylib/instrumentation/render_test.html.jinja",
    "pylib/local/machine/local_machine_hostside_tradefed_config.xml",
    "test_wrapper/logdog_wrapper.py",
    "//third_party/requests/",
  ]
  data_deps = [ ":logdog_wrapper_py" ]
  if (use_clang_coverage && checkout_clang_coverage_tools) {
    # Test runner script may use `llvm-profdata` (if exists) to merge
    # clang code coverage data after each instrumentation test to
    # save space. The tool only exists when `checkout_clang_coverage_tools`
    # gclient custom_var is true.
    data += [ "${clang_base_path}/bin/llvm-profdata" ]
  }
}

group("test_runner_device_support") {
  testonly = true

  # We hardcode using these tools from the public sdk in devil_chromium.json and
  # in pylib's constants.
  data = [
    "${public_android_sdk_build_tools}/aapt",
    "${public_android_sdk_build_tools}/aapt2",
    "${public_android_sdk_build_tools}/dexdump",
    "${public_android_sdk_build_tools}/lib64/libc++.so",
    "${public_android_sdk_build_tools}/split-select",
    "${public_android_sdk_root}/platform-tools/adb",
  ]
  data_deps = [
    ":apk_installer_data",
    ":devil_chromium_py",
    ":stack_tools",
  ]

  # Other //build users let devil library fetch these from Google Storage.
  if (build_with_chromium) {
    if (!is_component_build) {
      data_deps += [ "//tools/android/forwarder2" ]
    }
    data += [ "//tools/android/avd/proto/" ]
    if (enable_chrome_android_internal) {
      data += [ "//clank/tools/android/avd/proto/" ]
    }
    if (use_full_mte) {
      data_deps += [ "//tools/android/mte:mte_device_setup" ]
    }
  }

  # Proguard is needed only when using apks (rather than native executables).
  if (enable_java_templates) {
    data_deps += [ "//build/android/stacktrace:java_deobfuscate" ]
  }
}

python_library("logdog_wrapper_py") {
  pydeps_file = "test_wrapper/logdog_wrapper.pydeps"
}

python_library("resource_sizes_py") {
  pydeps_file = "resource_sizes.pydeps"
  data_deps = [
    ":devil_chromium_py",
    "//third_party/catapult/tracing:convert_chart_json",
  ]

  data = [
    build_vars_file,
    "//third_party/llvm-build/Release+Asserts/bin/llvm-readobj",
  ]
}

# Tools necessary for symbolizing tombstones or stack traces that are output to
# logcat.
# Hidden behind build_with_chromium because some third party repos that use
# //build don't pull in //third_party/android_platform.
# TODO(crbug.com/40145789): Move stack script into //build/third_party
#     and enable unconditionally.
group("stack_tools") {
  if (build_with_chromium) {
    data = [
      "tombstones.py",
      "pylib/symbols/",
      "stacktrace/",
    ]

    data_deps =
        [ "//third_party/android_platform/development/scripts:stack_py" ]
  }
}

# GN evaluates each .gn file once per toolchain, so restricting to default
# toolchain will ensure write_file() is called only once.
assert(current_toolchain == default_toolchain)

# NOTE: If other platforms would benefit from exporting variables, we should
# move this to a more top-level place.
# It is currently here (instead of //BUILD.gn) to ensure that the file is
# written even for non-chromium embedders of //build.
_build_vars_json = {
  # Underscore prefix so that it appears at the top.
  _HEADER = "Generated during 'gn gen' by //build/android/BUILD.gn."
  forward_variables_from(android_build_vars_json, "*")
}

write_file(build_vars_file, _build_vars_json, "json")