blob: 7bed6836d649e7aeab3dce9a82e6be1072592df2 [file] [log] [blame]
# Copyright 2021 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/sdk/sdk_atom.gni")
import("//sdk/cts/build/cts_version.gni")
# Copies specific sources to an SDK.
#
# This template should only be used directly to copy BUILD.gn files
# to the CTS SDK. All other uses should be through specific CTS build templates.
#
# Example:
# ```
# # This will create :build_sdk, which should be added to an sdk_molecule in the
# # same file.
# cts_copy_to_sdk("build") {
# testonly = true
# sources = [ "BUILD.gn" ]
# }
# ```
#
# Parameters
#
# sources (required)
# A list of sources to include in the atom.
# Type: list(labels)
#
# dest (optional)
# Location to copy this target to in the CTS archive.
# Optional if the target resides in //sdk/cts, in which case it will be put
# in the same path in the CTS archive by default.
# Required if the target resides outside of //sdk/cts.
# Type: string
template("cts_copy_to_sdk") {
assert(defined(invoker.sources), "Sources must be defined.")
if (defined(invoker.dest)) {
file_base = invoker.dest
} else {
path = get_label_info(":${target_name}", "dir")
# No need to specify 'dest' if the sources are in //sdk/cts (CTS at TOT)
# or a previously released CTS archive (//prebuilt/cts/...)
if (string_replace(path, "//sdk/cts", "") == path &&
string_replace(path, "//prebuilt/cts", "") == path) {
assert(defined(invoker.dest),
"dest must be defined if the source file is not in //sdk/cts")
}
file_base = ""
if (path != "//sdk/cts") {
file_base = string_replace(path, "//sdk/cts/", "")
}
}
main_target_name = target_name
sdk_target_name = "${target_name}_sdk"
relative_path = invoker.sources
relative_path = relative_path[0]
abspath = get_path_info(relative_path, "abspath")
dest_path = relative_path
# For generated artifacts, just copy the file directly instead of mirroring
# the relative path folder structure.
if (string_replace(dest_path, "//out", "") != dest_path) {
dest_path = get_path_info(relative_path, "file")
}
# If file_base is empty, the resource file will actually be copied into
# the '/' directory on the host instead of being copied into the SDK.
if (file_base != "") {
dest_path = "${file_base}/${dest_path}"
}
if (get_path_info(abspath, "extension") == "gn") {
transform_build_gn_target = "${target_name}_transform_build"
output_path = "${root_gen_dir}/cts/${abspath}"
action(transform_build_gn_target) {
script = "//sdk/cts/build/scripts/transform_build_gn.py"
inputs = [ relative_path ]
outputs = [ output_path ]
args = [
"--source",
rebase_path(abspath, root_build_dir),
"--dest",
rebase_path(output_path, root_build_dir),
]
if (cts_version != "") {
args += [
"--cts_version",
cts_version,
]
}
}
}
sdk_files = [
{
if (defined(transform_build_gn_target)) {
source = output_path
} else {
source = relative_path
}
dest = dest_path
},
]
sdk_atom(sdk_target_name) {
forward_variables_from(invoker,
[
"deps",
"testonly",
"metadata",
])
non_sdk_deps = []
if (defined(invoker.non_sdk_deps)) {
non_sdk_deps = invoker.non_sdk_deps
}
category = "cts"
if (file_base != "") {
id = "sdk://${file_base}/${main_target_name}"
} else {
id = "sdk://${main_target_name}"
}
files = sdk_files
meta = {
schema = "cts_resource"
if (file_base != "") {
dest = "${file_base}/json/${main_target_name}/meta.json"
} else {
dest = "json/${main_target_name}/meta.json"
}
value = {
type = "resource"
name = main_target_name
resources = [ dest_path ]
}
}
if (defined(transform_build_gn_target)) {
non_sdk_deps += [ ":${transform_build_gn_target}" ]
}
}
}