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

# TODO(jmadill): Redo the file entirely when we have standalone GN ANGLE.

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

if (build_with_chromium) {
  third_party_dir = "../../../../third_party"
} else {
  third_party_dir = "../../third_party"
}

glslang_dir = "$third_party_dir/glslang-angle/src"
spirv_headers_dir = "$third_party_dir/spirv-headers/src"
spirv_tools_dir = "$third_party_dir/spirv-tools-angle/src"
vulkan_layers_dir = "$third_party_dir/vulkan-validation-layers/src"
raw_vulkan_layers_dir = rebase_path(vulkan_layers_dir, root_build_dir)

# Subdirectory to place data files (e.g. layer JSON files).
data_dir = "angledata"

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

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

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

helper_script_and_deps = [
  [
    "vulkan_gen_dispatch_table_helper_h",
    "vk_dispatch_table_helper.h",
    "dispatch_table_helper_generator.py",
  ],
  [
    "vulkan_gen_enum_string_helper",
    "vk_enum_string_helper.h",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_extension_helper",
    "vk_extension_helper.h",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_layer_dispatch_table_h",
    "vk_layer_dispatch_table.h",
    "loader_extension_generator.py",
  ],
  [
    "vulkan_gen_loader_extensions_c",
    "vk_loader_extensions.c",
    "loader_extension_generator.py",
  ],
  [
    "vulkan_gen_loader_extensions_h",
    "vk_loader_extensions.h",
    "loader_extension_generator.py",
  ],
  [
    "vulkan_gen_object_types_h",
    "vk_object_types.h",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_safe_struct_cpp",
    "vk_safe_struct.cpp",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_safe_struct_h",
    "vk_safe_struct.h",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_struct_size_helper_c",
    "vk_struct_size_helper.c",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_struct_size_helper_h",
    "vk_struct_size_helper.h",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_thread_check_helper",
    "thread_check.h",
    "threading_generator.py",
  ],
  [
    "vulkan_gen_typemap_helper",
    "vk_typemap_helper.h",
    "helper_file_generator.py",
  ],
  [
    "vulkan_gen_object_tracker_cpp",
    "object_tracker.cpp",
    "object_tracker_generator.py",
  ],
  [
    "vulkan_gen_parameter_validation_cpp",
    "parameter_validation.cpp",
    "parameter_validation_generator.py",
  ],
  [
    "vulkan_gen_unique_objects_wrappers_helper",
    "unique_objects_wrappers.h",
    "unique_objects_generator.py",
  ],
]

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) {
    script = "$vulkan_layers_dir/scripts/lvl_genvk.py"
    inputs = [
      "$vulkan_layers_dir/scripts/$dep",
      "$vulkan_layers_dir/scripts/generator.py",
      "$vulkan_layers_dir/scripts/reg.py",
      "$vulkan_layers_dir/scripts/vk.xml",
    ]
    outputs = [
      "$vulkan_gen_dir/$file",
    ]
    args = [
      "-o",
      raw_vulkan_gen_dir,
      "-registry",
      "$raw_vulkan_layers_dir/scripts/vk.xml",
      "$file",
      "-quiet",
    ]
  }
}

# This could be generalized to a foreach if other revisions are added.
action("spirv_tools_external_revision_generate") {
  script = "$vulkan_layers_dir/scripts/external_revision_generator.py"
  inputs = [
    "$spirv_tools_dir/.git/HEAD",
    "$spirv_tools_dir/.git/index",
  ]
  outputs = [
    "$vulkan_gen_dir/spirv_tools_commit_id.h",
  ]
  args = [
    "$raw_spirv_tools_dir",
    "SPIRV_TOOLS_COMMIT_ID",
    "$raw_vulkan_gen_dir/spirv_tools_commit_id.h",
  ]
}

config("vulkan_generate_helper_files_config") {
  include_dirs = [
    vulkan_gen_dir,
    "$vulkan_layers_dir/include",
  ]
}

group("vulkan_generate_helper_files") {
  public_deps = [
    ":spirv_tools_external_revision_generate",
  ]
  public_configs = [ ":vulkan_generate_helper_files_config" ]
  foreach(script_and_dep, helper_script_and_deps) {
    target_name = script_and_dep[0]
    public_deps += [ ":$target_name" ]
  }
}

