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

# Defines a collect ebpf testcase template
#
# The collect_ebpf_testcase template is to collect the ebpf testcase to out/xxx/ebpf_testcase,
# and record the configuration file.
# Parameters
#   ebpf_testcase the ebp testcase of subsystem
#   subsystem_name
#
template("collect_ebpf_testcase") {
  assert(defined(invoker.ebpf_testcase))
  assert(defined(invoker.subsystem_name))
  assert(defined(invoker.part_name))

  forward_variables_from(invoker,
                         [
                           "ebpf_testcase",
                           "subsystem_name",
                           "part_name",
                         ])

  deps = []
  subsystem_testcase_collect_path =
      "${root_out_dir}/ebpf_testcase/${subsystem_name}/${part_name}"
  subsystem_testcase_config_file = "${subsystem_testcase_collect_path}/${target_name}_ebpf_testcase_config.json"
  copy("${target_name}_copy_testcase") {
    sources = []
    sources += ebpf_testcase
    outputs = [ "${subsystem_testcase_collect_path}/{{source_file_part}}" ]
  }

  src_testcase_list = []
  foreach(testcase, ebpf_testcase) {
    src_testcase_list += [ rebase_path(testcase, root_build_dir) ]
  }

  action("${target_name}") {
    deps += [ ":${target_name}_copy_testcase" ]
    script = "//build/gen_subsystem_ebpf_testcase_config.py"
    sources = ebpf_testcase
    outputs = [ subsystem_testcase_config_file ]
    args = [
      "--subsystem-name",
      subsystem_name,
      "--subsystem-ebpf-testcase-config-file",
      rebase_path(subsystem_testcase_config_file, root_build_dir),
    ]
    args += [ "--subsystem-testcase-list" ]
    args += src_testcase_list
    args += [ "--subsystem-testcase-collect-path" ]
    args += [ rebase_path(subsystem_testcase_collect_path, root_build_dir) ]
  }
}