# Copyright 2013 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/config.gni")
import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/ozone.gni")
import("//build/config/sysroot.gni")  # Imports android/config.gni.
import("//build/toolchain/gcc_toolchain.gni")

declare_args() {
  # Whether unstripped binaries, i.e. compiled with debug symbols, should be
  # considered runtime_deps rather than stripped ones.
  android_unstripped_runtime_outputs = true
}

template("android_clang_toolchain") {
  clang_toolchain(target_name) {
    assert(defined(invoker.toolchain_args),
           "toolchain_args must be defined for android_clang_toolchain()")

    toolchain_args = {
      forward_variables_from(invoker.toolchain_args, "*")
      current_os = "android"
      use_debug_fission = false
    }

    # Output linker map files for binary size analysis.
    enable_linker_map = true

    strip = rebase_path("$clang_base_path/bin/llvm-strip", root_build_dir)

    use_unstripped_as_runtime_outputs = android_unstripped_runtime_outputs

    # Don't use .cr.so for loadable_modules since they are always loaded via
    # absolute path.
    loadable_module_extension = ".so"

    # We propagate configs to allow cross-toolchain JNI include directories to
    # work. This flag does not otherwise affect our build, but if applied to
    # non-android toolchains, it causes unwanted configs from perfetto to
    # propagate from host_toolchain deps.
    propagates_configs = true
  }
}

android_clang_toolchain("android_clang_x86") {
  toolchain_args = {
    current_cpu = "x86"

    # This turns off all of the LaCrOS-specific flags. A LaCrOS related build
    # may use |ash_clang_x64| or |lacros_clang_x64| toolchain, which are
    # chromeos toolchains, to build Ash-Chrome or Lacros-Chrome in a
    # subdirectory, and because chromeos toolchain uses android toolchain, which
    # eventually resulted in that android toolchains being used inside a LaCrOS
    # build.
    also_build_ash_chrome = false
    also_build_lacros_chrome = false
    chromeos_is_browser_only = false
    ozone_platform = ""
    ozone_platform_wayland = false
  }
}

android_clang_toolchain("android_clang_arm") {
  toolchain_args = {
    current_cpu = "arm"
  }
}

android_clang_toolchain("android_clang_mipsel") {
  toolchain_args = {
    current_cpu = "mipsel"
  }
}

android_clang_toolchain("android_clang_x64") {
  toolchain_args = {
    current_cpu = "x64"

    # This turns off all of the LaCrOS-specific flags. A LaCrOS related build
    # may use |ash_clang_x64| or |lacros_clang_x64| toolchain, which are
    # chromeos toolchains, to build Ash-Chrome or Lacros-Chrome in a
    # subdirectory, and because chromeos toolchain uses android toolchain, which
    # eventually resulted in that android toolchains being used inside a LaCrOS
    # build.
    also_build_ash_chrome = false
    also_build_lacros_chrome = false
    chromeos_is_browser_only = false
    ozone_platform = ""
    ozone_platform_wayland = false
  }
}

android_clang_toolchain("android_clang_arm64") {
  toolchain_args = {
    current_cpu = "arm64"
  }
}

android_clang_toolchain("android_clang_arm64_hwasan") {
  toolchain_args = {
    current_cpu = "arm64"
    is_hwasan = true
    android64_ndk_api_level = 29
  }
}

android_clang_toolchain("android_clang_mips64el") {
  toolchain_args = {
    current_cpu = "mips64el"
  }
}

# Placeholder for riscv64 support, not tested since the toolchain is not ready.
android_clang_toolchain("android_clang_riscv64") {
  toolchain_args = {
    current_cpu = "riscv64"
  }
}

# Toolchain for creating native libraries that can be used by
# robolectric_binary targets. It does not emulate NDK APIs nor make available
# NDK header files.
# Targets that opt into defining JNI entrypoints should use the
# //third_party/jdk:jdk config to make jni.h available.
# This toolchain will set:
#   is_linux = true
#   is_android = false
#   is_robolectric = true
clang_toolchain("robolectric_$host_cpu") {
  toolchain_args = {
    current_os = host_os
    current_cpu = host_cpu
    is_robolectric = true
  }

  # TODO(crbug.com/1487407): Figure out why robolectric tests fail with component builds.
  toolchain_args.is_component_build = false
  shlib_extension = ".so"
}