chromium/third_party/blink/renderer/bindings/BUILD.gn

# 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/buildflag_header.gni")
import("//build/config/python.gni")
import("//third_party/blink/renderer/bindings/bindings.gni")
import("//third_party/blink/renderer/bindings/generated_in_core.gni")
import(
    "//third_party/blink/renderer/bindings/generated_in_extensions_chromeos.gni")
import(
    "//third_party/blink/renderer/bindings/generated_in_extensions_webview.gni")
import("//third_party/blink/renderer/bindings/generated_in_modules.gni")
import("//third_party/blink/renderer/bindings/idl_in_core.gni")
import("//third_party/blink/renderer/bindings/idl_in_extensions_chromeos.gni")
import("//third_party/blink/renderer/bindings/idl_in_extensions_webview.gni")
import("//third_party/blink/renderer/bindings/idl_in_modules.gni")

visibility = [ "//third_party/blink/renderer/*" ]

bindings_gen_dir = get_path_info(".", "gen_dir")

template("collect_idl_files") {
  action_with_pydeps(target_name) {
    script = "scripts/collect_idl_files.py"

    inputs = invoker.inputs
    outputs = [ invoker.output ]

    # List input file names in a temporary file.
    response_file_contents = rebase_path(inputs, root_build_dir)
    args = [
      "--idl_list_file",
      "{{response_file_name}}",
      "--component",
      invoker.component,
      "--output",
      rebase_path(invoker.output, root_build_dir),
    ]
    if (defined(invoker.for_testing) && invoker.for_testing) {
      args += [ "--for_testing" ]
    }

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
  }
}

collect_idl_files("web_idl_in_core") {
  component = "core"

  inputs = static_idl_files_in_core

  output = "${bindings_gen_dir}/web_idl_in_core.pickle"
}

collect_idl_files("web_idl_in_core_for_testing") {
  component = "core"
  for_testing = true

  inputs = static_idl_files_in_core_for_testing
  inputs += get_path_info(
          [
            "$root_gen_dir/third_party/blink/renderer/core/testing/internal_runtime_flags.idl",
            "$root_gen_dir/third_party/blink/renderer/core/testing/internal_settings_generated.idl",
          ],
          "abspath")
  deps = [
    "//third_party/blink/renderer/core:generated_testing_idls_internal_runtime_flags",
    "//third_party/blink/renderer/core:generated_testing_idls_settings",
  ]

  output = "${bindings_gen_dir}/web_idl_in_core_for_testing.pickle"
}

collect_idl_files("web_idl_in_extensions_chromeos") {
  component = "extensions_chromeos"

  inputs = static_idl_files_in_extensions_chromeos

  output = "${bindings_gen_dir}/web_idl_in_extensions_chromeos.pickle"
}

collect_idl_files("web_idl_in_extensions_webview") {
  component = "extensions_webview"

  inputs = static_idl_files_in_extensions_webview

  output = "${bindings_gen_dir}/web_idl_in_extensions_webview.pickle"
}

collect_idl_files("web_idl_in_modules") {
  component = "modules"

  inputs = static_idl_files_in_modules

  output = "${bindings_gen_dir}/web_idl_in_modules.pickle"
}

collect_idl_files("web_idl_in_modules_for_testing") {
  component = "modules"
  for_testing = true

  inputs = static_idl_files_in_modules_for_testing

  output = "${bindings_gen_dir}/web_idl_in_modules_for_testing.pickle"
}

action_with_pydeps("web_idl_database") {
  visibility += [
    "//testing/libfuzzer/research/fuzzilli_idl_fuzzing:*",
    "//tools/privacy_budget/blink_apis:*",
  ]

  script = "scripts/build_web_idl_database.py"

  input_data_files = get_target_outputs(":web_idl_in_core") +
                     get_target_outputs(":web_idl_in_core_for_testing") +
                     get_target_outputs(":web_idl_in_modules") +
                     get_target_outputs(":web_idl_in_modules_for_testing")

  runtime_enabled_features_file = "../platform/runtime_enabled_features.json5"
  deps = [
    ":web_idl_in_core",
    ":web_idl_in_core_for_testing",
    ":web_idl_in_modules",
    ":web_idl_in_modules_for_testing",
  ]
  if (use_blink_extensions_chromeos) {
    input_data_files += get_target_outputs(":web_idl_in_extensions_chromeos")
    deps += [ ":web_idl_in_extensions_chromeos" ]
  }
  if (use_blink_extensions_webview) {
    input_data_files += get_target_outputs(":web_idl_in_extensions_webview")
    deps += [ ":web_idl_in_extensions_webview" ]
  }
  inputs = input_data_files + [ runtime_enabled_features_file ]
  outputs = [ web_idl_database_filepath ]

  args = [
           "--output",
           rebase_path(web_idl_database_filepath, root_build_dir),
           "--runtime_enabled_features",
           rebase_path(runtime_enabled_features_file, root_build_dir),
           "--",
         ] + rebase_path(input_data_files, root_build_dir)
}

