# 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.

import("//base/update/updater/updater_default_cfg.gni")
import("//build/ohos.gni")

HDC_PATH = "//developtools/hdc/src"
updater_path = rebase_path("${updater_absolutely_path}", ".")

hash_sources = [
  "${hdc_base}/scripts/hdc_hash_gen.py",
  "${hdc_base}/src/common/base.cpp",
  "${hdc_base}/src/common/channel.h",
  "${hdc_base}/src/common/session.h",
  "${hdc_base}/src/common/transfer.h",
]

py_out_dir = "$root_build_dir/gen/" + rebase_path(".", "//")

config("hdc_config") {
  include_dirs = [ "${py_out_dir}" ]
  cflags_cc = [ "-std=c++17" ]
  if (is_mingw) {
    cflags_cc += [ "-Wno-inconsistent-dllimport" ]  # in mingw some sec api will
                                                    # overwrite by utilsecurec
  }
}

action("hdc_hash_gen") {
  script = "${hdc_base}/scripts/hdc_hash_gen.py"
  sources = hash_sources
  outputs = [ "$py_out_dir" ]
  args = [
    "-f",
    "hdc_hash_gen.h",
    "-i",
    rebase_path("${hdc_base}", root_build_dir),
    "-o",
    rebase_path("$py_out_dir" + "/", root_build_dir),
  ]
  public_configs = [ ":hdc_config" ]
}

common_sources = [
  "${HDC_PATH}/common/async_cmd.cpp",
  "${HDC_PATH}/common/auth.cpp",
  "${HDC_PATH}/common/base.cpp",
  "${HDC_PATH}/common/channel.cpp",
  "${HDC_PATH}/common/circle_buffer.cpp",
  "${HDC_PATH}/common/compress.cpp",
  "${HDC_PATH}/common/decompress.cpp",
  "${HDC_PATH}/common/entry.cpp",
  "${HDC_PATH}/common/hdc_statistic_reporter.cpp",
  "${HDC_PATH}/common/file.cpp",
  "${HDC_PATH}/common/file_descriptor.cpp",
  "${HDC_PATH}/common/forward.cpp",
  "${HDC_PATH}/common/header.cpp",
  "${HDC_PATH}/common/heartbeat.cpp",
  "${HDC_PATH}/common/server_cmd_log.cpp",
  "${HDC_PATH}/common/session.cpp",
  "${HDC_PATH}/common/task.cpp",
  "${HDC_PATH}/common/tcp.cpp",
  "${HDC_PATH}/common/tlv.cpp",
  "${HDC_PATH}/common/transfer.cpp",
  "${HDC_PATH}/common/usb.cpp",
  "${HDC_PATH}/common/uv_status.cpp",
]

config("flashd_hdc_config") {
  include_dirs = [ "./common" ]
  cflags_cc = [ "-std=c++17" ]
}

ohos_static_library("flashd_deamon") {
  sources = [
    "${HDC_PATH}/daemon/daemon_app.cpp",
    "${HDC_PATH}/daemon/daemon_forward.cpp",
    "${HDC_PATH}/daemon/daemon_tcp.cpp",
    "${HDC_PATH}/daemon/daemon_unity.cpp",
    "${HDC_PATH}/daemon/daemon_usb.cpp",
    "${HDC_PATH}/daemon/jdwp.cpp",
    "${HDC_PATH}/daemon/shell.cpp",
    "daemon/daemon.cpp",
    "daemon/system_depend.cpp",
  ]

  sources += common_sources

  defines = [
    "HARMONY_PROJECT",
    "OPENSSL_SUPPRESS_DEPRECATED",
  ]

  configs = [ ":flashd_hdc_config" ]

  external_deps = [
    "bounds_checking_function:libsec_static",
    "c_utils:utils",
    "libuv:uv",
    "lz4:liblz4_static",
    "openssl:libcrypto_shared",
  ]

  deps = [ "${updater_path}/services/flashd:hdc_hash_gen" ]

  if (updater_hdc_depend) {
    external_deps += [ "hdc:hdc_updater" ]
  }

  if (use_musl) {
    external_deps += [ "init:libbegetutil_static" ]
  }

  include_dirs = [
    "${updater_path}/services/common",
    "${updater_path}/services/flashd",
    "${updater_path}/services/flashd/daemon",
    "${updater_path}/services/flashd/common",
    "${updater_path}/services/flashd/host",
    "${updater_path}/services/include",
    "${updater_path}/services/include/fs_manager",
    "${updater_path}/services/include/package",
    "${updater_path}/services/include/applypatch",
    "${updater_path}/services/include/script",
    "${updater_path}/services/include/log",
    "${updater_path}/interfaces/kits/include",
    "${updater_path}/utils/include",
  ]

  if (build_selinux) {
    external_deps += [ "selinux:libselinux_static" ]
    defines += [
      "SURPPORT_SELINUX",
      "UPDATER_MODE",
    ]
  }

  if (build_variant == "user") {
    defines += [ "UPDATER_BUILD_VARIANT_USER" ]
  }

  subsystem_name = "updater"
  part_name = "updater"
}

