# 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("//build/toolchain/toolchain.gni")
import("//ppapi/native_client/nacl_test_data.gni")

group("nacl") {
  newlib = "//build/toolchain/nacl:clang_newlib_${target_cpu}"
  pnacl = "//build/toolchain/nacl:newlib_pnacl"
  data_deps = [
    ":exit_status_test($newlib)",
    ":exit_status_test($pnacl)",
    ":extension_validation_cache($newlib)",
    ":irt_exception_test($newlib)",
    ":irt_exception_test($pnacl)",
    ":irt_manifest_file($newlib)",
    ":pnacl_debug_url_test($pnacl)",
    ":pnacl_dyncode_syscall_disabled_test($pnacl)",
    ":pnacl_error_handling_test($pnacl)",
    ":pnacl_hw_eh_disabled_test($pnacl)",
    ":pnacl_mime_type_test($newlib)",
    ":pnacl_mime_type_test($pnacl)",
    ":pnacl_options_test($pnacl)",
    ":pnacl_url_loader_test($pnacl)",
    ":ppapi_bad_get_ppp_instance_crash($newlib)",
    ":ppapi_bad_no_ppp_instance($newlib)",
    ":ppapi_bad_ppp_initialize($newlib)",
    ":ppapi_bad_ppp_initialize_crash($newlib)",
    ":ppapi_bad_ppp_instance_didcreate($newlib)",
    ":ppapi_bad_ppp_instance_didcreate_crash($newlib)",
    ":ppapi_crash_in_callback($newlib)",
    ":ppapi_crash_in_callback($pnacl)",
    ":ppapi_crash_off_main_thread($newlib)",
    ":ppapi_crash_off_main_thread($pnacl)",
    ":ppapi_crash_ppapi_off_main_thread($newlib)",
    ":ppapi_crash_ppapi_off_main_thread($pnacl)",
    ":ppapi_crash_via_check_failure($newlib)",
    ":ppapi_crash_via_check_failure($pnacl)",
    ":ppapi_crash_via_exit_call($newlib)",
    ":ppapi_crash_via_exit_call($pnacl)",
    ":ppapi_extension_mime_handler($newlib)",
    ":ppapi_ppb_core($newlib)",
    ":ppapi_ppb_core($pnacl)",
    ":ppapi_ppb_instance($newlib)",
    ":ppapi_ppb_instance($pnacl)",
    ":ppapi_ppp_instance($newlib)",
    ":ppapi_ppp_instance($pnacl)",
    ":ppapi_progress_events($newlib)",
    ":ppapi_progress_events($pnacl)",
    ":shared_test_files($newlib)",
    ":shared_test_files($pnacl)",
    ":simple_test($newlib)",
    ":simple_test($pnacl)",
    ":sysconf_nprocessors_onln_test($newlib)",
    ":sysconf_nprocessors_onln_test($pnacl)",
  ]
  if (target_cpu != "arm") {
    data_deps += [ ":partly_invalid($newlib)" ]
  }
  if (target_cpu != "mipsel") {
    glibc = "//build/toolchain/nacl:glibc_${target_cpu}"
    data_deps += [
      ":exit_status_test($glibc)",
      ":extension_validation_cache($glibc)",
      ":irt_exception_test($glibc)",
      ":pnacl_mime_type_test($glibc)",
      ":ppapi_crash_in_callback($glibc)",
      ":ppapi_crash_off_main_thread($glibc)",
      ":ppapi_crash_ppapi_off_main_thread($glibc)",
      ":ppapi_crash_via_check_failure($glibc)",
      ":ppapi_crash_via_exit_call($glibc)",
      ":ppapi_ppb_core($glibc)",
      ":ppapi_ppb_instance($glibc)",
      ":ppapi_ppp_instance($glibc)",
      ":ppapi_progress_events($glibc)",
      ":shared_test_files($glibc)",
      ":simple_test($glibc)",
      ":sysconf_nprocessors_onln_test($glibc)",
    ]
  }
  if (is_linux && enable_nacl_nonsfi) {
    nonsfi = "//build/toolchain/nacl:newlib_pnacl_nonsfi"
    data_deps += [
      ":irt_exception_test($nonsfi)",
      ":irt_manifest_file($nonsfi)",
      ":shared_test_files($nonsfi)",
    ]
    if (is_clang) {
      data_deps += [ ":nonsfi_libc_free" ]
    }
  }
}