template("generate_bindings") {
  action_with_pydeps(target_name) {
    script = "scripts/generate_bindings.py"

    if (defined(invoker.visibility)) {
      visibility += invoker.visibility
    }

    if (defined(invoker.pool)) {
      pool = invoker.pool
    }

    web_idl_database_outputs = get_target_outputs(":web_idl_database")
    web_idl_database = web_idl_database_outputs[0]

    inputs = [ web_idl_database ]
    outputs = invoker.outputs

    args = [
      "--web_idl_database",
      rebase_path(web_idl_database, root_build_dir),
      "--root_src_dir",
      rebase_path("//", root_build_dir),
      "--root_gen_dir",
      rebase_path(root_gen_dir, root_build_dir),
      "--output_reldir",
      "core=" + rebase_path("${bindings_gen_dir}/core/v8/", root_gen_dir),
      "--output_reldir",
      "modules=" + rebase_path("${bindings_gen_dir}/modules/v8/", root_gen_dir),
    ]
    if (use_blink_extensions_chromeos) {
      args += [
        "--output_reldir",
        "extensions_chromeos=" +
            rebase_path("${bindings_gen_dir}/extensions_chromeos/v8/",
                        root_gen_dir),
      ]
    }
    if (use_blink_extensions_webview) {
      args += [
        "--output_reldir",
        "extensions_webview=" +
            rebase_path("${bindings_gen_dir}/extensions_webview/v8/",
                        root_gen_dir),
      ]
    }
    if (blink_enable_generated_code_formatting) {
      args += [ "--format_generated_files" ]
    }
    args += invoker.targets

    deps = [ ":web_idl_database" ]
  }
}

generate_bindings("generate_bindings_all") {
  visibility += [ "//third_party/blink/public/mojom/*" ]

  targets = []
  outputs = []
  targets += [ "async_iterator" ]
  outputs += generated_async_iterator_sources_in_core +
             generated_async_iterator_sources_in_modules
  targets += [ "callback_function" ]
  outputs += generated_callback_function_sources_in_core +
             generated_callback_function_sources_in_modules +
             generated_callback_function_sources_for_testing_in_core
  targets += [ "callback_interface" ]
  outputs += generated_callback_interface_sources_in_core +
             generated_callback_interface_sources_in_modules
  targets += [ "dictionary" ]
  outputs += generated_dictionary_sources_in_core +
             generated_dictionary_sources_in_modules +
             generated_dictionary_sources_for_testing_in_core +
             generated_dictionary_sources_for_testing_in_modules
  if (use_blink_extensions_chromeos) {
    outputs += generated_dictionary_sources_in_extensions_chromeos
  }
  if (use_blink_extensions_webview) {
    outputs += generated_dictionary_sources_in_extensions_webview
  }
  targets += [ "enumeration" ]
  outputs += generated_enumeration_sources_in_core +
             generated_enumeration_sources_in_modules +
             generated_enumeration_sources_for_testing_in_core +
             generated_enumeration_sources_for_testing_in_modules
  if (use_blink_extensions_chromeos) {
    outputs += generated_enumeration_sources_in_extensions_chromeos
  }
  if (use_blink_extensions_webview) {
    outputs += generated_enumeration_sources_in_extensions_webview
  }
  targets += [ "interface" ]
  outputs += generated_interface_sources_in_core +
             generated_interface_sources_in_modules +
             generated_interface_extra_sources_in_modules +
             generated_interface_sources_for_testing_in_core +
             generated_interface_sources_for_testing_in_modules +
             generated_interface_extra_sources_for_testing_in_modules
  if (use_blink_extensions_chromeos) {
    outputs += generated_interface_sources_in_extensions_chromeos
  }
  if (use_blink_extensions_webview) {
    outputs += generated_interface_sources_in_extensions_webview
  }
  targets += [ "namespace" ]
  outputs += generated_namespace_sources_in_core +
             generated_namespace_sources_in_modules
  targets += [ "observable_array" ]
  outputs += generated_observable_array_sources_in_core +
             generated_observable_array_sources_in_modules
  targets += [ "sync_iterator" ]
  outputs += generated_sync_iterator_sources_in_core +
             generated_sync_iterator_sources_in_modules
  targets += [ "typedef" ]
  outputs +=
      generated_typedef_sources_in_core + generated_typedef_sources_in_modules
  targets += [ "union" ]
  outputs +=
      generated_union_sources_in_core + generated_union_sources_in_modules +
      generated_union_sources_for_testing_in_core
}

