blob: 67dd6f183f1a8e518a34835f445650d7ca1466a9 [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
Daniel Normand5d70ea2019-06-05 15:13:43 -070029 --framework-target-files framework-target-files-zip-archive
30 The input target files package containing framework bits. This is a zip
Bill Peckhame9eb5f92019-02-01 15:52:10 -080031 archive.
32
Daniel Normand5d70ea2019-06-05 15:13:43 -070033 --framework-item-list framework-item-list-file
Daniel Norman2c99c5b2019-03-07 13:01:48 -080034 The optional path to a newline-separated config file that replaces the
Daniel Normand5d70ea2019-06-05 15:13:43 -070035 contents of DEFAULT_FRAMEWORK_ITEM_LIST if provided.
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 Norman2c99c5b2019-03-07 13:01:48 -080038 The optional path to a newline-separated config file that replaces the
Daniel Normand5d70ea2019-06-05 15:13:43 -070039 contents of DEFAULT_FRAMEWORK_MISC_INFO_KEYS if provided.
Daniel Norman2c99c5b2019-03-07 13:01:48 -080040
Daniel Normand5d70ea2019-06-05 15:13:43 -070041 --vendor-target-files vendor-target-files-zip-archive
42 The input target files package containing vendor bits. This is a zip
Bill Peckhame9eb5f92019-02-01 15:52:10 -080043 archive.
44
Daniel Normand5d70ea2019-06-05 15:13:43 -070045 --vendor-item-list vendor-item-list-file
Daniel Norman2c99c5b2019-03-07 13:01:48 -080046 The optional path to a newline-separated config file that replaces the
Daniel Normand5d70ea2019-06-05 15:13:43 -070047 contents of DEFAULT_VENDOR_ITEM_LIST if provided.
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
93 The following only apply when using the VSDK to perform dexopt on vendor apps:
94
95 --framework-dexpreopt-config
96 If provided, the location of framwework's dexpreopt_config.zip.
97
98 --framework-dexpreopt-tools
99 if provided, the location of framework's dexpreopt_tools.zip.
100
101 --vendor-dexpreopt-config
102 If provided, the location of vendor's dexpreopt_config.zip.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800103"""
104
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800105import logging
106import os
Daniel Normanfdb38812019-04-15 09:47:24 -0700107import shutil
Bill Peckham540d91a2019-04-25 14:18:16 -0700108import subprocess
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800109import sys
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800110
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800111import add_img_to_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700112import build_image
Daniel Normanf0318252019-04-15 11:34:56 -0700113import build_super_image
114import common
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700115import img_from_target_files
Daniel Norman2465fc82022-03-02 12:01:20 -0800116import merge_compatibility_checks
117import merge_dexopt
118import merge_meta
119import merge_utils
Daniel Norman3b64ce12019-04-16 16:11:35 -0700120import ota_from_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700121
Daniel Normana84d13b2022-02-17 14:16:40 -0800122from common import ExternalError
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800123
124logger = logging.getLogger(__name__)
Tao Bao2ad4b822019-06-27 16:52:12 -0700125
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800126OPTIONS = common.OPTIONS
Bill Peckhamcb848172020-04-03 12:50:47 -0700127# Always turn on verbose logging.
128OPTIONS.verbose = True
Daniel Normand5d70ea2019-06-05 15:13:43 -0700129OPTIONS.framework_target_files = None
130OPTIONS.framework_item_list = None
131OPTIONS.framework_misc_info_keys = None
132OPTIONS.vendor_target_files = None
133OPTIONS.vendor_item_list = None
Bill Peckhamf753e152019-02-19 18:02:46 -0800134OPTIONS.output_target_files = None
Daniel Normanfdb38812019-04-15 09:47:24 -0700135OPTIONS.output_dir = None
136OPTIONS.output_item_list = None
Daniel Norman3b64ce12019-04-16 16:11:35 -0700137OPTIONS.output_ota = None
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700138OPTIONS.output_img = None
Daniel Normanf0318252019-04-15 11:34:56 -0700139OPTIONS.output_super_empty = None
Daniel Normana4911da2019-03-15 14:36:21 -0700140OPTIONS.rebuild_recovery = False
Daniel Normanb0c75912020-09-24 14:30:21 -0700141# TODO(b/150582573): Remove this option.
142OPTIONS.allow_duplicate_apkapex_keys = False
Daniel Norman571e1822021-06-25 17:18:25 -0700143OPTIONS.vendor_otatools = None
144OPTIONS.rebuild_sepolicy = False
Bill Peckhamf753e152019-02-19 18:02:46 -0800145OPTIONS.keep_tmp = False
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700146OPTIONS.framework_dexpreopt_config = None
147OPTIONS.framework_dexpreopt_tools = None
148OPTIONS.vendor_dexpreopt_config = None
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800149
Daniel Normand5d70ea2019-06-05 15:13:43 -0700150# DEFAULT_FRAMEWORK_ITEM_LIST is a list of items to extract from the partial
151# framework target files package as is, meaning these items will land in the
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800152# output target files package exactly as they appear in the input partial
Daniel Normand5d70ea2019-06-05 15:13:43 -0700153# framework target files package.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800154
Daniel Normand5d70ea2019-06-05 15:13:43 -0700155DEFAULT_FRAMEWORK_ITEM_LIST = (
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800156 'META/apkcerts.txt',
157 'META/filesystem_config.txt',
158 'META/root_filesystem_config.txt',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800159 'META/update_engine_config.txt',
160 'PRODUCT/*',
161 'ROOT/*',
162 'SYSTEM/*',
Daniel Normanedf12472019-05-22 10:47:08 -0700163)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800164
Daniel Normand5d70ea2019-06-05 15:13:43 -0700165# DEFAULT_FRAMEWORK_MISC_INFO_KEYS is a list of keys to obtain from the
Daniel Normandbbf5a32020-10-22 16:03:32 -0700166# framework instance of META/misc_info.txt. The remaining keys should come
167# from the vendor instance.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800168
Daniel Normand5d70ea2019-06-05 15:13:43 -0700169DEFAULT_FRAMEWORK_MISC_INFO_KEYS = (
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800170 'avb_system_hashtree_enable',
171 'avb_system_add_hashtree_footer_args',
172 'avb_system_key_path',
173 'avb_system_algorithm',
174 'avb_system_rollback_index_location',
175 'avb_product_hashtree_enable',
176 'avb_product_add_hashtree_footer_args',
Justin Yun6151e3f2019-06-25 15:58:13 +0900177 'avb_system_ext_hashtree_enable',
178 'avb_system_ext_add_hashtree_footer_args',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800179 'system_root_image',
180 'root_dir',
181 'ab_update',
182 'default_system_dev_certificate',
183 'system_size',
Chris Gross203191b2020-05-30 02:39:12 +0000184 'building_system_image',
185 'building_system_ext_image',
186 'building_product_image',
Daniel Normanedf12472019-05-22 10:47:08 -0700187)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800188
Daniel Normand5d70ea2019-06-05 15:13:43 -0700189# DEFAULT_VENDOR_ITEM_LIST is a list of items to extract from the partial
190# vendor target files package as is, meaning these items will land in the output
191# target files package exactly as they appear in the input partial vendor target
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800192# files package.
193
Daniel Normand5d70ea2019-06-05 15:13:43 -0700194DEFAULT_VENDOR_ITEM_LIST = (
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800195 'META/boot_filesystem_config.txt',
196 'META/otakeys.txt',
197 'META/releasetools.py',
198 'META/vendor_filesystem_config.txt',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800199 'BOOT/*',
200 'DATA/*',
201 'ODM/*',
202 'OTA/android-info.txt',
203 'PREBUILT_IMAGES/*',
204 'RADIO/*',
205 'VENDOR/*',
Daniel Normanedf12472019-05-22 10:47:08 -0700206)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800207
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800208
Daniel Norman03747412022-02-25 10:38:37 -0800209def create_merged_package(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -0700210 """Merges two target files packages into one target files structure.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800211
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900212 Returns:
213 Path to merged package under temp directory.
214 """
Daniel Normandbbf5a32020-10-22 16:03:32 -0700215 # Extract "as is" items from the input framework and vendor partial target
216 # files packages directly into the output temporary directory, since these items
217 # do not need special case processing.
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800218
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800219 output_target_files_temp_dir = os.path.join(temp_dir, 'output')
Daniel Norman2465fc82022-03-02 12:01:20 -0800220 merge_utils.ExtractItems(
Daniel Norman03747412022-02-25 10:38:37 -0800221 input_zip=OPTIONS.framework_target_files,
222 output_dir=output_target_files_temp_dir,
223 extract_item_list=OPTIONS.framework_item_list)
Daniel Norman2465fc82022-03-02 12:01:20 -0800224 merge_utils.ExtractItems(
Daniel Norman03747412022-02-25 10:38:37 -0800225 input_zip=OPTIONS.vendor_target_files,
226 output_dir=output_target_files_temp_dir,
227 extract_item_list=OPTIONS.vendor_item_list)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800228
Daniel Normandbbf5a32020-10-22 16:03:32 -0700229 # Perform special case processing on META/* items.
230 # After this function completes successfully, all the files we need to create
231 # the output target files package are in place.
Daniel Norman2465fc82022-03-02 12:01:20 -0800232 merge_meta.MergeMetaFiles(
233 temp_dir=temp_dir, merged_dir=output_target_files_temp_dir)
Daniel Norman03747412022-02-25 10:38:37 -0800234
Daniel Norman2465fc82022-03-02 12:01:20 -0800235 merge_dexopt.MergeDexopt(
Daniel Norman03747412022-02-25 10:38:37 -0800236 temp_dir=temp_dir, output_target_files_dir=output_target_files_temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800237
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900238 return output_target_files_temp_dir
239
240
Daniel Norman03747412022-02-25 10:38:37 -0800241def generate_missing_images(target_files_dir):
242 """Generate any missing images from target files."""
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900243
244 # Regenerate IMAGES in the target directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800245
Daniel Normandbbf5a32020-10-22 16:03:32 -0700246 add_img_args = [
247 '--verbose',
248 '--add_missing',
249 ]
Daniel Norman03747412022-02-25 10:38:37 -0800250 if OPTIONS.rebuild_recovery:
Daniel Normana4911da2019-03-15 14:36:21 -0700251 add_img_args.append('--rebuild_recovery')
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900252 add_img_args.append(target_files_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800253
254 add_img_to_target_files.main(add_img_args)
255
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900256
Daniel Norman03747412022-02-25 10:38:37 -0800257def rebuild_image_with_sepolicy(target_files_dir):
Daniel Norman571e1822021-06-25 17:18:25 -0700258 """Rebuilds odm.img or vendor.img to include merged sepolicy files.
259
260 If odm is present then odm is preferred -- otherwise vendor is used.
Daniel Norman571e1822021-06-25 17:18:25 -0700261 """
262 partition = 'vendor'
263 if os.path.exists(os.path.join(target_files_dir, 'ODM')) or os.path.exists(
264 os.path.join(target_files_dir, 'IMAGES/odm.img')):
265 partition = 'odm'
266 partition_img = '{}.img'.format(partition)
Po Hua6c59122022-02-16 08:41:29 +0000267 partition_map = '{}.map'.format(partition)
Daniel Norman571e1822021-06-25 17:18:25 -0700268
269 logger.info('Recompiling %s using the merged sepolicy files.', partition_img)
270
271 # Copy the combined SEPolicy file and framework hashes to the image that is
272 # being rebuilt.
273 def copy_selinux_file(input_path, output_filename):
Po Hu0e4403e2021-07-06 17:05:56 +0800274 input_filename = os.path.join(target_files_dir, input_path)
275 if not os.path.exists(input_filename):
276 input_filename = input_filename.replace('SYSTEM_EXT/', 'SYSTEM/system_ext/') \
277 .replace('PRODUCT/', 'SYSTEM/product/')
278 if not os.path.exists(input_filename):
279 logger.info('Skipping copy_selinux_file for %s', input_filename)
280 return
Daniel Norman571e1822021-06-25 17:18:25 -0700281 shutil.copy(
Po Hu0e4403e2021-07-06 17:05:56 +0800282 input_filename,
Daniel Norman571e1822021-06-25 17:18:25 -0700283 os.path.join(target_files_dir, partition.upper(), 'etc/selinux',
284 output_filename))
285
286 copy_selinux_file('META/combined_sepolicy', 'precompiled_sepolicy')
287 copy_selinux_file('SYSTEM/etc/selinux/plat_sepolicy_and_mapping.sha256',
288 'precompiled_sepolicy.plat_sepolicy_and_mapping.sha256')
289 copy_selinux_file(
290 'SYSTEM_EXT/etc/selinux/system_ext_sepolicy_and_mapping.sha256',
291 'precompiled_sepolicy.system_ext_sepolicy_and_mapping.sha256')
292 copy_selinux_file('PRODUCT/etc/selinux/product_sepolicy_and_mapping.sha256',
293 'precompiled_sepolicy.product_sepolicy_and_mapping.sha256')
294
Daniel Norman03747412022-02-25 10:38:37 -0800295 if not OPTIONS.vendor_otatools:
Daniel Norman571e1822021-06-25 17:18:25 -0700296 # Remove the partition from the merged target-files archive. It will be
Daniel Norman03747412022-02-25 10:38:37 -0800297 # rebuilt later automatically by generate_missing_images().
Daniel Norman571e1822021-06-25 17:18:25 -0700298 os.remove(os.path.join(target_files_dir, 'IMAGES', partition_img))
Daniel Norman03747412022-02-25 10:38:37 -0800299 return
Daniel Norman571e1822021-06-25 17:18:25 -0700300
Daniel Norman03747412022-02-25 10:38:37 -0800301 # TODO(b/192253131): Remove the need for vendor_otatools by fixing
302 # backwards-compatibility issues when compiling images across releases.
303 if not OPTIONS.vendor_target_files:
304 raise ValueError(
305 'Expected vendor_target_files if vendor_otatools is not None.')
306 logger.info(
307 '%s recompilation will be performed using the vendor otatools.zip',
308 partition_img)
Daniel Norman571e1822021-06-25 17:18:25 -0700309
Daniel Norman03747412022-02-25 10:38:37 -0800310 # Unzip the vendor build's otatools.zip and target-files archive.
311 vendor_otatools_dir = common.MakeTempDir(
312 prefix='merge_target_files_vendor_otatools_')
313 vendor_target_files_dir = common.MakeTempDir(
314 prefix='merge_target_files_vendor_target_files_')
315 common.UnzipToDir(OPTIONS.vendor_otatools, vendor_otatools_dir)
316 common.UnzipToDir(OPTIONS.vendor_target_files, vendor_target_files_dir)
Daniel Norman571e1822021-06-25 17:18:25 -0700317
Daniel Norman03747412022-02-25 10:38:37 -0800318 # Copy the partition contents from the merged target-files archive to the
319 # vendor target-files archive.
320 shutil.rmtree(os.path.join(vendor_target_files_dir, partition.upper()))
321 shutil.copytree(
322 os.path.join(target_files_dir, partition.upper()),
323 os.path.join(vendor_target_files_dir, partition.upper()),
324 symlinks=True)
Daniel Norman571e1822021-06-25 17:18:25 -0700325
Daniel Norman03747412022-02-25 10:38:37 -0800326 # Delete then rebuild the partition.
327 os.remove(os.path.join(vendor_target_files_dir, 'IMAGES', partition_img))
328 rebuild_partition_command = [
329 os.path.join(vendor_otatools_dir, 'bin', 'add_img_to_target_files'),
330 '--verbose',
331 '--add_missing',
332 ]
333 if OPTIONS.rebuild_recovery:
334 rebuild_partition_command.append('--rebuild_recovery')
335 rebuild_partition_command.append(vendor_target_files_dir)
336 logger.info('Recompiling %s: %s', partition_img,
337 ' '.join(rebuild_partition_command))
338 common.RunAndCheckOutput(rebuild_partition_command, verbose=True)
Po Hua6c59122022-02-16 08:41:29 +0000339
Daniel Norman03747412022-02-25 10:38:37 -0800340 # Move the newly-created image to the merged target files dir.
341 if not os.path.exists(os.path.join(target_files_dir, 'IMAGES')):
342 os.makedirs(os.path.join(target_files_dir, 'IMAGES'))
343 shutil.move(
344 os.path.join(vendor_target_files_dir, 'IMAGES', partition_img),
345 os.path.join(target_files_dir, 'IMAGES', partition_img))
346 shutil.move(
347 os.path.join(vendor_target_files_dir, 'IMAGES', partition_map),
348 os.path.join(target_files_dir, 'IMAGES', partition_map))
Po Hua6c59122022-02-16 08:41:29 +0000349
Daniel Norman03747412022-02-25 10:38:37 -0800350 def copy_recovery_file(filename):
351 for subdir in ('VENDOR', 'SYSTEM/vendor'):
352 source = os.path.join(vendor_target_files_dir, subdir, filename)
353 if os.path.exists(source):
354 dest = os.path.join(target_files_dir, subdir, filename)
355 shutil.copy(source, dest)
356 return
357 logger.info('Skipping copy_recovery_file for %s, file not found', filename)
358
359 if OPTIONS.rebuild_recovery:
360 copy_recovery_file('etc/recovery.img')
361 copy_recovery_file('bin/install-recovery.sh')
362 copy_recovery_file('recovery-from-boot.p')
Daniel Norman571e1822021-06-25 17:18:25 -0700363
364
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900365def generate_super_empty_image(target_dir, output_super_empty):
Tao Bao2ad4b822019-06-27 16:52:12 -0700366 """Generates super_empty image from target package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900367
368 Args:
369 target_dir: Path to the target file package which contains misc_info.txt for
370 detailed information for super image.
371 output_super_empty: If provided, copies a super_empty.img file from the
372 target files package to this path.
373 """
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700374 # Create super_empty.img using the merged misc_info.txt.
375
Daniel Norman4cc9df62019-07-18 10:11:07 -0700376 misc_info_txt = os.path.join(target_dir, 'META', 'misc_info.txt')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700377
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +0900378 use_dynamic_partitions = common.LoadDictionaryFromFile(misc_info_txt).get(
379 'use_dynamic_partitions')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700380
381 if use_dynamic_partitions != 'true' and output_super_empty:
382 raise ValueError(
383 'Building super_empty.img requires use_dynamic_partitions=true.')
384 elif use_dynamic_partitions == 'true':
Daniel Norman4cc9df62019-07-18 10:11:07 -0700385 super_empty_img = os.path.join(target_dir, 'IMAGES', 'super_empty.img')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700386 build_super_image_args = [
387 misc_info_txt,
388 super_empty_img,
389 ]
390 build_super_image.main(build_super_image_args)
391
392 # Copy super_empty.img to the user-provided output_super_empty location.
393 if output_super_empty:
394 shutil.copyfile(super_empty_img, output_super_empty)
395
Daniel Normanb8a2f9d2019-04-24 12:55:51 -0700396
Daniel Norman03747412022-02-25 10:38:37 -0800397def create_target_files_archive(output_zip, source_dir, temp_dir):
398 """Creates a target_files zip archive from the input source dir.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900399
400 Args:
Daniel Norman03747412022-02-25 10:38:37 -0800401 output_zip: The name of the zip archive target files package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900402 source_dir: The target directory contains package to be archived.
403 temp_dir: Path to temporary directory for any intermediate files.
404 """
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800405 output_target_files_list = os.path.join(temp_dir, 'output.list')
Daniel Norman4cc9df62019-07-18 10:11:07 -0700406 output_target_files_meta_dir = os.path.join(source_dir, 'META')
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800407
Daniel Normandbbf5a32020-10-22 16:03:32 -0700408 def files_from_path(target_path, extra_args=None):
409 """Gets files under the given path and return a sorted list."""
410 find_command = ['find', target_path] + (extra_args or [])
411 find_process = common.Run(
412 find_command, stdout=subprocess.PIPE, verbose=False)
413 return common.RunAndCheckOutput(['sort'],
414 stdin=find_process.stdout,
415 verbose=False)
416
Daniel Norman03747412022-02-25 10:38:37 -0800417 # META content appears first in the zip. This is done by the
418 # standard build system for optimized extraction of those files,
419 # so we do the same step for merged target_files.zips here too.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900420 meta_content = files_from_path(output_target_files_meta_dir)
Daniel Norman4cc9df62019-07-18 10:11:07 -0700421 other_content = files_from_path(
422 source_dir,
423 ['-path', output_target_files_meta_dir, '-prune', '-o', '-print'])
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800424
Tao Bao2ad4b822019-06-27 16:52:12 -0700425 with open(output_target_files_list, 'w') as f:
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800426 f.write(meta_content)
427 f.write(other_content)
428
429 command = [
Bill Peckhamf753e152019-02-19 18:02:46 -0800430 'soong_zip',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800431 '-d',
Daniel Normane5b134a2019-04-17 14:54:06 -0700432 '-o',
Daniel Norman03747412022-02-25 10:38:37 -0800433 os.path.abspath(output_zip),
Daniel Normane5b134a2019-04-17 14:54:06 -0700434 '-C',
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900435 source_dir,
Daniel Normaneaf5c1d2021-02-09 11:01:42 -0800436 '-r',
Daniel Normane5b134a2019-04-17 14:54:06 -0700437 output_target_files_list,
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800438 ]
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900439
Daniel Norman03747412022-02-25 10:38:37 -0800440 logger.info('creating %s', output_zip)
Daniel Normaneaf5c1d2021-02-09 11:01:42 -0800441 common.RunAndCheckOutput(command, verbose=True)
Daniel Norman03747412022-02-25 10:38:37 -0800442 logger.info('finished creating %s', output_zip)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900443
444
Daniel Norman03747412022-02-25 10:38:37 -0800445def merge_target_files(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -0700446 """Merges two target files packages together.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900447
Daniel Norman03747412022-02-25 10:38:37 -0800448 This function uses framework and vendor target files packages as input,
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900449 performs various file extractions, special case processing, and finally
450 creates a merged zip archive as output.
451
452 Args:
453 temp_dir: The name of a directory we use when we extract items from the
454 input target files packages, and also a scratch directory that we use for
455 temporary files.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900456 """
457
458 logger.info('starting: merge framework %s and vendor %s into output %s',
Daniel Norman03747412022-02-25 10:38:37 -0800459 OPTIONS.framework_target_files, OPTIONS.vendor_target_files,
460 OPTIONS.output_target_files)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900461
Daniel Norman03747412022-02-25 10:38:37 -0800462 output_target_files_temp_dir = create_merged_package(temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900463
Daniel Norman21c34f72020-11-11 17:25:50 -0800464 partition_map = common.PartitionMapFromTargetFiles(
465 output_target_files_temp_dir)
466
Daniel Norman2465fc82022-03-02 12:01:20 -0800467 compatibility_errors = merge_compatibility_checks.CheckCompatibility(
468 target_files_dir=output_target_files_temp_dir,
469 partition_map=partition_map)
470 if compatibility_errors:
471 for error in compatibility_errors:
472 logger.error(error)
473 raise ExternalError(
474 'Found incompatibilities in the merged target files package.')
Daniel Normand3351562020-10-29 12:33:11 -0700475
Daniel Norman571e1822021-06-25 17:18:25 -0700476 # Include the compiled policy in an image if requested.
Daniel Norman03747412022-02-25 10:38:37 -0800477 if OPTIONS.rebuild_sepolicy:
478 rebuild_image_with_sepolicy(output_target_files_temp_dir)
Daniel Norman48603ff2021-02-22 15:15:24 -0800479
Daniel Norman03747412022-02-25 10:38:37 -0800480 generate_missing_images(output_target_files_temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900481
Daniel Norman03747412022-02-25 10:38:37 -0800482 generate_super_empty_image(output_target_files_temp_dir,
483 OPTIONS.output_super_empty)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900484
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900485 # Finally, create the output target files zip archive and/or copy the
486 # output items to the output target files directory.
487
Daniel Norman03747412022-02-25 10:38:37 -0800488 if OPTIONS.output_dir:
Daniel Norman2465fc82022-03-02 12:01:20 -0800489 merge_utils.CopyItems(output_target_files_temp_dir, OPTIONS.output_dir,
490 OPTIONS.output_item_list)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900491
Daniel Norman03747412022-02-25 10:38:37 -0800492 if not OPTIONS.output_target_files:
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900493 return
494
Daniel Norman03747412022-02-25 10:38:37 -0800495 create_target_files_archive(OPTIONS.output_target_files,
496 output_target_files_temp_dir, temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800497
Daniel Norman74eb74b2019-09-18 14:01:48 -0700498 # Create the IMG package from the merged target files package.
Daniel Norman03747412022-02-25 10:38:37 -0800499 if OPTIONS.output_img:
500 img_from_target_files.main(
501 [OPTIONS.output_target_files, OPTIONS.output_img])
Daniel Norman74eb74b2019-09-18 14:01:48 -0700502
Daniel Norman3b64ce12019-04-16 16:11:35 -0700503 # Create the OTA package from the merged target files package.
504
Daniel Norman03747412022-02-25 10:38:37 -0800505 if OPTIONS.output_ota:
506 ota_from_target_files.main(
507 [OPTIONS.output_target_files, OPTIONS.output_ota])
Daniel Norman3b64ce12019-04-16 16:11:35 -0700508
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700509
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800510def main():
511 """The main function.
512
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800513 Process command line arguments, then call merge_target_files to
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800514 perform the heavy lifting.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800515 """
516
517 common.InitLogging()
518
Bill Peckhamf753e152019-02-19 18:02:46 -0800519 def option_handler(o, a):
520 if o == '--system-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700521 logger.warning(
522 '--system-target-files has been renamed to --framework-target-files')
523 OPTIONS.framework_target_files = a
524 elif o == '--framework-target-files':
525 OPTIONS.framework_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800526 elif o == '--system-item-list':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700527 logger.warning(
528 '--system-item-list has been renamed to --framework-item-list')
529 OPTIONS.framework_item_list = a
530 elif o == '--framework-item-list':
531 OPTIONS.framework_item_list = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800532 elif o == '--system-misc-info-keys':
Daniel Norman4cc9df62019-07-18 10:11:07 -0700533 logger.warning('--system-misc-info-keys has been renamed to '
534 '--framework-misc-info-keys')
Daniel Normand5d70ea2019-06-05 15:13:43 -0700535 OPTIONS.framework_misc_info_keys = a
536 elif o == '--framework-misc-info-keys':
537 OPTIONS.framework_misc_info_keys = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800538 elif o == '--other-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700539 logger.warning(
540 '--other-target-files has been renamed to --vendor-target-files')
541 OPTIONS.vendor_target_files = a
542 elif o == '--vendor-target-files':
543 OPTIONS.vendor_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800544 elif o == '--other-item-list':
Daniel Norman2d7989a2021-04-05 17:40:47 +0000545 logger.warning('--other-item-list has been renamed to --vendor-item-list')
Daniel Normand5d70ea2019-06-05 15:13:43 -0700546 OPTIONS.vendor_item_list = a
547 elif o == '--vendor-item-list':
548 OPTIONS.vendor_item_list = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800549 elif o == '--output-target-files':
550 OPTIONS.output_target_files = a
Daniel Normanfdb38812019-04-15 09:47:24 -0700551 elif o == '--output-dir':
552 OPTIONS.output_dir = a
553 elif o == '--output-item-list':
554 OPTIONS.output_item_list = a
Daniel Norman3b64ce12019-04-16 16:11:35 -0700555 elif o == '--output-ota':
556 OPTIONS.output_ota = a
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700557 elif o == '--output-img':
558 OPTIONS.output_img = a
Daniel Normanf0318252019-04-15 11:34:56 -0700559 elif o == '--output-super-empty':
560 OPTIONS.output_super_empty = a
Daniel Norman2465fc82022-03-02 12:01:20 -0800561 elif o == '--rebuild_recovery' or o == '--rebuild-recovery':
Daniel Normana4911da2019-03-15 14:36:21 -0700562 OPTIONS.rebuild_recovery = True
Daniel Normanb0c75912020-09-24 14:30:21 -0700563 elif o == '--allow-duplicate-apkapex-keys':
564 OPTIONS.allow_duplicate_apkapex_keys = True
Daniel Norman571e1822021-06-25 17:18:25 -0700565 elif o == '--vendor-otatools':
566 OPTIONS.vendor_otatools = a
567 elif o == '--rebuild-sepolicy':
568 OPTIONS.rebuild_sepolicy = True
Bill Peckham364c1cc2019-03-29 18:27:23 -0700569 elif o == '--keep-tmp':
Bill Peckhamf753e152019-02-19 18:02:46 -0800570 OPTIONS.keep_tmp = True
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700571 elif o == '--framework-dexpreopt-config':
572 OPTIONS.framework_dexpreopt_config = a
573 elif o == '--framework-dexpreopt-tools':
574 OPTIONS.framework_dexpreopt_tools = a
575 elif o == '--vendor-dexpreopt-config':
576 OPTIONS.vendor_dexpreopt_config = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800577 else:
578 return False
579 return True
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800580
Bill Peckhamf753e152019-02-19 18:02:46 -0800581 args = common.ParseOptions(
Daniel Normane5b134a2019-04-17 14:54:06 -0700582 sys.argv[1:],
583 __doc__,
Bill Peckhamf753e152019-02-19 18:02:46 -0800584 extra_long_opts=[
585 'system-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700586 'framework-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800587 'system-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700588 'framework-item-list=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800589 'system-misc-info-keys=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700590 'framework-misc-info-keys=',
Bill Peckhamf753e152019-02-19 18:02:46 -0800591 'other-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700592 'vendor-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800593 'other-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700594 'vendor-item-list=',
Bill Peckhamf753e152019-02-19 18:02:46 -0800595 'output-target-files=',
Daniel Normanfdb38812019-04-15 09:47:24 -0700596 'output-dir=',
597 'output-item-list=',
Daniel Norman3b64ce12019-04-16 16:11:35 -0700598 'output-ota=',
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700599 'output-img=',
Daniel Normanf0318252019-04-15 11:34:56 -0700600 'output-super-empty=',
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700601 'framework-dexpreopt-config=',
602 'framework-dexpreopt-tools=',
603 'vendor-dexpreopt-config=',
Daniel Normana4911da2019-03-15 14:36:21 -0700604 'rebuild_recovery',
Daniel Norman2465fc82022-03-02 12:01:20 -0800605 'rebuild-recovery',
Daniel Normanb0c75912020-09-24 14:30:21 -0700606 'allow-duplicate-apkapex-keys',
Daniel Norman571e1822021-06-25 17:18:25 -0700607 'vendor-otatools=',
608 'rebuild-sepolicy',
Bill Peckham364c1cc2019-03-29 18:27:23 -0700609 'keep-tmp',
Bill Peckhamf753e152019-02-19 18:02:46 -0800610 ],
611 extra_option_handler=option_handler)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800612
Tao Bao2ad4b822019-06-27 16:52:12 -0700613 # pylint: disable=too-many-boolean-expressions
Daniel Normand5d70ea2019-06-05 15:13:43 -0700614 if (args or OPTIONS.framework_target_files is None or
615 OPTIONS.vendor_target_files is None or
Daniel Normane5b134a2019-04-17 14:54:06 -0700616 (OPTIONS.output_target_files is None and OPTIONS.output_dir is None) or
Po Hua6c59122022-02-16 08:41:29 +0000617 (OPTIONS.output_dir is not None and OPTIONS.output_item_list is None) or
618 (OPTIONS.rebuild_recovery and not OPTIONS.rebuild_sepolicy)):
Bill Peckhamf753e152019-02-19 18:02:46 -0800619 common.Usage(__doc__)
Bill Peckham889b0c62019-02-21 18:53:37 -0800620 sys.exit(1)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800621
Daniel Normand5d70ea2019-06-05 15:13:43 -0700622 if OPTIONS.framework_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800623 OPTIONS.framework_item_list = common.LoadListFromFile(
624 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800625 else:
Daniel Norman03747412022-02-25 10:38:37 -0800626 OPTIONS.framework_item_list = DEFAULT_FRAMEWORK_ITEM_LIST
Daniel Norman2465fc82022-03-02 12:01:20 -0800627 OPTIONS.framework_partition_set = merge_utils.ItemListToPartitionSet(
Daniel Norman03747412022-02-25 10:38:37 -0800628 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800629
Daniel Normand5d70ea2019-06-05 15:13:43 -0700630 if OPTIONS.framework_misc_info_keys:
Daniel Norman03747412022-02-25 10:38:37 -0800631 OPTIONS.framework_misc_info_keys = common.LoadListFromFile(
Daniel Normand5d70ea2019-06-05 15:13:43 -0700632 OPTIONS.framework_misc_info_keys)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800633 else:
Daniel Norman03747412022-02-25 10:38:37 -0800634 OPTIONS.framework_misc_info_keys = DEFAULT_FRAMEWORK_MISC_INFO_KEYS
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800635
Daniel Normand5d70ea2019-06-05 15:13:43 -0700636 if OPTIONS.vendor_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800637 OPTIONS.vendor_item_list = common.LoadListFromFile(OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800638 else:
Daniel Norman03747412022-02-25 10:38:37 -0800639 OPTIONS.vendor_item_list = DEFAULT_VENDOR_ITEM_LIST
Daniel Norman2465fc82022-03-02 12:01:20 -0800640 OPTIONS.vendor_partition_set = merge_utils.ItemListToPartitionSet(
Daniel Norman03747412022-02-25 10:38:37 -0800641 OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800642
Daniel Normanfdb38812019-04-15 09:47:24 -0700643 if OPTIONS.output_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800644 OPTIONS.output_item_list = common.LoadListFromFile(OPTIONS.output_item_list)
Daniel Normanfdb38812019-04-15 09:47:24 -0700645 else:
Daniel Norman03747412022-02-25 10:38:37 -0800646 OPTIONS.output_item_list = None
Daniel Normanfdb38812019-04-15 09:47:24 -0700647
Daniel Norman2465fc82022-03-02 12:01:20 -0800648 if not merge_utils.ValidateConfigLists():
Daniel Normane5964522019-03-19 10:32:03 -0700649 sys.exit(1)
650
Daniel Norman2465fc82022-03-02 12:01:20 -0800651 temp_dir = common.MakeTempDir(prefix='merge_target_files_')
652 try:
653 merge_target_files(temp_dir)
654 finally:
655 if OPTIONS.keep_tmp:
656 logger.info('Keeping temp_dir %s', temp_dir)
657 else:
658 common.Cleanup()
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800659
660
661if __name__ == '__main__':
Bill Peckham889b0c62019-02-21 18:53:37 -0800662 main()