# Copyright 2020 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/rust.gni")
import("//testing/libfuzzer/fuzzer_test.gni")

declare_args() {
  # Enables building a development / debugging binary.
  enable_qr_print = false
}

# Error type shared between C++ and Rust.
source_set("error") {
  sources = [ "error.h" ]
}

source_set("bitmap_generator") {
  sources = [
    "bitmap_generator.cc",
    "bitmap_generator.h",
    "dino_image.h",
  ]
  public_deps = [ ":error" ]
  deps = [
    ":qr_code_generator",
    "//base",
    "//components/vector_icons",
    "//skia",
    "//ui/base:features",
    "//ui/gfx",
  ]
}

# TODO(https://crbug.com/533546009): After Crubit bakes and sticks on the
# Stable release channel, then it should be okay to assume that
# `enable_cpp_api_from_rust` will be forever true (and remove
# `cxx::bridge`-related bits of QR code generator in this `BUILD.gn` file, bits
# in `.cc` files, and the whole `qr_code_generator_ffi_glue.rs` file).
buildflag_header("cpp_api_from_rust_buildflags") {
  header = "cpp_api_from_rust_buildflags.h"
  flags = [ "ENABLE_CPP_API_FROM_RUST=$enable_cpp_api_from_rust" ]
  visibility = [ ":qr_code_generator" ]
}

source_set("qr_code_generator") {
  sources = [
    "qr_code_generator.cc",
    "qr_code_generator.h",
  ]
  deps = [ ":cpp_api_from_rust_buildflags" ]
  if (enable_cpp_api_from_rust) {
    deps += [
      "//build/rust/crubit",
      "//third_party/rust/qr_code/v2:cpp_api_from_rust",
    ]
  } else {
    deps += [ ":qr_code_generator_ffi_glue" ]
  }
  public_deps = [
    ":error",
    "//base",
  ]
}

if (!enable_cpp_api_from_rust) {
  rust_static_library("qr_code_generator_ffi_glue") {
    allow_unsafe = true  # Needed for FFI that underpins the `cxx` crate.
    crate_root = "qr_code_generator_ffi_glue.rs"
    sources = [ "qr_code_generator_ffi_glue.rs" ]
    cxx_bindings = [ "qr_code_generator_ffi_glue.rs" ]
    visibility = [ ":qr_code_generator" ]
    deps = [
      ":error",
      "//third_party/rust/qr_code/v2:lib",
    ]
  }
}

source_set("unit_tests") {
  testonly = true
  sources = [
    "bitmap_generator_unittest.cc",
    "qr_code_generator_unittest.cc",
  ]
  deps = [
    ":bitmap_generator",
    ":qr_code_generator",
    "//base",
    "//base/test:test_support",
    "//skia:skia_core_public_headers",
    "//testing/gtest",
  ]
}

if (enable_qr_print) {
  executable("qr_print") {
    sources = [ "qr_print.cc" ]
    deps = [ ":qr_code_generator" ]
  }
}

fuzzer_test("qr_code_generator_fuzzer") {
  sources = [ "qr_code_generator_fuzzer.cc" ]
  deps = [ ":qr_code_generator" ]
}
