# 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/config/features.gni")
import("//build/config/nacl/config.gni")
import("//testing/test.gni")

assert(enable_nacl)

# This is separate so it can be used by ../broker:nacl64.
source_set("minimal") {
  sources = [
    "nacl_ipc_adapter.cc",
    "nacl_ipc_adapter.h",
    "nacl_listener.cc",
    "nacl_listener.h",
    "nacl_main.cc",
    "nacl_main_platform_delegate.h",
    "nacl_main_platform_delegate_linux.cc",
    "nacl_main_platform_delegate_mac.mm",
    "nacl_main_platform_delegate_win.cc",
    "nacl_trusted_listener.cc",
    "nacl_trusted_listener.h",
    "nacl_validation_db.h",
    "nacl_validation_query.cc",
    "nacl_validation_query.h",
  ]

  deps = [
    ":minimal_content_dummy",
    "//base",
    "//components/nacl/common:minimal",
    "//crypto",
    "//ipc",
    "//mojo/edk/system",
    "//native_client/src/trusted/service_runtime:sel_main_chrome",
    "//ppapi/c",
    "//ppapi/proxy:ipc",
    "//sandbox",
  ]
}

# This exists just to make 'gn check' happy with :minimal and
# :nacl_helper_win_64 (below).  They can't depend on //content/public/common
# or anything like that, because that would bring in lots more than counts
# as "minimal" (stuff that should not be in the nacl64.exe build).
source_set("minimal_content_dummy") {
  check_includes = false
  sources = [
    "//content/public/common/child_process_sandbox_support_linux.h",
    "//content/public/common/content_descriptors.h",
    "//content/public/common/content_switches.h",
    "//content/public/common/main_function_params.h",
    "//content/public/common/mojo_channel_switches.h",
    "//content/public/common/sandbox_init.h",
  ]
}

source_set("loader") {
  public_deps = [
    ":minimal",
  ]
  deps = [
    "//components/nacl/common",
    "//content/public/common",
    "//ppapi/shared_impl",
  ]

  data_deps = [
    "//ppapi/native_client:irt",
    "//ppapi/native_client/src/untrusted/pnacl_support_extension",
  ]
}

test("nacl_loader_unittests") {
  sources = [
    "nacl_ipc_adapter_unittest.cc",
    "nacl_validation_query_unittest.cc",
    "run_all_unittests.cc",
  ]

  deps = [
    ":loader",
    "//base/test:test_support",
    "//ipc:test_support",
    "//ppapi/c",
    "//testing/gtest",
  ]

  if (is_nacl_nonsfi) {
    sources += [
      # TODO(hamaji): Currently, we build them twice. Stop building
      # them for components_unittests. See crbug.com/364751
      "nonsfi/nonsfi_sandbox_sigsys_unittest.cc",
      "nonsfi/nonsfi_sandbox_unittest.cc",
    ]

    deps += [
      ":nacl_linux",
      "//sandbox/linux:sandbox_linux_test_utils",
    ]
  }
}

if (is_linux) {
  executable("nacl_helper") {
    sources = [
      "nacl_helper_linux.cc",
      "nacl_helper_linux.h",
    ]

    deps = [
      ":loader",
      "//base",
      "//build/config/sanitizers:deps_no_options",
      "//components/nacl/common:switches",
      "//components/nacl/loader/sandbox_linux",
      "//content/public/common",
      "//crypto",
      "//ipc",
      "//mojo/edk/system",
      "//sandbox/linux:sandbox_services",
      "//url/ipc:url_ipc",
    ]

    cflags = [ "-fPIE" ]

    ldflags = [ "-pie" ]

    data_deps = [
      "//native_client/src/trusted/service_runtime/linux:bootstrap",
    ]
  }

  # For setting up nacl_helper.
  source_set("nacl_helper_integration") {
    public = [
      "nacl_helper_linux.h",
    ]
    data_deps = [
      ":nacl_helper",
    ]
  }

  if (enable_nacl_nonsfi) {
    test("nacl_helper_nonsfi_unittests") {
      sources = [
        "nonsfi/nacl_helper_nonsfi_unittests.cc",
      ]
      deps = [
        "//base",
        "//base/test:test_launcher_nacl_nonsfi",
      ]
      data_deps = [
        ":nacl_helper_nonsfi_unittests_main_copy(//build/toolchain/nacl:newlib_pnacl_nonsfi)",
      ]
    }

    group("helper_nonsfi") {
      data_deps = [
        ":nacl_helper_nonsfi_copy(//build/toolchain/nacl:newlib_pnacl_nonsfi)",
      ]
    }
  }
}

