# Copyright (c) 2013 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.

import("//build/config/allocator.gni")
import("//build/config/c++/c++.gni")
import("//build/config/coverage/coverage.gni")
import("//build/config/features.gni")

import("//build/config/sanitizers/sanitizers.gni")

declare_args() {
  # When set (the default) enables C++ iterator debugging in debug builds.
  # Iterator debugging is always off in release builds (technically, this flag
  # affects the "debug" config, which is always available but applied by
  # default only in debug builds).
  #
  # Iterator debugging is generally useful for catching bugs. But it can
  # introduce extra locking to check the state of an iterator against the state
  # of the current object. For iterator- and thread-heavy code, this can
  # significantly slow execution.
  enable_iterator_debugging = true
  enable_predefined_macro = false
}

# ==============================================
#   PLEASE DO NOT ADD MORE THINGS TO THIS LIST
# ==============================================
#
# Legacy feature defines applied to all targets.
#
# These are applied to every single compile in the build and most of them are
# only relevant to a few files. This bloats command lines and causes
# unnecessary recompiles when flags are flipped.
#
# To pass defines to source code from the build, use the buildflag system which
# will write headers containing the defines you need. This isolates the define
# and means its definition can participate in the build graph, only recompiling
# things when it actually changes.
#
# See //build/buildflag_header.gni for instructions on generating headers.
#
# This will also allow you to scope your build flag to a BUILD.gn file (or a
# .gni file if you need it from more than one place) rather than making global
# flags. See //build/config/BUILDCONFIG.gn for advice on where to define
# build flags.
config("feature_flags") {
  # Don't use deprecated V8 APIs anywhere.
  defines = [ "V8_DEPRECATION_WARNINGS" ]
  if (use_udev) {
    defines += [ "USE_UDEV" ]
  }
  if (is_win || is_linux) {
    defines += [ "USE_AURA=1" ]
  }
  if (is_linux) {
    defines += [
      "USE_GLIB=1",
      "USE_NSS_CERTS=1",
      "USE_X11=1",
    ]
  }

  if ((is_asan || is_lsan || is_tsan || is_msan) && using_sanitizer) {
    defines += [
      "MEMORY_TOOL_REPLACES_ALLOCATOR",
      "MEMORY_SANITIZER_INITIAL_SIZE",
    ]
  }
  if (is_asan && using_sanitizer) {
    defines += [ "ADDRESS_SANITIZER" ]
  }
  if (is_lsan) {
    defines += [ "LEAK_SANITIZER" ]
  }
  if (is_tsan) {
    defines += [
      "THREAD_SANITIZER",
      "DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL=1",
      "WTF_USE_DYNAMIC_ANNOTATIONS_NOIMPL=1",
    ]
  }
  if (is_msan) {
    defines += [ "MEMORY_SANITIZER" ]
  }
  if (is_ubsan || is_ubsan_null || is_ubsan_vptr || is_ubsan_security) {
    defines += [ "UNDEFINED_SANITIZER" ]
  }
  if (use_clang_coverage) {
    defines += [ "CLANG_COVERAGE" ]
  }
  if (is_official_build) {
    defines += [ "OFFICIAL_BUILD" ]
  }

  # ==============================================
  #   PLEASE DO NOT ADD MORE THINGS TO THIS LIST
  # ==============================================
  #
  # See the comment at the top.
}

# Debug/release ----------------------------------------------------------------

config("debug") {
  defines = [
    "DYNAMIC_ANNOTATIONS_ENABLED=1",
    "WTF_USE_DYNAMIC_ANNOTATIONS=1",
  ]

  if (is_nacl) {
    defines += [ "DYNAMIC_ANNOTATIONS_PREFIX=NACL_" ]
  }

  if (is_win) {
    if (!enable_iterator_debugging) {
      # Iterator debugging is enabled by default by the compiler on debug
      # builds, and we have to tell it to turn it off.
      defines += [ "_HAS_ITERATOR_DEBUGGING=0" ]
    }
  } else if (is_linux && current_cpu == "x64" && enable_iterator_debugging) {
    # Enable libstdc++ debugging facilities to help catch problems early, see
    # http://crbug.com/65151 .
    # defines += [ "_GLIBCXX_DEBUG=1" ]
  }
}

