import("//Meta/gn/build/compiled_action.gni")
import("//Meta/gn/build/embed_as_string_view.gni")
import("generate_idl_bindings.gni")
import("idl_files.gni")

generate_idl_bindings("standard_idl_bindings") {
  idl_list = standard_idl_files
  type = "standard"
}

generate_idl_bindings("iterable_idl_bindings") {
  idl_list = iterable_idl_files
  type = "iterable"
}

generate_idl_bindings("namespace_idl_bindings") {
  idl_list = namespace_idl_files
  type = "namespace"
}

generate_idl_bindings("global_idl_bindings") {
  idl_list = global_idl_files
  type = "global"
}

generate_idl_targets = [
  ":standard_idl_bindings_generated",
  ":iterable_idl_bindings_generated",
  ":namespace_idl_bindings_generated",
  ":global_idl_bindings_generated",
]

idl_sources_targets = [
  ":standard_idl_bindings_sources",
  ":iterable_idl_bindings_sources",
  ":namespace_idl_bindings_sources",
  ":global_idl_bindings_sources",
]

compiled_action("generate_window_or_worker_interfaces") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateWindowOrWorkerInterfaces"
  inputs = standard_idl_files + iterable_idl_files + namespace_idl_files +
           global_idl_files
  outputs = [
    "$target_gen_dir/Bindings/Forward.h",
    "$target_gen_dir/Bindings/IntrinsicDefinitions.cpp",
    "$target_gen_dir/Bindings/DedicatedWorkerExposedInterfaces.cpp",
    "$target_gen_dir/Bindings/DedicatedWorkerExposedInterfaces.h",
    "$target_gen_dir/Bindings/SharedWorkerExposedInterfaces.cpp",
    "$target_gen_dir/Bindings/SharedWorkerExposedInterfaces.h",
    "$target_gen_dir/Bindings/WindowExposedInterfaces.cpp",
    "$target_gen_dir/Bindings/WindowExposedInterfaces.h",
  ]
  args = [
    "-o",
    rebase_path("$target_gen_dir/Bindings", root_build_dir),
    "-b",
    rebase_path("//Userland/Libraries/LibWeb", root_build_dir),
  ]
  foreach(idl, inputs) {
    args += [ rebase_path(idl, root_build_dir) ]
  }
}