action_with_pydeps("check_generated_file_list") {
  script = "scripts/check_generated_file_list.py"

  web_idl_database_outputs = get_target_outputs(":web_idl_database")
  web_idl_database = web_idl_database_outputs[0]
  result_file = "${bindings_gen_dir}/check_generated_file_list_results.txt"

  inputs = [
    web_idl_database,
    "//third_party/blink/renderer/bindings/generated_in_core.gni",
    "//third_party/blink/renderer/bindings/generated_in_extensions_chromeos.gni",
    "//third_party/blink/renderer/bindings/generated_in_extensions_webview.gni",
    "//third_party/blink/renderer/bindings/generated_in_modules.gni",
    "//third_party/blink/renderer/bindings/idl_in_core.gni",
    "//third_party/blink/renderer/bindings/idl_in_extensions_chromeos.gni",
    "//third_party/blink/renderer/bindings/idl_in_extensions_webview.gni",
    "//third_party/blink/renderer/bindings/idl_in_modules.gni",
  ]
  outputs = [ result_file ]

  file_list = []
  file_list += [ "--for_prod" ]
  file_list += [
    "--kind",
    "async_iterator",
  ]
  file_list += generated_async_iterator_sources_in_core +
               generated_async_iterator_sources_in_modules
  file_list += [
    "--kind",
    "callback_function",
  ]
  file_list += generated_callback_function_sources_in_core +
               generated_callback_function_sources_in_modules
  file_list += [
    "--kind",
    "callback_interface",
  ]
  file_list += generated_callback_interface_sources_in_core +
               generated_callback_interface_sources_in_modules
  file_list += [
    "--kind",
    "dictionary",
  ]
  file_list += generated_dictionary_sources_in_core +
               generated_dictionary_sources_in_modules
  if (use_blink_extensions_chromeos) {
    file_list += generated_dictionary_sources_in_extensions_chromeos
  }
  if (use_blink_extensions_webview) {
    file_list += generated_dictionary_sources_in_extensions_webview
  }
  file_list += [
    "--kind",
    "enumeration",
  ]
  file_list += generated_enumeration_sources_in_core +
               generated_enumeration_sources_in_modules
  if (use_blink_extensions_chromeos) {
    file_list += generated_enumeration_sources_in_extensions_chromeos
  }
  if (use_blink_extensions_webview) {
    file_list += generated_enumeration_sources_in_extensions_webview
  }
  file_list += [
    "--kind",
    "interface",
  ]
  file_list += generated_interface_sources_in_core +
               generated_interface_sources_in_modules
  if (use_blink_extensions_chromeos) {
    file_list += generated_interface_sources_in_extensions_chromeos
  }
  if (use_blink_extensions_webview) {
    file_list += generated_interface_sources_in_extensions_webview
  }
  file_list += [
    "--kind",
    "namespace",
  ]
  file_list += generated_namespace_sources_in_core +
               generated_namespace_sources_in_modules
  file_list += [
    "--kind",
    "observable_array",
  ]
  file_list += generated_observable_array_sources_in_core +
               generated_observable_array_sources_in_modules
  file_list += [
    "--kind",
    "sync_iterator",
  ]
  file_list += generated_sync_iterator_sources_in_core +
               generated_sync_iterator_sources_in_modules
  file_list += [
    "--kind",
    "union",
  ]
  file_list +=
      generated_union_sources_in_core + generated_union_sources_in_modules
  file_list += [ "--for_testing" ]
  file_list += [
    "--kind",
    "callback_function",
  ]
  file_list += generated_callback_function_sources_for_testing_in_core
  file_list += [
    "--kind",
    "dictionary",
  ]
  file_list += generated_dictionary_sources_for_testing_in_core +
               generated_dictionary_sources_for_testing_in_modules
  file_list += [
    "--kind",
    "enumeration",
  ]
  file_list += generated_enumeration_sources_for_testing_in_core +
               generated_enumeration_sources_for_testing_in_modules
  file_list += [
    "--kind",
    "interface",
  ]
  file_list += generated_interface_sources_for_testing_in_core +
               generated_interface_sources_for_testing_in_modules
  file_list += [
    "--kind",
    "union",
  ]
  file_list += generated_union_sources_for_testing_in_core

  response_file_contents = []
  foreach(token, file_list) {
    response_file_contents +=
        [ string_replace(token, "${root_gen_dir}/", "", 1) ]
  }

  args = [
    "--web_idl_database",
    rebase_path(web_idl_database, root_build_dir),
    "--root_src_dir",
    rebase_path("//", root_build_dir),
    "--root_gen_dir",
    rebase_path(root_gen_dir, root_build_dir),
    "--output_reldir",
    "core=" + rebase_path("${bindings_gen_dir}/core/v8/", root_gen_dir),
    "--output_reldir",
    "modules=" + rebase_path("${bindings_gen_dir}/modules/v8/", root_gen_dir),
  ]
  if (use_blink_extensions_chromeos) {
    args += [
      "--output_reldir",
      "extensions_chromeos=" +
          rebase_path("${bindings_gen_dir}/extensions_chromeos/v8/",
                      root_gen_dir),
    ]
  }
  if (use_blink_extensions_webview) {
    args += [
      "--output_reldir",
      "extensions_webview=" +
          rebase_path("${bindings_gen_dir}/extensions_webview/v8/",
                      root_gen_dir),
    ]
  }
  args += [
    "--generated_file_list",
    "{{response_file_name}}",
    "--output",
    rebase_path(result_file, root_build_dir),
  ]

  deps = [
    # A lot of C++ compile tasks are waiting for the completion of the
    # bindings generator because most Blink C++ source files directly or
    # indirectly depend on the generated bindings files.  Since we'd like
    # `generate_bindings_all` to use the most CPU power and memory so that
    # it unblocks Blink C++ compile tasks as soon as possible, we do not like
    # `check_generated_file_list` running in parallel to
    # `generate_bindings_all`.
    #
    # This trick (unnecessary dependency to `generate_bindings_all`) makes
    # `check_generated_file_list` wait for the completion of
    # `generate_bindings_all`.
    ":generate_bindings_all",
    ":web_idl_database",
  ]
}

