# Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

import("//build/config/crypto.gni")
import("//build/config/ui.gni")
import("../build/webrtc.gni")

config("rtc_base_config") {
  include_dirs = [
    "//third_party/jsoncpp/overrides/include",
    "//third_party/jsoncpp/source/include",
  ]

  defines = [
    "FEATURE_ENABLE_SSL",
    "LOGGING=1",
  ]

  # TODO(henrike): issue 3307, make rtc_base build without disabling
  # these flags.
  cflags_cc = [ "-Wno-non-virtual-dtor" ]
}

config("rtc_base_chromium_config") {
  defines = [
    "NO_MAIN_THREAD_WRAPPING",
  ]
}

config("openssl_config") {
  defines = [
    "SSL_USE_OPENSSL",
    "HAVE_OPENSSL_SSL_H",
  ]
}

config("nss_config") {
  defines = [
    "SSL_USE_NSS",
    "HAVE_NSS_SSL_H",
    "SSL_USE_NSS_RNG",
  ]
}

config("ios_config") {
  libs = [
    #"Foundation.framework",  # Already included in //build/config:default_libs.
    "Security.framework",
    "SystemConfiguration.framework",
    #"UIKit.framework",  # Already included in //build/config:default_libs.
  ]
}

config("mac_config") {
  libs = [
    "Cocoa.framework",
    #"Foundation.framework",  # Already included in //build/config:default_libs.
    #"IOKit.framework",  # Already included in //build/config:default_libs.
    #"Security.framework",  # Already included in //build/config:default_libs.
    "SystemConfiguration.framework",
  ]
}

config("mac_x86_config") {
  libs = [
    #"Carbon.framework",  # Already included in //build/config:default_libs.
  ]
}

if (is_linux && !build_with_chromium) {
  # Provides the same functionality as the //crypto:platform target, which
  # WebRTC cannot use as we don't sync src/crypto from Chromium.
  group("linux_system_ssl") {
    if (use_openssl) {
      deps = [ "//third_party/boringssl" ]
    } else {
      deps = [ "//net/third_party/nss/ssl:libssl" ]

      public_configs = [
        "//net/third_party/nss/ssl:ssl_config",
        "//third_party/nss:system_nss_no_ssl_config",
      ]
    }
  }
}

if (rtc_build_ssl == 0) {
  config("external_ssl_library") {
    assert(rtc_ssl_root != "",
           "You must specify rtc_ssl_root when rtc_build_ssl==0.")
    include_dirs = [ rtc_ssl_root ]
  }
}

# The subset of rtc_base approved for use outside of libjingle.
static_library("rtc_base_approved") {
  configs += [ "..:common_config" ]
  public_configs = [ "..:common_inherited_config" ]

  sources = [
    "checks.cc",
    "checks.h",
    "event.cc",
    "event.h",
    "exp_filter.cc",
    "exp_filter.h",
    "md5.cc",
    "md5.h",
    "md5digest.h",
    "platform_file.cc",
    "platform_file.h",
    "safe_conversions.h",
    "safe_conversions_impl.h",
    "stringencode.cc",
    "stringencode.h",
    "stringutils.cc",
    "stringutils.h",
    "template_util.h",
    "thread_annotations.h",
    "thread_checker.h",
    "thread_checker_impl.cc",
    "thread_checker_impl.h",
    "timeutils.cc",
    "timeutils.h",
  ]
}