if (is_nacl) {
  copy("shared_test_files") {
    sources = [
      # TODO(ncbray) move into chrome/test/data/nacl when all tests are
      # converted.
      "//ppapi/native_client/tools/browser_tester/browserdata/nacltest.js",

      # Files that aren't associated with any particular executable.
      "bad/ppapi_bad.html",
      "bad/ppapi_bad.js",
      "bad/ppapi_bad_doesnotexist.nmf",
      "bad/ppapi_bad_magic.nmf",
      "bad/ppapi_bad_manifest_bad_files.nmf",
      "bad/ppapi_bad_manifest_nexe_arch.nmf",
      "bad/ppapi_bad_manifest_uses_nexes.nmf",
      "bad/ppapi_bad_native.html",
      "crash/ppapi_crash.js",
      "crash/ppapi_crash_in_callback.html",
      "crash/ppapi_crash_off_main_thread.html",
      "crash/ppapi_crash_ppapi_off_main_thread.html",
      "crash/ppapi_crash_via_check_failure.html",
      "crash/ppapi_crash_via_exit_call.html",
      "load_util.js",
      "manifest_file/test_file.txt",
      "progress_event_listener.js",
      "simple_cc.js",
    ]
    outputs = [
      "${root_build_dir}/nacl_test_data/${nacl_toolchain_variant}/{{source_file_part}}",
    ]
  }

  source_set("ppapi_test_lib") {
    sources = [
      "ppapi_test_lib/get_browser_interface.cc",
      "ppapi_test_lib/get_browser_interface.h",
      "ppapi_test_lib/internal_utils.cc",
      "ppapi_test_lib/internal_utils.h",
      "ppapi_test_lib/module_instance.cc",
      "ppapi_test_lib/test_interface.cc",
      "ppapi_test_lib/test_interface.h",
      "ppapi_test_lib/testable_callback.cc",
      "ppapi_test_lib/testable_callback.h",
    ]
    deps = [
      "//native_client/src/shared/gio",
      "//native_client/src/shared/platform",
      "//ppapi/native_client:ppapi_lib",
    ]
  }

  nacl_test_data("irt_manifest_file") {
    sources = [
      "manifest_file/irt_manifest_file_test.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    nmfflags = [
      "-xtest_file:test_file.txt",
      "-xnmf says hello world:test_file.txt",

      # There is no dummy_test_file.txt file intentionally. This is just for
      # a test case where there is a manifest entry, but no actual file.
      "-xdummy_test_file:dummy_test_file.txt",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "manifest_file/irt_manifest_file_test.html" ]
  }

  nacl_test_data("irt_exception_test") {
    sources = [
      "irt_exception/irt_exception_test.cc",
    ]
    deps = [
      ":ppapi_test_lib",
    ]
    libs = [ "nacl_exception" ]
    destination_dir = "nacl_test_data"
    test_files = [ "irt_exception/irt_exception_test.html" ]
  }
}

