# Copyright 2018 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# BUILD settings for Vulkan Tools where Mock ICD is located
# Tools repo is at
# https://github.com/KhronosGroup/Vulkan-Tools

import("../../gni/angle.gni")

import("$angle_root/third_party/vulkan-headers/vulkan_headers_script_deps.gni")

# Vulkan-tools isn't ported to Fuchsia yet.
# TODO(spang): Port mock ICD to Fuchsia.
assert(!is_fuchsia)

vulkan_undefine_configs = []
if (is_win) {
  vulkan_undefine_configs += [
    "//build/config/win:nominmax",
    "//build/config/win:unicode",
  ]
}

raw_vulkan_icd_dir = rebase_path("src", root_build_dir)
raw_vulkan_headers_dir =
    rebase_path("$angle_root/third_party/vulkan-headers/src", root_build_dir)

vulkan_gen_dir = "$target_gen_dir/angle/vulkan"
raw_vulkan_gen_dir = rebase_path(vulkan_gen_dir, root_build_dir)

# Vulkan helper scripts
# ---------------------

helper_script_and_deps = [
  [
    "vulkan_gen_typemap_helper",
    "vk_typemap_helper.h",
    "vulkan_tools_helper_file_generator.py",
  ],
  [
    "vulkan_mock_icd_cpp",
    "mock_icd.cpp",
    "mock_icd_generator.py",
  ],
  [
    "vulkan_mock_icd_h",
    "mock_icd.h",
    "mock_icd_generator.py",
  ],
]

# Python scripts needed for codegen, copy them to a temp dir
# so that all dependencies are together
copy("python_gen_deps") {
  sources = vulkan_registry_script_files + [
              "src/scripts/common_codegen.py",
              "src/scripts/kvt_genvk.py",
              "src/scripts/mock_icd_generator.py",
              "src/scripts/vulkan_tools_helper_file_generator.py",
            ]
  outputs = [
    "$vulkan_gen_dir/{{source_file_part}}",
  ]
}

foreach(script_and_dep, helper_script_and_deps) {
  target_name = script_and_dep[0]
  file = script_and_dep[1]
  dep = script_and_dep[2]
  target("action", target_name) {
    public_deps = [
      ":python_gen_deps",
    ]
    script = "$vulkan_gen_dir/kvt_genvk.py"
    inputs = [
      "$vulkan_gen_dir/$dep",
      "$vulkan_gen_dir/common_codegen.py",
    ]
    outputs = [
      "$vulkan_gen_dir/$file",
    ]
    args = [
      "-o",
      raw_vulkan_gen_dir,
      "-registry",
      "$raw_vulkan_headers_dir/registry/vk.xml",
      "-scripts",
      "$raw_vulkan_headers_dir/registry",
      "$file",
      "-quiet",
    ]
  }
}

config("vulkan_generated_files_config") {
  include_dirs = [ vulkan_gen_dir ]
}

group("vulkan_generate_helper_files") {
  public_deps = [
    "$angle_root/third_party/vulkan-headers:vulkan_headers",
  ]
  public_configs = [ ":vulkan_generated_files_config" ]
  foreach(script_and_dep, helper_script_and_deps) {
    target_name = script_and_dep[0]
    public_deps += [ ":$target_name" ]
  }
}

config("vulkan_internal_config") {
  defines = [ "VULKAN_NON_CMAKE_BUILD" ]
  if (is_clang || !is_win) {
    cflags = [ "-Wno-unused-function" ]
  }
  if (is_linux) {
    defines += [
      "SYSCONFDIR=\"/etc\"",
      "FALLBACK_CONFIG_DIRS=\"/etc/xdg\"",
      "FALLBACK_DATA_DIRS=\"/usr/local/share:/usr/share\"",
    ]
  }
}

# Copy icd header to gen dir
copy("icd_header_dep") {
  sources = [
    "$angle_root/third_party/vulkan-headers/src/include/vulkan/vk_icd.h",
  ]
  outputs = [
    "$vulkan_gen_dir/vk_icd.h",
  ]
}

if (!is_android) {
  # Vulkan Mock ICD
  # ---------------
  group("vulkan_generate_mock_icd_files") {
    public_deps = [
      ":icd_header_dep",
      ":vulkan_generate_helper_files",
      ":vulkan_mock_icd_cpp",
      ":vulkan_mock_icd_h",
    ]
  }

  mock_icd_sources = [
    "$vulkan_gen_dir/mock_icd.cpp",
    "$vulkan_gen_dir/mock_icd.h",
  ]

  shared_library("VkICD_mock_icd") {
    configs -= vulkan_undefine_configs
    public_configs = [ "$angle_root:vulkan_config" ]
    deps = [
      ":vulkan_generate_mock_icd_files",
    ]
    data_deps = [
      ":vulkan_gen_icd_json_file",
    ]
    sources = mock_icd_sources
    if (is_win) {
      sources += [ "src/icd/VkICD_mock_icd.def" ]
    }
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [ "//build/config/compiler:no_chromium_code" ]
  }

  action("vulkan_gen_icd_json_file") {
    script = "$angle_root/scripts/generate_vulkan_layers_json.py"
    sources = [
      "../vulkan-headers/src/include/vulkan/vulkan_core.h",
    ]
    args = [ "--icd" ]
    if (is_win) {
      sources += [ "src/icd/windows/VkICD_mock_icd.json" ]
      args += [ "$raw_vulkan_icd_dir/icd/windows" ]
    }
    if (is_linux) {
      sources += [ "src/icd/linux/VkICD_mock_icd.json" ]
      args += [ "$raw_vulkan_icd_dir/icd/linux" ]
    }

    # The layer JSON files are part of the necessary data deps.
    outputs = [
      "$root_out_dir/$data_dir/VkICD_mock_icd.json",
    ]
    data = outputs
    args += [ rebase_path("$root_out_dir/$data_dir", root_build_dir) ] +
            rebase_path(sources, root_build_dir)
  }
}
