# Copyright 2017 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.

# TODO(michaelpg): Dedupe with Chrome installer.

import("//build/config/c++/c++.gni")
import("//build/config/chrome_build.gni")
import("//build/config/features.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/config/sysroot.gni")
import("//build/util/version.gni")
import("//chrome/process_version_rc_template.gni")  # For branding_file_path.
import("//components/nacl/features.gni")
import("//extensions/features/features.gni")

assert(is_desktop_linux)
assert(current_cpu == "x64")
assert(enable_extensions,
       "Cannot depend on extensions because enable_extensions=false.")

group("linux") {
  # See the "app_shell_lib" definition for why testonly is needed.
  testonly = true
  deps = [
    # TODO(michaelpg): Add beta/stable once we verify the unstable installer is
    # building and installing properly.
    ":unstable",
  ]
}

branding_dir = "//chrome/app/theme/$branding_path_component"

action("strip_app_shell_binary") {
  testonly = true
  prog_name = "$root_out_dir/app_shell"
  debug_file = prog_name + ".debug"
  stripped_file = prog_name + ".stripped"
  deps = [
    "//extensions/shell:app_shell",
  ]
  script = "//build/gn_run_binary.py"
  sources = [
    prog_name,
  ]
  outputs = [
    debug_file,
    stripped_file,
  ]
  args = [
    rebase_path("//third_party/eu-strip/bin/eu-strip", root_build_dir),
    "-o",
    rebase_path(stripped_file, root_build_dir),
    "-f",
    rebase_path(debug_file, root_build_dir),
    rebase_path(prog_name, root_build_dir),
  ]
}

copy("common_packaging_files") {
  visibility = [ ":*" ]
  sources = [
    "//chrome/installer/linux/common/apt.include",
    "//chrome/installer/linux/common/repo.cron",
    "//chrome/installer/linux/common/symlinks.include",
    "//chrome/installer/linux/common/variables.include",
    "/usr/bin/eu-strip",
    "common/installer.include",
    "common/wrapper",
  ]

  if (is_chrome_branded) {
    sources += [ "common/google-app-shell/google-app-shell.info" ]
  } else {
    sources += [ "common/chromium-app-shell/chromium-app-shell.info" ]
  }

  outputs = [
    "$root_out_dir/app_shell_installer/common/{{source_file_part}}",
  ]
}

copy("deb_packaging_files") {
  visibility = [ ":*" ]
  sources = [
    "//chrome/installer/linux/debian/changelog.template",
    "//chrome/installer/linux/debian/control.template",
    "debian/build.sh",
    "debian/postinst",
    "debian/postrm",
  ]
  outputs = [
    "$root_out_dir/app_shell_installer/debian/{{source_file_part}}",
  ]
}

copy("theme_files") {
  visibility = [ ":*" ]
  sources = [
    "$branding_dir/BRANDING",
  ]
  outputs = [
    "$root_out_dir/app_shell_installer/theme/{{source_file_part}}",
  ]
}

process_version("save_build_info") {
  # Just output the default version info variables (no template).
  process_only = true
  sources = [
    "//build/util/LASTCHANGE",
    "//chrome/VERSION",
    branding_file_path,
  ]
  output = "$root_out_dir/app_shell_installer/version.txt"
}

# Dependencies for all Linux installer targets.
group("installer_deps") {
  testonly = true

  # Though many of these things appear in data_deps further down the
  # dependency chain, they must appear here as public_deps so that they can
  # be listed as inputs to the actions that depend on ":installer_deps"
  # and are guaranteed to have been built before those actions run.

  public_deps = [
    ":common_packaging_files",
    ":deb_packaging_files",
    ":save_build_info",
    ":theme_files",
    "//extensions:shell_and_test_pak",
    "//extensions/shell:app_shell",
  ]
  if (enable_nacl) {
    public_deps += [
      "//components/nacl/loader:nacl_helper",

      # These are data_deps of nacl_helper, but that is not enough,
      # as explained above.
      "//native_client/src/trusted/service_runtime/linux:bootstrap",
      "//ppapi/native_client:irt",
    ]
  }

  if (!libcpp_is_static && use_custom_libcxx) {
    public_deps += [ "//buildtools/third_party/libc++:libc++" ]
  }
}

# Creates .deb installer package.
#
# channel:
#   Name of the channel.
template("linux_package") {
  testonly = true
  assert(defined(invoker.channel))
  channel = invoker.channel

  packaging_files_binaries = [
    # TODO(mmoss) Any convenient way to get all the relevant build
    # files? (e.g. all locales, resources, etc.)
    "$root_out_dir/app_shell",
    "$root_out_dir/extensions_shell_and_test.pak",
  ]

  if (enable_nacl) {
    packaging_files_binaries += [
      "$root_out_dir/nacl_helper",
      "$root_out_dir/nacl_helper_bootstrap",
      "$root_out_dir/nacl_irt_x86_64.nexe",
    ]
  }

  if (!libcpp_is_static && use_custom_libcxx) {
    packaging_files_binaries += [ "$root_out_dir/libc++.so" ]
  }

  deb_target_name = "${target_name}_deb"
  action(deb_target_name) {
    visibility = [ ":*" ]
    script = "//build/gn_run_binary.py"
    deb_arch = "amd64"

    inputs = packaging_files_binaries
    outputs = [
      "$root_out_dir/google-app-shell-${channel}_${chrome_version_full}-1_${deb_arch}.deb",
    ]

    args = [
      rebase_path("$root_out_dir/app_shell_installer/debian/build.sh",
                  root_build_dir),
      "-a",
      current_cpu,
      "-b",
      rebase_path(root_out_dir, root_build_dir),
      "-c",
      invoker.channel,
      "-d",
      branding_path_component,
      "-o",
      rebase_path(root_out_dir, root_build_dir),
      "-s",
      rebase_path(sysroot, root_build_dir),
    ]
    if (is_official_build) {
      args += [ "-f" ]
    }
    deps = [
      ":installer_deps",
      ":strip_app_shell_binary",
    ]
  }

  group(target_name) {
    deps = [
      ":$deb_target_name",
    ]
  }
}

# Standard packages.
linux_package("unstable") {
  channel = "unstable"
}