| # Copyright 2018 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/package_metadata.gni") | 
 | import("//build/prebuilt_binaries.gni") | 
 |  | 
 | declare_args() { | 
 |   # To ensure that everything can be built without debug symbols present we | 
 |   # gate weather or not these are consumed on a build argument. When set, | 
 |   # unpack_debug_archives creates an additional build step that unpacks | 
 |   # debug archives in tar.bzip2 format into the .build-id directory | 
 |   unpack_debug_archives = false | 
 | } | 
 |  | 
 | # Generate a signed, sealed package file from a prebuilt archive. | 
 | # | 
 | # Parameters | 
 | # | 
 | #   archive (required) | 
 | #     Path to archive containing a package. | 
 | # | 
 | #   debug_archive (optional) | 
 | #     Path to archive containing a .build-id directory for the package. | 
 | #     TODO(fxbug.dev/41478): Delete me. | 
 | # | 
 | #   package_name (optional) | 
 | #     Name of the package. | 
 | #     Defaults to the target's name. | 
 | # | 
 | #   deps (optional) | 
 | #   testonly (optional) | 
 | #   visibility (optional) | 
 | #     Usual GN meanings. | 
 | # | 
 | template("prebuilt_package") { | 
 |   pkg_target_name = target_name | 
 |   pkg_name = target_name | 
 |   if (defined(invoker.package_name)) { | 
 |     pkg_name = invoker.package_name | 
 |   } | 
 |   pkg_version = 0 | 
 |   manifest_target_name = target_name + ".manifest" | 
 |  | 
 |   meta_dir = target_out_dir + "/" + pkg_name + ".meta" | 
 |   blobs_json = "$meta_dir/blobs.json" | 
 |   package_manifest_json = "$meta_dir/package_manifest.json" | 
 |   blobs_manifest = "$meta_dir/blobs.manifest" | 
 |   blobs_json_path = rebase_path(blobs_json, root_build_dir) | 
 |  | 
 |   unpack_action_name = target_name + ".debug-archive" | 
 |  | 
 |   if (defined(invoker.debug_archive) && unpack_debug_archives) { | 
 |     prebuilt_binaries(unpack_action_name) { | 
 |       forward_variables_from(invoker, | 
 |                              [ | 
 |                                "debug_archive", | 
 |                                "testonly", | 
 |                              ]) | 
 |     } | 
 |   } else { | 
 |     if (defined(invoker.debug_archive)) { | 
 |       not_needed(invoker, [ "debug_archive" ]) | 
 |     } | 
 |     group(unpack_action_name) { | 
 |       metadata = { | 
 |         prebuilt_packages = [ | 
 |           { | 
 |             archive = invoker.archive | 
 |             if (defined(invoker.package_name)) { | 
 |               package_name = invoker.package_name | 
 |             } | 
 |           }, | 
 |         ] | 
 |       } | 
 |     } | 
 |   } | 
 |  | 
 |   metadata_target_name = "${target_name}_metadata" | 
 |   define_package_metadata(metadata_target_name) { | 
 |     package_name = pkg_name | 
 |     snapshot_entry = "$pkg_name/$pkg_version=$blobs_json_path" | 
 |     blob_manifest = blobs_manifest | 
 |     meta_far_merkle_index_entry = | 
 |         "$pkg_name/$pkg_version=" + | 
 |         rebase_path("$meta_dir/meta.far.merkle", root_build_dir) | 
 |     package_output_manifest = package_manifest_json | 
 |   } | 
 |  | 
 |   action(target_name) { | 
 |     forward_variables_from(invoker, | 
 |                            [ | 
 |                              "testonly", | 
 |                              "visibility", | 
 |                            ]) | 
 |  | 
 |     archive = invoker.archive | 
 |  | 
 |     if (defined(visibility)) { | 
 |       visibility += [ ":$manifest_target_name" ] | 
 |     } | 
 |  | 
 |     script = "//build/packages/prebuilt_package.py" | 
 |  | 
 |     system_rsp = "$target_out_dir/$pkg_name.system.rsp" | 
 |     meta_merkle = "$meta_dir/meta.far.merkle" | 
 |  | 
 |     args = [ | 
 |       "--pm-tool", | 
 |       rebase_path("$root_out_dir/host_$host_cpu/pm"), | 
 |       "--name", | 
 |       pkg_name, | 
 |       "--archive", | 
 |       rebase_path(archive), | 
 |       "--workdir", | 
 |       rebase_path(meta_dir, root_build_dir), | 
 |       "--system-rsp", | 
 |       rebase_path(system_rsp, root_build_dir), | 
 |     ] | 
 |     inputs = [ archive ] | 
 |     outputs = [ | 
 |       blobs_manifest, | 
 |       system_rsp, | 
 |       blobs_json, | 
 |       package_manifest_json, | 
 |       meta_merkle, | 
 |     ] | 
 |  | 
 |     deps = [ | 
 |       ":$metadata_target_name", | 
 |       ":$unpack_action_name($default_toolchain)", | 
 |       "//src/sys/pkg/bin/pm:pm_bin($host_toolchain)", | 
 |     ] | 
 |     if (defined(invoker.deps)) { | 
 |       deps += invoker.deps | 
 |     } | 
 |   } | 
 |  | 
 |   group(manifest_target_name) { | 
 |     forward_variables_from(invoker, [ "testonly" ]) | 
 |     public_deps = [ ":$pkg_target_name" ] | 
 |   } | 
 | } |