blob: 6bf1b4903183ed7b1f158f318e51778533ea2317 [file] [log] [blame]
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001#!/usr/bin/env python
2#
Daniel Norman03747412022-02-25 10:38:37 -08003# Copyright (C) 2022 The Android Open Source Project
Bill Peckhame9eb5f92019-02-01 15:52:10 -08004#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
Daniel Norman4cc9df62019-07-18 10:11:07 -070016#
17"""This script merges two partial target files packages.
Bill Peckhame9eb5f92019-02-01 15:52:10 -080018
Daniel Normandbbf5a32020-10-22 16:03:32 -070019One input package contains framework files, and the other contains vendor files.
Bill Peckhame9eb5f92019-02-01 15:52:10 -080020
Daniel Normandbbf5a32020-10-22 16:03:32 -070021This script produces a complete, merged target files package:
22 - This package can be used to generate a flashable IMG package.
23 See --output-img.
24 - This package can be used to generate an OTA package. See --output-ota.
25 - The merged package is checked for compatibility between the two inputs.
26
27Usage: merge_target_files [args]
Bill Peckhame9eb5f92019-02-01 15:52:10 -080028
Dennis Song5bfa43e2023-03-30 18:28:00 +080029 --framework-target-files framework-target-files-package
Daniel Normand5d70ea2019-06-05 15:13:43 -070030 The input target files package containing framework bits. This is a zip
Dennis Song5bfa43e2023-03-30 18:28:00 +080031 archive or a directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -080032
Daniel Normand5d70ea2019-06-05 15:13:43 -070033 --framework-item-list framework-item-list-file
Daniel Norman5f476772022-03-02 15:46:34 -080034 The optional path to a newline-separated config file of items that
35 are extracted as-is from the framework target files package.
Daniel Norman2c99c5b2019-03-07 13:01:48 -080036
Daniel Normand5d70ea2019-06-05 15:13:43 -070037 --framework-misc-info-keys framework-misc-info-keys-file
Daniel Norman5f476772022-03-02 15:46:34 -080038 The optional path to a newline-separated config file of keys to
39 extract from the framework META/misc_info.txt file.
Daniel Norman2c99c5b2019-03-07 13:01:48 -080040
Dennis Song5bfa43e2023-03-30 18:28:00 +080041 --vendor-target-files vendor-target-files-package
Daniel Normand5d70ea2019-06-05 15:13:43 -070042 The input target files package containing vendor bits. This is a zip
Dennis Song5bfa43e2023-03-30 18:28:00 +080043 archive or a directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -080044
Daniel Normand5d70ea2019-06-05 15:13:43 -070045 --vendor-item-list vendor-item-list-file
Daniel Norman5f476772022-03-02 15:46:34 -080046 The optional path to a newline-separated config file of items that
47 are extracted as-is from the vendor target files package.
Daniel Norman2c99c5b2019-03-07 13:01:48 -080048
Bill Peckhame9eb5f92019-02-01 15:52:10 -080049 --output-target-files output-target-files-package
Daniel Normanfdb38812019-04-15 09:47:24 -070050 If provided, the output merged target files package. Also a zip archive.
51
52 --output-dir output-directory
53 If provided, the destination directory for saving merged files. Requires
54 the --output-item-list flag.
55 Can be provided alongside --output-target-files, or by itself.
56
57 --output-item-list output-item-list-file.
58 The optional path to a newline-separated config file that specifies the
59 file patterns to copy into the --output-dir. Required if providing
60 the --output-dir flag.
Daniel Normana4911da2019-03-15 14:36:21 -070061
Daniel Norman3b64ce12019-04-16 16:11:35 -070062 --output-ota output-ota-package
63 The output ota package. This is a zip archive. Use of this flag may
64 require passing the --path common flag; see common.py.
65
Daniel Norman1bd2a1d2019-04-18 12:32:18 -070066 --output-img output-img-package
67 The output img package, suitable for use with 'fastboot update'. Use of
68 this flag may require passing the --path common flag; see common.py.
69
Daniel Normanf0318252019-04-15 11:34:56 -070070 --output-super-empty output-super-empty-image
71 If provided, creates a super_empty.img file from the merged target
72 files package and saves it at this path.
73
Daniel Normana4911da2019-03-15 14:36:21 -070074 --rebuild_recovery
Po Hua6c59122022-02-16 08:41:29 +000075 Copy the recovery image used by non-A/B devices, used when
76 regenerating vendor images with --rebuild-sepolicy.
Bill Peckham364c1cc2019-03-29 18:27:23 -070077
Daniel Normanb0c75912020-09-24 14:30:21 -070078 --allow-duplicate-apkapex-keys
79 If provided, duplicate APK/APEX keys are ignored and the value from the
80 framework is used.
81
Daniel Norman571e1822021-06-25 17:18:25 -070082 --rebuild-sepolicy
83 If provided, rebuilds odm.img or vendor.img to include merged sepolicy
84 files. If odm is present then odm is preferred.
85
86 --vendor-otatools otatools.zip
87 If provided, use this otatools.zip when recompiling the odm or vendor
88 image to include sepolicy.
89
Bill Peckham364c1cc2019-03-29 18:27:23 -070090 --keep-tmp
91 Keep tempoary files for debugging purposes.
Jose Galmes9c8f6eb2021-07-21 09:34:08 -070092
Dennis Song4aae62e2023-10-02 04:31:34 +000093 --avb-resolve-rollback-index-location-conflict
94 If provided, resolve the conflict AVB rollback index location when
95 necessary.
96
Jose Galmes9c8f6eb2021-07-21 09:34:08 -070097 The following only apply when using the VSDK to perform dexopt on vendor apps:
98
99 --framework-dexpreopt-config
100 If provided, the location of framwework's dexpreopt_config.zip.
101
102 --framework-dexpreopt-tools
103 if provided, the location of framework's dexpreopt_tools.zip.
104
105 --vendor-dexpreopt-config
106 If provided, the location of vendor's dexpreopt_config.zip.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800107"""
108
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800109import logging
110import os
Daniel Normanfdb38812019-04-15 09:47:24 -0700111import shutil
Bill Peckham540d91a2019-04-25 14:18:16 -0700112import subprocess
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800113import sys
Daniel Norman5f476772022-03-02 15:46:34 -0800114import zipfile
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800115
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800116import add_img_to_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700117import build_image
Daniel Normanf0318252019-04-15 11:34:56 -0700118import build_super_image
119import common
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700120import img_from_target_files
Daniel Norman2465fc82022-03-02 12:01:20 -0800121import merge_compatibility_checks
122import merge_dexopt
123import merge_meta
124import merge_utils
Daniel Norman3b64ce12019-04-16 16:11:35 -0700125import ota_from_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700126
Daniel Normana84d13b2022-02-17 14:16:40 -0800127from common import ExternalError
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800128
129logger = logging.getLogger(__name__)
Tao Bao2ad4b822019-06-27 16:52:12 -0700130
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800131OPTIONS = common.OPTIONS
Bill Peckhamcb848172020-04-03 12:50:47 -0700132# Always turn on verbose logging.
133OPTIONS.verbose = True
Daniel Normand5d70ea2019-06-05 15:13:43 -0700134OPTIONS.framework_target_files = None
Daniel Norman5f476772022-03-02 15:46:34 -0800135OPTIONS.framework_item_list = []
136OPTIONS.framework_misc_info_keys = []
Daniel Normand5d70ea2019-06-05 15:13:43 -0700137OPTIONS.vendor_target_files = None
Daniel Norman5f476772022-03-02 15:46:34 -0800138OPTIONS.vendor_item_list = []
Bill Peckhamf753e152019-02-19 18:02:46 -0800139OPTIONS.output_target_files = None
Daniel Normanfdb38812019-04-15 09:47:24 -0700140OPTIONS.output_dir = None
Daniel Norman5f476772022-03-02 15:46:34 -0800141OPTIONS.output_item_list = []
Daniel Norman3b64ce12019-04-16 16:11:35 -0700142OPTIONS.output_ota = None
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700143OPTIONS.output_img = None
Daniel Normanf0318252019-04-15 11:34:56 -0700144OPTIONS.output_super_empty = None
Daniel Normana4911da2019-03-15 14:36:21 -0700145OPTIONS.rebuild_recovery = False
Daniel Normanb0c75912020-09-24 14:30:21 -0700146# TODO(b/150582573): Remove this option.
147OPTIONS.allow_duplicate_apkapex_keys = False
Daniel Norman571e1822021-06-25 17:18:25 -0700148OPTIONS.vendor_otatools = None
149OPTIONS.rebuild_sepolicy = False
Bill Peckhamf753e152019-02-19 18:02:46 -0800150OPTIONS.keep_tmp = False
Dennis Song4aae62e2023-10-02 04:31:34 +0000151OPTIONS.avb_resolve_rollback_index_location_conflict = False
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700152OPTIONS.framework_dexpreopt_config = None
153OPTIONS.framework_dexpreopt_tools = None
154OPTIONS.vendor_dexpreopt_config = None
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800155
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800156
jiangxu52bd42732022-04-11 16:02:48 +0800157def move_only_exists(source, destination):
158 """Judge whether the file exists and then move the file."""
159
160 if os.path.exists(source):
161 shutil.move(source, destination)
162
163
Dennis Song16d6fe02023-02-23 23:37:42 +0800164def remove_file_if_exists(file_name):
165 """Remove the file if it exists and skip otherwise."""
166
167 try:
168 os.remove(file_name)
169 except FileNotFoundError:
170 pass
171
172
Po Hu09de3142023-04-26 08:49:42 +0800173def include_extra_in_list(item_list):
174 """
175 1. Include all `META/*` files in the item list.
Dennis Song09f5c5d2023-04-14 11:43:04 +0800176
177 To ensure that `AddImagesToTargetFiles` can still be used with vendor item
178 list that do not specify all of the required META/ files, those files should
179 be included by default. This preserves the backward compatibility of
180 `rebuild_image_with_sepolicy`.
Po Hu09de3142023-04-26 08:49:42 +0800181
182 2. Include `SYSTEM/build.prop` file in the item list.
183
184 To ensure that `AddImagesToTargetFiles` for GRF vendor images, can still
185 access SYSTEM/build.prop to pass GetPartitionFingerprint check in BuildInfo
186 constructor.
Dennis Song09f5c5d2023-04-14 11:43:04 +0800187 """
188 if not item_list:
189 return None
Po Hu09de3142023-04-26 08:49:42 +0800190 return list(item_list) + ['META/*'] + ['SYSTEM/build.prop']
Dennis Song09f5c5d2023-04-14 11:43:04 +0800191
192
Daniel Norman03747412022-02-25 10:38:37 -0800193def create_merged_package(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -0700194 """Merges two target files packages into one target files structure.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800195
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900196 Returns:
197 Path to merged package under temp directory.
198 """
Daniel Normandbbf5a32020-10-22 16:03:32 -0700199 # Extract "as is" items from the input framework and vendor partial target
Dennis Song5bfa43e2023-03-30 18:28:00 +0800200 # files packages directly into the output temporary directory, since these
201 # items do not need special case processing.
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800202
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800203 output_target_files_temp_dir = os.path.join(temp_dir, 'output')
Dennis Song5bfa43e2023-03-30 18:28:00 +0800204 merge_utils.CollectTargetFiles(
205 input_zipfile_or_dir=OPTIONS.framework_target_files,
Daniel Norman03747412022-02-25 10:38:37 -0800206 output_dir=output_target_files_temp_dir,
Dennis Song5bfa43e2023-03-30 18:28:00 +0800207 item_list=OPTIONS.framework_item_list)
208 merge_utils.CollectTargetFiles(
209 input_zipfile_or_dir=OPTIONS.vendor_target_files,
Daniel Norman03747412022-02-25 10:38:37 -0800210 output_dir=output_target_files_temp_dir,
Dennis Song5bfa43e2023-03-30 18:28:00 +0800211 item_list=OPTIONS.vendor_item_list)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800212
Daniel Normandbbf5a32020-10-22 16:03:32 -0700213 # Perform special case processing on META/* items.
214 # After this function completes successfully, all the files we need to create
215 # the output target files package are in place.
Daniel Norman2465fc82022-03-02 12:01:20 -0800216 merge_meta.MergeMetaFiles(
Dennis Song36ce3262023-09-13 06:53:00 +0000217 temp_dir=temp_dir,
218 merged_dir=output_target_files_temp_dir,
219 framework_partitions=OPTIONS.framework_partition_set)
Daniel Norman03747412022-02-25 10:38:37 -0800220
Daniel Norman2465fc82022-03-02 12:01:20 -0800221 merge_dexopt.MergeDexopt(
Daniel Norman03747412022-02-25 10:38:37 -0800222 temp_dir=temp_dir, output_target_files_dir=output_target_files_temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800223
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900224 return output_target_files_temp_dir
225
226
Daniel Norman03747412022-02-25 10:38:37 -0800227def generate_missing_images(target_files_dir):
228 """Generate any missing images from target files."""
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900229
230 # Regenerate IMAGES in the target directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800231
Daniel Normandbbf5a32020-10-22 16:03:32 -0700232 add_img_args = [
233 '--verbose',
234 '--add_missing',
235 ]
Daniel Norman03747412022-02-25 10:38:37 -0800236 if OPTIONS.rebuild_recovery:
Daniel Normana4911da2019-03-15 14:36:21 -0700237 add_img_args.append('--rebuild_recovery')
Dennis Song4aae62e2023-10-02 04:31:34 +0000238 if OPTIONS.avb_resolve_rollback_index_location_conflict:
239 add_img_args.append('--avb_resolve_rollback_index_location_conflict')
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900240 add_img_args.append(target_files_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800241
242 add_img_to_target_files.main(add_img_args)
243
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900244
Daniel Norman03747412022-02-25 10:38:37 -0800245def rebuild_image_with_sepolicy(target_files_dir):
Daniel Norman571e1822021-06-25 17:18:25 -0700246 """Rebuilds odm.img or vendor.img to include merged sepolicy files.
247
248 If odm is present then odm is preferred -- otherwise vendor is used.
Daniel Norman571e1822021-06-25 17:18:25 -0700249 """
250 partition = 'vendor'
Dennis Song16d6fe02023-02-23 23:37:42 +0800251 if os.path.exists(os.path.join(target_files_dir, 'ODM')):
Daniel Norman571e1822021-06-25 17:18:25 -0700252 partition = 'odm'
253 partition_img = '{}.img'.format(partition)
Po Hua6c59122022-02-16 08:41:29 +0000254 partition_map = '{}.map'.format(partition)
Daniel Norman571e1822021-06-25 17:18:25 -0700255
256 logger.info('Recompiling %s using the merged sepolicy files.', partition_img)
257
258 # Copy the combined SEPolicy file and framework hashes to the image that is
259 # being rebuilt.
260 def copy_selinux_file(input_path, output_filename):
Po Hu0e4403e2021-07-06 17:05:56 +0800261 input_filename = os.path.join(target_files_dir, input_path)
262 if not os.path.exists(input_filename):
Dennis Song5bfa43e2023-03-30 18:28:00 +0800263 input_filename = input_filename.replace('SYSTEM_EXT/',
264 'SYSTEM/system_ext/') \
Po Hu0e4403e2021-07-06 17:05:56 +0800265 .replace('PRODUCT/', 'SYSTEM/product/')
266 if not os.path.exists(input_filename):
267 logger.info('Skipping copy_selinux_file for %s', input_filename)
268 return
Daniel Norman571e1822021-06-25 17:18:25 -0700269 shutil.copy(
Po Hu0e4403e2021-07-06 17:05:56 +0800270 input_filename,
Daniel Norman571e1822021-06-25 17:18:25 -0700271 os.path.join(target_files_dir, partition.upper(), 'etc/selinux',
272 output_filename))
273
274 copy_selinux_file('META/combined_sepolicy', 'precompiled_sepolicy')
275 copy_selinux_file('SYSTEM/etc/selinux/plat_sepolicy_and_mapping.sha256',
276 'precompiled_sepolicy.plat_sepolicy_and_mapping.sha256')
277 copy_selinux_file(
278 'SYSTEM_EXT/etc/selinux/system_ext_sepolicy_and_mapping.sha256',
279 'precompiled_sepolicy.system_ext_sepolicy_and_mapping.sha256')
280 copy_selinux_file('PRODUCT/etc/selinux/product_sepolicy_and_mapping.sha256',
281 'precompiled_sepolicy.product_sepolicy_and_mapping.sha256')
282
Daniel Norman03747412022-02-25 10:38:37 -0800283 if not OPTIONS.vendor_otatools:
Daniel Norman571e1822021-06-25 17:18:25 -0700284 # Remove the partition from the merged target-files archive. It will be
Daniel Norman03747412022-02-25 10:38:37 -0800285 # rebuilt later automatically by generate_missing_images().
Dennis Song16d6fe02023-02-23 23:37:42 +0800286 remove_file_if_exists(
287 os.path.join(target_files_dir, 'IMAGES', partition_img))
Daniel Norman03747412022-02-25 10:38:37 -0800288 return
Daniel Norman571e1822021-06-25 17:18:25 -0700289
Daniel Norman03747412022-02-25 10:38:37 -0800290 # TODO(b/192253131): Remove the need for vendor_otatools by fixing
291 # backwards-compatibility issues when compiling images across releases.
292 if not OPTIONS.vendor_target_files:
293 raise ValueError(
294 'Expected vendor_target_files if vendor_otatools is not None.')
295 logger.info(
296 '%s recompilation will be performed using the vendor otatools.zip',
297 partition_img)
Daniel Norman571e1822021-06-25 17:18:25 -0700298
Daniel Norman03747412022-02-25 10:38:37 -0800299 # Unzip the vendor build's otatools.zip and target-files archive.
300 vendor_otatools_dir = common.MakeTempDir(
301 prefix='merge_target_files_vendor_otatools_')
302 vendor_target_files_dir = common.MakeTempDir(
303 prefix='merge_target_files_vendor_target_files_')
304 common.UnzipToDir(OPTIONS.vendor_otatools, vendor_otatools_dir)
Dennis Song5bfa43e2023-03-30 18:28:00 +0800305 merge_utils.CollectTargetFiles(
306 input_zipfile_or_dir=OPTIONS.vendor_target_files,
307 output_dir=vendor_target_files_dir,
Po Hu09de3142023-04-26 08:49:42 +0800308 item_list=include_extra_in_list(OPTIONS.vendor_item_list))
Daniel Norman571e1822021-06-25 17:18:25 -0700309
Daniel Norman03747412022-02-25 10:38:37 -0800310 # Copy the partition contents from the merged target-files archive to the
311 # vendor target-files archive.
312 shutil.rmtree(os.path.join(vendor_target_files_dir, partition.upper()))
313 shutil.copytree(
314 os.path.join(target_files_dir, partition.upper()),
315 os.path.join(vendor_target_files_dir, partition.upper()),
316 symlinks=True)
Daniel Norman571e1822021-06-25 17:18:25 -0700317
Daniel Norman03747412022-02-25 10:38:37 -0800318 # Delete then rebuild the partition.
Dennis Song16d6fe02023-02-23 23:37:42 +0800319 remove_file_if_exists(
320 os.path.join(vendor_target_files_dir, 'IMAGES', partition_img))
Daniel Norman03747412022-02-25 10:38:37 -0800321 rebuild_partition_command = [
322 os.path.join(vendor_otatools_dir, 'bin', 'add_img_to_target_files'),
323 '--verbose',
324 '--add_missing',
325 ]
326 if OPTIONS.rebuild_recovery:
327 rebuild_partition_command.append('--rebuild_recovery')
328 rebuild_partition_command.append(vendor_target_files_dir)
329 logger.info('Recompiling %s: %s', partition_img,
330 ' '.join(rebuild_partition_command))
331 common.RunAndCheckOutput(rebuild_partition_command, verbose=True)
Po Hua6c59122022-02-16 08:41:29 +0000332
Daniel Norman03747412022-02-25 10:38:37 -0800333 # Move the newly-created image to the merged target files dir.
334 if not os.path.exists(os.path.join(target_files_dir, 'IMAGES')):
335 os.makedirs(os.path.join(target_files_dir, 'IMAGES'))
336 shutil.move(
337 os.path.join(vendor_target_files_dir, 'IMAGES', partition_img),
338 os.path.join(target_files_dir, 'IMAGES', partition_img))
Dennis Song5bfa43e2023-03-30 18:28:00 +0800339 move_only_exists(
340 os.path.join(vendor_target_files_dir, 'IMAGES', partition_map),
341 os.path.join(target_files_dir, 'IMAGES', partition_map))
Po Hua6c59122022-02-16 08:41:29 +0000342
Daniel Norman03747412022-02-25 10:38:37 -0800343 def copy_recovery_file(filename):
344 for subdir in ('VENDOR', 'SYSTEM/vendor'):
345 source = os.path.join(vendor_target_files_dir, subdir, filename)
346 if os.path.exists(source):
347 dest = os.path.join(target_files_dir, subdir, filename)
348 shutil.copy(source, dest)
349 return
350 logger.info('Skipping copy_recovery_file for %s, file not found', filename)
351
352 if OPTIONS.rebuild_recovery:
353 copy_recovery_file('etc/recovery.img')
354 copy_recovery_file('bin/install-recovery.sh')
355 copy_recovery_file('recovery-from-boot.p')
Daniel Norman571e1822021-06-25 17:18:25 -0700356
357
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900358def generate_super_empty_image(target_dir, output_super_empty):
Tao Bao2ad4b822019-06-27 16:52:12 -0700359 """Generates super_empty image from target package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900360
361 Args:
362 target_dir: Path to the target file package which contains misc_info.txt for
363 detailed information for super image.
364 output_super_empty: If provided, copies a super_empty.img file from the
365 target files package to this path.
366 """
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700367 # Create super_empty.img using the merged misc_info.txt.
368
Daniel Norman4cc9df62019-07-18 10:11:07 -0700369 misc_info_txt = os.path.join(target_dir, 'META', 'misc_info.txt')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700370
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +0900371 use_dynamic_partitions = common.LoadDictionaryFromFile(misc_info_txt).get(
372 'use_dynamic_partitions')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700373
374 if use_dynamic_partitions != 'true' and output_super_empty:
375 raise ValueError(
376 'Building super_empty.img requires use_dynamic_partitions=true.')
377 elif use_dynamic_partitions == 'true':
Daniel Norman4cc9df62019-07-18 10:11:07 -0700378 super_empty_img = os.path.join(target_dir, 'IMAGES', 'super_empty.img')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700379 build_super_image_args = [
380 misc_info_txt,
381 super_empty_img,
382 ]
383 build_super_image.main(build_super_image_args)
384
385 # Copy super_empty.img to the user-provided output_super_empty location.
386 if output_super_empty:
387 shutil.copyfile(super_empty_img, output_super_empty)
388
Daniel Normanb8a2f9d2019-04-24 12:55:51 -0700389
Daniel Norman03747412022-02-25 10:38:37 -0800390def create_target_files_archive(output_zip, source_dir, temp_dir):
391 """Creates a target_files zip archive from the input source dir.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900392
393 Args:
Daniel Norman03747412022-02-25 10:38:37 -0800394 output_zip: The name of the zip archive target files package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900395 source_dir: The target directory contains package to be archived.
396 temp_dir: Path to temporary directory for any intermediate files.
397 """
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800398 output_target_files_list = os.path.join(temp_dir, 'output.list')
Daniel Norman4cc9df62019-07-18 10:11:07 -0700399 output_target_files_meta_dir = os.path.join(source_dir, 'META')
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800400
Daniel Normandbbf5a32020-10-22 16:03:32 -0700401 def files_from_path(target_path, extra_args=None):
402 """Gets files under the given path and return a sorted list."""
403 find_command = ['find', target_path] + (extra_args or [])
404 find_process = common.Run(
405 find_command, stdout=subprocess.PIPE, verbose=False)
406 return common.RunAndCheckOutput(['sort'],
407 stdin=find_process.stdout,
408 verbose=False)
409
Daniel Norman03747412022-02-25 10:38:37 -0800410 # META content appears first in the zip. This is done by the
411 # standard build system for optimized extraction of those files,
412 # so we do the same step for merged target_files.zips here too.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900413 meta_content = files_from_path(output_target_files_meta_dir)
Daniel Norman4cc9df62019-07-18 10:11:07 -0700414 other_content = files_from_path(
415 source_dir,
416 ['-path', output_target_files_meta_dir, '-prune', '-o', '-print'])
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800417
Tao Bao2ad4b822019-06-27 16:52:12 -0700418 with open(output_target_files_list, 'w') as f:
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800419 f.write(meta_content)
420 f.write(other_content)
421
422 command = [
Bill Peckhamf753e152019-02-19 18:02:46 -0800423 'soong_zip',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800424 '-d',
Daniel Normane5b134a2019-04-17 14:54:06 -0700425 '-o',
Daniel Norman03747412022-02-25 10:38:37 -0800426 os.path.abspath(output_zip),
Daniel Normane5b134a2019-04-17 14:54:06 -0700427 '-C',
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900428 source_dir,
Daniel Normaneaf5c1d2021-02-09 11:01:42 -0800429 '-r',
Daniel Normane5b134a2019-04-17 14:54:06 -0700430 output_target_files_list,
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800431 ]
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900432
Daniel Norman03747412022-02-25 10:38:37 -0800433 logger.info('creating %s', output_zip)
Daniel Normaneaf5c1d2021-02-09 11:01:42 -0800434 common.RunAndCheckOutput(command, verbose=True)
Daniel Norman03747412022-02-25 10:38:37 -0800435 logger.info('finished creating %s', output_zip)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900436
437
Daniel Norman03747412022-02-25 10:38:37 -0800438def merge_target_files(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -0700439 """Merges two target files packages together.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900440
Daniel Norman03747412022-02-25 10:38:37 -0800441 This function uses framework and vendor target files packages as input,
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900442 performs various file extractions, special case processing, and finally
443 creates a merged zip archive as output.
444
445 Args:
446 temp_dir: The name of a directory we use when we extract items from the
447 input target files packages, and also a scratch directory that we use for
448 temporary files.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900449 """
450
451 logger.info('starting: merge framework %s and vendor %s into output %s',
Daniel Norman03747412022-02-25 10:38:37 -0800452 OPTIONS.framework_target_files, OPTIONS.vendor_target_files,
453 OPTIONS.output_target_files)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900454
Daniel Norman03747412022-02-25 10:38:37 -0800455 output_target_files_temp_dir = create_merged_package(temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900456
Daniel Norman21c34f72020-11-11 17:25:50 -0800457 partition_map = common.PartitionMapFromTargetFiles(
458 output_target_files_temp_dir)
459
Daniel Norman2465fc82022-03-02 12:01:20 -0800460 compatibility_errors = merge_compatibility_checks.CheckCompatibility(
461 target_files_dir=output_target_files_temp_dir,
462 partition_map=partition_map)
463 if compatibility_errors:
464 for error in compatibility_errors:
465 logger.error(error)
466 raise ExternalError(
467 'Found incompatibilities in the merged target files package.')
Daniel Normand3351562020-10-29 12:33:11 -0700468
Daniel Norman571e1822021-06-25 17:18:25 -0700469 # Include the compiled policy in an image if requested.
Daniel Norman03747412022-02-25 10:38:37 -0800470 if OPTIONS.rebuild_sepolicy:
471 rebuild_image_with_sepolicy(output_target_files_temp_dir)
Daniel Norman48603ff2021-02-22 15:15:24 -0800472
Daniel Norman03747412022-02-25 10:38:37 -0800473 generate_missing_images(output_target_files_temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900474
Daniel Norman03747412022-02-25 10:38:37 -0800475 generate_super_empty_image(output_target_files_temp_dir,
476 OPTIONS.output_super_empty)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900477
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900478 # Finally, create the output target files zip archive and/or copy the
479 # output items to the output target files directory.
480
Daniel Norman03747412022-02-25 10:38:37 -0800481 if OPTIONS.output_dir:
Daniel Norman2465fc82022-03-02 12:01:20 -0800482 merge_utils.CopyItems(output_target_files_temp_dir, OPTIONS.output_dir,
483 OPTIONS.output_item_list)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900484
Daniel Norman03747412022-02-25 10:38:37 -0800485 if not OPTIONS.output_target_files:
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900486 return
487
Daniel Norman03747412022-02-25 10:38:37 -0800488 create_target_files_archive(OPTIONS.output_target_files,
489 output_target_files_temp_dir, temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800490
Daniel Norman74eb74b2019-09-18 14:01:48 -0700491 # Create the IMG package from the merged target files package.
Daniel Norman03747412022-02-25 10:38:37 -0800492 if OPTIONS.output_img:
493 img_from_target_files.main(
494 [OPTIONS.output_target_files, OPTIONS.output_img])
Daniel Norman74eb74b2019-09-18 14:01:48 -0700495
Daniel Norman3b64ce12019-04-16 16:11:35 -0700496 # Create the OTA package from the merged target files package.
497
Daniel Norman03747412022-02-25 10:38:37 -0800498 if OPTIONS.output_ota:
499 ota_from_target_files.main(
500 [OPTIONS.output_target_files, OPTIONS.output_ota])
Daniel Norman3b64ce12019-04-16 16:11:35 -0700501
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700502
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800503def main():
504 """The main function.
505
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800506 Process command line arguments, then call merge_target_files to
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800507 perform the heavy lifting.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800508 """
509
510 common.InitLogging()
511
Bill Peckhamf753e152019-02-19 18:02:46 -0800512 def option_handler(o, a):
513 if o == '--system-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700514 logger.warning(
515 '--system-target-files has been renamed to --framework-target-files')
516 OPTIONS.framework_target_files = a
517 elif o == '--framework-target-files':
518 OPTIONS.framework_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800519 elif o == '--system-item-list':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700520 logger.warning(
521 '--system-item-list has been renamed to --framework-item-list')
522 OPTIONS.framework_item_list = a
523 elif o == '--framework-item-list':
524 OPTIONS.framework_item_list = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800525 elif o == '--system-misc-info-keys':
Daniel Norman4cc9df62019-07-18 10:11:07 -0700526 logger.warning('--system-misc-info-keys has been renamed to '
527 '--framework-misc-info-keys')
Daniel Normand5d70ea2019-06-05 15:13:43 -0700528 OPTIONS.framework_misc_info_keys = a
529 elif o == '--framework-misc-info-keys':
530 OPTIONS.framework_misc_info_keys = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800531 elif o == '--other-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700532 logger.warning(
533 '--other-target-files has been renamed to --vendor-target-files')
534 OPTIONS.vendor_target_files = a
535 elif o == '--vendor-target-files':
536 OPTIONS.vendor_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800537 elif o == '--other-item-list':
Daniel Norman2d7989a2021-04-05 17:40:47 +0000538 logger.warning('--other-item-list has been renamed to --vendor-item-list')
Daniel Normand5d70ea2019-06-05 15:13:43 -0700539 OPTIONS.vendor_item_list = a
540 elif o == '--vendor-item-list':
541 OPTIONS.vendor_item_list = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800542 elif o == '--output-target-files':
543 OPTIONS.output_target_files = a
Daniel Normanfdb38812019-04-15 09:47:24 -0700544 elif o == '--output-dir':
545 OPTIONS.output_dir = a
546 elif o == '--output-item-list':
547 OPTIONS.output_item_list = a
Daniel Norman3b64ce12019-04-16 16:11:35 -0700548 elif o == '--output-ota':
549 OPTIONS.output_ota = a
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700550 elif o == '--output-img':
551 OPTIONS.output_img = a
Daniel Normanf0318252019-04-15 11:34:56 -0700552 elif o == '--output-super-empty':
553 OPTIONS.output_super_empty = a
Daniel Norman2465fc82022-03-02 12:01:20 -0800554 elif o == '--rebuild_recovery' or o == '--rebuild-recovery':
Daniel Normana4911da2019-03-15 14:36:21 -0700555 OPTIONS.rebuild_recovery = True
Daniel Normanb0c75912020-09-24 14:30:21 -0700556 elif o == '--allow-duplicate-apkapex-keys':
557 OPTIONS.allow_duplicate_apkapex_keys = True
Daniel Norman571e1822021-06-25 17:18:25 -0700558 elif o == '--vendor-otatools':
559 OPTIONS.vendor_otatools = a
560 elif o == '--rebuild-sepolicy':
561 OPTIONS.rebuild_sepolicy = True
Bill Peckham364c1cc2019-03-29 18:27:23 -0700562 elif o == '--keep-tmp':
Bill Peckhamf753e152019-02-19 18:02:46 -0800563 OPTIONS.keep_tmp = True
Dennis Song4aae62e2023-10-02 04:31:34 +0000564 elif o == '--avb-resolve-rollback-index-location-conflict':
565 OPTIONS.avb_resolve_rollback_index_location_conflict = True
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700566 elif o == '--framework-dexpreopt-config':
567 OPTIONS.framework_dexpreopt_config = a
568 elif o == '--framework-dexpreopt-tools':
569 OPTIONS.framework_dexpreopt_tools = a
570 elif o == '--vendor-dexpreopt-config':
571 OPTIONS.vendor_dexpreopt_config = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800572 else:
573 return False
574 return True
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800575
Bill Peckhamf753e152019-02-19 18:02:46 -0800576 args = common.ParseOptions(
Daniel Normane5b134a2019-04-17 14:54:06 -0700577 sys.argv[1:],
578 __doc__,
Bill Peckhamf753e152019-02-19 18:02:46 -0800579 extra_long_opts=[
580 'system-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700581 'framework-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800582 'system-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700583 'framework-item-list=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800584 'system-misc-info-keys=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700585 'framework-misc-info-keys=',
Bill Peckhamf753e152019-02-19 18:02:46 -0800586 'other-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700587 'vendor-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800588 'other-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700589 'vendor-item-list=',
Bill Peckhamf753e152019-02-19 18:02:46 -0800590 'output-target-files=',
Daniel Normanfdb38812019-04-15 09:47:24 -0700591 'output-dir=',
592 'output-item-list=',
Daniel Norman3b64ce12019-04-16 16:11:35 -0700593 'output-ota=',
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700594 'output-img=',
Daniel Normanf0318252019-04-15 11:34:56 -0700595 'output-super-empty=',
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700596 'framework-dexpreopt-config=',
597 'framework-dexpreopt-tools=',
598 'vendor-dexpreopt-config=',
Daniel Normana4911da2019-03-15 14:36:21 -0700599 'rebuild_recovery',
Daniel Norman2465fc82022-03-02 12:01:20 -0800600 'rebuild-recovery',
Daniel Normanb0c75912020-09-24 14:30:21 -0700601 'allow-duplicate-apkapex-keys',
Daniel Norman571e1822021-06-25 17:18:25 -0700602 'vendor-otatools=',
603 'rebuild-sepolicy',
Bill Peckham364c1cc2019-03-29 18:27:23 -0700604 'keep-tmp',
Dennis Song4aae62e2023-10-02 04:31:34 +0000605 'avb-resolve-rollback-index-location-conflict',
Bill Peckhamf753e152019-02-19 18:02:46 -0800606 ],
607 extra_option_handler=option_handler)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800608
Tao Bao2ad4b822019-06-27 16:52:12 -0700609 # pylint: disable=too-many-boolean-expressions
Daniel Normand5d70ea2019-06-05 15:13:43 -0700610 if (args or OPTIONS.framework_target_files is None or
611 OPTIONS.vendor_target_files is None or
Daniel Normane5b134a2019-04-17 14:54:06 -0700612 (OPTIONS.output_target_files is None and OPTIONS.output_dir is None) or
Daniel Norman5f476772022-03-02 15:46:34 -0800613 (OPTIONS.output_dir is not None and not OPTIONS.output_item_list) or
Po Hua6c59122022-02-16 08:41:29 +0000614 (OPTIONS.rebuild_recovery and not OPTIONS.rebuild_sepolicy)):
Bill Peckhamf753e152019-02-19 18:02:46 -0800615 common.Usage(__doc__)
Bill Peckham889b0c62019-02-21 18:53:37 -0800616 sys.exit(1)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800617
Dennis Song5bfa43e2023-03-30 18:28:00 +0800618 framework_namelist = merge_utils.GetTargetFilesItems(
619 OPTIONS.framework_target_files)
620 vendor_namelist = merge_utils.GetTargetFilesItems(
621 OPTIONS.vendor_target_files)
Daniel Norman5f476772022-03-02 15:46:34 -0800622
Daniel Normand5d70ea2019-06-05 15:13:43 -0700623 if OPTIONS.framework_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800624 OPTIONS.framework_item_list = common.LoadListFromFile(
625 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800626 else:
Daniel Norman5f476772022-03-02 15:46:34 -0800627 OPTIONS.framework_item_list = merge_utils.InferItemList(
628 input_namelist=framework_namelist, framework=True)
Daniel Norman2465fc82022-03-02 12:01:20 -0800629 OPTIONS.framework_partition_set = merge_utils.ItemListToPartitionSet(
Daniel Norman03747412022-02-25 10:38:37 -0800630 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800631
Daniel Normand5d70ea2019-06-05 15:13:43 -0700632 if OPTIONS.framework_misc_info_keys:
Daniel Norman03747412022-02-25 10:38:37 -0800633 OPTIONS.framework_misc_info_keys = common.LoadListFromFile(
Daniel Normand5d70ea2019-06-05 15:13:43 -0700634 OPTIONS.framework_misc_info_keys)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800635 else:
Daniel Norman5f476772022-03-02 15:46:34 -0800636 OPTIONS.framework_misc_info_keys = merge_utils.InferFrameworkMiscInfoKeys(
637 input_namelist=framework_namelist)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800638
Daniel Normand5d70ea2019-06-05 15:13:43 -0700639 if OPTIONS.vendor_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800640 OPTIONS.vendor_item_list = common.LoadListFromFile(OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800641 else:
Daniel Norman5f476772022-03-02 15:46:34 -0800642 OPTIONS.vendor_item_list = merge_utils.InferItemList(
643 input_namelist=vendor_namelist, framework=False)
Daniel Norman2465fc82022-03-02 12:01:20 -0800644 OPTIONS.vendor_partition_set = merge_utils.ItemListToPartitionSet(
Daniel Norman03747412022-02-25 10:38:37 -0800645 OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800646
Daniel Normanfdb38812019-04-15 09:47:24 -0700647 if OPTIONS.output_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800648 OPTIONS.output_item_list = common.LoadListFromFile(OPTIONS.output_item_list)
Daniel Normanfdb38812019-04-15 09:47:24 -0700649
Daniel Norman2465fc82022-03-02 12:01:20 -0800650 if not merge_utils.ValidateConfigLists():
Daniel Normane5964522019-03-19 10:32:03 -0700651 sys.exit(1)
652
Daniel Norman2465fc82022-03-02 12:01:20 -0800653 temp_dir = common.MakeTempDir(prefix='merge_target_files_')
654 try:
655 merge_target_files(temp_dir)
656 finally:
657 if OPTIONS.keep_tmp:
658 logger.info('Keeping temp_dir %s', temp_dir)
659 else:
660 common.Cleanup()
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800661
662
663if __name__ == '__main__':
Bill Peckham889b0c62019-02-21 18:53:37 -0800664 main()