# Copyright (C) 2023 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")

ohos_static_library("serialize_structs") {
  sources = [
    "src/cffi/bridge.cpp",
    "src/cffi/cmd.cpp",
    "src/cffi/getparameter.cpp",
    "src/cffi/log.cpp",
    "src/cffi/mount.cpp",
    "src/cffi/mount_wrapper.cpp",
    "src/cffi/oh_usb.cpp",
    "src/cffi/sendmsg.cpp",
    "src/cffi/serial_struct.cpp",
    "src/cffi/sys_para.cpp",
    "src/cffi/transfer.cpp",
    "src/cffi/uart.cpp",
    "src/cffi/uart_wrapper.cpp",
    "src/cffi/usb_util.cpp",
    "src/cffi/usb_wrapper.cpp",
    "src/cffi/utils.cpp",
  ]

  external_deps = [
    "bounds_checking_function:libsec_static",
    "hilog:libhilog",
    "init:libbegetutil",
    "lz4:liblz4_static",
  ]

  defines = [ "HDC_HILOG" ]

  if (is_mac) {
    defines = [ "HOST_MAC" ]
  }

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

  subsystem_name = "developtools"
  part_name = "hdc"
}

ohos_rust_executable("hdcd") {
  sources = [ "src/daemon/main.rs" ]
  deps = [
    ":serialize_structs",
    "//third_party/rust/crates/env_logger:lib",
    "//third_party/rust/crates/humantime:lib",
    "//third_party/rust/crates/log:lib",
    "//third_party/rust/crates/nix:lib",
    "//third_party/rust/crates/rust-openssl/openssl:lib",
  ]
  external_deps = [
    "hilog:hilog_rust",
    "rust_libc:lib",
    "ylong_runtime:ylong_runtime",
  ]

  if (!defined(ohos_lite)) {
    external_deps += [ "faultloggerd:panic_handler" ]
  }

  output_name = "hdcd"
  install_enable = true
  module_install_dir = "bin"
  install_images = [ "system" ]
  features = [ "daemon" ]
  if (is_emulator) {
    features += [ "emulator" ]
  }
  subsystem_name = "developtools"
  part_name = "hdc"
}

# hdc_rust compile begin
ohos_static_library("cffi_host") {
  sources = [
    "src/cffi/host/ctimer.cpp",
    "src/cffi/host/host_usb.cpp",
    "src/cffi/host/host_usb_wrapper.cpp",
    "src/cffi/serial_struct.cpp",
    "src/cffi/transfer.cpp",
    "src/cffi/uart.cpp",
    "src/cffi/uart_wrapper.cpp",
    "src/cffi/usb_util.cpp",
    "src/cffi/utils.cpp",
  ]

  if (!is_mingw) {
    sources += [
      "src/cffi/oh_usb.cpp",
      "src/cffi/sendmsg.cpp",
      "src/cffi/usb_wrapper.cpp",
    ]
  }

  include_dirs = [
    "//third_party/bounds_checking_function/include",
    "//third_party/lz4/lib",
    "src/cffi",
  ]

  external_deps = [
    "bounds_checking_function:libsec_static",
    "libusb:libusb",
    "lz4:liblz4_static",
  ]

  defines = [ "HDC_HOST" ]
  if (is_mingw) {
    defines += [ "HOST_MINGW" ]
  }

  if (is_mac) {
    defines = [ "HOST_MAC" ]
  }

  cflags = [ "-std=c++17" ]

  subsystem_name = "developtools"
  part_name = "hdc"
}

ohos_static_library("hdc_win_dep") {
  sources = [ "src/cffi/win_dep.cpp" ]
  include_dirs = [ "//prebuilts/mingw-w64/ohos/linux-x86_64/clang-mingw/x86_64-w64-mingw32/include" ]

  subsystem_name = "developtools"
  part_name = "hdc"
}

ohos_rust_static_library("hdc_library_host") {
  crate_name = "hdc"
  crate_type = "rlib"
  crate_root = "src/lib.rs"
  sources = [ "src/lib.rs" ]

  deps = [
    ":cffi_host",
    "//third_party/rust/crates/humantime:lib",
    "//third_party/rust/crates/log:lib",
    "//third_party/rust/crates/rust-openssl/openssl:lib",
  ]

  external_deps = [
    "rust_libc:lib",
    "ylong_runtime:ylong_runtime_static",
  ]

  features = [ "host" ]

  subsystem_name = "developtools"
  part_name = "hdc"
}

ohos_rust_executable("hdc_rust") {
  sources = [ "src/host/main.rs" ]
  deps = [
    ":cffi_host",
    ":hdc_library_host",
    "//third_party/rust/crates/humantime:lib",
    "//third_party/rust/crates/lazy-static.rs:lib",
    "//third_party/rust/crates/log:lib",
    "//third_party/rust/crates/rust-openssl/openssl:lib",
  ]

  if (is_mingw) {
    deps += [ ":hdc_win_dep" ]
  }

  # config to link libc++.a
  rust_static_link = true

  external_deps = [
    "rust_libc:lib",
    "ylong_runtime:ylong_runtime_static",
  ]

  features = [ "host" ]
  subsystem_name = "developtools"
  part_name = "hdc"
}