# Copyright 2019 The Chromium Authors
# 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/sanitizers/sanitizers.gni")
import("//build/config/ui.gni")
import("//build/config/win/console_app.gni")
import("//build/config/win/manifest.gni")
import("//tools/grit/grit_rule.gni")
import("//tools/grit/repack.gni")
import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
if (is_android) {
  import("//build/config/android/config.gni")
}

static_library("webengine_shell_lib") {
  testonly = true
  sources = [
    "app/shell_main_params.cc",
    "app/shell_main_params.h",
    "browser/shell.cc",
    "browser/shell.h",
    "common/shell_switches.cc",
    "common/shell_switches.h",
  ]

  if (is_android) {
    sources += [ "browser/shell_android.cc" ]
  }

  configs += [ "//build/config:precompiled_headers" ]

  public_deps = [ "//weblayer:weblayer_lib" ]
  deps = [
    ":resources",
    "//base",
    "//base:base_static",
    "//base/third_party/dynamic_annotations",
    "//net",
    "//net:net_resources",
    "//sandbox",
    "//skia",
    "//third_party/icu",
    "//ui/base",
    "//ui/base/clipboard",
    "//ui/base/ime/init",
    "//ui/display",
    "//ui/events:events_base",
    "//ui/gfx",
    "//ui/gfx/geometry",
    "//ui/gfx/ipc",
    "//ui/gfx/ipc/geometry",
    "//ui/gfx/ipc/skia",
    "//ui/gl",
    "//ui/platform_window",
    "//url",
    "//v8",
    "//weblayer:resources",
  ]

  if (is_linux || is_chromeos) {
    deps += [
      "//third_party/fontconfig",
      "//ui/gfx:test_support",
    ]
  }

  if (is_android) {
    deps += [ "//ui/android" ]
  }

  if (toolkit_views) {
    deps += [ "//ui/views" ]
  }

  if (use_aura) {
    deps += [
      "//ui/aura",
      "//ui/aura:test_support",
      "//ui/events",
      "//ui/strings",
      "//ui/wm",
    ]

    if (toolkit_views) {
      sources += [ "browser/shell_views.cc" ]
      deps += [
        "//ui/color:color_headers",
        "//ui/native_theme",
        "//ui/resources",
        "//ui/views:test_support",
        "//ui/views/controls/webview",
        "//ui/wm:test_support",
      ]
    }
  }

  if (is_linux || is_chromeos) {
    deps += [ "//build/config/freetype" ]
  }
}

grit("webengine_shell_resources_grit") {
  testonly = true

  # External code should depend on ":resources" instead.
  visibility = [ ":*" ]
  source = "shell_resources.grd"
  outputs = [
    "grit/shell_resources.h",
    "webengine_shell_resources.pak",
  ]
}

copy("copy_shell_resources") {
  testonly = true

  sources = [ "$target_gen_dir/webengine_shell_resources.pak" ]
  outputs = [ "$root_out_dir/webengine_shell_resources.pak" ]

  public_deps = [ ":webengine_shell_resources_grit" ]
}

group("resources") {
  testonly = true

  public_deps = [ ":copy_shell_resources" ]
}

repack("shell_pak") {
  testonly = true

  sources = [ "$root_gen_dir/weblayer/shell/webengine_shell_resources.pak" ]

  deps = [ ":resources" ]
  output = "$root_out_dir/webengine_shell.pak"
}

repack("support_pak") {
  testonly = true

  sources = [
    "$root_gen_dir/components/components_resources.pak",
    "$root_gen_dir/components/components_resources_100_percent.pak",
    "$root_gen_dir/components/strings/components_chromium_strings_en-US.pak",
    "$root_gen_dir/components/strings/components_locale_settings_en-US.pak",
    "$root_gen_dir/components/strings/components_strings_en-US.pak",
    "$root_gen_dir/content/content_resources.pak",
    "$root_gen_dir/content/dev_ui_content_resources.pak",
    "$root_gen_dir/mojo/public/js/mojo_bindings_resources.pak",
    "$root_gen_dir/net/net_resources.pak",
    "$root_gen_dir/third_party/blink/public/resources/blink_resources.pak",
    "$root_gen_dir/third_party/blink/public/resources/blink_scaled_resources_100_percent.pak",
    "$root_gen_dir/third_party/blink/public/strings/blink_strings_en-US.pak",
    "$root_gen_dir/ui/resources/ui_resources_100_percent.pak",
    "$root_gen_dir/ui/resources/webui_resources.pak",
    "$root_gen_dir/ui/strings/app_locale_settings_en-US.pak",
    "$root_gen_dir/ui/strings/ax_strings_en-US.pak",
    "$root_gen_dir/ui/strings/ui_strings_en-US.pak",
    "$root_gen_dir/weblayer/weblayer_resources.pak",
  ]

  deps = [
    "//components/resources",
    "//components/strings",
    "//content:content_resources",
    "//content:dev_ui_content_resources",
    "//mojo/public/js:resources",
    "//net:net_resources",
    "//third_party/blink/public:resources",
    "//third_party/blink/public:scaled_resources_100_percent",
    "//third_party/blink/public/strings",
    "//ui/resources",
    "//ui/strings",
    "//weblayer:resources",
  ]

  if (toolkit_views) {
    deps += [ "//ui/views/resources" ]
    sources +=
        [ "$root_gen_dir/ui/views/resources/views_resources_100_percent.pak" ]
  }
  if (is_android) {
    deps += [ "//ui/android:ui_java_resources" ]
  } else {
    deps += [ "//content/browser/tracing:resources" ]
    sources += [ "$root_gen_dir/content/browser/tracing/tracing_resources.pak" ]
  }
  output = "$root_out_dir/weblayer_support.pak"
}

repack("pak") {
  testonly = true

  sources = [
    "$root_out_dir/webengine_shell.pak",
    "$root_out_dir/weblayer_support.pak",
  ]

  deps = [
    ":shell_pak",
    ":support_pak",
  ]
  output = "$root_out_dir/weblayer.pak"
}

if (is_android) {
  group("weblayer_support") {
    testonly = true
    deps = [ "//weblayer/shell/android:weblayer_support_apk" ]
  }
} else {
  executable("webengine_shell") {
    testonly = true

    sources = [ "app/shell_main.cc" ]

    if (is_win) {
      sources += [ "app/shell.rc" ]
    }

    defines = []

    deps = [
      ":webengine_shell_lib",
      "//base",
      "//build/win:default_exe_manifest",
      "//net",
    ]

    data_deps = [
      ":pak",
      "//tools/v8_context_snapshot:v8_context_snapshot",
    ]

    if (is_win) {
      deps += [ "//sandbox" ]
      if (win_console_app) {
        defines += [ "WIN_CONSOLE_APP" ]
      } else {
        # Set /SUBSYSTEM:WINDOWS unless a console build has been requested.
        configs -= [ "//build/config/win:console" ]
        configs += [ "//build/config/win:windowed" ]
      }

      data_deps +=
          [ "//third_party/crashpad/crashpad/handler:crashpad_handler" ]
    }

    if ((is_linux || is_chromeos) && !is_component_build) {
      # Set rpath to find our own libfreetype even in a non-component build.
      configs += [ "//build/config/gcc:rpath_for_built_shared_libraries" ]
    }
  }
}