xref: /aosp_15_r20/external/pigweed/pw_build/update_bundle.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2021 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Workerimport("//build_overrides/pigweed.gni")
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/python_action.gni")
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker# GN target that creates update bundles.
20*61c4878aSAndroid Build Coastguard Worker#
21*61c4878aSAndroid Build Coastguard Worker# Args:
22*61c4878aSAndroid Build Coastguard Worker#   out: Filename at which to output the serialized update bundle.
23*61c4878aSAndroid Build Coastguard Worker#   targets: List of targets mapping filenames to target names.
24*61c4878aSAndroid Build Coastguard Worker#   persist: Optional boolean; if true, the raw tuf repo will be persisted to
25*61c4878aSAndroid Build Coastguard Worker#       disk in the target out dir in addition to being serialized as a bundle.
26*61c4878aSAndroid Build Coastguard Worker#   user_manifest: Optional path to an extra user-defined manifest file; if
27*61c4878aSAndroid Build Coastguard Worker#       provided, this file will be included as a target payload, but handled
28*61c4878aSAndroid Build Coastguard Worker#       specially. See the following file for details:
29*61c4878aSAndroid Build Coastguard Worker#         pw_software_update/public/pw_software_update/bundled_update_backend.h
30*61c4878aSAndroid Build Coastguard Worker#   targets_metadata_version: Optional manually-specified int version number for
31*61c4878aSAndroid Build Coastguard Worker#       the targets metadata.
32*61c4878aSAndroid Build Coastguard Worker#   targets_metadata_version_file: Optional manually-specified path/to/file
33*61c4878aSAndroid Build Coastguard Worker#        containing int version number for the targets metadata. Cannot be
34*61c4878aSAndroid Build Coastguard Worker#        specified together with targets_metadata_version
35*61c4878aSAndroid Build Coastguard Worker#   signed_root_metadata: Optional path to a .pb file containing a serialized
36*61c4878aSAndroid Build Coastguard Worker#       SignedRootMetadata (generated and signed via the tools in the
37*61c4878aSAndroid Build Coastguard Worker#       pw_software_update Python module).
38*61c4878aSAndroid Build Coastguard Worker#
39*61c4878aSAndroid Build Coastguard Worker# Each target in targets should be a string formatted as follows:
40*61c4878aSAndroid Build Coastguard Worker#   "/path/to/file > target_name"
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard Workertemplate("pw_update_bundle") {
43*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.out), "An output path must be provided via 'out'")
44*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.targets),
45*61c4878aSAndroid Build Coastguard Worker         "A list of targets must be provided via 'targets'")
46*61c4878aSAndroid Build Coastguard Worker  pw_python_action(target_name) {
47*61c4878aSAndroid Build Coastguard Worker    _delimiter = ">"
48*61c4878aSAndroid Build Coastguard Worker    forward_variables_from(invoker,
49*61c4878aSAndroid Build Coastguard Worker                           [
50*61c4878aSAndroid Build Coastguard Worker                             "deps",
51*61c4878aSAndroid Build Coastguard Worker                             "public_deps",
52*61c4878aSAndroid Build Coastguard Worker                           ])
53*61c4878aSAndroid Build Coastguard Worker    _out_path = invoker.out
54*61c4878aSAndroid Build Coastguard Worker    _persist_path = ""
55*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.persist) && invoker.persist) {
56*61c4878aSAndroid Build Coastguard Worker      _persist_path = "${target_out_dir}/${target_name}/tuf_repo"
57*61c4878aSAndroid Build Coastguard Worker    }
58*61c4878aSAndroid Build Coastguard Worker    module = "pw_software_update.update_bundle"
59*61c4878aSAndroid Build Coastguard Worker    python_deps = [ "$dir_pw_software_update/py" ]
60*61c4878aSAndroid Build Coastguard Worker    args = [
61*61c4878aSAndroid Build Coastguard Worker      "--out",
62*61c4878aSAndroid Build Coastguard Worker      rebase_path(_out_path),
63*61c4878aSAndroid Build Coastguard Worker      "--targets",
64*61c4878aSAndroid Build Coastguard Worker    ]
65*61c4878aSAndroid Build Coastguard Worker    outputs = [ _out_path ]
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker    foreach(tuf_target, invoker.targets) {
68*61c4878aSAndroid Build Coastguard Worker      # Remove possible spaces around the delimiter before splitting
69*61c4878aSAndroid Build Coastguard Worker      tuf_target = string_replace(tuf_target, " $_delimiter", _delimiter)
70*61c4878aSAndroid Build Coastguard Worker      tuf_target = string_replace(tuf_target, "$_delimiter ", _delimiter)
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker      tuf_target_list = []
73*61c4878aSAndroid Build Coastguard Worker      tuf_target_list = string_split(tuf_target, _delimiter)
74*61c4878aSAndroid Build Coastguard Worker      tuf_target_path = rebase_path(tuf_target_list[0], root_build_dir)
75*61c4878aSAndroid Build Coastguard Worker      tuf_target_name = tuf_target_list[1]
76*61c4878aSAndroid Build Coastguard Worker      assert(tuf_target_name != "user_manifest",
77*61c4878aSAndroid Build Coastguard Worker             "The target name 'user_manifest' is reserved for special use.")
78*61c4878aSAndroid Build Coastguard Worker      args += [ "${tuf_target_path} > ${tuf_target_name}" ]
79*61c4878aSAndroid Build Coastguard Worker      if (_persist_path != "") {
80*61c4878aSAndroid Build Coastguard Worker        outputs += [ "${_persist_path}/${tuf_target_name}" ]
81*61c4878aSAndroid Build Coastguard Worker      }
82*61c4878aSAndroid Build Coastguard Worker    }
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.user_manifest)) {
85*61c4878aSAndroid Build Coastguard Worker      args += [ rebase_path(invoker.user_manifest, root_build_dir) +
86*61c4878aSAndroid Build Coastguard Worker                " > user_manifest" ]
87*61c4878aSAndroid Build Coastguard Worker    }
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker    if (_persist_path != "") {
90*61c4878aSAndroid Build Coastguard Worker      args += [
91*61c4878aSAndroid Build Coastguard Worker        "--persist",
92*61c4878aSAndroid Build Coastguard Worker        rebase_path(_persist_path),
93*61c4878aSAndroid Build Coastguard Worker      ]
94*61c4878aSAndroid Build Coastguard Worker    }
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.targets_metadata_version)) {
97*61c4878aSAndroid Build Coastguard Worker      args += [
98*61c4878aSAndroid Build Coastguard Worker        "--targets-metadata-version",
99*61c4878aSAndroid Build Coastguard Worker        invoker.targets_metadata_version,
100*61c4878aSAndroid Build Coastguard Worker      ]
101*61c4878aSAndroid Build Coastguard Worker    }
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.targets_metadata_version_file)) {
104*61c4878aSAndroid Build Coastguard Worker      args += [
105*61c4878aSAndroid Build Coastguard Worker        "--targets-metadata-version-file",
106*61c4878aSAndroid Build Coastguard Worker        rebase_path(invoker.targets_metadata_version_file),
107*61c4878aSAndroid Build Coastguard Worker      ]
108*61c4878aSAndroid Build Coastguard Worker    }
109*61c4878aSAndroid Build Coastguard Worker
110*61c4878aSAndroid Build Coastguard Worker    assert(
111*61c4878aSAndroid Build Coastguard Worker        !(defined(invoker.targets_metadata_version_file) &&
112*61c4878aSAndroid Build Coastguard Worker              defined(invoker.targets_metadata_version)),
113*61c4878aSAndroid Build Coastguard Worker        "Only one of targets_metadata_version and targets_metadata_version_file can be specified")
114*61c4878aSAndroid Build Coastguard Worker
115*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.signed_root_metadata)) {
116*61c4878aSAndroid Build Coastguard Worker      args += [
117*61c4878aSAndroid Build Coastguard Worker        "--signed-root-metadata",
118*61c4878aSAndroid Build Coastguard Worker        rebase_path(invoker.signed_root_metadata),
119*61c4878aSAndroid Build Coastguard Worker      ]
120*61c4878aSAndroid Build Coastguard Worker    }
121*61c4878aSAndroid Build Coastguard Worker  }
122*61c4878aSAndroid Build Coastguard Worker}
123