compiled_action("generate_aria_roles") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateAriaRoles"
  inputs = [ "ARIA/AriaRoles.json" ]
  outputs = [
    "$target_gen_dir/ARIA/AriaRoles.h",
    "$target_gen_dir/ARIA/AriaRoles.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_easing_functions") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSEasingFunctions"
  inputs = [ "CSS/EasingFunctions.json" ]
  outputs = [
    "$target_gen_dir/CSS/EasingFunctions.h",
    "$target_gen_dir/CSS/EasingFunctions.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_enums") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSEnums"
  inputs = [ "CSS/Enums.json" ]
  outputs = [
    "$target_gen_dir/CSS/Enums.h",
    "$target_gen_dir/CSS/Enums.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_math_functions") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSMathFunctions"
  inputs = [ "CSS/MathFunctions.json" ]
  outputs = [
    "$target_gen_dir/CSS/MathFunctions.h",
    "$target_gen_dir/CSS/MathFunctions.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_media_feature_id") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSMediaFeatureID"
  inputs = [ "CSS/MediaFeatures.json" ]
  outputs = [
    "$target_gen_dir/CSS/MediaFeatureID.h",
    "$target_gen_dir/CSS/MediaFeatureID.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_property_id") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSPropertyID"
  inputs = [ "CSS/Properties.json" ]
  outputs = [
    "$target_gen_dir/CSS/PropertyID.h",
    "$target_gen_dir/CSS/PropertyID.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_transform_functions") {
  tool =
      "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSTransformFunctions"
  inputs = [ "CSS/TransformFunctions.json" ]
  outputs = [
    "$target_gen_dir/CSS/TransformFunctions.h",
    "$target_gen_dir/CSS/TransformFunctions.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

compiled_action("generate_css_value_id") {
  tool = "//Meta/Lagom/Tools/CodeGenerators/LibWeb:GenerateCSSValueID"
  inputs = [ "CSS/Identifiers.json" ]
  outputs = [
    "$target_gen_dir/CSS/ValueID.h",
    "$target_gen_dir/CSS/ValueID.cpp",
  ]
  args = [
    "-h",
    rebase_path(outputs[0], root_build_dir),
    "-c",
    rebase_path(outputs[1], root_build_dir),
    "-j",
    rebase_path(inputs[0], root_build_dir),
  ]
}

embed_as_string_view("generate_default_stylesheet_source") {
  input = "CSS/Default.css"
  output = "$target_gen_dir/CSS/DefaultStyleSheetSource.cpp"
  variable_name = "default_stylesheet_source"
  namespace = "Web::CSS"
}

embed_as_string_view("generate_quirks_mode_stylesheet_source") {
  input = "CSS/QuirksMode.css"
  output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp"
  variable_name = "quirks_mode_stylesheet_source"
  namespace = "Web::CSS"
}

source_set("all_generated") {
  generated_deps = [
    ":generate_aria_roles",
    ":generate_css_easing_functions",
    ":generate_css_enums",
    ":generate_css_math_functions",
    ":generate_css_media_feature_id",
    ":generate_css_property_id",
    ":generate_css_transform_functions",
    ":generate_css_value_id",
    ":generate_default_stylesheet_source",
    ":generate_quirks_mode_stylesheet_source",
    ":generate_window_or_worker_interfaces",
  ]
  configs += [ ":configs" ]
  sources = []
  foreach(dep, generated_deps) {
    sources += get_target_outputs(dep)
  }
  deps = generated_deps + generate_idl_targets
}

config("configs") {
  include_dirs = [
    "//Userland/Libraries",
    "$target_gen_dir/..",
  ]
  configs = [ "//AK:ak_headers" ]
}

shared_library("LibWeb") {
  output_name = "web"
  public_configs = [ ":configs" ]
  sources = [
    "Dump.cpp",
    "FontCache.cpp",
    "Namespace.cpp",
    "PixelUnits.cpp",
  ]
  deps = [
           ":all_generated",
           "ARIA",
           "Bindings",
           "CSS",
           "Cookie",
           "Crypto",
           "DOM",
           "DOMParsing",
           "Encoding",
           "Fetch",
           "FileAPI",
           "Geometry",
           "HTML",
           "HighResolutionTime",
           "Infra",
           "IntersectionObserver",
           "Layout",
           "Loader",
           "MimeSniff",
           "NavigationTiming",
           "Page",
           "Painting",
           "PerformanceTimeline",
           "PermissionsPolicy",
           "Platform",
           "ReferrerPolicy",
           "RequestIdleCallback",
           "ResizeObserver",
           "SRI",
           "SVG",
           "SecureContexts",
           "Selection",
           "Streams",
           "UIEvents",
           "URL",
           "UserTiming",
           "WebAssembly",
           "WebDriver",
           "WebGL",
           "WebIDL",
           "WebSockets",
           "XHR",
           "XML",
           "//AK",
           "//Userland/Libraries/LibAudio",
           "//Userland/Libraries/LibCore",
           "//Userland/Libraries/LibCrypto",
           "//Userland/Libraries/LibGL",
           "//Userland/Libraries/LibGUI",
           "//Userland/Libraries/LibGemini",
           "//Userland/Libraries/LibGfx",
           "//Userland/Libraries/LibHTTP",
           "//Userland/Libraries/LibIDL",
           "//Userland/Libraries/LibIPC",
           "//Userland/Libraries/LibJS",
           "//Userland/Libraries/LibLocale",
           "//Userland/Libraries/LibMarkdown",
           "//Userland/Libraries/LibRegex",
           "//Userland/Libraries/LibSoftGPU",
           "//Userland/Libraries/LibSyntax",
           "//Userland/Libraries/LibTextCodec",
           "//Userland/Libraries/LibUnicode",
           "//Userland/Libraries/LibVideo",
           "//Userland/Libraries/LibWasm",
           "//Userland/Libraries/LibXML",
         ] + idl_sources_targets
}