config("release") {
  defines = [ "NDEBUG" ]

  # Sanitizers.
  if (is_tsan) {
    defines += [
      "DYNAMIC_ANNOTATIONS_ENABLED=1",
      "WTF_USE_DYNAMIC_ANNOTATIONS=1",
    ]
  } else {
    defines += [ "NVALGRIND" ]
    if (!is_nacl) {
      # NaCl always enables dynamic annotations. Currently this value is set to
      # 1 for all .nexes.
      defines += [ "DYNAMIC_ANNOTATIONS_ENABLED=0" ]
    }
  }
}

# define custom board_config
config("cust_board_config") {
  cflags = []
  cflags_cc = []
  ldflags = []
  include_dirs = []
  defines = []
  asmflags = []
  if (defined(cust_board_cflags)) {
    cflags += cust_board_cflags
  }
  if (defined(cust_board_cxx_flags)) {
    cflags_cc += cust_board_cxx_flags
  }
  if (defined(cust_board_ld_flags)) {
    ldflags += cust_board_ld_flags
  }
  if (defined(cust_board_include_dirs)) {
    include_dirs += cust_board_include_dirs
  }
  if (defined(cust_board_macro_defines)) {
    defines += cust_board_macro_defines
  }
  if (defined(cust_board_asmflags)) {
    asmflags += cust_board_asmflags
  }
}

# Default libraries ------------------------------------------------------------

# This config defines the default libraries applied to all targets.
config("default_libs") {
  if (is_win) {
    libs = [
      "advapi32.lib",
      "comdlg32.lib",
      "dbghelp.lib",
      "dnsapi.lib",
      "gdi32.lib",
      "msimg32.lib",
      "odbc32.lib",
      "odbccp32.lib",
      "oleaut32.lib",
      "psapi.lib",
      "shell32.lib",
      "shlwapi.lib",
      "user32.lib",
      "usp10.lib",
      "uuid.lib",
      "version.lib",
      "wininet.lib",
      "winmm.lib",
      "winspool.lib",
      "ws2_32.lib",

      # Please don't add more stuff here. We should actually be making this
      # list smaller, since all common things should be covered. If you need
      # some extra libraries, please just add a libs = [ "foo.lib" ] to your
      # target that needs it.
    ]
    if (current_os == "winuwp") {
      # These libraries are needed for Windows UWP (i.e. store apps).
      libs += [
        "dloadhelper.lib",
        "WindowsApp.lib",
      ]
    } else {
      # These libraries are not compatible with Windows UWP (i.e. store apps.)
      libs += [
        "delayimp.lib",
        "kernel32.lib",
        "ole32.lib",
      ]
    }
  } else if (is_ohos) {
    libs = [
      "dl",
      "m",
    ]
  } else if (is_mac) {
    # Targets should choose to explicitly link frameworks they require. Since
    # linking can have run-time side effects, nothing should be listed here.
    libs = []
  } else if (is_linux) {
    libs = [
      "dl",
      "pthread",
      "rt",
    ]
  }
}

# Only //build/config/BUILDCONFIG.gn should reference this.
group("common_deps") {
  public_deps = []

  if (using_sanitizer) {
    public_deps += [ "//build/config/sanitizers:deps" ]
  }

  if (use_custom_libcxx) {
    if (is_double_framework) {
      public_deps += [ "${asdk_libs_dir}/ndk/libcxx:libcxx" ]
    } else {
      public_deps += [ "//third_party/libcxx:libcxx" ]
    }
  }

  if (use_afl) {
    public_deps += [ "//third_party/afl" ]
  }

  if (is_ohos && use_order_profiling) {
    public_deps += []
  }

  if (use_musl && current_toolchain != host_toolchain && !is_mingw &&
      !is_linux) {
    if (defined(ohos_indep_compiler_enable) && ohos_indep_compiler_enable) {
      external_deps = [ "musl:soft_shared_libs" ]
    } else {
      public_deps += [ "//third_party/musl:soft_shared_libs" ]
    }
  }

  if (is_ohos && ohos_indep_compiler_enable &&
      current_toolchain != host_toolchain) {
    public_deps += [ "//build/common/ubsan:ubsan" ]
  }
}

group("executable_deps") {
  public_deps = [ ":common_deps" ]
  if (export_libcxxabi_from_executables) {
    if (!is_double_framework) {
      public_deps += [ "//third_party/libcxxabi:libc++abi" ]
    }
  }
}

