#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.

import("//build/ohos.gni")

SANE_CONFIG_DIR = "/data/service/el1/public/print_service/sane/config"
SANE_DATA_DIR = "/data/service/el1/public/print_service/sane/data"
SANE_TMP_DIR = "/data/service/el2/public/print_service/sane/tmp"
SANE_LOCK_DIR = "/data/service/el1/public/print_service/sane/lock"
SANE_LIB_DIR = "/data/service/el1/public/print_service/sane/backend"
SANE_V_MAJOR = 1
SANE_V_MINOR = 2
enable_hilog = true
enable_thread_pool = true
enable_scan_service = true
source_dir = "//third_party/backends"
target_dir = "${target_gen_dir}/sane"

backends_generated_sources = [
  "${target_dir}/backend/dll.c",
  "${target_dir}/threadpool/src/threadpool_c.cpp",
  "${target_dir}/threadpool/src/threadpool_wrapper.cpp",
  "${target_dir}/sanei/sanei_usb.c",
  "${target_dir}/sanei/sanei_directio.c",
  "${target_dir}/sanei/sanei_ab306.c",
  "${target_dir}/sanei/sanei_constrain_value.c",
  "${target_dir}/sanei/sanei_init_debug.c",
  "${target_dir}/sanei/sanei_net.c",
  "${target_dir}/sanei/sanei_wire.c",
  "${target_dir}/sanei/sanei_codec_ascii.c",
  "${target_dir}/sanei/sanei_codec_bin.c",
  "${target_dir}/sanei/sanei_scsi.c",
  "${target_dir}/sanei/sanei_config.c",
  "${target_dir}/sanei/sanei_config2.c",
  "${target_dir}/sanei/sanei_pio.c",
  "${target_dir}/sanei/sanei_pa4s2.c",
  "${target_dir}/sanei/sanei_auth.c",
  "${target_dir}/sanei/sanei_thread.c",
  "${target_dir}/sanei/sanei_pv8630.c",
  "${target_dir}/sanei/sanei_pp.c",
  "${target_dir}/sanei/sanei_lm983x.c",
  "${target_dir}/sanei/sanei_access.c",
  "${target_dir}/sanei/sanei_tcp.c",
  "${target_dir}/sanei/sanei_udp.c",
  "${target_dir}/sanei/sanei_magic.c",
  "${target_dir}/sanei/sanei_ir.c",
  "${target_dir}/sanei/sanei_jpeg.c",
  "${target_dir}/backend/sane_strstatus.c",
  "${target_dir}/backend/stubs.c",
  "${target_dir}/lib/md5.c"
]

backends_generated_header = [
  "${target_dir}/include/sane/sanei_debug.h",
  "${target_dir}/threadpool/include/threadpool_c.h",
  "${target_dir}/threadpool/include/threadpool_wrapper.h",
]

action("backends_action") {
  print("backends_action is exec")
  script = "//third_party/backends/install.py"
  inputs = [
    "${source_dir}/install.py",
    "${source_dir}/patches/modifying_driver_search_path.patch",
    "${source_dir}/patches/add_thread_poll.patch",
    "${source_dir}/patches/hilog_debug.patch",
    "${source_dir}/patches/modify_load_function.patch",
  ]
  outputs = []
  outputs += backends_generated_sources
  outputs += backends_generated_header
  
  backends_source_dir = rebase_path("${source_dir}", root_build_dir)
  backends_target_dir = rebase_path("${target_dir}", root_build_dir)
  
  args = [
    "--source-dir",
    backends_source_dir,
    "--target-dir",
    backends_target_dir,
  ]
}

config("backends_public_config") {
  include_dirs = [
    "${target_dir}/include",
    "${target_dir}/include/sane",
  ]
}

config("backends_private_config") {
  cflags = [
    "-Wall",
    "-g",
    "-O2",
    "-fPIC",
    "-DPIC",
    "-D_REENTRANT",
    "-DHAVE_CONFIG_H",
    "-Wno-format",
    "-Wno-unused-const-variable",
    "-Wno-unused-variable",
    "-Wno-unused-but-set-variable",
    "-Wno-unused-function",
  ]

  if (enable_hilog) {
    cflags += [ "-DENABLE_HILOG" ]
  }

  if (enable_thread_pool) {
    cflags += [ "-DHAVE_THREAD_POLL"]
  }

  if (enable_scan_service) {
    cflags += [ "-DHAVE_SCAN_SERVICE"]
  }

  defines = [
    "PATH_SANE_CONFIG_DIR=$SANE_CONFIG_DIR",
    "PATH_SANE_DATA_DIR=$SANE_DATA_DIR",
    "PATH_SANE_LOCK_DIR=$SANE_LOCK_DIR",
    "PATH_SANE_TMP_DIR=$SANE_TMP_DIR",
    "V_MAJOR=$SANE_V_MAJOR",
    "V_MINOR=$SANE_V_MINOR",
    "LIBDIR=\"$SANE_LIB_DIR\"",
  ]
}

