aboutsummaryrefslogtreecommitdiff
path: root/pw_build/update_bundle.gni
blob: 8e4b766de38e368897dca96a4f6f774d459493ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright 2021 The Pigweed Authors
#
# 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
#
#     https://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.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/python_action.gni")

# GN target that creates update bundles.
#
# Args:
#   out: Filename at which to output the serialized update bundle.
#   targets: List of targets mapping filenames to target names.
#   persist: Optional boolean; if true, the raw tuf repo will be persisted to
#       disk in the target out dir in addition to being serialized as a bundle.
#   user_manifest: Optional path to an extra user-defined manifest file; if
#       provided, this file will be included as a target payload, but handled
#       specially. See the following file for details:
#         pw_software_update/public/pw_software_update/bundled_update_backend.h
#   targets_metadata_version: Optional manually-specified int version number for
#       the targets metadata.
#   targets_metadata_version_file: Optional manually-specified path/to/file
#        containing int version number for the targets metadata. Cannot be
#        specified together with targets_metadata_version
#   signed_root_metadata: Optional path to a .pb file containing a serialized
#       SignedRootMetadata (generated and signed via the tools in the
#       pw_software_update Python module).
#
# Each target in targets should be a string formatted as follows:
#   "/path/to/file > target_name"

template("pw_update_bundle") {
  assert(defined(invoker.out), "An output path must be provided via 'out'")
  assert(defined(invoker.targets),
         "A list of targets must be provided via 'targets'")
  pw_python_action(target_name) {
    _delimiter = ">"
    forward_variables_from(invoker,
                           [
                             "deps",
                             "public_deps",
                           ])
    _out_path = invoker.out
    _persist_path = ""
    if (defined(invoker.persist) && invoker.persist) {
      _persist_path = "${target_out_dir}/${target_name}/tuf_repo"
    }
    module = "pw_software_update.update_bundle"
    python_deps = [ "$dir_pw_software_update/py" ]
    args = [
      "--out",
      rebase_path(_out_path),
      "--targets",
    ]
    outputs = [ _out_path ]

    foreach(tuf_target, invoker.targets) {
      # Remove possible spaces around the delimiter before splitting
      tuf_target = string_replace(tuf_target, " $_delimiter", _delimiter)
      tuf_target = string_replace(tuf_target, "$_delimiter ", _delimiter)

      tuf_target_list = []
      tuf_target_list = string_split(tuf_target, _delimiter)
      tuf_target_path = rebase_path(tuf_target_list[0], root_build_dir)
      tuf_target_name = tuf_target_list[1]
      assert(tuf_target_name != "user_manifest",
             "The target name 'user_manifest' is reserved for special use.")
      args += [ "${tuf_target_path} > ${tuf_target_name}" ]
      if (_persist_path != "") {
        outputs += [ "${_persist_path}/${tuf_target_name}" ]
      }
    }

    if (defined(invoker.user_manifest)) {
      args += [ rebase_path(invoker.user_manifest, root_build_dir) +
                " > user_manifest" ]
    }

    if (_persist_path != "") {
      args += [
        "--persist",
        rebase_path(_persist_path),
      ]
    }

    if (defined(invoker.targets_metadata_version)) {
      args += [
        "--targets-metadata-version",
        invoker.targets_metadata_version,
      ]
    }

    if (defined(invoker.targets_metadata_version_file)) {
      args += [
        "--targets-metadata-version-file",
        rebase_path(invoker.targets_metadata_version_file),
      ]
    }

    assert(
        !(defined(invoker.targets_metadata_version_file) &&
              defined(invoker.targets_metadata_version)),
        "Only one of targets_metadata_version and targets_metadata_version_file can be specified")

    if (defined(invoker.signed_root_metadata)) {
      args += [
        "--signed-root-metadata",
        rebase_path(invoker.signed_root_metadata),
      ]
    }
  }
}