# 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")
import("//foundation/distributeddatamgr/preferences/preferences.gni")
config("native_preferences_config") {
  visibility = [ ":*" ]
  include_dirs = [
    "include",
    "${preferences_base_path}/frameworks/common/include",
    "${preferences_native_path}/include",
    "${preferences_native_path}/platform/include/",
  ]
}

config("mock_config") {
  include_dirs = [
    "${preferences_native_path}/mock/ability_runtime/dataobs_manager/include",
    "${preferences_native_path}/mock/ability_base/zuri/include",
  ]
  if (!is_android) {
    include_dirs +=
        [ "${preferences_native_path}/mock/c_utils/utils/base/include" ]
  }
}

config("native_preferences_public_config") {
  visibility = [ ":*" ]
  include_dirs = [ "include" ]
}

config("adaptor_config") {
  include_dirs = [ "//commonlibrary/c_utils/base/include" ]
}

base_sources = [
  "${preferences_native_path}/platform/src/preferences_dfx_adapter.cpp",
  "${preferences_native_path}/platform/src/preferences_file_lock.cpp",
  "${preferences_native_path}/platform/src/preferences_task_processor.cpp",
  "${preferences_native_path}/platform/src/preferences_thread.cpp",
  "${preferences_native_path}/src/base64_helper.cpp",
  "${preferences_native_path}/src/preferences_base.cpp",
  "${preferences_native_path}/src/preferences_helper.cpp",
  "${preferences_native_path}/src/preferences_impl.cpp",
  "${preferences_native_path}/src/preferences_observer.cpp",
  "${preferences_native_path}/src/preferences_utils.cpp",
  "${preferences_native_path}/src/preferences_value.cpp",
  "${preferences_native_path}/src/preferences_xml_utils.cpp",
]

if (!is_ohos) {
  mock_sources = [
    "${preferences_native_path}/mock/ability_base/zuri/src/uri.cpp",
    "${preferences_native_path}/mock/ability_runtime/dataobs_manager/src/data_ability_observer_stub.cpp",
    "${preferences_native_path}/mock/ability_runtime/dataobs_manager/src/dataobs_mgr_client.cpp",
  ]
  if (!is_android) {
    mock_sources +=
        [ "${preferences_native_path}/mock/c_utils/utils/base/src/refbase.cpp" ]
  }
}

if (is_ohos) {
  ohos_shared_library("native_preferences") {
    defines = []
    if (!defined(global_parts_info) ||
        defined(global_parts_info.distributeddatamgr_arkdata_database_core)) {
      defines += [ "ARKDATA_DATABASE_CORE_ENABLE" ]
    }
    branch_protector_ret = "pac_ret"
    sanitize = {
      boundary_sanitize = true
      ubsan = true
      cfi = true
      cfi_cross_dso = true
      debug = false
    }
    cflags_cc = [
      "-std=c++17",
      "-stdlib=libc++",
      "-fvisibility=hidden",
      "-Werror=vla",
    ]
    sources = base_sources
    sources += [
      "${preferences_native_path}/platform/src/preferences_db_adapter.cpp",
      "${preferences_native_path}/src/preferences_enhance_impl.cpp",
      "${preferences_native_path}/src/preferences_value_parcel.cpp",
      "${preferences_native_path}/platform/src/preferences_ffrt_task_processor.cpp",
    ]

    innerapi_tags = [
      "platformsdk",
      "sasdk",
    ]

    configs = [ ":native_preferences_config" ]

    external_deps = [
      "ability_base:zuri",
      "ability_runtime:dataobs_manager",
      "access_token:libaccesstoken_sdk",
      "bounds_checking_function:libsec_shared",
      "c_utils:utils",
      "ffrt:libffrt",
      "hilog:libhilog",
      "hisysevent:libhisysevent",
      "hitrace:hitrace_meter",
      "ipc:ipc_single",
      "libxml2:libxml2",
    ]
    public_configs = [ ":native_preferences_public_config" ]
    subsystem_name = "distributeddatamgr"
    part_name = "preferences"
  }
} else if (is_mingw || is_mac) {
  ohos_shared_library("native_preferences") {
    sources = base_sources
    sources += mock_sources
    sources += [
      "${preferences_native_path}/platform/src/preferences_executor_pool_task_processor.cpp",
    ]
    innerapi_tags = [ "platformsdk" ]

    configs = [ ":native_preferences_config" ]

    configs += [ ":mock_config" ]
    configs += [ ":adaptor_config" ]

    branch_protector_ret = "pac_ret"
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      debug = false
    }
    cflags_cc = [
      "-std=c++17",
      "-stdlib=libc++",
      "-Werror=vla",
    ]
    if (is_mac) {
      buildos = "mac"
      defines = [
        "MAC_PLATFORM",
        "CROSS_PLATFORM",
      ]
    } else {
      buildos = "windows"
      defines = [
        "WINDOWS_PLATFORM",
        "CROSS_PLATFORM",
      ]
      ldflags = [ "-lws2_32" ]
    }
    deps = [
      "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_${buildos}",
    ]
    external_deps = [
      "bounds_checking_function:libsec_shared",
      "libxml2:static_libxml2",
    ]
    public_configs = [ ":native_preferences_public_config" ]

    subsystem_name = "distributeddatamgr"
    part_name = "preferences"
  }
} else if (is_android) {
  ohos_source_set("native_preferences") {
    sources = base_sources
    sources += mock_sources
    sources += [
      "${preferences_native_path}/platform/src/preferences_executor_pool_task_processor.cpp",
    ]

    defines = [
      "ANDROID_PLATFORM",
      "CROSS_PLATFORM",
    ]

    configs = [ ":native_preferences_config" ]
    configs += [ ":mock_config" ]
    cflags_cc = [
      "-fvisibility=hidden",
      "-Werror=vla",
    ]
    deps = [
      "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_android",
      "//commonlibrary/c_utils/base:utilsbase",
      "//third_party/bounds_checking_function:libsec_static",
      "//third_party/libxml2:libxml2",
    ]
    public_configs = [ ":native_preferences_public_config" ]
    subsystem_name = "distributeddatamgr"
    part_name = "preferences"
  }
} else if (is_ios) {
  ohos_source_set("native_preferences") {
    sources = base_sources
    sources += mock_sources
    sources += [
      "${preferences_native_path}/platform/src/preferences_executor_pool_task_processor.cpp",
    ]
    defines = [
      "IOS_PLATFORM",
      "CROSS_PLATFORM",
    ]

    configs = [ ":native_preferences_config" ]
    configs += [ ":mock_config" ]
    cflags_cc = [
      "-fvisibility=hidden",
      "-Werror=vla",
    ]
    deps = [
      "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_${target_os}",
      "//commonlibrary/c_utils/base:utilsbase",
      "//third_party/bounds_checking_function:libsec_static",
      "//third_party/libxml2:libxml2",
    ]
    public_configs = [ ":native_preferences_public_config" ]
    subsystem_name = "distributeddatamgr"
    part_name = "preferences"
  }
}

