#
# Copyright (c) 2020-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("//build/ohos.gni")

if (!defined(global_parts_info) ||
    defined(global_parts_info.account_os_account)) {
  has_os_account_part = true
} else {
  has_os_account_part = false
}

hichian_sources = [
  "auth_info/auth_info.c",
  "auth_info/exchange_auth_info_client.c",
  "auth_info/remove_auth_info_client.c",
  "hichain.c",
  "huks_adapter/huks_adapter.c",
  "json/commonutil.c",
  "json/jsonutil.c",
  "key_agreement/key_agreement.c",
  "key_agreement/key_agreement_client.c",
  "key_agreement/key_agreement_server.c",
  "key_agreement/pake_client.c",
  "key_agreement/pake_server.c",
  "key_agreement/sec_clone_server.c",
  "key_agreement/sts_client.c",
  "key_agreement/sts_server.c",
  "log/log.c",
  "os_account_adapter/os_account_adapter.cpp",
  "schedule/build_object.c",
  "schedule/distribution.c",
  "struct/add_auth_info_data.c",
  "struct/auth_ack_request.c",
  "struct/auth_ack_response.c",
  "struct/auth_start_request.c",
  "struct/auth_start_response.c",
  "struct/exchange_auth_data.c",
  "struct/exchange_request.c",
  "struct/exchange_response.c",
  "struct/inform_message.c",
  "struct/key_agreement_version.c",
  "struct/key_agreement_version.h",
  "struct/pake_client_confirm.c",
  "struct/pake_request.c",
  "struct/pake_response.c",
  "struct/pake_server_confirm.c",
  "struct/parsedata.c",
  "struct/rmv_auth_info_data.c",
  "struct/rmv_auth_info_request.c",
  "struct/rmv_auth_info_response.c",
  "struct/sec_clone_data.c",
]

config("hichain_config") {
  include_dirs = [
    "//third_party/bounds_checking_function/include",
    "//base/security/device_auth/interfaces/inner_api/deviceauth_lite",
    "//third_party/cJSON",
    "../inc/base",
    "../inc/huks_adapter",
    "../inc/json",
    "../inc/key_agreement",
    "../inc/log",
    "../inc/os_account_adapter",
    "../inc/schedule",
    "../inc/struct",
    "../inc/auth_info",
  ]

  defines = [
    "_SCANTY_MEMORY_",
    "_CUT_STS_CLIENT_",
    "_CUT_REMOVE_",
    "_CUT_ADD_",
    "_CUT_LOG_",
  ]
  if (has_os_account_part) {
    defines += [ "HAS_OS_ACCOUNT_PART" ]
  }
}

config("hichain_standard_config") {
  include_dirs = [
    "//base/security/device_auth/interfaces/inner_api/deviceauth_lite",
    "../inc/base",
    "../inc/huks_adapter",
    "../inc/json",
    "../inc/key_agreement",
    "../inc/log",
    "../inc/os_account_adapter",
    "../inc/schedule",
    "../inc/struct",
    "../inc/auth_info",
  ]

  defines = [
    #    "_SCANTY_MEMORY_",
    #    "_CUT_STS_CLIENT_",
    "_CUT_REMOVE_",
    #    "_CUT_ADD_",
    #    "_CUT_LOG_",
  ]
  if (has_os_account_part) {
    defines += [ "HAS_OS_ACCOUNT_PART" ]
  }
}

if (os_level == "small" || os_level == "mini") {
  if (ohos_kernel_type == "liteos_m") {
    static_library("hichainsdk") {
      sources = hichian_sources
      public_configs = [ ":hichain_config" ]
      deps =
          [ "//base/security/huks/interfaces/inner_api/huks_lite:huks_3.0_sdk" ]
      if (board_toolchain_type == "iccarm") {
        cflags = [
          "--diag_suppress",
          "Pe1097,Pe546,Pe188,Pe111",
        ]
      }
    }
  } else {
    shared_library("hichainsdk") {
      sources = hichian_sources
      public_configs = [ ":hichain_config" ]
      deps = [
        "//base/security/huks/interfaces/inner_api/huks_lite:huks_3.0_sdk",
        "//build/lite/config/component/cJSON:cjson_shared",
        "//third_party/bounds_checking_function:libsec_shared",
      ]
      cflags = [
        "-flto",
        "-Wall",
        "-fvisibility=hidden",
      ]
    }
  }
} else {
  ohos_shared_library("hichainsdk") {
    subsystem_name = "security"
    part_name = "device_auth"
    sources = hichian_sources
    public_configs = [ ":hichain_standard_config" ]
    external_deps = [
      "bounds_checking_function:libsec_shared",
      "cJSON:cjson",
      "c_utils:utils",
      "huks:libhukssdk",
      "ipc:ipc_core",
    ]
    branch_protector_ret = "pac_ret"
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      integer_overflow = true
      boundary_sanitize = true
      ubsan = true
      debug = false
    }
    if (has_os_account_part) {
      external_deps += [ "os_account:os_account_innerkits" ]
    }
    cflags = [
      "-flto",
      "-Wall",
      "-fvisibility=hidden",
    ]
  }
  ohos_static_library("hichainsdk_static") {
    subsystem_name = "security"
    part_name = "device_auth"
    sources = hichian_sources
    public_configs = [ ":hichain_standard_config" ]
    external_deps = [
      "bounds_checking_function:libsec_shared",
      "cJSON:cjson",
      "c_utils:utils",
      "huks:libhukssdk",
      "ipc:ipc_core",
    ]
    branch_protector_ret = "pac_ret"
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      integer_overflow = true
      boundary_sanitize = true
      ubsan = true
      debug = false
    }
    if (has_os_account_part) {
      external_deps += [ "os_account:os_account_innerkits" ]
    }
    cflags = [
      "-flto",
      "-Wall",
      "-fvisibility=hidden",
      "--coverage",
    ]
    ldflags = [ "--coverage" ]
  }
}