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