ohos_static_library("native_preferences_static") {
  sources = base_sources
  configs = [ ":native_preferences_config" ]

  if (is_mingw || is_mac) {
    configs += [ ":mock_config" ]
    configs += [ ":adaptor_config" ]

    sources += mock_sources
    sources += [
      "${preferences_native_path}/platform/src/preferences_executor_pool_task_processor.cpp",
    ]
    cflags_cc = [
      "-std=c++17",
      "-stdlib=libc++",
      "-Werror=vla",
    ]
    if (is_mac) {
      buildos = "mac"
      defines = [
        "MAC_PLATFORM",
        "CROSS_PLATFORM",
      ]
    } else {
      buildos = "windows"
      defines = [
        "WINDOWS_PLATFORM",
        "CROSS_PLATFORM",
      ]
      ldflags = [ "-lws2_32" ]
    }
    deps = [
      "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_${buildos}",
    ]
    external_deps = [
      "bounds_checking_function:libsec_shared",
      "libxml2:static_libxml2",
    ]
  } else {
    if (is_ohos) {
      defines = []
      if (!defined(global_parts_info) ||
          defined(global_parts_info.distributeddatamgr_arkdata_database_core)) {
        defines += [ "ARKDATA_DATABASE_CORE_ENABLE" ]
      }
      branch_protector_ret = "pac_ret"
      sanitize = {
        boundary_sanitize = true
        ubsan = true
        cfi = true
        cfi_cross_dso = true
        debug = false
      }
      sources += [
        "${preferences_native_path}/platform/src/preferences_db_adapter.cpp",
        "${preferences_native_path}/src/preferences_enhance_impl.cpp",
        "${preferences_native_path}/src/preferences_value_parcel.cpp",
        "${preferences_native_path}/platform/src/preferences_ffrt_task_processor.cpp",
      ]
    }
    external_deps = [
      "ability_base:zuri",
      "ability_runtime:dataobs_manager",
      "access_token:libaccesstoken_sdk",
      "bounds_checking_function:libsec_shared",
      "c_utils:utils",
      "ffrt:libffrt",
      "hilog:libhilog",
      "hisysevent:libhisysevent",
      "hitrace:hitrace_meter",
      "ipc:ipc_single",
      "libxml2:libxml2",
    ]
  }

  public_configs = [ ":native_preferences_public_config" ]

  subsystem_name = "distributeddatamgr"
  part_name = "preferences"
}