ohos_static_library("libflashd") {
  sources = [
    "${updater_path}/services/flashd/daemon/commander.cpp",
    "${updater_path}/services/flashd/daemon/commander_factory.cpp",
    "${updater_path}/services/flashd/daemon/daemon_updater.cpp",
    "${updater_path}/services/flashd/daemon/erase_commander.cpp",
    "${updater_path}/services/flashd/daemon/flash_commander.cpp",
    "${updater_path}/services/flashd/daemon/flashd_main.cpp",
    "${updater_path}/services/flashd/daemon/flashd_utils.cpp",
    "${updater_path}/services/flashd/daemon/format_commander.cpp",
    "${updater_path}/services/flashd/daemon/update_commander.cpp",
    "${updater_path}/services/flashd/image_writer/image_writer.cpp",
    "${updater_path}/services/flashd/partition.cpp",
    "${updater_path}/services/hdi/client/update_hdi_client.cpp",
    "${updater_path}/services/updater_binary/update_image_block.cpp",
    "${updater_path}/services/updater_binary/update_image_patch.cpp",
    "${updater_path}/services/updater_binary/update_partitions.cpp",
    "${updater_path}/services/updater_binary/update_processor.cpp",
  ]

  defines = [
    "HARMONY_PROJECT",
    "OPENSSL_SUPPRESS_DEPRECATED",
  ]

  deps = [
    ":flashd_deamon",
    "${updater_path}/interfaces/kits/misc_info:libmiscinfo",
    "${updater_path}/interfaces/kits/slot_info:libslotinfo",
    "${updater_path}/interfaces/kits/updaterkits:libupdaterkits",
    "${updater_path}/services/applypatch:libapplypatch",
    "${updater_path}/services/common/ring_buffer:libringbuffer",
    "${updater_path}/services/diffpatch/patch:libpatch",
    "${updater_path}/services/flashd:hdc_hash_gen",
    "${updater_path}/services/flow_update/update_bin:libBinFlowUpdate",
    "${updater_path}/services/fs_manager:libfsmanager",
    "${updater_path}/services/hdi/server:libupdate_hdi_impl",
    "${updater_path}/services/log:libupdaterlog",
    "${updater_path}/services/package:libupdaterpackage",
    "${updater_path}/services/script:libupdaterscript",
    "${updater_path}/utils:libutils",
  ]

  include_dirs = [
    "${updater_path}/services",
    "${updater_path}/services/common",
    "${updater_path}/services/flashd",
    "${updater_path}/services/flashd/daemon",
    "${updater_path}/services/flashd/common",
    "${updater_path}/services/flow_update",
    "${updater_path}/interfaces/kits/include",
    "${updater_path}/services/include/package",
    "${updater_path}/services/include/ptable_parse",
    "${updater_path}/services/include/script",
    "${updater_path}/services/include/log",
    "${updater_path}/services/include",
    "${updater_path}/services/ptable_parse",
    "${updater_path}/utils/include",
    "${updater_path}/utils/json",
  ]

  if (defined(use_ptable)) {
    include_dirs += [ "${updater_path}/services/ptable_parse" ]
    deps += [ "${updater_path}/services/ptable_parse:libptableparse" ]
    defines += [ "UPDATER_USE_PTABLE" ]
  }

  external_deps = [
    "bounds_checking_function:libsec_static",
    "bzip2:libbz2",
    "cJSON:cjson",
    "c_utils:utils",
    "init:libbegetutil_static",
    "libuv:uv",
    "lz4:liblz4_static",
    "openssl:libcrypto_shared",
    "zlib:libz",
  ]

  if (updater_hdc_depend) {
    external_deps += [ "hdc:hdc_updater" ]
  }

  if (build_selinux) {
    external_deps += [ "selinux:libselinux_static" ]
    defines += [ "SURPPORT_SELINUX" ]
  }

  if (defined(binary_custom_external_deps)) {
    external_deps += binary_custom_external_deps
  }

  part_name = "updater"
  subsystem_name = "updater"
}

group("updater_flashd") {
  deps = [ ":libflashd" ]
}