static_library("rtc_base") {
  cflags = []
  cflags_cc = []
  libs = []
  deps = [
    ":rtc_base_approved",
  ]

  configs += [
    "..:common_config",
    ":rtc_base_config",
  ]

  public_configs = [
    "..:common_inherited_config",
    ":rtc_base_config",
  ]

  defines = [
    "LOGGING=1",
  ]

  sources = [
    "arraysize.h",
    "asyncfile.cc",
    "asyncfile.h",
    "asynchttprequest.cc",
    "asynchttprequest.h",
    "asyncpacketsocket.h",
    "asyncsocket.cc",
    "asyncsocket.h",
    "asynctcpsocket.cc",
    "asynctcpsocket.h",
    "asyncudpsocket.cc",
    "asyncudpsocket.h",
    "autodetectproxy.cc",
    "autodetectproxy.h",
    "base64.cc",
    "base64.h",
    "basicdefs.h",
    "bytebuffer.cc",
    "bytebuffer.h",
    "byteorder.h",
    "common.cc",
    "common.h",
    "cpumonitor.cc",
    "cpumonitor.h",
    "crc32.cc",
    "crc32.h",
    "criticalsection.h",
    "cryptstring.h",
    "diskcache.cc",
    "diskcache.h",
    "fileutils.cc",
    "fileutils.h",
    "firewallsocketserver.cc",
    "firewallsocketserver.h",
    "flags.cc",
    "flags.h",
    "format_macros.h",
    "gunit_prod.h",
    "helpers.cc",
    "helpers.h",
    "httpbase.cc",
    "httpbase.h",
    "httpclient.cc",
    "httpclient.h",
    "httpcommon-inl.h",
    "httpcommon.cc",
    "httpcommon.h",
    "httprequest.cc",
    "httprequest.h",
    "iosfilesystem.mm",
    "ipaddress.cc",
    "ipaddress.h",
    "linked_ptr.h",
    "mathutils.h",
    "messagedigest.cc",
    "messagedigest.h",
    "messagehandler.cc",
    "messagehandler.h",
    "messagequeue.cc",
    "messagequeue.h",
    "nethelpers.cc",
    "nethelpers.h",
    "network.cc",
    "network.h",
    "nullsocketserver.h",
    "pathutils.cc",
    "pathutils.h",
    "physicalsocketserver.cc",
    "physicalsocketserver.h",
    "proxydetect.cc",
    "proxydetect.h",
    "proxyinfo.cc",
    "proxyinfo.h",
    "ratelimiter.cc",
    "ratelimiter.h",
    "ratetracker.cc",
    "ratetracker.h",
    "scoped_autorelease_pool.h",
    "scoped_autorelease_pool.mm",
    "scoped_ptr.h",
    "sha1.cc",
    "sha1.h",
    "sha1digest.h",
    "signalthread.cc",
    "signalthread.h",
    "sigslot.h",
    "sigslotrepeater.h",
    "socket.h",
    "socketadapters.cc",
    "socketadapters.h",
    "socketaddress.cc",
    "socketaddress.h",
    "socketaddresspair.cc",
    "socketaddresspair.h",
    "socketfactory.h",
    "socketpool.cc",
    "socketpool.h",
    "socketserver.h",
    "socketstream.cc",
    "socketstream.h",
    "ssladapter.cc",
    "ssladapter.h",
    "sslfingerprint.cc",
    "sslfingerprint.h",
    "sslidentity.cc",
    "sslidentity.h",
    "sslsocketfactory.cc",
    "sslsocketfactory.h",
    "sslstreamadapter.cc",
    "sslstreamadapter.h",
    "sslstreamadapterhelper.cc",
    "sslstreamadapterhelper.h",
    "stream.cc",
    "stream.h",
    "systeminfo.cc",
    "systeminfo.h",
    "task.cc",
    "task.h",
    "taskparent.cc",
    "taskparent.h",
    "taskrunner.cc",
    "taskrunner.h",
    "thread.cc",
    "thread.h",
    "timing.cc",
    "timing.h",
    "urlencode.cc",
    "urlencode.h",
    "worker.cc",
    "worker.h",
  ]

  if (is_posix) {
    sources += [
      "unixfilesystem.cc",
      "unixfilesystem.h",
    ]
  }

  if (build_with_chromium) {
    sources += [
      "../overrides/webrtc/base/basictypes.h",
      "../overrides/webrtc/base/constructormagic.h",
      "../overrides/webrtc/base/logging.cc",
      "../overrides/webrtc/base/logging.h",
    ]

    if (is_win) {
      sources += [ "../overrides/webrtc/base/win32socketinit.cc" ]
    }

    include_dirs = [
      "../overrides",
      "../../boringssl/src/include",
    ]

    public_configs += [ ":rtc_base_chromium_config" ]
  } else {
    sources += [
      "asyncinvoker.cc",
      "asyncinvoker.h",
      "asyncinvoker-inl.h",
      "asyncresolverinterface.h",
      "atomicops.h",
      "bandwidthsmoother.cc",
      "bandwidthsmoother.h",
      "basictypes.h",
      "bind.h",
      "bind.h.pump",
      "buffer.h",
      "callback.h",
      "callback.h.pump",
      "constructormagic.h",
      "filelock.cc",
      "filelock.h",
      "fileutils_mock.h",
      "genericslot.h",
      "genericslot.h.pump",
      "httpserver.cc",
      "httpserver.h",
      "json.cc",
      "json.h",
      "logging.cc",
      "logging.h",
      "mathutils.h",
      "multipart.cc",
      "multipart.h",
      "natserver.cc",
      "natserver.h",
      "natsocketfactory.cc",
      "natsocketfactory.h",
      "nattypes.cc",
      "nattypes.h",
      "optionsfile.cc",
      "optionsfile.h",
      "profiler.cc",
      "profiler.h",
      "proxyserver.cc",
      "proxyserver.h",
      "refcount.h",
      "referencecountedsingletonfactory.h",
      "rollingaccumulator.h",
      "scopedptrcollection.h",
      "scoped_ref_ptr.h",
      "sec_buffer.h",
      "sharedexclusivelock.cc",
      "sharedexclusivelock.h",
      "sslconfig.h",
      "sslroots.h",
      "stringdigest.h",
      "testclient.cc",
      "testclient.h",
      "transformadapter.cc",
      "transformadapter.h",
      "versionparsing.cc",
      "versionparsing.h",
      "virtualsocketserver.cc",
      "virtualsocketserver.h",
      "window.h",
      "windowpickerfactory.h",
      "windowpicker.h",
    ]

    if (is_posix) {
      sources += [
        "latebindingsymboltable.cc",
        "latebindingsymboltable.cc.def",
        "latebindingsymboltable.h",
        "latebindingsymboltable.h.def",
        "posix.cc",
        "posix.h",
      ]
    }

    if (is_linux) {
      sources += [
        "dbus.cc",
        "dbus.h",
        "libdbusglibsymboltable.cc",
        "libdbusglibsymboltable.h",
        "linuxfdwalk.c",
        "linuxfdwalk.h",
      ]
    }

    if (is_mac) {
      sources += [
        "macasyncsocket.cc",
        "macasyncsocket.h",
        "maccocoasocketserver.h",
        "maccocoasocketserver.mm",
        "macsocketserver.cc",
        "macsocketserver.h",
        "macwindowpicker.cc",
        "macwindowpicker.h",
      ]
    }

    if (is_win) {
      sources += [
        "diskcache_win32.cc",
        "diskcache_win32.h",
        "win32regkey.cc",
        "win32regkey.h",
        "win32socketinit.cc",
        "win32socketinit.h",
        "win32socketserver.cc",
        "win32socketserver.h",
      ]
    }
    if (rtc_build_json) {
      deps += [ "//third_party/jsoncpp" ]
    } else {
      include_dirs += [ rtc_jsoncpp_root ]

      # When defined changes the include path for json.h to where it is
      # expected to be when building json outside of the standalone build.
      defines += [ "WEBRTC_EXTERNAL_JSON" ]
    }
  }  # !build_with_chromium

  if (is_clang) {
    # Suppress warnings from the Chrome Clang plugins.
    # See http://code.google.com/p/webrtc/issues/detail?id=163 for details.
    configs -= [ "//build/config/clang:find_bad_constructs" ]
  }

  # TODO(henrike): issue 3307, make rtc_base build with the Chromium default
  # compiler settings.
  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [ "//build/config/compiler:no_chromium_code" ]
  if (!is_win) {
    cflags += [ "-Wno-uninitialized" ]
    cflags_cc += [ "-Wno-non-virtual-dtor" ]
  }

  if (use_openssl) {
    public_configs += [ ":openssl_config" ]
    if (rtc_build_ssl) {
      deps += [ "//third_party/boringssl" ]
    } else {
      configs += [ "external_ssl_library" ]
    }
    sources += [
      "openssl.h",
      "openssladapter.cc",
      "openssladapter.h",
      "openssldigest.cc",
      "openssldigest.h",
      "opensslidentity.cc",
      "opensslidentity.h",
      "opensslstreamadapter.cc",
      "opensslstreamadapter.h",
    ]
  } else {
    public_configs += [ ":nss_config" ]
    if (rtc_build_ssl) {
      if (build_with_chromium) {
        deps += [ "//crypto:platform" ]
      } else {
        deps += [ "//net/third_party/nss/ssl:libssl" ]
        if (is_linux) {
          deps += [ ":linux_system_ssl" ]
        } else {
          deps += [
            "//third_party/nss:nspr",
            "//third_party/nss:nss",
          ]
        }
      }
    } else {
      configs += [ "external_ssl_library" ]
    }
    sources += [
      "nssidentity.cc",
      "nssidentity.h",
      "nssstreamadapter.cc",
      "nssstreamadapter.h",
    ]
  }

  if (is_android) {
    sources += [
      "ifaddrs-android.cc",
      "ifaddrs-android.h",
    ]

    libs += [
      "log",
      "GLESv2"
    ]
  }

  if (is_ios) {
    all_dependent_configs += [ ":ios_config" ]
  }

  if (use_x11) {
    sources += [
      "x11windowpicker.cc",
      "x11windowpicker.h",
    ]
    libs += [
      "dl",
      "rt",
      "Xext",
      "X11",
      "Xcomposite",
      "Xrender",
    ]
  }

  if (is_linux) {
    libs += [
      "dl",
      "rt",
    ]
  }

  if (is_mac) {
    sources += [
      "maccocoathreadhelper.h",
      "maccocoathreadhelper.mm",
      "macconversion.cc",
      "macconversion.h",
      "macutils.cc",
      "macutils.h",
    ]

    all_dependent_configs = [ ":mac_config" ]

    if (cpu_arch == "x86") {
      all_dependent_configs += [ ":mac_x86_config" ]
    }
  }

  if (is_win) {
    sources += [
      "schanneladapter.cc",
      "schanneladapter.h",
      "win32.cc",
      "win32.h",
      "win32filesystem.cc",
      "win32filesystem.h",
      "win32securityerrors.cc",
      "win32window.cc",
      "win32window.h",
      "win32windowpicker.cc",
      "win32windowpicker.h",
      "winfirewall.cc",
      "winfirewall.h",
      "winping.cc",
      "winping.h",
    ]

    libs += [
      "crypt32.lib",
      "iphlpapi.lib",
      "secur32.lib",
    ]

    cflags += [
      # Suppress warnings about WIN32_LEAN_AND_MEAN.
      "/wd4005",
      "/wd4703",
    ]

    defines += [ "_CRT_NONSTDC_NO_DEPRECATE" ]
  }

  if (is_posix && is_debug) {
    # The Chromium build/common.gypi defines this for all posix
    # _except_ for ios & mac.  We want it there as well, e.g.
    # because ASSERT and friends trigger off of it.
    defines += [ "_DEBUG" ]
  }

  if (is_ios || (is_mac && cpu_arch != "x86")) {
    defines += [ "CARBON_DEPRECATED=YES" ]
  }

  if (is_linux || is_android) {
    sources += [
      "linux.cc",
      "linux.h",
    ]
  }
}
