# Copyright (c) 2021-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.

DSOFTBUS_ROOT_PATH = "./../../.."
import("$DSOFTBUS_ROOT_PATH/dsoftbus.gni")

# basic compilation options
cflags = [
  "-DENABLE_USER_LOG",
  "-DNSTACKX_DFINDER_HIDUMP",
  "-DNSTACKX_EXTEND_BUSINESSDATA",
  "-DDFINDER_SAVE_DEVICE_LIST",
  "-DDFINDER_SUPPORT_SET_SCREEN_STATUS",
  "-DDFINDER_DISTINGUISH_ACTIVE_PASSIVE_DISCOVERY",
]

# configure cflags based on build toolchain type
if (defined(board_toolchain_type)) {
  if (board_toolchain_type != "iccarm") {
    cflags += [ "-Wall" ]
  }
} else {
  cflags += [ "-Wall" ]
}

base_src = [
  "core/json_payload.c",
  "core/nstackx_common.c",
  "core/nstackx_device.c",
  "core/nstackx_device_local.c",
  "core/nstackx_device_remote.c",
  "core/nstackx_dfinder_log.c",
]

base_include_dirs = [
  "interface",
  "include",
  "../nstackx_util/interface",
]

# mini platform
if (defined(ohos_lite) && ohos_kernel_type == "liteos_m") {
  mini_diff_src = [
    "core/mini_discover/coap_adapter.c",
    "core/mini_discover/coap_app.c",
    "core/mini_discover/coap_discover.c",
  ]
  mini_diff_include_dirs = [
    "include/mini_discover",
    "../nstackx_util/platform/liteos",
  ]
  cflags += [
    "-DNSTACKX_WITH_LITEOS",
    "-DNSTACKX_WITH_LITEOS_M",
    "-DLWIP_LITEOS_A_COMPAT",
    "-D_GNU_SOURCE",
    "-DDFINDER_USE_MINI_NSTACKX",
  ]
}

# standard and small platform
if (!defined(ohos_lite) ||
    (defined(ohos_lite) && ohos_kernel_type != "liteos_m")) {
  standard_small_diff_src = [
    "core/coap_discover/coap_app.c",
    "core/coap_discover/coap_client.c",
    "core/coap_discover/coap_discover.c",
    "core/nstackx_dfinder_hidump.c",
    "core/nstackx_dfinder_mgt_msg_log.c",
    "core/nstackx_dfinder_hievent.c",
    "core/nstackx_statistics.c",
    "core/nstackx_smartgenius.c",
  ]
  standard_small_diff_include_dirs = [ "include/coap_discover" ]
  cflags += [ "-DDFINDER_MGT_MSG_LOG" ]
}

config("nstackx_ctrl_interface") {
  include_dirs = [ "interface" ]
}

if (defined(ohos_lite)) {
  if (ohos_kernel_type == "liteos_m") {
    import("//build/lite/config/component/lite_component.gni")
    static_library("nstackx_ctrl") {
      sources = base_src
      sources += mini_diff_src
      include_dirs = base_include_dirs
      include_dirs += mini_diff_include_dirs
      include_dirs += [ "//third_party/cJSON" ]
      public_configs = [ ":nstackx_ctrl_interface" ]
      deps = [ "../nstackx_util:nstackx_util.open" ]
      if (board_toolchain_type != "iccarm") {
        cflags += [ "-std=c99" ]
        cflags_cc = cflags
      } else {
        cflags += [
          "--diag_suppress",
          "Pa181",
        ]
        include_dirs += [
          "//kernel/liteos_m/components/net/lwip-2.1/porting/include",
          "//third_party/lwip/src/include",
        ]
      }
    }
  } else {
    import("//build/lite/config/component/lite_component.gni")
    shared_library("nstackx_ctrl") {
      sources = base_src
      sources += standard_small_diff_src
      include_dirs = base_include_dirs
      include_dirs += standard_small_diff_include_dirs
      public_configs = [ ":nstackx_ctrl_interface" ]
      deps = [
        "../nstackx_util:nstackx_util.open",
        "//build/lite/config/component/cJSON:cjson_shared",
      ]
      external_deps = [
        "bounds_checking_function:libsec_shared",
        "libcoap:libcoap",
      ]
      if (ohos_kernel_type == "liteos_a") {
        cflags += [
          "-DNSTACKX_WITH_LITEOS",
          "-DLWIP_LITEOS_A_COMPAT",
        ]
      }
      cflags += [
        "-fPIC",
        "-std=c99",
      ]
      cflags_cc = cflags
      ldflags = [
        "-Wl,-z,relro,-z,now",
        "-s",
        "-fPIC",
      ]
    }
  }
} else {
  import("//build/ohos.gni")
  ohos_shared_library("nstackx_ctrl") {
    sanitize = {
      cfi = true
      cfi_cross_dso = true
      debug = false
    }
    branch_protector_ret = "pac_ret"

    cflags += [ "-DDFINDER_MGT_MSG_LOG" ]
    cflags_cc = cflags
    sources = base_src
    sources += standard_small_diff_src
    include_dirs = base_include_dirs
    include_dirs += standard_small_diff_include_dirs
    include_dirs += [ "../nstackx_util/platform/unix" ]
    public_configs = [ ":nstackx_ctrl_interface" ]
    deps = [ "../nstackx_util:nstackx_util.open" ]
    external_deps = [
      "bounds_checking_function:libsec_shared",
      "cJSON:cjson",
      "libcoap:libcoap",
    ]
    subsystem_name = "communication"
    innerapi_tags = [ "platformsdk_indirect" ]
    part_name = "dsoftbus"
  }
}