# Copyright (C) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/ohos.gni")
import("//build/test.gni")
rust_cxx("preload_native_rust_gen") {
  sources = [ "src/wrapper.rs" ]
}

ohos_rust_static_library("preload_native_rlib") {
  sanitize = {
    integer_overflow = true
    cfi = true
    cfi_cross_dso = true
    boundary_sanitize = true
    all_ubsan = true
    debug = false
  }

  features = [
    "ohos",
    "netstack",
  ]
  rustflags = [
    "-Copt-level=z",
    "-Ccodegen-units=1",
  ]

  sources = [ "src/lib.rs" ]

  deps = [
    ":preload_native_static",
    "../../../common/ffrt_rs:ffrt_rs",
    "../../../common/netstack_rs:netstack_rs",
    "../../../common/utils:request_utils",
    "../cache_core:cache_core",
  ]
  external_deps = [
    "rust_cxx:lib",
    "hilog:libhilog",
    "hilog:hilog_rust",
  ]

  clippy_lints = "none"

  subsystem_name = "request"
  part_name = "request"
}

ohos_rust_shared_ffi("preload_native_rust") {
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }
  features = [
    "ohos",
    "netstack",
  ]
  rustflags = [
    "-Copt-level=z",
    "-Ccodegen-units=1",
  ]

  sources = [ "src/lib.rs" ]
  deps = [
    ":preload_native_static",
    "../../../common/ffrt_rs:ffrt_rs",
    "../../../common/netstack_rs:netstack_rs",
    "../../../common/utils:request_utils",
    "../../../common/utils:request_data_ability",
    "../cache_core:cache_core",
  ]
  external_deps = [
    "rust_cxx:lib",
    "hilog:libhilog",
    "hilog:hilog_rust",
  ]

  subsystem_name = "request"
  part_name = "request"
}

config("preload_native_config") {
  include_dirs = [
    "include",
    "../../../common/include",
    "../../../common/utf8_utils/include",
    "${target_gen_dir}/src",
    "../../../interfaces/inner_kits/cache_download/native/include",
  ]
  cflags = [
    "-Os",
    "-fdata-sections",
    "-ffunction-sections",
  ]
  ldflags = [
    "-Wl,--gc-sections",
  ]
}

ohos_shared_library("preload_native") {
  branch_protector_ret = "pac_ret"
  stack_protector_ret = true
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  sources = [
    "src/cxx/preload_callback.cpp",
    "src/cxx/request_preload.cpp",
  ]
  sources += get_target_outputs(":preload_native_rust_gen")
  deps = [
    ":preload_native_rust",
    ":preload_native_rust_gen",
    "../../../common/utf8_utils:request_utf8_utils",
  ]

  public_configs = [ ":preload_native_config" ]

  external_deps = [
    "ability_runtime:app_context",
    "access_token:libaccesstoken_sdk",
    "access_token:libtokenid_sdk",
    "hilog:libhilog",
    "ipc:ipc_single",
    "netstack:http_client",
    "rust_cxx:cxx_cppdeps",
  ]

  subsystem_name = "request"
  part_name = "request"
}

ohos_static_library("preload_native_static") {
  branch_protector_ret = "pac_ret"
  stack_protector_ret = true
  sanitize = {
    integer_overflow = true
    ubsan = true
    boundary_sanitize = true
    cfi = true
    cfi_cross_dso = true
    debug = false
  }

  public_configs = [ ":preload_native_config" ]

  sources = [
    "src/cxx/preload_callback.cpp",
    "src/cxx/request_preload.cpp",
  ]
  sources += get_target_outputs(":preload_native_rust_gen")
  deps = [
    ":preload_native_rust_gen",
    "../../../common/utf8_utils:request_utf8_utils",
  ]

  external_deps = [
    "ability_runtime:app_context",
    "hilog:libhilog",
    "ipc:ipc_single",
    "netstack:http_client",
    "rust_cxx:cxx_cppdeps",
  ]

  subsystem_name = "request"
  part_name = "request"
}

ohos_rust_unittest("rust_cache_download_ut_test") {
  module_out_path = "request/request/preload"

  sources = [ "src/lib.rs" ]

  rustflags = [
    "--cfg=feature=\"ohos\"",
    "--cfg=feature=\"netstack\"",
  ]

  deps = [
    ":preload_native_static",
    "../../../common/ffrt_rs:ffrt_rs",
    "../../../common/netstack_rs:netstack_rs",
    "../../../common/utils:request_utils",
    "../../../common/utils:request_application",
    "../cache_core:cache_core",
  ]
  external_deps = [
    "rust_cxx:lib",
    "hilog:libhilog",
    "hilog:hilog_rust",
  ]

  subsystem_name = "request"
  part_name = "request"
}

group("unittest") {
  testonly = true
  deps = []
  if (!use_clang_coverage) {
    deps = [ ":rust_cache_download_ut_test" ]
  }
}