# Copyright 2023 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/config/features.gni")
import("//components/optimization_guide/features.gni")
import("//services/on_device_model/on_device_model.gni")

source_set("api") {
  public = [
    "chrome_ml_api.h",
    "chrome_ml_audio_buffer.h",
    "chrome_ml_tool_types.h",
    "chrome_ml_types.h",
  ]
  sources = [
    "chrome_ml_api.cc",
    "chrome_ml_audio_buffer.cc",
    "chrome_ml_tool_types.cc",
  ]
  deps = [ "//third_party/dawn/src/dawn/native:headers" ]

  # Any target that pulls in this source set needs to specify its own
  # skia_config.
}

if (use_blink || (is_ios && build_with_internal_optimization_guide)) {
  source_set("ml_holder_no_internal") {
    public = [ "chrome_ml_holder.h" ]
    sources = [ "chrome_ml_holder.cc" ]

    deps = [ "//services/on_device_model/public/cpp" ]

    public_deps = [
      ":api",
      "//base",
    ]
  }

  component("ml_no_internal") {
    public = [
      "chrome_ml.h",
      "constraint_factory.h",
      "gpu_blocklist.h",
      "on_device_model_executor.h",
      "performance_class.h",
      "session_accessor.h",
    ]

    defines = [ "IS_ON_DEVICE_MODEL_ML_IMPL" ]

    sources = [
      "chrome_ml.cc",
      "constraint_factory.cc",
      "on_device_model_executor.cc",
      "performance_class.cc",
      "session_accessor.cc",
    ]
    if (use_blink) {
      sources += [ "gpu_blocklist.cc" ]
    } else {
      assert(is_ios)
      sources += [ "gpu_blocklist_ios.cc" ]
    }

    deps = [
      "//components/crash/core/common:crash_key",
      "//components/optimization_guide/core:features",
      "//services/on_device_model:backend_interfaces",
      "//services/on_device_model/safety:safety_util",
      "//third_party/abseil-cpp:absl",
      "//third_party/angle:includes",
      "//third_party/dawn/src/dawn:proc",
      "//third_party/dawn/src/dawn/native",
    ]

    if (enable_constraints) {
      defines += [ "ENABLE_ON_DEVICE_CONSTRAINTS" ]
      deps += [ "//third_party/rust/llguidance/v1:lib" ]
    }
    if (use_blink) {
      deps += [ "//gpu/config" ]
    }
    public_deps = [
      ":api",
      ":ml_holder_no_internal",
      "//base",
      "//mojo/public/cpp/bindings",
      "//services/on_device_model/public/cpp",
      "//services/on_device_model/public/mojom",
    ]
    if (use_blink) {
      public_deps += [ "//gpu/config" ]
    }
  }

  if (enable_ml_internal) {
    source_set("ml_holder") {
      public_deps = [ ":ml_holder_no_internal" ]
      data_deps = [
        "//components/optimization_guide/internal:optimization_guide_internal",
      ]
    }

    source_set("ml") {
      public_deps = [ ":ml_no_internal" ]
      data_deps = [
        "//components/optimization_guide/internal:optimization_guide_internal",
      ]
      if (is_ios) {
        deps = [ "//components/optimization_guide/internal:optimization_guide_internal+bundle" ]
      }
    }
  }
}
