# Copyright 2016 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("//build/config/compiler/compiler.gni")

# This file builds nacl64.exe, which is a 64-bit x86 Windows executable
# used only in the 32-bit x86 Windows build.  The :broker code runs both
# in nacl64.exe and in the 32-bit chrome executable, to launch
# nacl64.exe and communicate with it.

assert(enable_nacl)
assert(is_win)
assert(target_cpu == "x86")

source_set("broker") {
  sources = [
    "nacl_broker_listener.cc",
    "nacl_broker_listener.h",
  ]

  deps = [
    "//base",
    "//components/nacl/common:debug_exception_handler",
    "//components/nacl/common:minimal",
    "//components/nacl/common:switches",
    "//content/public/common:static_switches",
    "//ipc",
    "//mojo/edk/system",
    "//sandbox",
  ]

  if (current_cpu == target_cpu) {
    deps += [ "//content/public/common" ]
  } else {
    deps += [ ":content_dummy" ]
  }
}

# This exists just to make 'gn check' happy with :broker.  It can't depend
# on //content/public/common or anything like that, because that would
# bring in lots more stuff that should not be in the nacl64.exe build.
source_set("content_dummy") {
  check_includes = false
  sources = [
    "//content/public/common/sandbox_init.h",
    "//content/public/common/sandboxed_process_launcher_delegate.h",
  ]
}

if (current_cpu == "x86") {
  # Tests, packaging rules, etc. will expect to find nacl64.exe in the root
  # of the output directory.  It gets built in a non-default toolchain and
  # so will be delivered in the toolchain output subdirectory.  So this
  # just copies it to the expected place.  Having this target also makes
  # it simpler for things to depend on nacl64, since they don't have to
  # use a toolchain qualifier.
  copy("nacl64") {
    # NOTE: This must match what //build/config/BUILDCONFIG.gn uses
    # as default toolchain for the corresponding x64 build.
    if (is_clang) {
      x64_toolchain = "//build/toolchain/win:clang_nacl_win64"
    } else {
      x64_toolchain = "//build/toolchain/win:nacl_win64"
    }
    nacl64_label = ":nacl64($x64_toolchain)"
    nacl64_out_dir = get_label_info(nacl64_label, "root_out_dir")
    sources = [
      "$nacl64_out_dir/nacl64.exe",
    ]
    if (symbol_level != 0) {
      sources += [ "$nacl64_out_dir/nacl64.exe.pdb" ]
    }
    outputs = [
      "$root_out_dir/{{source_file_part}}",
    ]
    deps = [
      nacl64_label,
    ]
  }
} else if (current_cpu == "x64") {
  # In the x64 toolchain context, build nacl64.exe for real.
  executable("nacl64") {
    configs += [ "//build/config/win:windowed" ]

    # //build/config/compiler:optimize{,_max} adds this for official builds
    # only, as it only reduces binary size and is not necessary for
    # correctness.  But for nacl64.exe, it makes more than a six-fold
    # difference in the binary size, so always use it in release builds.
    # Note that using this flag disables incremental linking.  In debug
    # builds, incremental rebuild time is usually of more concern than
    # binary size, so incremental linking is preferable to size reduction.
    if (!is_debug) {
      ldflags = [ "/OPT:REF" ]
    }

    sources = [
      "//chrome/nacl/nacl_exe_win_64.cc",
    ]

    deps = [
      ":broker",
      ":nacl64_content",
      ":nacl64_crash_reporter_client",
      "//base",
      "//breakpad:breakpad_handler",
      "//build/win:default_exe_manifest",
      "//chrome:nacl64_exe_version",
      "//chrome/install_static:install_static_util",
      "//components/crash/content/app:app_breakpad_mac_win_to_be_deleted",
      "//components/nacl/loader:nacl_helper_win_64",
      "//content/public/common:static_features",
      "//content/public/common:static_switches",
      "//ppapi/proxy:ipc",
      "//sandbox",
    ]
  }

  # This is a tiny subset of //content built specially for nacl64.exe.
  # There are no subcomponents of //content small enough to get just
  # what nacl64.exe needs without bringing in other stuff that causes
  # problems for the build.
  source_set("nacl64_content") {
    sources = [
      "//content/app/sandbox_helper_win.cc",
      "//content/common/sandbox_init_win.cc",
      "//content/common/sandbox_win.cc",
      "//content/public/common/sandboxed_process_launcher_delegate.cc",
    ]

    defines = [
      "COMPILE_CONTENT_STATICALLY",
      "NACL_WIN64",
    ]

    # This defangs 'gn check', which does not like this cherry-picking.
    # All the source files here are part of other proper components
    # under //content, where their #include discipline will be checked.
    check_includes = false

    deps = [
      "//base",
      "//content/public/common:static_switches",
      "//sandbox",
    ]
  }

  source_set("nacl64_crash_reporter_client") {
    sources = [
      "//chrome/app/chrome_crash_reporter_client_win.cc",
      "//chrome/common/crash_keys.cc",
    ]

    defines = [ "NACL_WIN64" ]

    check_includes = false

    deps = [
      "//chrome/common:constants",
      "//chrome/install_static:install_static_util",
      "//chrome/installer/util:with_no_strings",
      "//components/browser_watcher:browser_watcher_client",
      "//components/flags_ui:switches",
      "//components/policy:generated",
      "//content/public/common:static_switches",
      "//ipc",
      "//third_party/kasko",
    ]
  }
}
