# 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/cast.gni")
import("//build/config/cronet/config.gni")
import("//build/config/gclient_args.gni")
import("//build/toolchain/kythe.gni")
import("//build/toolchain/rbe.gni")
import("//build/toolchain/siso.gni")
import("//build/toolchain/toolchain.gni")
import("//build_overrides/build.gni")

default_clang_base_path = "//third_party/llvm-build/Release+Asserts"

# These platforms have been well-tested with clang modules.
# Platforms not in this list *may* work with clang modules, but we don't
# guarantee it, and we don't test them on CI/CQ with modules, so breakages may
# occur.
linux_supported_cpus = [
  "arm",
  "arm64",
  "x64",
  "x86",
]
clang_modules_platform_supported =
    (is_linux && filter_include(linux_supported_cpus, [ current_cpu ]) != []) ||
    (is_android && current_cpu == "x64") ||
    (is_android && current_cpu == "arm64")

declare_args() {
  # Indicates if the build should use the Chrome-specific plugins for enforcing
  # coding guidelines, etc. Only used when compiling with Chromium's Clang.
  # Setting this enables all of the functionality of the plugin.
  clang_use_chrome_plugins = is_clang && current_os != "zos"

  # Use this instead of clang_use_chrome_plugins to enable just the raw-ptr
  # functionality of the plugin.
  clang_use_raw_ptr_plugin = false

  enable_check_raw_ptr_fields =
      build_with_chromium && !is_official_build &&
      ((is_linux && !is_castos) || (is_android && !is_cast_android) || is_mac ||
       is_ios || is_win || is_chromeos)

  # TODO(crbug.com/40268473): Merge with enable_check_raw_ptr_fields once both
  # checks are activated on the same set of platforms.
  enable_check_raw_ref_fields =
      build_with_chromium && !is_official_build &&
      ((is_linux && !is_castos) || (is_android && !is_cast_android) || is_win ||
       is_mac || is_ios || is_chromeos)

  # Use this instead of clang_use_chrome_plugins to enable just the unsafe
  # buffers functionality of the plugin
  clang_use_unsafe_buffers_plugin = false

  clang_base_path = default_clang_base_path

  # Specifies whether or not bitcode should be embedded during compilation.
  # This is used for creating a MLGO corpus from Chromium in the non-ThinLTO case.
  clang_embed_bitcode = false

  # Set to true to enable output of ThinLTO index and import files used for
  # creating a Chromium MLGO corpus in the ThinLTO case.
  lld_emit_indexes_and_imports = false

  # Use explicit Clang header modules for libc++.
  # We'll gradually expand supported platform of modules
  # (https://crbug.com/40440396).
  # For details on the current state of modules in Chromium see
  # https://chromium.googlesource.com/chromium/src/+/main/docs/modules.md
  # Currently we enables this only when building for linux x64.
  #
  # TODO(https://crbug.com/440260716): Enable for Cronet in AOSP once gn2bp
  # supports copy targets.
  use_clang_modules =
      # Clang modules doesn't work with translation_unit used in codesearch
      # pipeline http://b/436082487.
      !enable_kythe_annotations && is_clang &&
      clang_modules_platform_supported &&
      # reclient doesn't handle headers in modulemap config.
      !use_reclient && !is_cronet_for_aosp_build
}

# We don't really need to collect a corpus for the host tools, just for the target.
lld_emit_indexes_and_imports =
    lld_emit_indexes_and_imports && is_a_target_toolchain

# TODO(crbug.com/326584510): Reclient does not upload `inputs` from C/C++
# targets. We work around the bug in Reclient by
# specifying the files here.
rbe_bug_326584510_missing_input_list = []
if (clang_use_chrome_plugins && defined(clang_unsafe_buffers_paths) &&
    "$clang_unsafe_buffers_paths" != "") {
  rbe_bug_326584510_missing_input_list +=
      [ rebase_path(clang_unsafe_buffers_paths, rbe_exec_root) ]
}
if (defined(clang_warning_suppression_file) &&
    "$clang_warning_suppression_file" != "") {
  rbe_bug_326584510_missing_input_list +=
      [ rebase_path(clang_warning_suppression_file, rbe_exec_root) ]
}

# The leading space is important, if the string is non-empty.
rbe_bug_326584510_missing_inputs =
    " -inputs=" + string_join(",", rbe_bug_326584510_missing_input_list)

if (is_win) {
  clang_arg_prefix = "/clang:"
} else {
  clang_arg_prefix = ""
}