action_with_pydeps("validate_web_idl") {
  script = "scripts/validate_web_idl.py"

  web_idl_database_outputs = get_target_outputs(":web_idl_database")
  web_idl_database = web_idl_database_outputs[0]
  idl_syntax_known_issues = "idl_syntax_known_issues.txt"
  validate_web_idl_result = "${bindings_gen_dir}/validate_web_idl_results.txt"

  inputs = [
    web_idl_database,
    idl_syntax_known_issues,
  ]
  outputs = [ validate_web_idl_result ]

  args = [
    "--web_idl_database",
    rebase_path(web_idl_database, root_build_dir),
    "--idl_syntax_known_issues",
    rebase_path(idl_syntax_known_issues, root_build_dir),
    "--output",
    rebase_path(validate_web_idl_result, root_build_dir),
  ]

  deps = [
    # A lot of C++ compile tasks are waiting for the completion of the
    # bindings generator because most Blink C++ source files directly or
    # indirectly depend on the generated bindings files.  Since we'd like
    # `generate_bindings_all` to use the most CPU power and memory so that
    # it unblocks Blink C++ compile tasks as soon as possible, we do not like
    # `validate_web_idl` running in parallel to `generate_bindings_all`.
    #
    # This trick (unnecessary dependency to `generate_bindings_all`) makes
    # `validate_web_idl` wait for the completion of `generate_bindings_all`.
    ":generate_bindings_all",
    ":web_idl_database",
  ]
}

# Exposes the libraries that affect the resulting blob image of the V8 context
# snapshot.
group("v8_context_snapshot_influential_libs") {
  visibility = []
  visibility = [ "//tools/v8_context_snapshot/*" ]

  public_deps = [
    "//third_party/blink/renderer/core",
    "//third_party/blink/renderer/modules",
    "//third_party/blink/renderer/platform",
  ]
}

buildflag_header("buildflags") {
  header = "buildflags.h"
  header_dir = "third_party/blink/renderer/bindings"

  flags = [ "PRODUCE_V8_COMPILE_HINTS=$produce_v8_compile_hints" ]
}