# Copyright (C) 2021-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("//base/security/device_auth/deps_adapter/deviceauth_hals.gni")
import("//build/ohos.gni")
import("deviceauth.gni")

config("deviceauth_config") {
  include_dirs = [ "//base/security/device_auth/interfaces/inner_api" ]
}
include_dirs = inc_path + hals_inc_path
if (os_level == "mini" || os_level == "small") {
  static_library("deviceauth") {
    public_configs = [ ":deviceauth_config" ]
    include_dirs += [
      "//third_party/cJSON",
      "//commonlibrary/utils_lite/include",
      "//third_party/bounds_checking_function/include",
      "//foundation/communication/dsoftbus/interfaces/kits/common",
      "//foundation/communication/dsoftbus/interfaces/kits/transport",
      "//foundation/communication/dsoftbus/interfaces/inner_kits/transport",
      "${dev_frameworks_path}/inc/hiview_adapter",
    ]
    sources = deviceauth_files
    defines = [ "HILOG_ENABLE" ]
    defines += deviceauth_defines

    sources += identity_manager_files
    include_dirs += identity_manager_inc

    deps = [
      "${deps_adapter_path}:${hal_module_name}",
      "//build/lite/config/component/cJSON:cjson_shared",
      "//commonlibrary/utils_lite:utils",
    ]
    if (device_auth_enable_soft_bus_channel) {
      deps += [ "//foundation/communication/dsoftbus/sdk:softbus_client" ]
    }
    if (enable_extend_plugin) {
      defines += [ "DEV_AUTH_PLUGIN_ENABLE" ]
      if (os_level == "mini") {
        sources += [
          "${dev_frameworks_path}/src/plugin_adapter/static_plugin_adapter.c",
        ]
      } else {
        sources += [
          "${dev_frameworks_path}/src/plugin_adapter/dynamic_plugin_adapter.c",
          "${dev_frameworks_path}/src/plugin_adapter/ext_part/account_lifecycle_plugin_proxy.c",
          "${dev_frameworks_path}/src/plugin_adapter/ext_part/ext_part_proxy.c",
        ]
      }
    }
    if (board_toolchain_type == "iccarm" && os_level == "mini") {
      cflags = [
        "--diag_suppress",
        "Pe546,Pe223,Pe111,Pe188",
      ]
    } else {
      cflags = build_flags
    }
    cflags += [
      "-DDEV_AUTH_WORK_THREAD_STACK_SIZE=${device_auth_hichain_thread_stack_size}",
      "-DMAX_AUTH_SESSION_COUNT=${max_auth_session_count}",
    ]
    if (ohos_kernel_type == "linux" || ohos_kernel_type == "liteos_a") {
      include_dirs +=
          [ "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog" ]
      deps += [
        "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
        "//third_party/bounds_checking_function:libsec_shared",
      ]
    } else {
      include_dirs +=
          [ "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite" ]
      deps += [ "//base/hiviewdfx/hilog_lite/frameworks/mini:hilog_lite" ]
    }
  }

  if (os_level == "mini") {
    group("deviceauth_sdk") {
    }
    group("deviceauth_service") {
    }
  }

  if (ohos_kernel_type == "linux" || ohos_kernel_type == "liteos_a") {
    executable("deviceauth_service") {
      include_dirs += [
        "//third_party/cJSON",
        "//commonlibrary/utils_lite/include",
        "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
        "//third_party/bounds_checking_function/include",
        "${frameworks_path}/inc/lite",
        "//foundation/systemabilitymgr/samgr_lite/interfaces/kits/samgr",
        "${dev_frameworks_path}/inc/hiview_adapter",
      ]

      sources = deviceauth_ipc_files
      sources += [
        "${frameworks_path}/src/ipc_service.c",
        "${frameworks_path}/src/lite/ipc_service_init.c",
      ]

      defines = [ "HILOG_ENABLE" ]
      if (ohos_kernel_type == "linux") {
        defines += [ "__LINUX__" ]
      }
      ldflags = [ "-pthread" ]

      deps = [
        ":deviceauth",
        "${deps_adapter_path}:${hal_module_name}",
        "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
        "//build/lite/config/component/cJSON:cjson_shared",
        "//commonlibrary/utils_lite:utils",
        "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single",
        "//foundation/systemabilitymgr/samgr_lite/samgr:samgr",
        "//third_party/bounds_checking_function:libsec_shared",
      ]
    }

    shared_library("deviceauth_sdk") {
      public_configs = [ ":deviceauth_config" ]
      include_dirs += [
        "//third_party/cJSON",
        "//commonlibrary/utils_lite/include",
        "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog",
        "//third_party/bounds_checking_function/include",
        "${frameworks_path}/inc/lite",
        "//foundation/communication/ipc/interfaces/innerkits/c/ipc/include",
        "//foundation/systemabilitymgr/samgr_lite/interfaces/kits/samgr",
        "${dev_frameworks_path}/inc/hiview_adapter",
      ]

      sources = deviceauth_ipc_files
      sources += [ "${frameworks_path}/src/ipc_sdk.c" ]

      defines = [ "HILOG_ENABLE" ]
      if (ohos_kernel_type == "linux") {
        defines += [ "__LINUX__" ]
      }
      cflags = build_flags
      cflags += [ "-fPIC" ]

      deps = [
        "${deps_adapter_path}:${hal_module_name}",
        "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
        "//commonlibrary/utils_lite:utils",
        "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single",
        "//foundation/systemabilitymgr/samgr_lite/samgr:samgr",
        "//third_party/bounds_checking_function:libsec_shared",
      ]
    }
  }
} else {
  ohos_static_library("deviceauth") {
    subsystem_name = "security"
    part_name = "device_auth"
    include_dirs += [
      "${frameworks_path}/inc/standard",
      "${dev_frameworks_path}/inc/hiview_adapter",
    ]

    if (support_os_account) {
      include_dirs += [ "${dev_frameworks_path}/inc/account_subscriber" ]
      include_dirs += [ "${dev_frameworks_path}/inc/sa_subscriber" ]
    }

    sources = deviceauth_files
    if (support_os_account) {
      sources += account_subscriber_files
      sources += sa_subscriber_files
    }
    sources += hiview_adapter_files
    defines = deviceauth_defines
    defines += [ "HILOG_ENABLE" ]
    defines += [ "DEV_AUTH_HIVIEW_ENABLE" ]
    cflags = build_flags
    cflags += [
      "-DDEV_AUTH_WORK_THREAD_STACK_SIZE=${device_auth_hichain_thread_stack_size}",
      "-DMAX_AUTH_SESSION_COUNT=${max_auth_session_count}",
    ]
    if (target_cpu == "arm") {
      cflags += [ "-DBINDER_IPC_32BIT" ]
    }
    if (enable_extend_plugin) {
      defines += [ "DEV_AUTH_PLUGIN_ENABLE" ]
      sources += [
        "${dev_frameworks_path}/src/plugin_adapter/dynamic_plugin_adapter.c",
        "${dev_frameworks_path}/src/plugin_adapter/ext_part/account_lifecycle_plugin_proxy.c",
        "${dev_frameworks_path}/src/plugin_adapter/ext_part/ext_part_proxy.c",
      ]
    }

    sources += identity_manager_files
    include_dirs += identity_manager_inc

    branch_protector_ret = "pac_ret"
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      integer_overflow = true
      boundary_sanitize = true
      ubsan = true
      debug = false
    }

    deps = [ "${deps_adapter_path}:${hal_module_name}" ]

    external_deps = [
      "bounds_checking_function:libsec_shared",
      "cJSON:cjson",
      "c_utils:utils",
      "dsoftbus:softbus_client",
      "hilog:libhilog",
      "hisysevent:libhisysevent",
      "hitrace:hitrace_meter",
    ]
    if (support_os_account) {
      external_deps += [
        "ability_base:want",
        "common_event_service:cesfwk_innerkits",
        "ipc:ipc_single",
        "os_account:os_account_innerkits",
        "samgr:samgr_proxy",
      ]
    }
  }

  ohos_prebuilt_etc("auth_service.rc") {
    source = "${frameworks_path}/src/standard/deviceauth_service.cfg"
    relative_install_dir = "init"
    subsystem_name = "security"
    part_name = "device_auth"
  }

  group("etc") {
    deps = [ ":auth_service.rc" ]
  }

  ohos_executable("deviceauth_service") {
    subsystem_name = "security"
    part_name = "device_auth"
    install_enable = true

    include_dirs += [
      "${frameworks_path}/inc/standard",
      "${dev_frameworks_path}/inc/permission_adapter",
      "${dev_frameworks_path}/inc/hiview_adapter",
    ]

    sources = deviceauth_ipc_files
    sources += permission_adapter_files
    sources += [ "${frameworks_path}/src/ipc_service.c" ]

    defines = [ "HILOG_ENABLE" ]
    defines += [ "DEV_AUTH_HIVIEW_ENABLE" ]
    defines += [ "DEV_AUTH_SERVICE_BUILD" ]
    cflags = build_flags
    if (target_cpu == "arm") {
      cflags += [ "-DBINDER_IPC_32BIT" ]
    }

    branch_protector_ret = "pac_ret"
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      integer_overflow = true
      boundary_sanitize = true
      ubsan = true
      debug = false
    }

    deps = [
      ":auth_service.rc",
      ":deviceauth",
      "${deps_adapter_path}:${hal_module_name}",
    ]

    external_deps = [
      "access_token:libaccesstoken_sdk",
      "bounds_checking_function:libsec_shared",
      "cJSON:cjson",
      "c_utils:utils",
      "hilog:libhilog",
      "init:libbegetutil",
      "ipc:ipc_single",
      "samgr:samgr_proxy",
    ]
  }

  ohos_shared_library("deviceauth_sdk") {
    subsystem_name = "security"
    part_name = "device_auth"
    innerapi_tags = [ "platformsdk" ]
    version_script = "device_auth.map"
    public_configs = [ ":deviceauth_config" ]

    include_dirs += [
      "${frameworks_path}/inc/standard",
      "${dev_frameworks_path}/inc/permission_adapter",
      "${dev_frameworks_path}/inc/hiview_adapter",
    ]

    sources = deviceauth_ipc_files
    sources += permission_adapter_files
    sources += [ "${frameworks_path}/src/ipc_sdk.c" ]

    defines = [
      "__LINUX__",
      "HILOG_ENABLE",
    ]
    cflags = build_flags
    cflags += [ "-fPIC" ]
    if (target_cpu == "arm") {
      cflags += [ "-DBINDER_IPC_32BIT" ]
    }

    branch_protector_ret = "pac_ret"
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      integer_overflow = true
      boundary_sanitize = true
      ubsan = true
      debug = false
    }

    deps = [ "${deps_adapter_path}:${hal_module_name}" ]

    external_deps = [
      "access_token:libaccesstoken_sdk",
      "bounds_checking_function:libsec_shared",
      "cJSON:cjson",
      "c_utils:utils",
      "hilog:libhilog",
      "hisysevent:libhisysevent",
      "hitrace:hitrace_meter",
      "init:libbegetutil",
      "ipc:ipc_single",
      "samgr:samgr_proxy",
    ]
  }
}