blob: 54122b0b05e0ea8c22a8f97721eee373f20648b2 [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 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
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 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
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
Daniel Norman5f476772022-03-02 15:46:34 -0800110import zipfile
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800111
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800112import add_img_to_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700113import build_image
Daniel Normanf0318252019-04-15 11:34:56 -0700114import build_super_image
115import common
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700116import img_from_target_files
Daniel Norman2465fc82022-03-02 12:01:20 -0800117import merge_compatibility_checks
118import merge_dexopt
119import merge_meta
120import merge_utils
Daniel Norman3b64ce12019-04-16 16:11:35 -0700121import ota_from_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700122
Daniel Normana84d13b2022-02-17 14:16:40 -0800123from common import ExternalError
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800124
125logger = logging.getLogger(__name__)
Tao Bao2ad4b822019-06-27 16:52:12 -0700126
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800127OPTIONS = common.OPTIONS
Bill Peckhamcb848172020-04-03 12:50:47 -0700128# Always turn on verbose logging.
129OPTIONS.verbose = True
Daniel Normand5d70ea2019-06-05 15:13:43 -0700130OPTIONS.framework_target_files = None
Daniel Norman5f476772022-03-02 15:46:34 -0800131OPTIONS.framework_item_list = []
132OPTIONS.framework_misc_info_keys = []
Daniel Normand5d70ea2019-06-05 15:13:43 -0700133OPTIONS.vendor_target_files = None
Daniel Norman5f476772022-03-02 15:46:34 -0800134OPTIONS.vendor_item_list = []
Bill Peckhamf753e152019-02-19 18:02:46 -0800135OPTIONS.output_target_files = None
Daniel Normanfdb38812019-04-15 09:47:24 -0700136OPTIONS.output_dir = None
Daniel Norman5f476772022-03-02 15:46:34 -0800137OPTIONS.output_item_list = []
Daniel Norman3b64ce12019-04-16 16:11:35 -0700138OPTIONS.output_ota = None
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700139OPTIONS.output_img = None
Daniel Normanf0318252019-04-15 11:34:56 -0700140OPTIONS.output_super_empty = None
Daniel Normana4911da2019-03-15 14:36:21 -0700141OPTIONS.rebuild_recovery = False
Daniel Normanb0c75912020-09-24 14:30:21 -0700142# TODO(b/150582573): Remove this option.
143OPTIONS.allow_duplicate_apkapex_keys = False
Daniel Norman571e1822021-06-25 17:18:25 -0700144OPTIONS.vendor_otatools = None
145OPTIONS.rebuild_sepolicy = False
Bill Peckhamf753e152019-02-19 18:02:46 -0800146OPTIONS.keep_tmp = False
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700147OPTIONS.framework_dexpreopt_config = None
148OPTIONS.framework_dexpreopt_tools = None
149OPTIONS.vendor_dexpreopt_config = None
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800150
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800151
jiangxu52bd42732022-04-11 16:02:48 +0800152def move_only_exists(source, destination):
153 """Judge whether the file exists and then move the file."""
154
155 if os.path.exists(source):
156 shutil.move(source, destination)
157
158
Dennis Song16d6fe02023-02-23 23:37:42 +0800159def remove_file_if_exists(file_name):
160 """Remove the file if it exists and skip otherwise."""
161
162 try:
163 os.remove(file_name)
164 except FileNotFoundError:
165 pass
166
167
Daniel Norman03747412022-02-25 10:38:37 -0800168def create_merged_package(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -0700169 """Merges two target files packages into one target files structure.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800170
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900171 Returns:
172 Path to merged package under temp directory.
173 """
Daniel Normandbbf5a32020-10-22 16:03:32 -0700174 # Extract "as is" items from the input framework and vendor partial target
175 # files packages directly into the output temporary directory, since these items
176 # do not need special case processing.
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800177
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800178 output_target_files_temp_dir = os.path.join(temp_dir, 'output')
Daniel Norman2465fc82022-03-02 12:01:20 -0800179 merge_utils.ExtractItems(
Daniel Norman03747412022-02-25 10:38:37 -0800180 input_zip=OPTIONS.framework_target_files,
181 output_dir=output_target_files_temp_dir,
182 extract_item_list=OPTIONS.framework_item_list)
Daniel Norman2465fc82022-03-02 12:01:20 -0800183 merge_utils.ExtractItems(
Daniel Norman03747412022-02-25 10:38:37 -0800184 input_zip=OPTIONS.vendor_target_files,
185 output_dir=output_target_files_temp_dir,
186 extract_item_list=OPTIONS.vendor_item_list)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800187
Daniel Normandbbf5a32020-10-22 16:03:32 -0700188 # Perform special case processing on META/* items.
189 # After this function completes successfully, all the files we need to create
190 # the output target files package are in place.
Daniel Norman2465fc82022-03-02 12:01:20 -0800191 merge_meta.MergeMetaFiles(
192 temp_dir=temp_dir, merged_dir=output_target_files_temp_dir)
Daniel Norman03747412022-02-25 10:38:37 -0800193
Daniel Norman2465fc82022-03-02 12:01:20 -0800194 merge_dexopt.MergeDexopt(
Daniel Norman03747412022-02-25 10:38:37 -0800195 temp_dir=temp_dir, output_target_files_dir=output_target_files_temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800196
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900197 return output_target_files_temp_dir
198
199
Daniel Norman03747412022-02-25 10:38:37 -0800200def generate_missing_images(target_files_dir):
201 """Generate any missing images from target files."""
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900202
203 # Regenerate IMAGES in the target directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800204
Daniel Normandbbf5a32020-10-22 16:03:32 -0700205 add_img_args = [
206 '--verbose',
207 '--add_missing',
208 ]
Daniel Norman03747412022-02-25 10:38:37 -0800209 if OPTIONS.rebuild_recovery:
Daniel Normana4911da2019-03-15 14:36:21 -0700210 add_img_args.append('--rebuild_recovery')
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900211 add_img_args.append(target_files_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800212
213 add_img_to_target_files.main(add_img_args)
214
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900215
Daniel Norman03747412022-02-25 10:38:37 -0800216def rebuild_image_with_sepolicy(target_files_dir):
Daniel Norman571e1822021-06-25 17:18:25 -0700217 """Rebuilds odm.img or vendor.img to include merged sepolicy files.
218
219 If odm is present then odm is preferred -- otherwise vendor is used.
Daniel Norman571e1822021-06-25 17:18:25 -0700220 """
221 partition = 'vendor'
Dennis Song16d6fe02023-02-23 23:37:42 +0800222 if os.path.exists(os.path.join(target_files_dir, 'ODM')):
Daniel Norman571e1822021-06-25 17:18:25 -0700223 partition = 'odm'
224 partition_img = '{}.img'.format(partition)
Po Hua6c59122022-02-16 08:41:29 +0000225 partition_map = '{}.map'.format(partition)
Daniel Norman571e1822021-06-25 17:18:25 -0700226
227 logger.info('Recompiling %s using the merged sepolicy files.', partition_img)
228
229 # Copy the combined SEPolicy file and framework hashes to the image that is
230 # being rebuilt.
231 def copy_selinux_file(input_path, output_filename):
Po Hu0e4403e2021-07-06 17:05:56 +0800232 input_filename = os.path.join(target_files_dir, input_path)
233 if not os.path.exists(input_filename):
234 input_filename = input_filename.replace('SYSTEM_EXT/', 'SYSTEM/system_ext/') \
235 .replace('PRODUCT/', 'SYSTEM/product/')
236 if not os.path.exists(input_filename):
237 logger.info('Skipping copy_selinux_file for %s', input_filename)
238 return
Daniel Norman571e1822021-06-25 17:18:25 -0700239 shutil.copy(
Po Hu0e4403e2021-07-06 17:05:56 +0800240 input_filename,
Daniel Norman571e1822021-06-25 17:18:25 -0700241 os.path.join(target_files_dir, partition.upper(), 'etc/selinux',
242 output_filename))
243
244 copy_selinux_file('META/combined_sepolicy', 'precompiled_sepolicy')
245 copy_selinux_file('SYSTEM/etc/selinux/plat_sepolicy_and_mapping.sha256',
246 'precompiled_sepolicy.plat_sepolicy_and_mapping.sha256')
247 copy_selinux_file(
248 'SYSTEM_EXT/etc/selinux/system_ext_sepolicy_and_mapping.sha256',
249 'precompiled_sepolicy.system_ext_sepolicy_and_mapping.sha256')
250 copy_selinux_file('PRODUCT/etc/selinux/product_sepolicy_and_mapping.sha256',
251 'precompiled_sepolicy.product_sepolicy_and_mapping.sha256')
252
Daniel Norman03747412022-02-25 10:38:37 -0800253 if not OPTIONS.vendor_otatools:
Daniel Norman571e1822021-06-25 17:18:25 -0700254 # Remove the partition from the merged target-files archive. It will be
Daniel Norman03747412022-02-25 10:38:37 -0800255 # rebuilt later automatically by generate_missing_images().
Dennis Song16d6fe02023-02-23 23:37:42 +0800256 remove_file_if_exists(
257 os.path.join(target_files_dir, 'IMAGES', partition_img))
Daniel Norman03747412022-02-25 10:38:37 -0800258 return
Daniel Norman571e1822021-06-25 17:18:25 -0700259
Daniel Norman03747412022-02-25 10:38:37 -0800260 # TODO(b/192253131): Remove the need for vendor_otatools by fixing
261 # backwards-compatibility issues when compiling images across releases.
262 if not OPTIONS.vendor_target_files:
263 raise ValueError(
264 'Expected vendor_target_files if vendor_otatools is not None.')
265 logger.info(
266 '%s recompilation will be performed using the vendor otatools.zip',
267 partition_img)
Daniel Norman571e1822021-06-25 17:18:25 -0700268
Daniel Norman03747412022-02-25 10:38:37 -0800269 # Unzip the vendor build's otatools.zip and target-files archive.
270 vendor_otatools_dir = common.MakeTempDir(
271 prefix='merge_target_files_vendor_otatools_')
272 vendor_target_files_dir = common.MakeTempDir(
273 prefix='merge_target_files_vendor_target_files_')
274 common.UnzipToDir(OPTIONS.vendor_otatools, vendor_otatools_dir)
275 common.UnzipToDir(OPTIONS.vendor_target_files, vendor_target_files_dir)
Daniel Norman571e1822021-06-25 17:18:25 -0700276
Daniel Norman03747412022-02-25 10:38:37 -0800277 # Copy the partition contents from the merged target-files archive to the
278 # vendor target-files archive.
279 shutil.rmtree(os.path.join(vendor_target_files_dir, partition.upper()))
280 shutil.copytree(
281 os.path.join(target_files_dir, partition.upper()),
282 os.path.join(vendor_target_files_dir, partition.upper()),
283 symlinks=True)
Daniel Norman571e1822021-06-25 17:18:25 -0700284
Daniel Norman03747412022-02-25 10:38:37 -0800285 # Delete then rebuild the partition.
Dennis Song16d6fe02023-02-23 23:37:42 +0800286 remove_file_if_exists(
287 os.path.join(vendor_target_files_dir, 'IMAGES', partition_img))
Daniel Norman03747412022-02-25 10:38:37 -0800288 rebuild_partition_command = [
289 os.path.join(vendor_otatools_dir, 'bin', 'add_img_to_target_files'),
290 '--verbose',
291 '--add_missing',
292 ]
293 if OPTIONS.rebuild_recovery:
294 rebuild_partition_command.append('--rebuild_recovery')
295 rebuild_partition_command.append(vendor_target_files_dir)
296 logger.info('Recompiling %s: %s', partition_img,
297 ' '.join(rebuild_partition_command))
298 common.RunAndCheckOutput(rebuild_partition_command, verbose=True)
Po Hua6c59122022-02-16 08:41:29 +0000299
Daniel Norman03747412022-02-25 10:38:37 -0800300 # Move the newly-created image to the merged target files dir.
301 if not os.path.exists(os.path.join(target_files_dir, 'IMAGES')):
302 os.makedirs(os.path.join(target_files_dir, 'IMAGES'))
303 shutil.move(
304 os.path.join(vendor_target_files_dir, 'IMAGES', partition_img),
305 os.path.join(target_files_dir, 'IMAGES', partition_img))
jiangxu52bd42732022-04-11 16:02:48 +0800306 move_only_exists(os.path.join(vendor_target_files_dir, 'IMAGES', partition_map),
307 os.path.join(target_files_dir, 'IMAGES', partition_map))
Po Hua6c59122022-02-16 08:41:29 +0000308
Daniel Norman03747412022-02-25 10:38:37 -0800309 def copy_recovery_file(filename):
310 for subdir in ('VENDOR', 'SYSTEM/vendor'):
311 source = os.path.join(vendor_target_files_dir, subdir, filename)
312 if os.path.exists(source):
313 dest = os.path.join(target_files_dir, subdir, filename)
314 shutil.copy(source, dest)
315 return
316 logger.info('Skipping copy_recovery_file for %s, file not found', filename)
317
318 if OPTIONS.rebuild_recovery:
319 copy_recovery_file('etc/recovery.img')
320 copy_recovery_file('bin/install-recovery.sh')
321 copy_recovery_file('recovery-from-boot.p')
Daniel Norman571e1822021-06-25 17:18:25 -0700322
323
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900324def generate_super_empty_image(target_dir, output_super_empty):
Tao Bao2ad4b822019-06-27 16:52:12 -0700325 """Generates super_empty image from target package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900326
327 Args:
328 target_dir: Path to the target file package which contains misc_info.txt for
329 detailed information for super image.
330 output_super_empty: If provided, copies a super_empty.img file from the
331 target files package to this path.
332 """
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700333 # Create super_empty.img using the merged misc_info.txt.
334
Daniel Norman4cc9df62019-07-18 10:11:07 -0700335 misc_info_txt = os.path.join(target_dir, 'META', 'misc_info.txt')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700336
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +0900337 use_dynamic_partitions = common.LoadDictionaryFromFile(misc_info_txt).get(
338 'use_dynamic_partitions')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700339
340 if use_dynamic_partitions != 'true' and output_super_empty:
341 raise ValueError(
342 'Building super_empty.img requires use_dynamic_partitions=true.')
343 elif use_dynamic_partitions == 'true':
Daniel Norman4cc9df62019-07-18 10:11:07 -0700344 super_empty_img = os.path.join(target_dir, 'IMAGES', 'super_empty.img')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700345 build_super_image_args = [
346 misc_info_txt,
347 super_empty_img,
348 ]
349 build_super_image.main(build_super_image_args)
350
351 # Copy super_empty.img to the user-provided output_super_empty location.
352 if output_super_empty:
353 shutil.copyfile(super_empty_img, output_super_empty)
354
Daniel Normanb8a2f9d2019-04-24 12:55:51 -0700355
Daniel Norman03747412022-02-25 10:38:37 -0800356def create_target_files_archive(output_zip, source_dir, temp_dir):
357 """Creates a target_files zip archive from the input source dir.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900358
359 Args:
Daniel Norman03747412022-02-25 10:38:37 -0800360 output_zip: The name of the zip archive target files package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900361 source_dir: The target directory contains package to be archived.
362 temp_dir: Path to temporary directory for any intermediate files.
363 """
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800364 output_target_files_list = os.path.join(temp_dir, 'output.list')
Daniel Norman4cc9df62019-07-18 10:11:07 -0700365 output_target_files_meta_dir = os.path.join(source_dir, 'META')
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800366
Daniel Normandbbf5a32020-10-22 16:03:32 -0700367 def files_from_path(target_path, extra_args=None):
368 """Gets files under the given path and return a sorted list."""
369 find_command = ['find', target_path] + (extra_args or [])
370 find_process = common.Run(
371 find_command, stdout=subprocess.PIPE, verbose=False)
372 return common.RunAndCheckOutput(['sort'],
373 stdin=find_process.stdout,
374 verbose=False)
375
Daniel Norman03747412022-02-25 10:38:37 -0800376 # META content appears first in the zip. This is done by the
377 # standard build system for optimized extraction of those files,
378 # so we do the same step for merged target_files.zips here too.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900379 meta_content = files_from_path(output_target_files_meta_dir)
Daniel Norman4cc9df62019-07-18 10:11:07 -0700380 other_content = files_from_path(
381 source_dir,
382 ['-path', output_target_files_meta_dir, '-prune', '-o', '-print'])
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800383
Tao Bao2ad4b822019-06-27 16:52:12 -0700384 with open(output_target_files_list, 'w') as f:
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800385 f.write(meta_content)
386 f.write(other_content)
387
388 command = [
Bill Peckhamf753e152019-02-19 18:02:46 -0800389 'soong_zip',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800390 '-d',
Daniel Normane5b134a2019-04-17 14:54:06 -0700391 '-o',
Daniel Norman03747412022-02-25 10:38:37 -0800392 os.path.abspath(output_zip),
Daniel Normane5b134a2019-04-17 14:54:06 -0700393 '-C',
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900394 source_dir,
Daniel Normaneaf5c1d2021-02-09 11:01:42 -0800395 '-r',
Daniel Normane5b134a2019-04-17 14:54:06 -0700396 output_target_files_list,
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800397 ]
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900398
Daniel Norman03747412022-02-25 10:38:37 -0800399 logger.info('creating %s', output_zip)
Daniel Normaneaf5c1d2021-02-09 11:01:42 -0800400 common.RunAndCheckOutput(command, verbose=True)
Daniel Norman03747412022-02-25 10:38:37 -0800401 logger.info('finished creating %s', output_zip)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900402
403
Daniel Norman03747412022-02-25 10:38:37 -0800404def merge_target_files(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -0700405 """Merges two target files packages together.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900406
Daniel Norman03747412022-02-25 10:38:37 -0800407 This function uses framework and vendor target files packages as input,
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900408 performs various file extractions, special case processing, and finally
409 creates a merged zip archive as output.
410
411 Args:
412 temp_dir: The name of a directory we use when we extract items from the
413 input target files packages, and also a scratch directory that we use for
414 temporary files.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900415 """
416
417 logger.info('starting: merge framework %s and vendor %s into output %s',
Daniel Norman03747412022-02-25 10:38:37 -0800418 OPTIONS.framework_target_files, OPTIONS.vendor_target_files,
419 OPTIONS.output_target_files)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900420
Daniel Norman03747412022-02-25 10:38:37 -0800421 output_target_files_temp_dir = create_merged_package(temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900422
Daniel Norman21c34f72020-11-11 17:25:50 -0800423 partition_map = common.PartitionMapFromTargetFiles(
424 output_target_files_temp_dir)
425
Daniel Norman2465fc82022-03-02 12:01:20 -0800426 compatibility_errors = merge_compatibility_checks.CheckCompatibility(
427 target_files_dir=output_target_files_temp_dir,
428 partition_map=partition_map)
429 if compatibility_errors:
430 for error in compatibility_errors:
431 logger.error(error)
432 raise ExternalError(
433 'Found incompatibilities in the merged target files package.')
Daniel Normand3351562020-10-29 12:33:11 -0700434
Daniel Norman571e1822021-06-25 17:18:25 -0700435 # Include the compiled policy in an image if requested.
Daniel Norman03747412022-02-25 10:38:37 -0800436 if OPTIONS.rebuild_sepolicy:
437 rebuild_image_with_sepolicy(output_target_files_temp_dir)
Daniel Norman48603ff2021-02-22 15:15:24 -0800438
Daniel Norman03747412022-02-25 10:38:37 -0800439 generate_missing_images(output_target_files_temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900440
Daniel Norman03747412022-02-25 10:38:37 -0800441 generate_super_empty_image(output_target_files_temp_dir,
442 OPTIONS.output_super_empty)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900443
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900444 # Finally, create the output target files zip archive and/or copy the
445 # output items to the output target files directory.
446
Daniel Norman03747412022-02-25 10:38:37 -0800447 if OPTIONS.output_dir:
Daniel Norman2465fc82022-03-02 12:01:20 -0800448 merge_utils.CopyItems(output_target_files_temp_dir, OPTIONS.output_dir,
449 OPTIONS.output_item_list)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900450
Daniel Norman03747412022-02-25 10:38:37 -0800451 if not OPTIONS.output_target_files:
Kiyoung Kim7cbeda72019-06-28 13:26:04 +0900452 return
453
Daniel Norman03747412022-02-25 10:38:37 -0800454 create_target_files_archive(OPTIONS.output_target_files,
455 output_target_files_temp_dir, temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800456
Daniel Norman74eb74b2019-09-18 14:01:48 -0700457 # Create the IMG package from the merged target files package.
Daniel Norman03747412022-02-25 10:38:37 -0800458 if OPTIONS.output_img:
459 img_from_target_files.main(
460 [OPTIONS.output_target_files, OPTIONS.output_img])
Daniel Norman74eb74b2019-09-18 14:01:48 -0700461
Daniel Norman3b64ce12019-04-16 16:11:35 -0700462 # Create the OTA package from the merged target files package.
463
Daniel Norman03747412022-02-25 10:38:37 -0800464 if OPTIONS.output_ota:
465 ota_from_target_files.main(
466 [OPTIONS.output_target_files, OPTIONS.output_ota])
Daniel Norman3b64ce12019-04-16 16:11:35 -0700467
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700468
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800469def main():
470 """The main function.
471
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800472 Process command line arguments, then call merge_target_files to
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800473 perform the heavy lifting.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800474 """
475
476 common.InitLogging()
477
Bill Peckhamf753e152019-02-19 18:02:46 -0800478 def option_handler(o, a):
479 if o == '--system-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700480 logger.warning(
481 '--system-target-files has been renamed to --framework-target-files')
482 OPTIONS.framework_target_files = a
483 elif o == '--framework-target-files':
484 OPTIONS.framework_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800485 elif o == '--system-item-list':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700486 logger.warning(
487 '--system-item-list has been renamed to --framework-item-list')
488 OPTIONS.framework_item_list = a
489 elif o == '--framework-item-list':
490 OPTIONS.framework_item_list = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800491 elif o == '--system-misc-info-keys':
Daniel Norman4cc9df62019-07-18 10:11:07 -0700492 logger.warning('--system-misc-info-keys has been renamed to '
493 '--framework-misc-info-keys')
Daniel Normand5d70ea2019-06-05 15:13:43 -0700494 OPTIONS.framework_misc_info_keys = a
495 elif o == '--framework-misc-info-keys':
496 OPTIONS.framework_misc_info_keys = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800497 elif o == '--other-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -0700498 logger.warning(
499 '--other-target-files has been renamed to --vendor-target-files')
500 OPTIONS.vendor_target_files = a
501 elif o == '--vendor-target-files':
502 OPTIONS.vendor_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800503 elif o == '--other-item-list':
Daniel Norman2d7989a2021-04-05 17:40:47 +0000504 logger.warning('--other-item-list has been renamed to --vendor-item-list')
Daniel Normand5d70ea2019-06-05 15:13:43 -0700505 OPTIONS.vendor_item_list = a
506 elif o == '--vendor-item-list':
507 OPTIONS.vendor_item_list = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800508 elif o == '--output-target-files':
509 OPTIONS.output_target_files = a
Daniel Normanfdb38812019-04-15 09:47:24 -0700510 elif o == '--output-dir':
511 OPTIONS.output_dir = a
512 elif o == '--output-item-list':
513 OPTIONS.output_item_list = a
Daniel Norman3b64ce12019-04-16 16:11:35 -0700514 elif o == '--output-ota':
515 OPTIONS.output_ota = a
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700516 elif o == '--output-img':
517 OPTIONS.output_img = a
Daniel Normanf0318252019-04-15 11:34:56 -0700518 elif o == '--output-super-empty':
519 OPTIONS.output_super_empty = a
Daniel Norman2465fc82022-03-02 12:01:20 -0800520 elif o == '--rebuild_recovery' or o == '--rebuild-recovery':
Daniel Normana4911da2019-03-15 14:36:21 -0700521 OPTIONS.rebuild_recovery = True
Daniel Normanb0c75912020-09-24 14:30:21 -0700522 elif o == '--allow-duplicate-apkapex-keys':
523 OPTIONS.allow_duplicate_apkapex_keys = True
Daniel Norman571e1822021-06-25 17:18:25 -0700524 elif o == '--vendor-otatools':
525 OPTIONS.vendor_otatools = a
526 elif o == '--rebuild-sepolicy':
527 OPTIONS.rebuild_sepolicy = True
Bill Peckham364c1cc2019-03-29 18:27:23 -0700528 elif o == '--keep-tmp':
Bill Peckhamf753e152019-02-19 18:02:46 -0800529 OPTIONS.keep_tmp = True
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700530 elif o == '--framework-dexpreopt-config':
531 OPTIONS.framework_dexpreopt_config = a
532 elif o == '--framework-dexpreopt-tools':
533 OPTIONS.framework_dexpreopt_tools = a
534 elif o == '--vendor-dexpreopt-config':
535 OPTIONS.vendor_dexpreopt_config = a
Bill Peckhamf753e152019-02-19 18:02:46 -0800536 else:
537 return False
538 return True
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800539
Bill Peckhamf753e152019-02-19 18:02:46 -0800540 args = common.ParseOptions(
Daniel Normane5b134a2019-04-17 14:54:06 -0700541 sys.argv[1:],
542 __doc__,
Bill Peckhamf753e152019-02-19 18:02:46 -0800543 extra_long_opts=[
544 'system-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700545 'framework-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800546 'system-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700547 'framework-item-list=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800548 'system-misc-info-keys=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700549 'framework-misc-info-keys=',
Bill Peckhamf753e152019-02-19 18:02:46 -0800550 'other-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700551 'vendor-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800552 'other-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -0700553 'vendor-item-list=',
Bill Peckhamf753e152019-02-19 18:02:46 -0800554 'output-target-files=',
Daniel Normanfdb38812019-04-15 09:47:24 -0700555 'output-dir=',
556 'output-item-list=',
Daniel Norman3b64ce12019-04-16 16:11:35 -0700557 'output-ota=',
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700558 'output-img=',
Daniel Normanf0318252019-04-15 11:34:56 -0700559 'output-super-empty=',
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700560 'framework-dexpreopt-config=',
561 'framework-dexpreopt-tools=',
562 'vendor-dexpreopt-config=',
Daniel Normana4911da2019-03-15 14:36:21 -0700563 'rebuild_recovery',
Daniel Norman2465fc82022-03-02 12:01:20 -0800564 'rebuild-recovery',
Daniel Normanb0c75912020-09-24 14:30:21 -0700565 'allow-duplicate-apkapex-keys',
Daniel Norman571e1822021-06-25 17:18:25 -0700566 'vendor-otatools=',
567 'rebuild-sepolicy',
Bill Peckham364c1cc2019-03-29 18:27:23 -0700568 'keep-tmp',
Bill Peckhamf753e152019-02-19 18:02:46 -0800569 ],
570 extra_option_handler=option_handler)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800571
Tao Bao2ad4b822019-06-27 16:52:12 -0700572 # pylint: disable=too-many-boolean-expressions
Daniel Normand5d70ea2019-06-05 15:13:43 -0700573 if (args or OPTIONS.framework_target_files is None or
574 OPTIONS.vendor_target_files is None or
Daniel Normane5b134a2019-04-17 14:54:06 -0700575 (OPTIONS.output_target_files is None and OPTIONS.output_dir is None) or
Daniel Norman5f476772022-03-02 15:46:34 -0800576 (OPTIONS.output_dir is not None and not OPTIONS.output_item_list) or
Po Hua6c59122022-02-16 08:41:29 +0000577 (OPTIONS.rebuild_recovery and not OPTIONS.rebuild_sepolicy)):
Bill Peckhamf753e152019-02-19 18:02:46 -0800578 common.Usage(__doc__)
Bill Peckham889b0c62019-02-21 18:53:37 -0800579 sys.exit(1)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800580
Daniel Norman5f476772022-03-02 15:46:34 -0800581 with zipfile.ZipFile(OPTIONS.framework_target_files, allowZip64=True) as fz:
582 framework_namelist = fz.namelist()
583 with zipfile.ZipFile(OPTIONS.vendor_target_files, allowZip64=True) as vz:
584 vendor_namelist = vz.namelist()
585
Daniel Normand5d70ea2019-06-05 15:13:43 -0700586 if OPTIONS.framework_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800587 OPTIONS.framework_item_list = common.LoadListFromFile(
588 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800589 else:
Daniel Norman5f476772022-03-02 15:46:34 -0800590 OPTIONS.framework_item_list = merge_utils.InferItemList(
591 input_namelist=framework_namelist, framework=True)
Daniel Norman2465fc82022-03-02 12:01:20 -0800592 OPTIONS.framework_partition_set = merge_utils.ItemListToPartitionSet(
Daniel Norman03747412022-02-25 10:38:37 -0800593 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800594
Daniel Normand5d70ea2019-06-05 15:13:43 -0700595 if OPTIONS.framework_misc_info_keys:
Daniel Norman03747412022-02-25 10:38:37 -0800596 OPTIONS.framework_misc_info_keys = common.LoadListFromFile(
Daniel Normand5d70ea2019-06-05 15:13:43 -0700597 OPTIONS.framework_misc_info_keys)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800598 else:
Daniel Norman5f476772022-03-02 15:46:34 -0800599 OPTIONS.framework_misc_info_keys = merge_utils.InferFrameworkMiscInfoKeys(
600 input_namelist=framework_namelist)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800601
Daniel Normand5d70ea2019-06-05 15:13:43 -0700602 if OPTIONS.vendor_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800603 OPTIONS.vendor_item_list = common.LoadListFromFile(OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800604 else:
Daniel Norman5f476772022-03-02 15:46:34 -0800605 OPTIONS.vendor_item_list = merge_utils.InferItemList(
606 input_namelist=vendor_namelist, framework=False)
Daniel Norman2465fc82022-03-02 12:01:20 -0800607 OPTIONS.vendor_partition_set = merge_utils.ItemListToPartitionSet(
Daniel Norman03747412022-02-25 10:38:37 -0800608 OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -0800609
Daniel Normanfdb38812019-04-15 09:47:24 -0700610 if OPTIONS.output_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800611 OPTIONS.output_item_list = common.LoadListFromFile(OPTIONS.output_item_list)
Daniel Normanfdb38812019-04-15 09:47:24 -0700612
Daniel Norman2465fc82022-03-02 12:01:20 -0800613 if not merge_utils.ValidateConfigLists():
Daniel Normane5964522019-03-19 10:32:03 -0700614 sys.exit(1)
615
Daniel Norman2465fc82022-03-02 12:01:20 -0800616 temp_dir = common.MakeTempDir(prefix='merge_target_files_')
617 try:
618 merge_target_files(temp_dir)
619 finally:
620 if OPTIONS.keep_tmp:
621 logger.info('Keeping temp_dir %s', temp_dir)
622 else:
623 common.Cleanup()
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800624
625
626if __name__ == '__main__':
Bill Peckham889b0c62019-02-21 18:53:37 -0800627 main()