if (is_nacl && !is_nacl_nonsfi) {
  nacl_test_data("simple_test") {
    output_name = "simple"
    sources = [
      "simple.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "nacl_load_test.html" ]
  }

  nacl_test_data("exit_status_test") {
    output_name = "pm_exit_status_test"
    sources = [
      "exit_status/pm_exit_status_test.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "exit_status/pm_exit_status_test.html" ]
  }

  nacl_test_data("extension_validation_cache") {
    sources = [
      "simple.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]

    # Need a new directory to not clash with with other extension
    # tests's files (e.g., manifest.json).
    destination_dir = "nacl_test_data/extension_vcache_test"
    test_files = [
      # TODO(ncbray) move into chrome/test/data/nacl when all tests are
      # converted.
      "//ppapi/native_client/tools/browser_tester/browserdata/nacltest.js",
      "extension_validation_cache/extension_validation_cache.html",
      "extension_validation_cache/extension_validation_cache.js",

      # Turns the test data directory into an extension.
      # Use a different nexe_destination_dir to isolate the files.
      # Note that the .nexe names are embedded in this file.
      "extension_validation_cache/manifest.json",
      "load_util.js",
      "simple_cc.js",
    ]
  }

  nacl_test_data("sysconf_nprocessors_onln_test") {
    sources = [
      "sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    destination_dir = "nacl_test_data"
    test_files =
        [ "sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.html" ]
  }

  nacl_test_data("ppapi_progress_events") {
    sources = [
      "progress_events/ppapi_progress_events.cc",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "progress_events/ppapi_progress_events.html" ]
    deps = [
      ":ppapi_test_lib",
    ]
  }

  nacl_test_data("ppapi_bad_ppp_initialize") {
    sources = [
      "bad/ppapi_bad_ppp_initialize.cc",
    ]
    destination_dir = "nacl_test_data"
  }

  nacl_test_data("ppapi_bad_ppp_initialize_crash") {
    sources = [
      "bad/ppapi_bad_ppp_initialize_crash.cc",
    ]
    destination_dir = "nacl_test_data"
  }

  nacl_test_data("ppapi_bad_no_ppp_instance") {
    sources = [
      "bad/ppapi_bad_no_ppp_instance.cc",
    ]
    deps = [
      "//native_client/src/shared/platform",
    ]
    destination_dir = "nacl_test_data"
  }

  nacl_test_data("ppapi_bad_get_ppp_instance_crash") {
    sources = [
      "bad/ppapi_bad_get_ppp_instance_crash.cc",
    ]
    destination_dir = "nacl_test_data"
  }

  nacl_test_data("ppapi_bad_ppp_instance_didcreate") {
    sources = [
      "bad/ppapi_bad_ppp_instance_didcreate.cc",
    ]
    destination_dir = "nacl_test_data"
  }

  nacl_test_data("ppapi_bad_ppp_instance_didcreate_crash") {
    sources = [
      "bad/ppapi_bad_ppp_instance_didcreate_crash.cc",
    ]
    destination_dir = "nacl_test_data"
  }

  nacl_test_data("ppapi_crash_via_check_failure") {
    sources = [
      "crash/ppapi_crash_via_check_failure.cc",
    ]
    destination_dir = "nacl_test_data"
    deps = [
      ":ppapi_test_lib",
    ]
  }

  nacl_test_data("ppapi_crash_via_exit_call") {
    sources = [
      "crash/ppapi_crash_via_exit_call.cc",
    ]
    destination_dir = "nacl_test_data"
    deps = [
      ":ppapi_test_lib",
    ]
  }

  nacl_test_data("ppapi_crash_in_callback") {
    sources = [
      "crash/ppapi_crash_in_callback.cc",
    ]
    destination_dir = "nacl_test_data"
    deps = [
      ":ppapi_test_lib",
    ]
  }

  nacl_test_data("ppapi_crash_off_main_thread") {
    sources = [
      "crash/ppapi_crash_off_main_thread.cc",
    ]
    destination_dir = "nacl_test_data"
    deps = [
      ":ppapi_test_lib",
    ]
  }

  nacl_test_data("ppapi_crash_ppapi_off_main_thread") {
    sources = [
      "crash/ppapi_crash_ppapi_off_main_thread.cc",
    ]
    destination_dir = "nacl_test_data"
    deps = [
      ":ppapi_test_lib",
    ]
  }

  nacl_test_data("ppapi_extension_mime_handler") {
    sources = [
      "extension_mime_handler/ppapi_extension_mime_handler.cc",
    ]
    deps = [
      ":ppapi_test_lib",
    ]
    destination_dir = "nacl_test_data"
    test_files = [
      "extension_mime_handler/ppapi_extension_mime_handler.html",
      "extension_mime_handler/mime_test_data.dat",

      # For faking the file's MIME type.
      "extension_mime_handler/mime_test_data.dat.mock-http-headers",

      # Turns the test data directory into an extension.  Hackish.
      # Note that the .nexe names are embedded in this file.
      "extension_mime_handler/manifest.json",
    ]
  }

  nacl_test_data("pnacl_debug_url_test") {
    output_name = "pnacl_debug_url"
    sources = [
      "simple.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    generate_nmf = false
    if (current_cpu == "pnacl") {
      debug_pexe = true
    }
    destination_dir = "nacl_test_data"
    test_files = [
      "pnacl_debug_url/pnacl_debug_url.html",
      "pnacl_debug_url/pnacl_has_debug.nmf",
      "pnacl_debug_url/pnacl_has_debug_flag_off.nmf",
      "pnacl_debug_url/pnacl_no_debug.nmf",
    ]
  }

  nacl_test_data("pnacl_error_handling_test") {
    output_name = "pnacl_errors"
    sources = [
      "simple.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    generate_nmf = false
    if (current_cpu == "pnacl") {
      nonstable_pexe = true
    }
    destination_dir = "nacl_test_data"
    test_files = [
      "pnacl_error_handling/pnacl_error_handling.html",
      "pnacl_error_handling/bad.pexe",
      "pnacl_error_handling/pnacl_bad_pexe.nmf",
      "pnacl_error_handling/pnacl_bad_pexe_O0.nmf",
      "pnacl_error_handling/pnacl_bad_doesnotexist.nmf",
      "pnacl_error_handling/pnacl_illformed_manifest.nmf",
      "pnacl_error_handling/pnacl_nonfinal_pexe_O0.nmf",
    ]
  }

  copy("pnacl_mime_type_test") {
    sources = [
      "pnacl_mime_type/pnacl_mime_type.html",
    ]
    outputs = [
      "${root_build_dir}/nacl_test_data/${nacl_toolchain_variant}/{{source_file_part}}",
    ]
  }

  nacl_test_data("pnacl_options_test") {
    output_name = "pnacl_options"
    sources = [
      "simple.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    generate_nmf = false
    destination_dir = "nacl_test_data"
    test_files = [
      "pnacl_nmf_options/pnacl_options.html",
      "pnacl_nmf_options/pnacl_o_0.nmf",
      "pnacl_nmf_options/pnacl_o_2.nmf",
      "pnacl_nmf_options/pnacl_o_large.nmf",
    ]
  }

  nacl_test_data("pnacl_url_loader_test") {
    output_name = "pnacl_url_loader"
    sources = [
      "pnacl_url_loader/pnacl_url_loader.cc",
    ]
    deps = [
      "//ppapi/cpp",
    ]
    generate_nmf = true
    destination_dir = "nacl_test_data"
    test_files = [ "pnacl_url_loader/pnacl_url_loader.html" ]
  }

  nacl_test_data("pnacl_dyncode_syscall_disabled_test") {
    output_name = "pnacl_dyncode_syscall_disabled"
    sources = [
      "pnacl_dyncode_syscall_disabled/pnacl_dyncode_syscall_disabled.cc",
    ]
    deps = [
      ":ppapi_test_lib",
      "//native_client/src/untrusted/nacl:nacl_dyncode_private",
    ]
    destination_dir = "nacl_test_data"
    test_files =
        [ "pnacl_dyncode_syscall_disabled/pnacl_dyncode_syscall_disabled.html" ]
  }

  nacl_test_data("pnacl_hw_eh_disabled_test") {
    output_name = "pnacl_hw_eh_disabled"
    sources = [
      "pnacl_hw_eh_disabled/pnacl_hw_eh_disabled.cc",
    ]
    deps = [
      ":ppapi_test_lib",
      "//native_client/src/untrusted/nacl:nacl_exception_private",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "pnacl_hw_eh_disabled/pnacl_hw_eh_disabled.html" ]
  }

  # Legacy NaCl PPAPI interface tests being here.
  nacl_test_data("ppapi_ppb_core") {
    sources = [
      "ppapi/ppb_core/ppapi_ppb_core.cc",
    ]
    deps = [
      ":ppapi_test_lib",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "ppapi/ppb_core/ppapi_ppb_core.html" ]
  }

  nacl_test_data("ppapi_ppb_instance") {
    sources = [
      "ppapi/ppb_instance/ppapi_ppb_instance.cc",
    ]
    deps = [
      ":ppapi_test_lib",
    ]
    destination_dir = "nacl_test_data"
    test_files = [ "ppapi/ppb_instance/ppapi_ppb_instance.html" ]
  }

  nacl_test_data("ppapi_ppp_instance") {
    sources = [
      "ppapi/ppp_instance/ppapi_ppp_instance.cc",
    ]
    deps = [
      ":ppapi_test_lib",
    ]
    destination_dir = "nacl_test_data"
    test_files = [
      "ppapi/ppp_instance/ppapi_ppp_instance.html",
      "ppapi/ppp_instance/ppapi_ppp_instance.js",
    ]
  }

  if (target_cpu != "arm") {
    # Source file does not have asm for ARM.
    nacl_test_data("partly_invalid") {
      sources = [
        "//native_client/tests/stubout_mode/partly_invalid.c",
      ]
      destination_dir = "nacl_test_data"
    }
  }
}

if (is_linux && is_clang) {
  if (target_cpu == "x86") {
    arch = "x86_32"
  } else if (target_cpu == "x64") {
    arch = "x86_64"
  } else {
    arch = target_cpu
  }

  config("nonsfi_libc_free_nexe_config") {
    cflags = [
      "-Wno-sign-compare",

      # Stack-Smashing protector does not work with libc-free context.
      "-fno-stack-protector",

      # Optimizers may translate the original code to code which
      # requires builtin functions and/or relocations. Specifically,
      # the LLVM's optimizer translates for-loop based zero
      # clear to memset.
      "-O0",

      # ARM GCC emits symbols like __aeabi_unwind_cpp_pr0 in
      # .exidx sections without this flag.
      "-fno-unwind-tables",
    ]

    # Workaround against LLVM bug.
    # In short, an LTO optimization pass might recognize
    # naive implementations of builtins (such as memset)
    # and replace them with references to the real builtins,
    # which, in the case of -nostdlib, might cause the binary
    # to get undefined references to those symbols.
    #
    # See more details:
    # https://llvm.org/bugs/show_bug.cgi?id=30403
    if (use_thin_lto) {
      ldflags = [ "-Wl,-plugin-opt,O0" ]
    }
  }

  executable("nonsfi_libc_free_nexe") {
    output_name = "libc_free_$arch"
    output_extension = "nexe"
    sources = [
      "nonsfi/libc_free.c",
    ]

    # Here, we would like to link a relocatable, libc-free executable.
    # -shared/-fPIC make this binary relocatable. -nostdlib ensures
    # this is libc-free.
    # The program does not apply any dynamic relocations at start up,
    # so it cannot rely on relocations having been applied.
    # In addition, -fvisibility=hidden avoids creating some types
    # of relocation.
    cflags = [
      "-fPIC",
      "-fvisibility=hidden",
    ]
    ldflags = [
      "-nostdlib",
      "-shared",

      # This binary cannot relocate itself, so we should have no
      # undefined references left.
      "-Wl,--no-undefined",

      # Silence the warning about the unused '-pthread' argument.
      # TODO(gn): Come up with a way to remove the -pthread flag instead.
      "-Qunused-arguments",
    ]
    defines = [ "NACL_LINUX=1" ]
    include_dirs = [ "../../../.." ]

    # Do not use any sanitizers tools, which require a few symbols.
    configs -= [ "//build/config/sanitizers:default_sanitizer_flags" ]
    configs += [ ":nonsfi_libc_free_nexe_config" ]
  }

  copy("nonsfi_libc_free") {
    sources = [
      "${root_out_dir}/libc_free_${arch}.nexe",

      # TODO(ncbray) move into chrome/test/data/nacl when all tests are
      # converted.
      "//ppapi/native_client/tools/browser_tester/browserdata/nacltest.js",
      "nonsfi/irt_test.html",
      "nonsfi/libc_free.html",
      "nonsfi/libc_free.nmf",
    ]
    outputs = [
      "${root_build_dir}/nacl_test_data/libc-free/{{source_file_part}}",
    ]
    deps = [
      ":nonsfi_libc_free_nexe",
    ]
  }
}