config("vulkan_config") {
  if (is_win) {
    defines = [
      "VK_USE_PLATFORM_WIN32_KHR",
      "VK_USE_PLATFORM_WIN32_KHX",
    ]
  }
  if (is_linux) {
    defines = [
      "VK_USE_PLATFORM_XCB_KHR",
      "VK_USE_PLATFORM_XCB_KHX",
    ]
  }
}

config("vulkan_internal_config") {
  defines = [ "VULKAN_NON_CMAKE_BUILD" ]
  if (is_clang || !is_win) {
    cflags = [ "-Wno-unused-function" ]

    # See https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/1649
    # TODO(jmadill): Lift this once github issue is fixed.
    cflags += [ "-Wno-ignored-attributes" ]
  }
  if (is_linux) {
    defines += [
      "SYSCONFDIR=\"/etc\"",
      "FALLBACK_CONFIG_DIRS=\"/etc/xdg\"",
      "FALLBACK_DATA_DIRS=\"/usr/local/share:/usr/share\"",
    ]
  }
}

# Vulkan loader
# -------------

config("vulkan_loader_config") {
  include_dirs = [
    vulkan_gen_dir,
    "$vulkan_layers_dir/include",
    "$vulkan_layers_dir/loader",
  ]
  defines = [
    "ANGLE_VK_LAYERS_DIR=\"$data_dir\"",
    "API_NAME=\"Vulkan\"",
  ]

  if (is_win) {
    cflags = [ "/wd4201" ]
  }
  if (is_linux) {
    # assume secure_getenv() is available
    defines += [ "HAVE_SECURE_GETENV" ]
  }
}

static_library("vulkan_loader") {
  sources = [
    "$vulkan_layers_dir/loader/cJSON.c",
    "$vulkan_layers_dir/loader/cJSON.h",
    "$vulkan_layers_dir/loader/debug_report.c",
    "$vulkan_layers_dir/loader/debug_report.h",
    "$vulkan_layers_dir/loader/dev_ext_trampoline.c",
    "$vulkan_layers_dir/loader/extension_manual.c",
    "$vulkan_layers_dir/loader/extension_manual.h",
    "$vulkan_layers_dir/loader/gpa_helper.h",
    "$vulkan_layers_dir/loader/loader.c",
    "$vulkan_layers_dir/loader/loader.h",
    "$vulkan_layers_dir/loader/murmurhash.c",
    "$vulkan_layers_dir/loader/murmurhash.h",
    "$vulkan_layers_dir/loader/phys_dev_ext.c",
    "$vulkan_layers_dir/loader/trampoline.c",

    # TODO(jmadill): Use assembler where available.
    "$vulkan_layers_dir/loader/unknown_ext_chain.c",
    "$vulkan_layers_dir/loader/vk_loader_platform.h",
    "$vulkan_layers_dir/loader/wsi.c",
    "$vulkan_layers_dir/loader/wsi.h",
  ]
  if (is_win) {
    sources += [
      "$vulkan_layers_dir/loader/dirent_on_windows.c",
      "$vulkan_layers_dir/loader/dirent_on_windows.h",
    ]
    if (!is_clang) {
      cflags = [
        "/wd4054",  # Type cast from function pointer
        "/wd4055",  # Type cast from data pointer
        "/wd4100",  # Unreferenced formal parameter
        "/wd4152",  # Nonstandard extension used (pointer conversion)
        "/wd4201",  # Nonstandard extension used: nameless struct/union
        "/wd4214",  # Nonstandard extension used: bit field types other than int
        "/wd4232",  # Nonstandard extension used: address of dllimport is not static
        "/wd4305",  # Type cast truncation
        "/wd4706",  # Assignment within conditional expression
        "/wd4996",  # Unsafe stdlib function
      ]
    }
  }
  deps = [
    ":vulkan_generate_helper_files",
  ]
  configs += [ ":vulkan_internal_config" ]
  public_configs = [
    ":vulkan_config",
    ":vulkan_loader_config",
  ]
  configs -= vulkan_undefine_configs
}

# SPIRV-tools
# -----------