group("loadable_module_deps") {
  public_deps = [ ":common_deps" ]
}

group("shared_library_deps") {
  public_deps = [ ":common_deps" ]
}

group("rust_library_deps") {
  public_deps = [ ":common_deps" ]
}

group("rust_proc_macro_deps") {
  public_deps = [ ":common_deps" ]
}

group("static_library_deps") {
  if (use_musl && current_toolchain != host_toolchain && !is_mingw &&
      !is_linux) {
    if (defined(ohos_indep_compiler_enable) && ohos_indep_compiler_enable) {
      external_deps = [ "musl:musl_headers" ]
    } else {
      public_deps = [ "//third_party/musl:musl_headers" ]
    }
  }
}

group("source_set_deps") {
  if (use_musl && current_toolchain != host_toolchain && !is_mingw &&
      !is_linux) {
    if (defined(ohos_indep_compiler_enable) && ohos_indep_compiler_enable) {
      external_deps = [ "musl:musl_headers" ]
    } else {
      public_deps = [ "//third_party/musl:musl_headers" ]
    }
  }
}

# Executable configs -----------------------------------------------------------

# Windows linker setup for EXEs and DLLs.
if (is_win) {
  _windows_linker_configs = [
    "//build/config/win:sdk_link",
    "//build/config/win:common_linker_setup",
  ]
}

# This config defines the configs applied to all executables.
config("executable_config") {
  configs = []

  if (is_win) {
    configs += _windows_linker_configs

    # Currently only turn on linker CFI for executables.
    configs += [ "//build/config/win:cfi_linker" ]
  } else if (is_mac) {
    configs += [ "//build/config/mac:mac_dynamic_flags" ]
  } else if (is_linux || is_ohos || current_os == "aix") {
    configs += [ "//build/config/gcc:executable_ldconfig" ]
    if (is_ohos) {
      configs += [ "//build/config/ohos:executable_config" ]
    } else if (is_linux) {
      configs += [ "//build/config/linux:executable_config" ]
    }
  }

  # If we're using the prebuilt instrumented libraries with the sanitizers, we
  # need to add ldflags to every binary to make sure they are picked up.
  if (prebuilt_instrumented_libraries_available) {
    configs += [ "//third_party/instrumented_libraries:prebuilt_ldflags" ]
  }
  if (use_locally_built_instrumented_libraries) {
    configs += [ "//third_party/instrumented_libraries:locally_built_ldflags" ]
  }
  configs += [ "//build/config/sanitizers:link_executable" ]
}

# Shared library configs -------------------------------------------------------

# This config defines the configs applied to all shared libraries.
config("shared_library_config") {
  configs = []

  if (is_win) {
    configs += _windows_linker_configs
  } else if (is_mac) {
    configs += [ "//build/config/mac:mac_dynamic_flags" ]
  }

  # If we're using the prebuilt instrumented libraries with the sanitizers, we
  # need to add ldflags to every binary to make sure they are picked up.
  if (prebuilt_instrumented_libraries_available) {
    configs += [ "//third_party/instrumented_libraries:prebuilt_ldflags" ]
  }
  if (use_locally_built_instrumented_libraries) {
    configs += [ "//third_party/instrumented_libraries:locally_built_ldflags" ]
  }
  configs += [ "//build/config/sanitizers:link_shared_library" ]
}

config("predefined_macro") {
  if (enable_predefined_macro) {
    defines = []
    if (is_ohos) {
      defines += [ "__OHOS__" ]
    }
    if (is_mac) {
      defines += [ "__MAC__" ]
    }
    if (is_linux) {
      defines += [ "__LINUX__" ]
    }
    if (is_nacl) {
      defines += [ "__NACL__" ]
    }
    if (is_win) {
      defines += [ "__WINDOWS__" ]
    }
    if (is_mingw) {
      defines += [ "___MINGW32__" ]
    }
    if (is_android) {
      defines += [ "__ANDROID__" ]
    }
    if (is_ios) {
      defines += [ "__IOS__" ]
    }
    if (current_cpu == "x86") {
      defines += [ "__x86_32__" ]
    }
    if (current_cpu == "x86_64") {
      defines += [ "__x86_64__" ]
    }
    if (current_cpu == "arm") {
      defines += [ "__arm32__" ]
    }
    if (current_cpu == "arm64") {
      defines += [ "__arm64__" ]
    }
  }
}