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

# Defines a subsystem
#
# The lite_subsystem template defines all the modules contained in a subsystem
#
# Parameters
#
#   subsystem_components (required)
#     [list of scopes] Defines all modules in the subsystem.
#
template("lite_subsystem") {
  assert(defined(invoker.subsystem_components),
         "subsystem_components in required.")

  lite_subsystem_components = invoker.subsystem_components

  group(target_name) {
    deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    # add subsystem packages
    foreach(pkg_label, lite_subsystem_components) {
      deps += [ pkg_label ]
    }
  }
}

template("lite_subsystem_test") {
  assert(defined(invoker.subsystem_components),
         "subsystem_components in required.")

  lite_subsystem_components = invoker.subsystem_components

  group(target_name) {
    deps = []
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    # add subsystem packages
    foreach(pkg_label, lite_subsystem_components) {
      deps += [ pkg_label ]
    }
  }
}

template("lite_subsystem_sdk") {
  assert(defined(invoker.interface_header), "interface_header in required.")
  assert(defined(invoker.sdk_libs_path), "sdk_libs_path in required.")
  assert(defined(invoker.subsystem_name), "subsystem_name in required.")

  interface_header = invoker.interface_header
  sdk_libs_path = invoker.sdk_libs_path
  subsystem_name = invoker.subsystem_name
  deps = [ ":${subsystem_name}" ]

  outdir = rebase_path("$root_out_dir/${subsystem_name}")
  prebuilts = "mkdir -p $outdir/interface $outdir/sdk_libs"
  command = "cp -r $interface_header $outdir/interface && cp -r $sdk_libs_path $outdir/sdk_libs"
  exec_path = rebase_path(".", root_build_dir)
  action(target_name) {
    args = [
      "--path=$exec_path",
      "--prebuilts=$prebuilts",
      "--command=$command",
      "--enable=$LOSCFG_SDK_BUILD",
    ]
    script = "//build/lite/build_ext_components.py"
    outputs = [ "$target_out_dir/sdk_build_file_log.txt" ]
  }
}

template("lite_vendor_sdk") {
  prebuilts = invoker.prebuilts
  command = invoker.command
  exec_path = rebase_path(".", root_build_dir)
  action(target_name) {
    args = [
      "--path=$exec_path",
      "--prebuilts=$prebuilts",
      "--command=$command",
      "--enable=$LOSCFG_SDK_BUILD",
    ]
    script = "//build/lite/build_ext_components.py"
    outputs = [ "$target_out_dir/sdk_build_file_log.txt" ]
  }
}