spirv_source_dir = "$spirv_tools_dir/source"
spirv_include_dir = "$spirv_headers_dir/include/spirv"
raw_spirv_source_dir = rebase_path(spirv_source_dir, root_build_dir)
raw_spirv_include_dir = rebase_path(spirv_include_dir, root_build_dir)
raw_spirv_headers_dir = rebase_path(spirv_headers_dir, root_build_dir)

grammar_processing_script = "$spirv_tools_dir/utils/generate_grammar_tables.py"

action("spirv_tools_gen_enum_string_mapping") {
  script = grammar_processing_script
  sources = [
    "$spirv_include_dir/1.2/spirv.core.grammar.json",
  ]
  outputs = [
    "$vulkan_gen_dir/extension_enum.inc",
    "$vulkan_gen_dir/enum_string_mapping.inc",
  ]
  args = [
    "--spirv-core-grammar=$raw_spirv_include_dir/1.2/spirv.core.grammar.json",
    "--extension-enum-output=$raw_vulkan_gen_dir/extension_enum.inc",
    "--enum-string-mapping-output=$raw_vulkan_gen_dir/enum_string_mapping.inc",
  ]
}

spvtools_core_tables = [
  "1.0",
  "1.1",
  "1.2",
]

foreach(version, spvtools_core_tables) {
  action("spirv_tools_gen_core_tables_" + version) {
    script = grammar_processing_script
    sources = [
      "$spirv_include_dir/$version/spirv.core.grammar.json",
    ]
    outputs = [
      "$vulkan_gen_dir/core.insts-$version.inc",
      "$vulkan_gen_dir/operand.kinds-$version.inc",
    ]
    args = [
      "--spirv-core-grammar=$raw_spirv_include_dir/$version/spirv.core.grammar.json",
      "--core-insts-output=$raw_vulkan_gen_dir/core.insts-$version.inc",
      "--operand-kinds-output=$raw_vulkan_gen_dir/operand.kinds-$version.inc",
    ]
  }
}

action("spirv_tools_gen_glsl_tables") {
  script = grammar_processing_script
  sources = [
    "$spirv_include_dir/1.0/extinst.glsl.std.450.grammar.json",
    "$spirv_include_dir/1.0/spirv.core.grammar.json",
  ]
  outputs = [
    "$vulkan_gen_dir/glsl.std.450.insts-1.0.inc",
  ]
  args = [
    "--spirv-core-grammar=$raw_spirv_include_dir/1.0/spirv.core.grammar.json",
    "--extinst-glsl-grammar=$raw_spirv_include_dir/1.0/extinst.glsl.std.450.grammar.json",
    "--glsl-insts-output=$raw_vulkan_gen_dir/glsl.std.450.insts-1.0.inc",
  ]
}

action("spirv_tools_gen_opencl_tables") {
  script = grammar_processing_script
  sources = [
    "$spirv_include_dir/1.0/extinst.opencl.std.100.grammar.json",
    "$spirv_include_dir/1.0/spirv.core.grammar.json",
  ]
  outputs = [
    "$vulkan_gen_dir/opencl.std.insts-1.0.inc",
  ]
  args = [
    "--spirv-core-grammar=$raw_spirv_include_dir/1.0/spirv.core.grammar.json",
    "--extinst-opencl-grammar=$raw_spirv_include_dir/1.0/extinst.opencl.std.100.grammar.json",
    "--opencl-insts-output=$raw_vulkan_gen_dir/opencl.std.insts-1.0.inc",
  ]
}

action("spirv_tools_gen_generators_inc") {
  script = "$spirv_tools_dir/utils/generate_registry_tables.py"
  sources = [
    "$spirv_headers_dir/include/spirv/spir-v.xml",
  ]
  outputs = [
    "$vulkan_gen_dir/generators.inc",
  ]
  args = [
    "--xml=$raw_spirv_headers_dir/include/spirv/spir-v.xml",
    "--generator-output=$raw_vulkan_gen_dir/generators.inc",
  ]
}

spvtools_vendor_tables = [
  "spv-amd-shader-explicit-vertex-parameter",
  "spv-amd-shader-trinary-minmax",
  "spv-amd-gcn-shader",
  "spv-amd-shader-ballot",
]

