# Copyright 2014 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/features.gni")
import("//remoting/remoting_enable.gni")
import("//remoting/remoting_locales.gni")
import("//remoting/remoting_version.gni")

if (is_win) {
  import("//remoting/host/predefines_win.gni")
}
if (is_mac) {
  import("//build/config/mac/rules.gni")
  import("//third_party/icu/config.gni")
}

source_set("common") {
  sources = [
    "it2me_confirmation_dialog.h",
    "it2me_confirmation_dialog_linux.cc",
    "it2me_confirmation_dialog_mac.mm",
    "it2me_confirmation_dialog_proxy.cc",
    "it2me_confirmation_dialog_proxy.h",
    "it2me_confirmation_dialog_win.cc",
    "it2me_host.cc",
    "it2me_host.h",
    "it2me_native_messaging_host.cc",
    "it2me_native_messaging_host.h",
  ]

  if (is_chromeos) {
    sources += [ "it2me_confirmation_dialog_chromeos.cc" ]

    sources -= [ "it2me_confirmation_dialog_linux.cc" ]
  }

  configs += [
    "//build/config/compiler:wexit_time_destructors",
    "//remoting/build/config:version",
  ]

  deps = [
    "//base:i18n",
    "//net",
    "//remoting/base",
    "//remoting/host",
    "//remoting/protocol",
    "//remoting/resources",
    "//remoting/signaling",
  ]

  if (is_desktop_linux) {
    # GTK2 pulls pangoft2 as dependency, and pangoft2 depends on harfbuzz.
    # To avoid missing indirectly referenced harfbuzz symbols from pango,
    # some hack is required when bundled harfbuzz is used and component build is
    # disabled.
    # See crbug.com/462689 for details.
    all_dependent_configs = [ "//third_party/harfbuzz-ng:pangoft2_link_hack" ]

    if (use_gtk3) {
      deps += [ "//build/config/linux/gtk3" ]
    } else {
      deps += [ "//build/config/linux/gtk2" ]
    }
  }
}

if (!is_chromeos && enable_remoting_host) {
  if (is_win) {
    executable("remote_assistance_host") {
      configs += [ "//build/config/compiler:wexit_time_destructors" ]

      deps = [
        "//base/allocator",
        "//remoting/host/win:dpi_aware_exe_manifest",
        "//remoting/host/win:remoting_core",
        "//remoting/host/win:remoting_windows_resources",
      ]

      sources = [
        "$root_gen_dir/remoting/version.rc",
        "it2me_native_messaging_host_entry_point.cc",
      ]

      defines = host_predefines + [ "BINARY=BINARY_REMOTE_ASSISTANCE_HOST" ]

      ldflags = [
        # "/NODEFAULTLIB", TODO(zijiehe): Why IgnoreAllDefaultLibraries: true in
        # GYP does not take effect?
        "comctl32.lib",
      ]
    }

    executable("remote_assistance_host_uiaccess") {
      configs += [ "//build/config/compiler:wexit_time_destructors" ]

      deps = [
        "//base/allocator",
        "//remoting/host/win:remoting_core",
        "//remoting/host/win:remoting_windows_resources",
      ]

      sources = [
        "$root_gen_dir/remoting/version.rc",
        "it2me_native_messaging_host_entry_point.cc",
      ]

      defines = host_predefines + [ "BINARY=BINARY_HOST_IT2ME_UIACCESS" ]

      ldflags = [
        # "/NODEFAULTLIB", TODO(zijiehe): Why IgnoreAllDefaultLibraries: true in
        # GYP does not take effect?
        "comctl32.lib",
      ]

      # uiAccess requires a signed build to work correctly, so only enable the
      # manifest flag when the binary will be properly signed.
      if (is_official_build) {
        deps += [ "//remoting/host/win:dpi_aware_uiaccess_exe_manifest" ]
      } else {
        deps += [ "//remoting/host/win:dpi_aware_exe_manifest" ]
      }
    }
  } else {
    if (is_mac) {
      app_target_type = "mac_app_bundle"

      bundle_data("remote_assistance_host_dialog_icon") {
        sources = [
          "//remoting/resources/chromoting128.png",
        ]

        outputs = [
          "{{bundle_resources_dir}}/{{source_file_part}}",
        ]
      }

      # remote_assistance_host-InfoPlist.strings
      foreach(locale, remoting_locales_with_underscores) {
        bundle_data("remote_assistance_host_strings_${locale}_bundle_data") {
          sources = [
            "$root_gen_dir/remoting/host/remote_assistance_host-InfoPlist.strings/$locale.lproj/InfoPlist.strings",
          ]
          outputs = [
            "{{bundle_resources_dir}}/$locale.lproj/{{source_file_part}}",
          ]
          deps = [
            "//remoting/host:remoting_infoplist_strings",
          ]
        }
      }

      mac_xib_bundle_data("remote_assistance_host_xibs") {
        sources = [
          "//remoting/host/mac/disconnect_window.xib",
        ]
      }

      if (icu_use_data_file) {
        bundle_data("remote_assistance_host_resources") {
          sources = [
            "$root_out_dir/icudtl.dat",
          ]

          outputs = [
            "{{bundle_resources_dir}}/{{source_file_part}}",
          ]

          public_deps = [
            "//third_party/icu:icudata",
          ]
        }
      }
    } else {
      app_target_type = "executable"
    }

    target(app_target_type, "remote_assistance_host") {
      if (is_mac) {
        info_plist = "remote_assistance_host-Info.plist"
        extra_substitutions = [
          "BUNDLE_ID=$remote_assistance_host_bundle_id",
          "VERSION_FULL=$remoting_version_full",
          "VERSION_SHORT=$remoting_version_short",
          "MACOSX_DEPLOYMENT_TARGET=10.7",
        ]
        extra_configs = [
          "//build/config/compiler:wexit_time_destructors",
          "//remoting/build/config:version",
        ]
      } else {
        configs += [
          "//build/config/compiler:wexit_time_destructors",
          "//remoting/build/config:version",
        ]
      }

      sources = [
        "it2me_native_messaging_host_entry_point.cc",
        "it2me_native_messaging_host_main.cc",
        "it2me_native_messaging_host_main.h",
      ]

      deps = [
        ":common",
        "//build/config/sanitizers:deps",
        "//remoting/host",
        "//remoting/host/native_messaging",
        "//remoting/proto",
        "//ui/gfx",
      ]
      if (is_mac) {
        foreach(locale, remoting_locales_with_underscores) {
          deps += [
            ":remote_assistance_host_strings_${locale}_bundle_data",
            "//remoting/host/mac:remoting_host_locale_${locale}_bundle_data",
          ]
        }
        deps += [
          ":remote_assistance_host_dialog_icon",
          ":remote_assistance_host_xibs",
          "//remoting/host:remoting_infoplist_strings",
          "//remoting/resources:copy_locales",
        ]
        if (icu_use_data_file) {
          deps += [ ":remote_assistance_host_resources" ]
        }
      }

      if (enable_webrtc) {
        deps += [ "//third_party/libjingle:libjingle_webrtc" ]
      }

      if (is_desktop_linux) {
        if (use_gtk3) {
          deps += [ "//build/config/linux/gtk3" ]
        } else {
          deps += [ "//build/config/linux/gtk2" ]
        }
      }
    }
  }
}