#build targets in /lib
ohos_source_set("lib") {
  sources = [ "${target_dir}/lib/md5.c" ]

  deps = [
    ":backends_action",
  ]

  public_configs = [ ":backends_public_config" ]

  configs = [ ":backends_private_config" ]

  subsystem_name = "thirdparty"
  part_name = "backends"
}

#build targets in /sanei
sanei_names = [
  "sanei_directio",
  "sanei_ab306",
  "sanei_constrain_value",
  "sanei_init_debug",
  "sanei_net",
  "sanei_wire",
  "sanei_codec_ascii",
  "sanei_codec_bin",
  "sanei_scsi",
  "sanei_config",
  "sanei_config2",
  "sanei_pio",
  "sanei_pa4s2",
  "sanei_auth",
  "sanei_thread",
  "sanei_pv8630",
  "sanei_pp",
  "sanei_lm983x",
  "sanei_access",
  "sanei_tcp",
  "sanei_udp",
  "sanei_magic",
  "sanei_ir",
  "sanei_jpeg",
]

ohos_source_set("sanei_usb") {
  sources = [ "${target_dir}/sanei/sanei_usb.c" ]

  external_deps = [ "libusb:libusb" ]

  if (enable_hilog) {
    external_deps += [ "hilog:libhilog" ]
  }

  deps = [
    ":backends_action",
  ]

  public_configs = [ ":backends_public_config" ]

  configs = [ ":backends_private_config" ]

  subsystem_name = "thirdparty"
  part_name = "backends"
}

foreach(name, sanei_names) {
  ohos_source_set("$name") {
    sources = [ "${target_dir}/sanei/$name.c" ]

    if (enable_hilog) {
      external_deps = [ "hilog:libhilog" ]
    }

    deps = [
      ":backends_action",
    ]

    public_configs = [ ":backends_public_config" ]

    configs = [ ":backends_private_config" ]

    subsystem_name = "thirdparty"
    part_name = "backends"
  }
}

ohos_static_library("sanei") {
  sources = []

  foreach(name, sanei_names) {
    sources += [ "${target_dir}/sanei/$name.c" ]
  }

  if (enable_hilog) {
    external_deps = [ "hilog:libhilog" ]
  }

  deps = [
    ":backends_action",
  ]

  public_configs = [ ":backends_public_config" ]

  configs = [ ":backends_private_config" ]

  subsystem_name = "thirdparty"
  part_name = "backends"
}

#build targets in /backend
ohos_source_set("sane_strstatus") {
  sources = [ "${target_dir}/backend/sane_strstatus.c" ]

  deps = [
    ":backends_action",
  ]

  public_configs = [ ":backends_public_config" ]

  configs = [ ":backends_private_config" ]

  subsystem_name = "thirdparty"
  part_name = "backends"
}


ohos_source_set("threadpool_c") {
  sources = [
    "${target_dir}/threadpool/src/threadpool_c.cpp",
    "${target_dir}/threadpool/src/threadpool_wrapper.cpp"
  ]

  include_dirs = [
    "${target_dir}/threadpool/include",
  ]

  deps = [
    ":backends_action",
  ]

  external_deps = [
    "c_utils:utils",
  ]
  
  if (enable_hilog) {
    external_deps += [ "hilog:libhilog" ]
  }

  public_configs = [ ":backends_public_config" ]

  configs = [ ":backends_private_config" ]

  subsystem_name = "thirdparty"
  part_name = "backends"
}

ohos_shared_library("sane") {
  sources = [
    "${target_dir}/backend/dll.c",
    "${target_dir}/backend/stubs.c",
  ]

  public_configs = [ ":backends_public_config" ]

  configs = [ ":backends_private_config" ]

  defines = [ "BACKEND_NAME=dll" ]

  deps = [
    ":backends_action",
    ":lib",
    ":sane_strstatus",
    ":sanei_config",
    ":sanei_constrain_value",
    ":sanei_init_debug",
    ":sanei_usb",
  ]

  include_dirs = []

  if (enable_thread_pool) {
    deps += [ ":threadpool_c" ]
    include_dirs += [ "${target_dir}/threadpool/include" ]
  }

  if (enable_hilog) {
    external_deps = [ "hilog:libhilog" ]
  }

  ldflags = [ "-ldl" ]

  subsystem_name = "thirdparty"
  part_name = "backends"
}

#the target group
group("third_sane") {
  deps = [ ":sane" ]
}