# Copyright (c) 2021 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.

if (!is_linux && !is_mingw && !is_mac) {
  import("//base/update/updater/updater_default_cfg.gni")
  updater_path = rebase_path("${updater_absolutely_path}", ".")
} else {
  updater_path = "../.."
}
import("//build/ohos.gni")

config("package_config") {
  visibility = [ ":*" ]
  cflags_cc = [ "-std=c++17" ]
  include_dirs = [
    "${updater_path}/interfaces/kits/include",
    "${updater_path}/utils/include",
    "${updater_path}/services/common",
    "${updater_path}/services/include",
    "${updater_path}/services/include",
    "${updater_path}/services/include/package",
    "${updater_path}/services/include/script",
    "${updater_path}/services/include/log",
    "pkg_algorithm",
    "pkg_manager",
    "pkg_package",
    "pkg_verify",
  ]
}

config("libupdaterpackage_exported_headers") {
  visibility = [ ":*" ]
  include_dirs = [
    "${updater_path}/services",
    "${updater_path}/service/common",
    "${updater_path}/service/common/ring_buffer",
    "${updater_path}/services/include/package",
    "${updater_path}/services/include",
    "${updater_path}/services/package",
    "${updater_path}/services/package/pkg_algorithm",
    "${updater_path}/services/package/pkg_manager",
    "${updater_path}/services/package/pkg_verify",
    "${updater_path}/services/package/pkg_package",
    "${updater_path}/services/include/script",
    "${updater_path}/interfaces/kits/include",
  ]
}

libverify_common_source = [
  "./pkg_manager/pkg_utils.cpp",
  "./pkg_verify/cert_verify.cpp",
  "./pkg_verify/openssl_util.cpp",
  "./pkg_verify/pkcs7_signed_data.cpp",
]

libupdaterpackage_common_source = [
  "./pkg_algorithm/pkg_algo_deflate.cpp",
  "./pkg_algorithm/pkg_algo_digest.cpp",
  "./pkg_algorithm/pkg_algo_lz4.cpp",
  "./pkg_algorithm/pkg_algo_sign.cpp",
  "./pkg_algorithm/pkg_algorithm.cpp",
  "./pkg_manager/pkg_managerImpl.cpp",
  "./pkg_manager/pkg_stream.cpp",
  "./pkg_package/packages_info.cpp",
  "./pkg_package/pkg_gzipfile.cpp",
  "./pkg_package/pkg_lz4file.cpp",
  "./pkg_package/pkg_pkgfile.cpp",
  "./pkg_package/pkg_upgradefile.cpp",
  "./pkg_package/pkg_streamfile.cpp",
  "./pkg_package/pkg_zipfile.cpp",
  "./pkg_verify/pkg_verify_util.cpp",
  "./pkg_verify/zip_pkg_parse.cpp",
  "./pkg_verify/bin_pkg_parse.cpp",
  "pkg_verify/hash_data_verifier.cpp",
]

ohos_static_library("libupdaterpackage") {
  sources = libupdaterpackage_common_source
  sources += libverify_common_source

  deps = [
    "${updater_path}/services/common/ring_buffer:libringbuffer",
    "${updater_path}/services/log:libupdaterlog",
  ]

  defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ]

  if (!is_linux && !is_mingw && !is_mac && host_cpu != "arm64") {
    deps += [
      "../rust/hash_signed_data:rust_hash_signed_data",
      "${updater_path}/utils:libutils",
    ]
  } else {
    defines += [ "DIFF_PATCH_SDK" ]
  }

  external_deps = [
    "bounds_checking_function:libsec_static",
    "lz4:liblz4_static",
    "openssl:libcrypto_static",
    "zlib:libz",
  ]

  configs = [ ":package_config" ]

  public_configs = [ ":libupdaterpackage_exported_headers" ]
  if (is_mingw) {
    libs = [
      "setupapi",
      "psapi",
      "user32",
      "advapi32",
      "iphlpapi",
      "userenv",
      "ws2_32",
    ]

    ldflags = [
      "-Wl,--whole-archive",
      "-lpthread",
      "-Wl,--no-whole-archive",
    ]
  }

  subsystem_name = "updater"
  part_name = "updater"
}

ohos_shared_library("libupdaterpackage_shared") {
  sources = libupdaterpackage_common_source

  deps = [
    "${updater_path}/services/common/ring_buffer:libringbuffer",
    "${updater_path}/services/log:libupdaterlog_shared",
    "${updater_path}/services/package:libverify_shared",
    "${updater_path}/utils:libutils",
  ]

  external_deps = [
    "bounds_checking_function:libsec_static",
    "lz4:liblz4_static",
    "openssl:libcrypto_shared",
    "zlib:shared_libz",
  ]

  if (!is_linux && !is_mingw && !is_mac) {
    deps += [ "../rust/hash_signed_data:rust_hash_signed_data" ]
  }

  configs = [ ":package_config" ]

  public_configs = [ ":libupdaterpackage_exported_headers" ]
  defines = [
    "OPENSSL_SUPPRESS_DEPRECATED",
  ]

  subsystem_name = "updater"
  part_name = "updater"
}

ohos_shared_library("libverify_shared") {
  sources = libverify_common_source

  deps = [
    "${updater_path}/services/log:libupdaterlog_shared",
  ]

  external_deps = [
    "bounds_checking_function:libsec_static",
    "openssl:libcrypto_shared",
  ]

  configs = [ ":package_config" ]

  public_configs = [ ":libupdaterpackage_exported_headers" ]
  defines = [
    "DIFF_PATCH_SDK",
    "OPENSSL_SUPPRESS_DEPRECATED",
  ]

  subsystem_name = "updater"
  part_name = "updater"
}