chromium/components/crash/core/common/BUILD.gn

# Copyright 2015 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/buildflag_header.gni")
import("//build/config/cronet/config.gni")
import("//build/config/features.gni")
import("//components/gwp_asan/buildflags/buildflags.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_apple) {
    public_deps += [ ":zombies" ]
  }

  if (is_ios) {
    public_deps += [ ":reporter_running_ios" ]
  }
}

use_crashpad_annotation =
    (is_mac || is_ios || is_win || is_android || is_chromeos ||
     (is_linux && !is_castos)) && !use_crash_key_stubs

# Make sure that breakpad use is limited to is_castos here so that there's no
# unknown unenumerated target.
assert(use_crash_key_stubs || use_crashpad_annotation || is_castos)

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

group("crash_key") {
  public_deps = [ ":crash_key_lib" ]
}

# 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_lib") {
  sources = [
    "crash_export.h",
    "crash_key.cc",
    "crash_key.h",
    "crash_key_base_support.cc",
    "crash_key_base_support.h",
  ]

  defines = []

  # Consumers should depend on `:crash_key`.
  visibility = [ ":*" ]

  # 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",
    ]
  }

  public_deps = [ ":crash_buildflags" ]

  deps = [ "//base" ]

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

    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_apple) {
  component("zombies") {
    visibility = [ ":common" ]

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

    defines = [ "CRASH_CORE_COMMON_IMPLEMENTATION" ]

    # Do not compile with ARC because ARC would prevent intercepting and
    # interfering with low-level object destruction.
    configs -= [ "//build/config/compiler:enable_arc" ]

    deps = [
      ":crash_key",
      "//base",
      "//components/gwp_asan/buildflags",
    ]

    if (enable_gwp_asan_malloc) {
      deps += [ "//components/gwp_asan/client" ]
    }

    frameworks = [ "Foundation.framework" ]
  }
}

if (is_ios) {
  source_set("reporter_running_ios") {
    sources = [
      "reporter_running_ios.cc",
      "reporter_running_ios.h",
    ]
  }
}

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

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

  if (is_apple) {
    sources += [ "objc_zombie_unittest.mm" ]

    # Do not compile with ARC because ARC would prevent intercepting and
    # interfering with low-level object destruction.
    configs -= [ "//build/config/compiler:enable_arc" ]
  }

  if (is_castos) {
    include_dirs = [ "//third_party/breakpad/breakpad/src/" ]
    sources += [ "crash_key_breakpad_unittest.cc" ]
  }

  # TODO(crbug.com/40172607): Enable when crash keys are supported on Fuchsia.
  if (is_fuchsia) {
    sources -= [
      "crash_key_unittest.cc",
      "crash_keys_unittest.cc",
    ]
  }
}