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

NSTACKX_ROOT = "./.."
import("$NSTACKX_ROOT/t2stack.gni")

cflags = [ "-DENABLE_USER_LOG" ]
if (defined(ohos_lite)) {
  import("//build/lite/config/component/lite_component.gni")
  if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "liteos_m") {
    config("nstackx_util_header") {
      include_dirs = [
        "interface",
        "platform/liteos",
      ]
    }
  } else if (ohos_kernel_type == "linux") {
    config("nstackx_util_header") {
      include_dirs = [
        "interface",
        "platform/unix",
      ]
    }
  }
  if (ohos_kernel_type == "liteos_m") {
    static_library("nstackx_util.open") {
      cflags += [
        "-D_GNU_SOURCE",
        "-DNSTACKX_WITH_LITEOS",
        "-DLWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS",
        "-DLWIP_LITEOS_A_COMPAT",
        "-DMBEDTLS_INCLUDED",
        "-DNSTACKX_WITH_LITEOS_M",
        "-DENABLE_USER_LOG",
      ]
      if (board_toolchain_type != "iccarm") {
        cflags += [ "-Wall" ]
      }
      cflags_cc = cflags
      include_dirs = [
        "include",
        "interface",
        "platform/liteos",
        "$NSTACKX_ROOT/nstackx_core/",
        "$hilog_lite_include_path",
        "$utils_lite_include_path",
      ]
      sources = [
        "core/nstackx_event.c",
        "core/nstackx_log.c",
        "core/nstackx_socket.c",
        "core/nstackx_timer.c",
      ]

      sources += [
        "core/nstackx_getopt.c",
        "platform/liteos/sys_dev.c",
        "platform/liteos/sys_epoll.c",
        "platform/liteos/sys_event.c",
        "platform/liteos/sys_log.c",
        "platform/liteos/sys_socket.c",
        "platform/liteos/sys_timer.c",
        "platform/liteos/sys_util.c",
      ]

      public_configs = [ ":nstackx_util_header" ]
    }
  } else {
    shared_library("nstackx_util.open") {
      if (ohos_kernel_type == "liteos_a") {
        cflags += [
          "-Wall",
          "-D_GNU_SOURCE",
          "-DNSTACKX_WITH_LITEOS",
          "-DLWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS",
          "-DLWIP_LITEOS_A_COMPAT",
          "-DMBEDTLS_INCLUDED",
          "-DENABLE_USER_LOG",
        ]
        cflags_cc = cflags
        include_dirs = [
          "include",
          "interface",
          "platform/liteos",
          "$NSTACKX_ROOT/nstackx_core/",
          "$hilog_lite_include_path",
          "$utils_lite_include_path",
        ]
        sources = [
          "core/nstackx_dev.c",
          "core/nstackx_event.c",
          "core/nstackx_getopt.c",
          "core/nstackx_log.c",
          "core/nstackx_mbedtls.c",
          "core/nstackx_socket.c",
          "core/nstackx_timer.c",
          "core/nstackx_util.c",
          "platform/liteos/sys_dev.c",
          "platform/liteos/sys_epoll.c",
          "platform/liteos/sys_event.c",
          "platform/liteos/sys_log.c",
          "platform/liteos/sys_socket.c",
          "platform/liteos/sys_timer.c",
          "platform/liteos/sys_util.c",
        ]
        deps = [ "$hilog_lite_deps_path" ]
        external_deps = [
          "bounds_checking_function:libsec_shared",
          "mbedtls:mbedtls_shared",
        ]
      } else if (ohos_kernel_type == "linux") {
        cflags += [
          "-Wall",
          "-DNSTACKX_WITH_LINUX",
          "-DMBEDTLS_INCLUDED",
          "-DENABLE_USER_LOG",
        ]
        cflags_cc = cflags
        include_dirs = [
          "include",
          "interface",
          "platform/unix",
          "$NSTACKX_ROOT/nstackx_core/",
          "$hilog_lite_include_path",
          "$utils_lite_include_path",
        ]
        sources = [
          "core/nstackx_dev.c",
          "core/nstackx_event.c",
          "core/nstackx_getopt.c",
          "core/nstackx_log.c",
          "core/nstackx_mbedtls.c",
          "core/nstackx_socket.c",
          "core/nstackx_timer.c",
          "core/nstackx_util.c",
          "platform/unix/sys_dev.c",
          "platform/unix/sys_epoll.c",
          "platform/unix/sys_event.c",
          "platform/unix/sys_log.c",
          "platform/unix/sys_socket.c",
          "platform/unix/sys_timer.c",
          "platform/unix/sys_util.c",
        ]
        deps = [ "$hilog_lite_deps_path" ]
        external_deps = [
          "bounds_checking_function:libsec_shared",
          "mbedtls:mbedtls_shared",
        ]
      }
      public_configs = [ ":nstackx_util_header" ]
    }
  }
} else {
  import("//build/ohos.gni")

  config("nstackx_util_header") {
    include_dirs = [
      "interface",
      "platform/unix",
    ]
  }

  ohos_shared_library("nstackx_util.open") {
    sanitize = {
      ubsan = true
      integer_overflow = true
      boundary_sanitize = true
      cfi = true
      cfi_cross_dso = true
      debug = false
    }
    branch_protector_ret = "pac_ret"

    cflags += [
      "-DNSTACKX_WITH_LINUX",
      "-DENABLE_USER_LOG",
      "-DNSTACKX_WITH_LINUX_STANDARD",
    ]
    if (defined(board_toolchain_type)) {
      if (board_toolchain_type != "iccarm") {
        cflags += [ "-Wall" ]
      }
    } else {
      cflags += [ "-Wall" ]
    }
    cflags_cc = cflags
    include_dirs = [
      "include",
      "interface",
      "platform/unix",
      "$NSTACKX_ROOT/nstackx_core/",
    ]
    sources = [
      "core/nstackx_dev.c",
      "core/nstackx_event.c",
      "core/nstackx_getopt.c",
      "core/nstackx_log.c",
      "core/nstackx_socket.c",
      "core/nstackx_timer.c",
      "core/nstackx_util.c",
      "platform/unix/sys_dev.c",
      "platform/unix/sys_epoll.c",
      "platform/unix/sys_event.c",
      "platform/unix/sys_log.c",
      "platform/unix/sys_socket.c",
      "platform/unix/sys_timer.c",
      "platform/unix/sys_util.c",
    ]

    external_deps = [
      "bounds_checking_function:libsec_shared",
      "hilog:libhilog",
    ]
    if (is_standard_system) {
      cflags += [ "-DSSL_AND_CRYPTO_INCLUDED" ]
      sources += [ "core/nstackx_openssl.c" ]
      external_deps += [
        "c_utils:utils",
        "openssl:libcrypto_shared",
      ]
    }
    public_configs = [ ":nstackx_util_header" ]
    innerapi_tags = [ "platformsdk_indirect" ]
    part_name = "t2stack"
    subsystem_name = "communication"
  }
}