foreach(target_name, spvtools_vendor_tables) {
  insts_file = "$target_name.insts.inc"
  grammar_file = "extinst.$target_name.grammar.json"

  action(target_name) {
    script = grammar_processing_script

    sources = [
      "$spirv_source_dir/$grammar_file",
    ]

    outputs = [
      "$vulkan_gen_dir/$insts_file",
    ]

    args = [
      "--extinst-vendor-grammar=$raw_spirv_source_dir/$grammar_file",
      "--vendor-insts-output=$raw_vulkan_gen_dir/$insts_file",
    ]
  }
}

config("spirv_tools_config") {
  include_dirs = [ "$spirv_tools_dir/include" ]
  if (is_win) {
    cflags = [
      "/wd4706",

      # These are triggered in higher optimization levels. Disable for now until
      # fixes are landed upstream. See https://crbug.com/677837.
      "/wd4701",
      "/wd4703",
    ]
  }
}

static_library("spirv_tools") {
  deps = [
    ":spirv_tools_gen_core_tables_1.0",
    ":spirv_tools_gen_core_tables_1.1",
    ":spirv_tools_gen_core_tables_1.2",
    ":spirv_tools_gen_enum_string_mapping",
    ":spirv_tools_gen_generators_inc",
    ":spirv_tools_gen_glsl_tables",
    ":spirv_tools_gen_opencl_tables",
  ]
  include_dirs = [
    vulkan_gen_dir,
    "$spirv_headers_dir/include",
    "$spirv_tools_dir/source",
  ]
  sources = [
    "$spirv_tools_dir/source/assembly_grammar.cpp",
    "$spirv_tools_dir/source/assembly_grammar.h",
    "$spirv_tools_dir/source/binary.cpp",
    "$spirv_tools_dir/source/binary.h",
    "$spirv_tools_dir/source/diagnostic.cpp",
    "$spirv_tools_dir/source/diagnostic.h",
    "$spirv_tools_dir/source/disassemble.cpp",
    "$spirv_tools_dir/source/enum_set.h",
    "$spirv_tools_dir/source/ext_inst.cpp",
    "$spirv_tools_dir/source/ext_inst.h",
    "$spirv_tools_dir/source/instruction.h",
    "$spirv_tools_dir/source/libspirv.cpp",
    "$spirv_tools_dir/source/macro.h",
    "$spirv_tools_dir/source/message.cpp",
    "$spirv_tools_dir/source/name_mapper.cpp",
    "$spirv_tools_dir/source/name_mapper.h",
    "$spirv_tools_dir/source/opcode.cpp",
    "$spirv_tools_dir/source/opcode.h",
    "$spirv_tools_dir/source/operand.cpp",
    "$spirv_tools_dir/source/operand.h",
    "$spirv_tools_dir/source/parsed_operand.cpp",
    "$spirv_tools_dir/source/parsed_operand.h",
    "$spirv_tools_dir/source/print.cpp",
    "$spirv_tools_dir/source/print.h",

    # TODO(jmadill): Determine if this is ever needed.
    #"$spirv_tools_dir/source/software_version.cpp",
    "$spirv_tools_dir/source/enum_string_mapping.cpp",
    "$spirv_tools_dir/source/extensions.cpp",
    "$spirv_tools_dir/source/extensions.h",
    "$spirv_tools_dir/source/spirv_constant.h",
    "$spirv_tools_dir/source/spirv_definition.h",
    "$spirv_tools_dir/source/spirv_endian.cpp",
    "$spirv_tools_dir/source/spirv_endian.h",
    "$spirv_tools_dir/source/spirv_target_env.cpp",
    "$spirv_tools_dir/source/spirv_target_env.h",
    "$spirv_tools_dir/source/spirv_validator_options.cpp",
    "$spirv_tools_dir/source/spirv_validator_options.h",
    "$spirv_tools_dir/source/table.cpp",
    "$spirv_tools_dir/source/table.h",
    "$spirv_tools_dir/source/text.cpp",
    "$spirv_tools_dir/source/text.h",
    "$spirv_tools_dir/source/text_handler.cpp",
    "$spirv_tools_dir/source/text_handler.h",
    "$spirv_tools_dir/source/util/bitutils.h",
    "$spirv_tools_dir/source/util/hex_float.h",
    "$spirv_tools_dir/source/util/parse_number.cpp",
    "$spirv_tools_dir/source/util/parse_number.h",
    "$spirv_tools_dir/source/util/string_utils.cpp",
    "$spirv_tools_dir/source/util/string_utils.h",
    "$spirv_tools_dir/source/val/basic_block.cpp",
    "$spirv_tools_dir/source/val/construct.cpp",
    "$spirv_tools_dir/source/val/function.cpp",
    "$spirv_tools_dir/source/val/instruction.cpp",
    "$spirv_tools_dir/source/val/validation_state.cpp",
    "$spirv_tools_dir/source/validate.cpp",
    "$spirv_tools_dir/source/validate.h",
    "$spirv_tools_dir/source/validate_arithmetics.cpp",
    "$spirv_tools_dir/source/validate_bitwise.cpp",
    "$spirv_tools_dir/source/validate_capability.cpp",
    "$spirv_tools_dir/source/validate_cfg.cpp",
    "$spirv_tools_dir/source/validate_conversion.cpp",
    "$spirv_tools_dir/source/validate_datarules.cpp",
    "$spirv_tools_dir/source/validate_decorations.cpp",
    "$spirv_tools_dir/source/validate_derivatives.cpp",
    "$spirv_tools_dir/source/validate_id.cpp",
    "$spirv_tools_dir/source/validate_image.cpp",
    "$spirv_tools_dir/source/validate_instruction.cpp",
    "$spirv_tools_dir/source/validate_layout.cpp",
    "$spirv_tools_dir/source/validate_logicals.cpp",
    "$spirv_tools_dir/source/validate_type_unique.cpp",
  ]
  public_configs = [ ":spirv_tools_config" ]

  foreach(target_name, spvtools_vendor_tables) {
    deps += [ ":$target_name" ]
  }
}

