# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/config/clang/clang.gni")
import("//build/config/sysroot.gni")  # Imports aosp/config.gni.
import("//build/toolchain/gcc_toolchain.gni")
import("//build_plugins/config/aosp/config.gni")

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

# The AOSP clang toolchains share most of the same parameters, so we have this
# wrapper around gcc_toolchain to avoid duplication of logic.
#
# Parameters:
#  - toolchain_root
#      Path to cpu-specific toolchain within the ndk.
#  - sysroot
#      Sysroot for this architecture.
#  - lib_dir
#      Subdirectory inside of sysroot where libs go.
#  - binary_prefix
#      Prefix of compiler executables.
template("aosp_clang_toolchain") {
  gcc_toolchain(target_name) {
    assert(defined(invoker.toolchain_args),
           "toolchain_args must be defined for aosp_clang_toolchain()")
    toolchain_args = invoker.toolchain_args
    toolchain_args.current_os = "android"
    if (defined(invoker.extra_cppflags)) {
      extra_cppflags = invoker.extra_cppflags
    }

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

    # Make our manually injected libs relative to the build dir.
    _ndk_lib =
        rebase_path(invoker.sysroot + "/" + invoker.lib_dir, root_build_dir)

    libs_section_prefix = "$_ndk_lib/crtbegin_dynamic.o"
    libs_section_postfix = "$_ndk_lib/crtend_android.o"

    solink_libs_section_prefix = "$_ndk_lib/crtbegin_so.o"
    solink_libs_section_postfix = "$_ndk_lib/crtend_so.o"

    _aosp_tool_prefix =
        "${invoker.toolchain_root}/bin/${invoker.binary_prefix}-"

    # The tools should be run relative to the build dir.
    _tool_prefix = rebase_path("$_aosp_tool_prefix", root_build_dir)

    if (defined(toolchain_args.clang_base_path) &&
        toolchain_args.clang_base_path != "") {
      clang_base_path = toolchain_args.clang_base_path
    }

    if (defined(use_android_clang) && use_android_clang) {
      clang_base_path = "$aosp_ndk_root/toolchains/llvm/prebuilt/${aosp_host_os}-${aosp_host_arch}"
    }

    _prefix = rebase_path("$clang_base_path/bin", root_build_dir)
    cc = "$_prefix/clang"
    cxx = "$_prefix/clang++"
    ar = "$_prefix/llvm-ar"
    ld = cxx
    readelf = _tool_prefix + "readelf"
    nm = _tool_prefix + "nm"
    if (!defined(toolchain_args.is_debug)) {
      strip = rebase_path("$clang_base_path/bin/llvm-strip", root_build_dir)
      use_unstripped_as_runtime_outputs = aosp_unstripped_runtime_outputs
    }

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

aosp_clang_toolchain("aosp_clang_arm") {
  toolchain_root = arm_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "arm-linux-androideabi"
  toolchain_args = {
    current_cpu = "arm"
  }
}

aosp_clang_toolchain("aosp_clang_arm_release") {
  toolchain_root = arm_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "arm-linux-androideabi"
  toolchain_args = {
    current_cpu = "arm"
    runtime_mode = "release"
  }
}

aosp_clang_toolchain("aosp_clang_arm_profile") {
  toolchain_root = arm_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "arm-linux-androideabi"
  toolchain_args = {
    current_cpu = "arm"
    runtime_mode = "profile"
  }
}

aosp_clang_toolchain("aosp_clang_arm_debug") {
  toolchain_root = arm_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "arm-linux-androideabi"
  toolchain_args = {
    current_cpu = "arm"
    runtime_mode = "debug"
  }
}

aosp_clang_toolchain("aosp_clang_arm64") {
  toolchain_root = arm64_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm64_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "aarch64-linux-android"
  toolchain_args = {
    current_cpu = "arm64"
    target_platform = "phone"
  }
}

aosp_clang_toolchain("aosp_clang_arm64_release") {
  toolchain_root = arm64_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm64_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "aarch64-linux-android"
  toolchain_args = {
    current_cpu = "arm64"
    target_platform = "phone"
    runtime_mode = "release"
  }
}

aosp_clang_toolchain("aosp_clang_arm64_profile") {
  toolchain_root = arm64_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm64_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "aarch64-linux-android"
  toolchain_args = {
    current_cpu = "arm64"
    target_platform = "phone"
    runtime_mode = "profile"
  }
}

aosp_clang_toolchain("aosp_clang_arm64_debug") {
  toolchain_root = arm64_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$arm64_aosp_sysroot_subdir"
  lib_dir = "usr/lib"
  binary_prefix = "aarch64-linux-android"
  toolchain_args = {
    current_cpu = "arm64"
    target_platform = "phone"
    runtime_mode = "debug"
  }
}

aosp_clang_toolchain("aosp_clang_x86_64") {
  toolchain_root = x86_64_aosp_toolchain_root
  sysroot = "$aosp_ndk_root/$x86_64_aosp_sysroot_subdir"
  lib_dir = "usr/lib64"
  binary_prefix = "x86_64-linux-android"
  toolchain_args = {
    current_cpu = "x86_64"
    target_platform = "phone"
    clang_base_path = "//prebuilts/clang/ohos/${host_platform_dir}/llvm-13"
    runtime_mode = "release"
  }
}