• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/android/rules.gni")
6
7# Creates a stub .apk suitable for use with compressed system APKs.
8#
9# Variables:
10#   package_name: Package name to use for the stub.
11#   version_code: Version code for the stub.
12#   version_name: Version name for the stub.
13#   package_info_from_target: Use the package name, version_code, and <permission>s
14#       from this apk/bundle target.
15#   static_library_name: For static library apks, name for the <static-library>.
16#   static_library_version: For static library apks, version for the
17#       <static-library> tag (for TrichromeLibrary, we set this to be the same
18#       as the package's version_code)
19#   stub_output: Path to output stub apk (default: do not create a stub).
20#
21# package_name and package_info_from_target are mutually exclusive.
22template("system_image_stub_apk") {
23  # Android requires stubs end with -Stub.apk.
24  assert(filter_exclude([ invoker.stub_output ], [ "*-Stub.apk" ]) == [],
25         "stub_output \"${invoker.stub_output}\" must end with \"-Stub.apk\"")
26
27  _resource_apk_path = "${target_out_dir}/$target_name.ap_"
28  _resource_apk_target_name = "${target_name}__compile_resources"
29
30  _manifest_target_name = "${target_name}__manifest"
31  _manifest_path = "$target_gen_dir/$_manifest_target_name.xml"
32
33  if (defined(invoker.package_info_from_target)) {
34    _apk_or_bundle_target = invoker.package_info_from_target
35    _build_config =
36        get_label_info(_apk_or_bundle_target, "target_gen_dir") + "/" +
37        get_label_info(_apk_or_bundle_target, "name") + ".build_config.json"
38    _rebased_build_config = rebase_path(_build_config, root_build_dir)
39  }
40  action_with_pydeps("$_manifest_target_name") {
41    outputs = [ _manifest_path ]
42    script = "//build/android/gyp/create_stub_manifest.py"
43    args = [
44      "--output",
45      rebase_path(_manifest_path, root_build_dir),
46    ]
47    if (defined(invoker.static_library_name)) {
48      args += [
49        "--static-library-name",
50        invoker.static_library_name,
51      ]
52
53      # TODO(crbug.com/40253411): Make static_library_version mandatory.
54      if (defined(invoker.static_library_version)) {
55        args += [
56          "--static-library-version",
57          invoker.static_library_version,
58        ]
59      } else {
60        args += [ "--static-library-version=1" ]
61      }
62    }
63    if (defined(_apk_or_bundle_target)) {
64      deps = [ "$_apk_or_bundle_target$build_config_target_suffix" ]
65      inputs = [ _build_config ]
66      args += [ "--use-permissions-from=@FileArg($_rebased_build_config:deps_info:lint_android_manifest)" ]
67    }
68  }
69
70  _target_sdk_version = default_target_sdk_version
71  if (defined(invoker.override_target_sdk)) {
72    _target_sdk_version = invoker.override_target_sdk
73  }
74
75  action_with_pydeps(_resource_apk_target_name) {
76    script = "//build/android/gyp/compile_resources.py"
77    inputs = [
78      _manifest_path,
79      android_sdk_tools_bundle_aapt2,
80      android_sdk_jar,
81
82      # TODO(b/315080809#comment4): remove these files after fixing
83      # build/print_python_deps.py.
84      "//third_party/protobuf/python/google/__init__.py",
85      "//third_party/protobuf/python/google/protobuf/__init__.py",
86      "//third_party/protobuf/python/google/protobuf/compiler/__init__.py",
87      "//third_party/protobuf/python/google/protobuf/compiler/plugin_pb2.py",
88      "//third_party/protobuf/python/google/protobuf/descriptor.py",
89      "//third_party/protobuf/python/google/protobuf/descriptor_database.py",
90      "//third_party/protobuf/python/google/protobuf/descriptor_pb2.py",
91      "//third_party/protobuf/python/google/protobuf/descriptor_pool.py",
92      "//third_party/protobuf/python/google/protobuf/internal/__init__.py",
93      "//third_party/protobuf/python/google/protobuf/internal/_parameterized.py",
94      "//third_party/protobuf/python/google/protobuf/internal/api_implementation.py",
95      "//third_party/protobuf/python/google/protobuf/internal/builder.py",
96      "//third_party/protobuf/python/google/protobuf/internal/containers.py",
97      "//third_party/protobuf/python/google/protobuf/internal/decoder.py",
98      "//third_party/protobuf/python/google/protobuf/internal/descriptor_database_test.py",
99      "//third_party/protobuf/python/google/protobuf/internal/descriptor_pool_test.py",
100      "//third_party/protobuf/python/google/protobuf/internal/descriptor_test.py",
101      "//third_party/protobuf/python/google/protobuf/internal/encoder.py",
102      "//third_party/protobuf/python/google/protobuf/internal/enum_type_wrapper.py",
103      "//third_party/protobuf/python/google/protobuf/internal/extension_dict.py",
104      "//third_party/protobuf/python/google/protobuf/internal/field_mask.py",
105      "//third_party/protobuf/python/google/protobuf/internal/generator_test.py",
106      "//third_party/protobuf/python/google/protobuf/internal/import_test.py",
107      "//third_party/protobuf/python/google/protobuf/internal/import_test_package/__init__.py",
108      "//third_party/protobuf/python/google/protobuf/internal/json_format_test.py",
109      "//third_party/protobuf/python/google/protobuf/internal/keywords_test.py",
110      "//third_party/protobuf/python/google/protobuf/internal/message_factory_test.py",
111      "//third_party/protobuf/python/google/protobuf/internal/message_listener.py",
112      "//third_party/protobuf/python/google/protobuf/internal/message_test.py",
113      "//third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py",
114      "//third_party/protobuf/python/google/protobuf/internal/python_edition_defaults.py",
115      "//third_party/protobuf/python/google/protobuf/internal/python_message.py",
116      "//third_party/protobuf/python/google/protobuf/internal/reflection_test.py",
117      "//third_party/protobuf/python/google/protobuf/internal/service_reflection_test.py",
118      "//third_party/protobuf/python/google/protobuf/internal/symbol_database_test.py",
119      "//third_party/protobuf/python/google/protobuf/internal/test_util.py",
120      "//third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py",
121      "//third_party/protobuf/python/google/protobuf/internal/text_encoding_test.py",
122      "//third_party/protobuf/python/google/protobuf/internal/text_format_test.py",
123      "//third_party/protobuf/python/google/protobuf/internal/type_checkers.py",
124      "//third_party/protobuf/python/google/protobuf/internal/unknown_fields_test.py",
125      "//third_party/protobuf/python/google/protobuf/internal/well_known_types.py",
126      "//third_party/protobuf/python/google/protobuf/internal/well_known_types_test.py",
127      "//third_party/protobuf/python/google/protobuf/internal/wire_format.py",
128      "//third_party/protobuf/python/google/protobuf/internal/wire_format_test.py",
129      "//third_party/protobuf/python/google/protobuf/json_format.py",
130      "//third_party/protobuf/python/google/protobuf/message.py",
131      "//third_party/protobuf/python/google/protobuf/message_factory.py",
132      "//third_party/protobuf/python/google/protobuf/proto_builder.py",
133      "//third_party/protobuf/python/google/protobuf/pyext/__init__.py",
134      "//third_party/protobuf/python/google/protobuf/pyext/cpp_message.py",
135      "//third_party/protobuf/python/google/protobuf/reflection.py",
136      "//third_party/protobuf/python/google/protobuf/runtime_version.py",
137      "//third_party/protobuf/python/google/protobuf/service.py",
138      "//third_party/protobuf/python/google/protobuf/service_reflection.py",
139      "//third_party/protobuf/python/google/protobuf/symbol_database.py",
140      "//third_party/protobuf/python/google/protobuf/text_encoding.py",
141      "//third_party/protobuf/python/google/protobuf/text_format.py",
142      "//third_party/protobuf/python/google/protobuf/unknown_fields.py",
143      "//third_party/protobuf/python/google/protobuf/util/__init__.py",
144    ]
145    outputs = [ _resource_apk_path ]
146    args = [
147      "--aapt2-path",
148      rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
149      "--min-sdk-version=$default_min_sdk_version",
150      "--target-sdk-version=$_target_sdk_version",
151      "--android-manifest",
152      rebase_path(_manifest_path, root_build_dir),
153      "--arsc-path",
154      rebase_path(_resource_apk_path, root_build_dir),
155    ]
156    deps = [ ":$_manifest_target_name" ]
157    if (defined(invoker.package_name)) {
158      _package_name = invoker.package_name
159      _version_code = invoker.version_code
160      _version_name = invoker.version_name
161
162      # TODO(crbug.com/40253411): Make static_library_version mandatory.
163      if (defined(invoker.static_library_version)) {
164        assert(invoker.static_library_version == _version_code,
165               "$invoker.static_library_version must equal $_version_code.")
166      }
167    } else {
168      deps += [ "${_apk_or_bundle_target}$build_config_target_suffix" ]
169      inputs += [ _build_config ]
170      _package_name = "@FileArg($_rebased_build_config:deps_info:package_name)"
171      _version_code = "@FileArg($_rebased_build_config:deps_info:version_code)"
172      _version_name = "@FileArg($_rebased_build_config:deps_info:version_name)"
173
174      # TODO(crbug.com/40253411): Make static_library_version mandatory.
175      # Pass this through to ensure that the version code in the build config is
176      # the same as the static library version.
177      if (defined(invoker.static_library_version)) {
178        args += [
179          "--static-library-version",
180          invoker.static_library_version,
181        ]
182      }
183    }
184
185    args += [
186      "--rename-manifest-package=$_package_name",
187      "--arsc-package-name=$_package_name",
188      "--version-code=$_version_code",
189      "--version-name=$_version_name",
190      "--include-resources",
191      rebase_path(android_sdk_jar, root_build_dir),
192    ]
193  }
194
195  package_apk(target_name) {
196    forward_variables_from(invoker,
197                           [
198                             "keystore_name",
199                             "keystore_path",
200                             "keystore_password",
201                           ])
202    min_sdk_version = default_min_sdk_version
203    deps = [ ":$_resource_apk_target_name" ]
204
205    packaged_resources_path = _resource_apk_path
206    output_apk_path = invoker.stub_output
207  }
208}
209
210# Generates artifacts for system APKs.
211#
212# Variables:
213#   apk_or_bundle_target: Target that creates input bundle or apk.
214#   input_apk_or_bundle: Path to input .apk or .aab.
215#   static_library_name: For static library apks, name for the <static-library>.
216#   static_library_version: For static library apks, version for the
217#       <static-library> tag (for TrichromeLibrary, we set this to be the same
218#       as the package's version_code)
219#   output: Path to the output system .apk or .zip.
220#   fuse_apk: Fuse all apk splits into a single .apk (default: false).
221#   stub_output: Path to output stub apk (default: do not create a stub).
222#
223template("system_image_apks") {
224  if (defined(invoker.stub_output)) {
225    _stub_apk_target_name = "${target_name}__stub"
226    system_image_stub_apk(_stub_apk_target_name) {
227      forward_variables_from(invoker,
228                             [
229                               "static_library_name",
230                               "static_library_version",
231                               "override_target_sdk",
232                             ])
233      package_info_from_target = invoker.apk_or_bundle_target
234      stub_output = invoker.stub_output
235    }
236  }
237
238  action_with_pydeps(target_name) {
239    script = "//build/android/gyp/system_image_apks.py"
240    deps = [ invoker.apk_or_bundle_target ]
241    inputs = [ invoker.input_apk_or_bundle ]
242    if (defined(invoker.stub_output)) {
243      public_deps = [ ":$_stub_apk_target_name" ]
244    }
245    outputs = [ invoker.output ]
246    args = [
247      "--input",
248      rebase_path(invoker.input_apk_or_bundle, root_out_dir),
249      "--output",
250      rebase_path(invoker.output, root_out_dir),
251    ]
252
253    _is_bundle =
254        filter_exclude([ invoker.input_apk_or_bundle ], [ "*.aab" ]) == []
255
256    if (_is_bundle) {
257      _wrapper_path = "$root_out_dir/bin/" +
258                      get_label_info(invoker.apk_or_bundle_target, "name")
259      args += [
260        "--bundle-wrapper",
261        rebase_path(_wrapper_path, root_out_dir),
262      ]
263      inputs += [ _wrapper_path ]
264      deps += [ "//build/android:apk_operations_py" ]
265      if (defined(invoker.fuse_apk) && invoker.fuse_apk) {
266        args += [ "--fuse-apk" ]
267      }
268    }
269  }
270}
271