# glslang
# -------

config("glslang_config") {
  include_dirs = [
    glslang_dir,
    "$glslang_dir/glslang/Public",
  ]
}

config("glslang_internal_config") {
  if (is_clang || !is_win) {
    cflags = [
      "-Wno-ignored-qualifiers",
      "-Wno-reorder",
    ]
  }
}

static_library("glslang") {
  sources = [
    "$glslang_dir/OGLCompilersDLL/InitializeDll.cpp",
    "$glslang_dir/OGLCompilersDLL/InitializeDll.h",
    "$glslang_dir/SPIRV/GLSL.ext.KHR.h",
    "$glslang_dir/SPIRV/GLSL.std.450.h",
    "$glslang_dir/SPIRV/GlslangToSpv.cpp",
    "$glslang_dir/SPIRV/GlslangToSpv.h",
    "$glslang_dir/SPIRV/InReadableOrder.cpp",
    "$glslang_dir/SPIRV/Logger.cpp",
    "$glslang_dir/SPIRV/Logger.h",
    "$glslang_dir/SPIRV/SpvBuilder.cpp",
    "$glslang_dir/SPIRV/SpvBuilder.h",
    "$glslang_dir/SPIRV/bitutils.h",
    "$glslang_dir/SPIRV/disassemble.cpp",
    "$glslang_dir/SPIRV/disassemble.h",
    "$glslang_dir/SPIRV/doc.cpp",
    "$glslang_dir/SPIRV/doc.h",
    "$glslang_dir/SPIRV/hex_float.h",
    "$glslang_dir/SPIRV/spirv.hpp",
    "$glslang_dir/SPIRV/spvIR.h",
    "$glslang_dir/StandAlone/ResourceLimits.cpp",
    "$glslang_dir/StandAlone/ResourceLimits.h",
    "$glslang_dir/glslang/GenericCodeGen/CodeGen.cpp",
    "$glslang_dir/glslang/GenericCodeGen/Link.cpp",
    "$glslang_dir/glslang/Include/BaseTypes.h",
    "$glslang_dir/glslang/Include/Common.h",
    "$glslang_dir/glslang/Include/ConstantUnion.h",
    "$glslang_dir/glslang/Include/InfoSink.h",
    "$glslang_dir/glslang/Include/InitializeGlobals.h",
    "$glslang_dir/glslang/Include/PoolAlloc.h",
    "$glslang_dir/glslang/Include/ResourceLimits.h",
    "$glslang_dir/glslang/Include/ShHandle.h",
    "$glslang_dir/glslang/Include/Types.h",
    "$glslang_dir/glslang/Include/arrays.h",
    "$glslang_dir/glslang/Include/intermediate.h",
    "$glslang_dir/glslang/Include/revision.h",
    "$glslang_dir/glslang/MachineIndependent/Constant.cpp",
    "$glslang_dir/glslang/MachineIndependent/InfoSink.cpp",
    "$glslang_dir/glslang/MachineIndependent/Initialize.cpp",
    "$glslang_dir/glslang/MachineIndependent/Initialize.h",
    "$glslang_dir/glslang/MachineIndependent/IntermTraverse.cpp",
    "$glslang_dir/glslang/MachineIndependent/Intermediate.cpp",
    "$glslang_dir/glslang/MachineIndependent/LiveTraverser.h",
    "$glslang_dir/glslang/MachineIndependent/ParseContextBase.cpp",
    "$glslang_dir/glslang/MachineIndependent/ParseHelper.cpp",
    "$glslang_dir/glslang/MachineIndependent/ParseHelper.h",
    "$glslang_dir/glslang/MachineIndependent/PoolAlloc.cpp",
    "$glslang_dir/glslang/MachineIndependent/RemoveTree.cpp",
    "$glslang_dir/glslang/MachineIndependent/RemoveTree.h",
    "$glslang_dir/glslang/MachineIndependent/Scan.cpp",
    "$glslang_dir/glslang/MachineIndependent/Scan.h",
    "$glslang_dir/glslang/MachineIndependent/ScanContext.h",
    "$glslang_dir/glslang/MachineIndependent/ShaderLang.cpp",
    "$glslang_dir/glslang/MachineIndependent/SymbolTable.cpp",
    "$glslang_dir/glslang/MachineIndependent/SymbolTable.h",
    "$glslang_dir/glslang/MachineIndependent/Versions.cpp",
    "$glslang_dir/glslang/MachineIndependent/Versions.h",
    "$glslang_dir/glslang/MachineIndependent/gl_types.h",
    "$glslang_dir/glslang/MachineIndependent/glslang.y",
    "$glslang_dir/glslang/MachineIndependent/glslang_tab.cpp",
    "$glslang_dir/glslang/MachineIndependent/glslang_tab.cpp.h",
    "$glslang_dir/glslang/MachineIndependent/intermOut.cpp",
    "$glslang_dir/glslang/MachineIndependent/iomapper.cpp",
    "$glslang_dir/glslang/MachineIndependent/iomapper.h",
    "$glslang_dir/glslang/MachineIndependent/limits.cpp",
    "$glslang_dir/glslang/MachineIndependent/linkValidate.cpp",
    "$glslang_dir/glslang/MachineIndependent/localintermediate.h",
    "$glslang_dir/glslang/MachineIndependent/parseConst.cpp",
    "$glslang_dir/glslang/MachineIndependent/parseVersions.h",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/Pp.cpp",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/PpAtom.cpp",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/PpContext.cpp",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/PpContext.h",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/PpScanner.cpp",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/PpTokens.cpp",
    "$glslang_dir/glslang/MachineIndependent/preprocessor/PpTokens.h",
    "$glslang_dir/glslang/MachineIndependent/propagateNoContraction.cpp",
    "$glslang_dir/glslang/MachineIndependent/propagateNoContraction.h",
    "$glslang_dir/glslang/MachineIndependent/reflection.cpp",
    "$glslang_dir/glslang/MachineIndependent/reflection.h",
    "$glslang_dir/glslang/OSDependent/osinclude.h",
    "$glslang_dir/glslang/Public/ShaderLang.h",
  ]
  public_configs = [ ":glslang_config" ]
  configs += [ ":glslang_internal_config" ]

  if (is_win) {
    cflags = [
      "/wd4100",  # Unreferenced formal parameter
      "/wd4456",  # Declaration hides previous local declaration
      "/wd4457",  # Declaration hides function parameter
      "/wd4458",  # Declaration hides class member
      "/wd4702",  # Unreachable code (from glslang_tab.cpp)
      "/wd4718",  # Recursive call has no side effects (from PpContext.cpp)
    ]

    sources += [ "$glslang_dir/glslang/OSDependent/Windows/ossource.cpp" ]
  }
  if (is_linux) {
    sources += [ "$glslang_dir/glslang/OSDependent/Unix/ossource.cpp" ]
  }
}

