# Copyright 2015 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/buildflag_header.gni")

declare_args() {
  # If set to true, this will stub out and disable the entire crash key system.
  use_crash_key_stubs = is_fuchsia
}

group("common") {
  public_deps = [
    ":crash_key",
    ":crash_key_utils",
  ]

  if ((is_mac || is_ios) && !use_cobalt_customizations) {
    public_deps += [ ":zombies" ]
  }
}

use_crashpad_annotation = (is_mac || is_win) && !use_crash_key_stubs

buildflag_header("crash_buildflags") {
  header = "crash_buildflags.h"
  flags = [
    "USE_CRASHPAD_ANNOTATION=$use_crashpad_annotation",
    "USE_CRASH_KEY_STUBS=$use_crash_key_stubs",
  ]
}

# Crashpad's annotation system can store data on a per-module basis (i.e.,
# in different shared libraries in the component build) without issue. The
# Breakpad implementation uses a static global variable, so ensure there is
# only one instance of the symbol in the component build by making this
# target a component.
if (use_crash_key_stubs || use_crashpad_annotation) {
  crash_key_target_type = "static_library"
} else {
  crash_key_target_type = "component"
}
target(crash_key_target_type, "crash_key") {
  sources = [
    "crash_export.h",
    "crash_key.cc",
    "crash_key.h",
    "crash_key_base_support.cc",
    "crash_key_base_support.h",
  ]

  defines = []

  # This target is not always a component, depending on the implementation.
  # When it is not a component, annotating functions with the standard
  # CRASH_EXPORT macro causes linking errors on Windows (clients of this target
  # expect it to be dllimport but it is linked statically). Instead, provide a
  # wrapper macro CRASH_KEY_EXPORT that only evaluates to CRASH_EXPORT if this
  # target is really a component.
  if (crash_key_target_type == "component") {
    defines += [
      "CRASH_KEY_EXPORT=CRASH_EXPORT",
      "CRASH_CORE_COMMON_IMPLEMENTATION",
    ]
  }

  deps = [
    ":crash_buildflags",
    "//base",
  ]

  if (use_crash_key_stubs || use_cobalt_customizations) {
    sources += [ "crash_key_stubs.cc" ]
  } else if (use_crashpad_annotation) {
    sources += [ "crash_key_crashpad.cc" ]
    deps += [ "//third_party/crashpad/crashpad/client" ]
  } else {
    include_dirs = [ "//third_party/breakpad/breakpad/src" ]

    if (is_ios) {
      sources += [ "crash_key_breakpad_ios.mm" ]

      configs += [ "//build/config/compiler:enable_arc" ]
    } else {
      sources += [
        "crash_key_breakpad.cc",
        "crash_key_internal.h",
      ]
    }

    deps += [ "//third_party/breakpad:client" ]
  }
}

static_library("crash_key_utils") {
  visibility = [ ":*" ]

  sources = [
    "crash_keys.cc",
    "crash_keys.h",
  ]

  deps = [
    ":crash_key",
    "//base",
  ]
}

if ((is_mac || is_ios) && !use_cobalt_customizations) {
  component("zombies") {
    visibility = [ ":common" ]

    sources = [
      "objc_zombie.h",
      "objc_zombie.mm",
    ]

    defines = [ "CRASH_CORE_COMMON_IMPLEMENTATION" ]

    deps = [
      ":crash_key",
      "//base",
    ]

    libs = [ "Foundation.framework" ]
  }
}

if (!use_cobalt_customizations) {
  source_set("unit_tests") {
    testonly = true
    sources = [
      "crash_key_unittest.cc",
      "crash_keys_unittest.cc",
    ]

    deps = [
      ":common",
      "//base",
      "//testing/gtest",
    ]

    if (is_mac || is_ios) {
      sources += [ "objc_zombie_unittest.mm" ]
    }

    if (!is_mac && !is_win && !is_fuchsia) {
      include_dirs = [ "//third_party/breakpad/breakpad/src/" ]
      sources += [ "crash_key_breakpad_unittest.cc" ]
    }

    if (is_fuchsia) {
      sources -= [ "crash_key_unittest.cc" ]
    }
  }
}