if (is_win && target_cpu == "x86" && current_cpu == "x64") {
  source_set("nacl_helper_win_64") {
    sources = [
      "nacl_helper_win_64.cc",
      "nacl_helper_win_64.h",
    ]

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

if (is_nacl_nonsfi) {
  executable("nacl_helper_nonsfi_nexe") {
    output_name = "nacl_helper_nonsfi"
    set_sources_assignment_filter([])
    sources = [
      # TODO(brettw) can this just depend on //components/nacl/common?
      "../common/nacl_messages.cc",
      "../common/nacl_messages.h",
      "../common/nacl_types.cc",
      "../common/nacl_types.h",
      "../common/nacl_types_param_traits.cc",
      "../common/nacl_types_param_traits.h",
      "nacl_helper_linux.cc",
      "nacl_helper_linux.h",
      "nacl_trusted_listener.cc",
      "nacl_trusted_listener.h",
      "nonsfi/nonsfi_listener.cc",
      "nonsfi/nonsfi_listener.h",
      "nonsfi/nonsfi_main.cc",
      "nonsfi/nonsfi_main.h",
    ]
    deps = [
      ":nacl_helper_nonsfi_sandbox",
      "//base",
      "//components/nacl/common:switches",
      "//components/tracing",
      "//content",
      "//ipc",
      "//mojo/edk/system",
      "//native_client/src/nonsfi/irt:nacl_sys_private",
      "//native_client/src/nonsfi/loader:elf_loader",

      # Normally libnacl is included implicitly by libc and is part of the
      # toolchain. But //build/config/nacl:compiler uses -nodefaultlibs and so
      # omits the default libnacl. When the nonsfi toolchain is more complete
      # and that kludge is no longer required, this dependency should be
      # removed.
      "//native_client/src/untrusted/nacl",
      "//ppapi/proxy",
      "//sandbox/linux:sandbox",
    ]
  }

  source_set("nacl_helper_nonsfi_sandbox") {
    set_sources_assignment_filter([])
    sources = [
      "nonsfi/nonsfi_sandbox.cc",
      "nonsfi/nonsfi_sandbox.h",
      "sandbox_linux/nacl_sandbox_linux.cc",
      "sandbox_linux/nacl_sandbox_linux.h",
    ]
    deps = [
      "//base",
      "//components/nacl/common:switches",
      "//content",
      "//sandbox/linux:sandbox",
      "//sandbox/linux:sandbox_services_headers",
    ]
  }

  copy("nacl_helper_nonsfi_copy") {
    sources = [
      "${root_out_dir}/nacl_helper_nonsfi",
    ]
    outputs = [
      "${root_build_dir}/{{source_file_part}}",
    ]
    deps = [
      ":nacl_helper_nonsfi_nexe",
    ]
  }

  test("nacl_helper_nonsfi_unittests_main") {
    sources = [
      "nonsfi/nonsfi_sandbox_sigsys_unittest.cc",
      "nonsfi/nonsfi_sandbox_unittest.cc",
      "nonsfi/run_all_unittests.cc",
    ]
    deps = [
      ":nacl_helper_nonsfi_sandbox",
      "//base",
      "//base/test:test_support",
      "//content",
      "//native_client/src/nonsfi/irt:nacl_sys_private",
      "//native_client/src/untrusted/nacl:nacl",
      "//sandbox/linux:sandbox",
      "//sandbox/linux:sandbox_linux_test_utils",
      "//sandbox/linux:sandbox_services_headers",
      "//testing/gtest",
    ]
  }

  copy("nacl_helper_nonsfi_unittests_main_copy") {
    testonly = true
    sources = [
      "${root_out_dir}/nacl_helper_nonsfi_unittests_main",
    ]
    outputs = [
      "${root_build_dir}/{{source_file_part}}",
    ]
    deps = [
      ":nacl_helper_nonsfi_unittests_main",
    ]
  }
}