# The validation layers
# ---------------------

config("vulkan_layer_config") {
  include_dirs = [ "$vulkan_layers_dir/layers" ]
}

source_set("vulkan_layer_table") {
  configs -= vulkan_undefine_configs
  public_deps = [
    ":vulkan_generate_helper_files",
  ]
  public_configs = [
    ":vulkan_config",
    ":vulkan_layer_config",
    ":vulkan_internal_config",
  ]
  sources = [
    "$target_gen_dir/angle/vulkan/vk_dispatch_table_helper.h",
    "$target_gen_dir/angle/vulkan/vk_enum_string_helper.h",
    "$vulkan_layers_dir/layers/vk_layer_table.cpp",
    "$vulkan_layers_dir/layers/vk_layer_table.h",
  ]
}

core_validation_sources = [
  # This file is manually included in the layer
  # "$vulkan_gen_dir/vk_safe_struct.cpp",
  "$vulkan_gen_dir/vk_safe_struct.h",
  "$vulkan_layers_dir/layers/buffer_validation.cpp",
  "$vulkan_layers_dir/layers/buffer_validation.h",
  "$vulkan_layers_dir/layers/core_validation.cpp",
  "$vulkan_layers_dir/layers/core_validation.h",
  "$vulkan_layers_dir/layers/descriptor_sets.cpp",
  "$vulkan_layers_dir/layers/descriptor_sets.h",
  "$vulkan_layers_dir/layers/shader_validation.cpp",
  "$vulkan_layers_dir/layers/shader_validation.h",
  "$vulkan_layers_dir/layers/xxhash.c",
  "$vulkan_layers_dir/layers/xxhash.h",
]

object_tracker_sources = [
  "$vulkan_gen_dir/object_tracker.cpp",
  "$vulkan_layers_dir/layers/object_tracker.h",
  "$vulkan_layers_dir/layers/object_tracker_utils.cpp",
]

parameter_validation_sources = [
  "$vulkan_gen_dir/parameter_validation.cpp",
  "$vulkan_layers_dir/layers/parameter_validation.h",
  "$vulkan_layers_dir/layers/parameter_validation_utils.cpp",
]

threading_sources = [
  "$vulkan_gen_dir/thread_check.h",
  "$vulkan_layers_dir/layers/threading.cpp",
  "$vulkan_layers_dir/layers/threading.h",
]

unique_objects_sources = [
  "$vulkan_gen_dir/unique_objects_wrappers.h",

  # This file is manually included in the layer
  # "$vulkan_gen_dir/vk_safe_struct.cpp",
  "$vulkan_gen_dir/vk_safe_struct.h",
  "$vulkan_layers_dir/layers/unique_objects.cpp",
  "$vulkan_layers_dir/layers/unique_objects.h",
]

layers = [
  [
    "core_validation",
    core_validation_sources,
    ":vulkan_core_validation_glslang",
  ],
  [
    "object_tracker",
    object_tracker_sources,
    ":vulkan_gen_object_tracker_cpp",
  ],
  [
    "parameter_validation",
    parameter_validation_sources,
    ":vulkan_gen_parameter_validation",
  ],
  [
    "threading",
    threading_sources,
    ":vulkan_gen_thread_check_helper",
  ],
  [
    "unique_objects",
    unique_objects_sources,
    ":vulkan_gen_unique_objects_wrappers_helper",
  ],
]

vulkan_gen_json_files_outputs = [
  "$root_out_dir/$data_dir/VkLayer_core_validation.json",
  "$root_out_dir/$data_dir/VkLayer_object_tracker.json",
  "$root_out_dir/$data_dir/VkLayer_parameter_validation.json",
  "$root_out_dir/$data_dir/VkLayer_threading.json",
  "$root_out_dir/$data_dir/VkLayer_unique_objects.json",
]

action("vulkan_gen_json_files") {
  script = "$angle_root/scripts/generate_vulkan_layers_json.py"
  if (is_win) {
    sources = [
      "$vulkan_layers_dir/layers/windows/VkLayer_core_validation.json",
      "$vulkan_layers_dir/layers/windows/VkLayer_object_tracker.json",
      "$vulkan_layers_dir/layers/windows/VkLayer_parameter_validation.json",
      "$vulkan_layers_dir/layers/windows/VkLayer_threading.json",
      "$vulkan_layers_dir/layers/windows/VkLayer_unique_objects.json",
    ]
    args = [ "$raw_vulkan_layers_dir/layers/windows" ]
  }
  if (is_linux) {
    sources = [
      "$vulkan_layers_dir/layers/linux/VkLayer_core_validation.json",
      "$vulkan_layers_dir/layers/linux/VkLayer_object_tracker.json",
      "$vulkan_layers_dir/layers/linux/VkLayer_parameter_validation.json",
      "$vulkan_layers_dir/layers/linux/VkLayer_threading.json",
      "$vulkan_layers_dir/layers/linux/VkLayer_unique_objects.json",
    ]
    args = [ "$raw_vulkan_layers_dir/layers/linux" ]
  }

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

source_set("vulkan_layer_utils") {
  sources = [
    "$vulkan_layers_dir/layers/vk_format_utils.cpp",
    "$vulkan_layers_dir/layers/vk_format_utils.h",
    "$vulkan_layers_dir/layers/vk_layer_config.cpp",
    "$vulkan_layers_dir/layers/vk_layer_config.h",
    "$vulkan_layers_dir/layers/vk_layer_extension_utils.cpp",
    "$vulkan_layers_dir/layers/vk_layer_extension_utils.h",
    "$vulkan_layers_dir/layers/vk_layer_utils.cpp",
    "$vulkan_layers_dir/layers/vk_layer_utils.h",
  ]
  public_configs = [
    ":vulkan_config",
    ":vulkan_loader_config",
    ":vulkan_internal_config",
  ]
  public_deps = [
    ":vulkan_generate_helper_files",
  ]
  configs -= vulkan_undefine_configs
}

config("vulkan_core_validation_config") {
  include_dirs = [ glslang_dir ]
}

source_set("vulkan_core_validation_glslang") {
  public_deps = [
    ":spirv_tools",
  ]
  public_configs = [ ":vulkan_core_validation_config" ]
}

config("vulkan_parameter_validation_config") {
  if (is_clang) {
    cflags_cc = [ "-Wno-unused-const-variable" ]
  }
}

# This special action is needed to remove the generated param header.
# Otherwise the param gen cpp file will pick up the old file and the
# build will fail. It's a bit unfortunate but necessary.
action("vulkan_clean_parameter_gen_header") {
  script = "$angle_root/scripts/remove_file_if_exists.py"
  inputs = parameter_validation_sources
  deps = [
    ":vulkan_gen_parameter_validation_cpp",
  ]
  outputs = [
    "$vulkan_gen_dir/parameter_validation_h_is_removed",
  ]
  args = [
    "$raw_vulkan_gen_dir/parameter_validation.h",
    "$raw_vulkan_gen_dir/parameter_validation_h_is_removed",
  ]
}

source_set("vulkan_gen_parameter_validation") {
  deps = [
    ":vulkan_clean_parameter_gen_header",
    ":vulkan_gen_parameter_validation_cpp",
  ]
  public_configs = [ ":vulkan_parameter_validation_config" ]
}

foreach(layer_info, layers) {
  name = layer_info[0]
  shared_library("VkLayer_$name") {
    configs -= vulkan_undefine_configs
    deps = [
      ":vulkan_layer_table",
      ":vulkan_layer_utils",
      "//build/config:exe_and_shlib_deps",
    ]
    if (layer_info[2] != "") {
      deps += [ layer_info[2] ]
    }
    sources = layer_info[1]
    if (is_win) {
      sources += [ "$vulkan_layers_dir/layers/VkLayer_$name.def" ]
    }
  }
}

# Use this target to include everything ANGLE needs for Vulkan.
group("angle_vulkan") {
  deps = [
    ":glslang",
    ":vulkan_loader",
  ]
  data_deps = [
    ":vulkan_gen_json_files",
  ]
  foreach(layer_info, layers) {
    name = layer_info[0]
    data_deps += [ ":VkLayer_$name" ]
  }
  public_configs = [
    ":vulkan_config",
    ":vulkan_loader_config",
    ":glslang_config",
  ]
}
