blob: 25ed60e67448f771434d4ab7e7b78eaf04e70189 [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 fnmatch
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700106import glob
Daniel Normand3351562020-10-29 12:33:11 -0700107import json
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800108import logging
109import os
Bill Peckham19c3feb2020-03-20 18:31:43 -0700110import re
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
114import zipfile
Daniel Norman48603ff2021-02-22 15:15:24 -0800115from xml.etree import ElementTree
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800116
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800117import add_img_to_target_files
Daniel Normane9af70a2021-04-15 16:39:22 -0700118import apex_utils
Daniel Normandb8cacc2021-04-09 15:34:43 -0700119import build_image
Daniel Normanf0318252019-04-15 11:34:56 -0700120import build_super_image
Yifan Hongade0d3f2019-08-21 16:37:11 -0700121import check_target_files_vintf
Daniel Normanf0318252019-04-15 11:34:56 -0700122import common
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700123import img_from_target_files
Daniel Normanb8d52a22020-10-26 17:55:00 -0700124import find_shareduid_violation
Daniel Norman3b64ce12019-04-16 16:11:35 -0700125import ota_from_target_files
Daniel Normandb8cacc2021-04-09 15:34:43 -0700126import sparse_img
127import verity_utils
128
Daniel Normana84d13b2022-02-17 14:16:40 -0800129from common import ExternalError
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800130
131logger = logging.getLogger(__name__)
Tao Bao2ad4b822019-06-27 16:52:12 -0700132
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800133OPTIONS = common.OPTIONS
Bill Peckhamcb848172020-04-03 12:50:47 -0700134# Always turn on verbose logging.
135OPTIONS.verbose = True
Daniel Normand5d70ea2019-06-05 15:13:43 -0700136OPTIONS.framework_target_files = None
137OPTIONS.framework_item_list = None
138OPTIONS.framework_misc_info_keys = None
139OPTIONS.vendor_target_files = None
140OPTIONS.vendor_item_list = None
Bill Peckhamf753e152019-02-19 18:02:46 -0800141OPTIONS.output_target_files = None
Daniel Normanfdb38812019-04-15 09:47:24 -0700142OPTIONS.output_dir = None
143OPTIONS.output_item_list = None
Daniel Norman3b64ce12019-04-16 16:11:35 -0700144OPTIONS.output_ota = None
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700145OPTIONS.output_img = None
Daniel Normanf0318252019-04-15 11:34:56 -0700146OPTIONS.output_super_empty = None
Daniel Normana4911da2019-03-15 14:36:21 -0700147OPTIONS.rebuild_recovery = False
Daniel Normanb0c75912020-09-24 14:30:21 -0700148# TODO(b/150582573): Remove this option.
149OPTIONS.allow_duplicate_apkapex_keys = False
Daniel Norman571e1822021-06-25 17:18:25 -0700150OPTIONS.vendor_otatools = None
151OPTIONS.rebuild_sepolicy = False
Bill Peckhamf753e152019-02-19 18:02:46 -0800152OPTIONS.keep_tmp = False
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700153OPTIONS.framework_dexpreopt_config = None
154OPTIONS.framework_dexpreopt_tools = None
155OPTIONS.vendor_dexpreopt_config = None
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800156
Bill Peckham19c3feb2020-03-20 18:31:43 -0700157# In an item list (framework or vendor), we may see entries that select whole
158# partitions. Such an entry might look like this 'SYSTEM/*' (e.g., for the
159# system partition). The following regex matches this and extracts the
160# partition name.
161
162PARTITION_ITEM_PATTERN = re.compile(r'^([A-Z_]+)/\*$')
163
Bill Peckham5c7b0342020-04-03 15:36:23 -0700164# In apexkeys.txt or apkcerts.txt, we will find partition tags on each entry in
165# the file. We use these partition tags to filter the entries in those files
166# from the two different target files packages to produce a merged apexkeys.txt
167# or apkcerts.txt file. A partition tag (e.g., for the product partition) looks
168# like this: 'partition="product"'. We use the group syntax grab the value of
169# the tag. We use non-greedy matching in case there are other fields on the
170# same line.
Bill Peckham19c3feb2020-03-20 18:31:43 -0700171
Bill Peckham5c7b0342020-04-03 15:36:23 -0700172PARTITION_TAG_PATTERN = re.compile(r'partition="(.*?)"')
Bill Peckham19c3feb2020-03-20 18:31:43 -0700173
174# The sorting algorithm for apexkeys.txt and apkcerts.txt does not include the
175# ".apex" or ".apk" suffix, so we use the following pattern to extract a key.
176
177MODULE_KEY_PATTERN = re.compile(r'name="(.+)\.(apex|apk)"')
178
Daniel Normand5d70ea2019-06-05 15:13:43 -0700179# DEFAULT_FRAMEWORK_ITEM_LIST is a list of items to extract from the partial
180# framework target files package as is, meaning these items will land in the
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800181# output target files package exactly as they appear in the input partial
Daniel Normand5d70ea2019-06-05 15:13:43 -0700182# framework target files package.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800183
Daniel Normand5d70ea2019-06-05 15:13:43 -0700184DEFAULT_FRAMEWORK_ITEM_LIST = (
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800185 'META/apkcerts.txt',
186 'META/filesystem_config.txt',
187 'META/root_filesystem_config.txt',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800188 'META/update_engine_config.txt',
189 'PRODUCT/*',
190 'ROOT/*',
191 'SYSTEM/*',
Daniel Normanedf12472019-05-22 10:47:08 -0700192)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800193
Daniel Normand5d70ea2019-06-05 15:13:43 -0700194# DEFAULT_FRAMEWORK_MISC_INFO_KEYS is a list of keys to obtain from the
Daniel Normandbbf5a32020-10-22 16:03:32 -0700195# framework instance of META/misc_info.txt. The remaining keys should come
196# from the vendor instance.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800197
Daniel Normand5d70ea2019-06-05 15:13:43 -0700198DEFAULT_FRAMEWORK_MISC_INFO_KEYS = (
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800199 'avb_system_hashtree_enable',
200 'avb_system_add_hashtree_footer_args',
201 'avb_system_key_path',
202 'avb_system_algorithm',
203 'avb_system_rollback_index_location',
204 'avb_product_hashtree_enable',
205 'avb_product_add_hashtree_footer_args',
Justin Yun6151e3f2019-06-25 15:58:13 +0900206 'avb_system_ext_hashtree_enable',
207 'avb_system_ext_add_hashtree_footer_args',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800208 'system_root_image',
209 'root_dir',
210 'ab_update',
211 'default_system_dev_certificate',
212 'system_size',
Chris Gross203191b2020-05-30 02:39:12 +0000213 'building_system_image',
214 'building_system_ext_image',
215 'building_product_image',
Daniel Normanedf12472019-05-22 10:47:08 -0700216)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800217
Daniel Normand5d70ea2019-06-05 15:13:43 -0700218# DEFAULT_VENDOR_ITEM_LIST is a list of items to extract from the partial
219# vendor target files package as is, meaning these items will land in the output
220# target files package exactly as they appear in the input partial vendor target
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800221# files package.
222
Daniel Normand5d70ea2019-06-05 15:13:43 -0700223DEFAULT_VENDOR_ITEM_LIST = (
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800224 'META/boot_filesystem_config.txt',
225 'META/otakeys.txt',
226 'META/releasetools.py',
227 'META/vendor_filesystem_config.txt',
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800228 'BOOT/*',
229 'DATA/*',
230 'ODM/*',
231 'OTA/android-info.txt',
232 'PREBUILT_IMAGES/*',
233 'RADIO/*',
234 'VENDOR/*',
Daniel Normanedf12472019-05-22 10:47:08 -0700235)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800236
Daniel Normanedf12472019-05-22 10:47:08 -0700237# The merge config lists should not attempt to extract items from both
238# builds for any of the following partitions. The partitions in
239# SINGLE_BUILD_PARTITIONS should come entirely from a single build (either
Daniel Normand5d70ea2019-06-05 15:13:43 -0700240# framework or vendor, but not both).
Daniel Normanedf12472019-05-22 10:47:08 -0700241
242SINGLE_BUILD_PARTITIONS = (
243 'BOOT/',
244 'DATA/',
245 'ODM/',
246 'PRODUCT/',
Justin Yun6151e3f2019-06-25 15:58:13 +0900247 'SYSTEM_EXT/',
Daniel Normanedf12472019-05-22 10:47:08 -0700248 'RADIO/',
249 'RECOVERY/',
250 'ROOT/',
251 'SYSTEM/',
252 'SYSTEM_OTHER/',
253 'VENDOR/',
Yifan Hongcfb917a2020-05-07 14:58:20 -0700254 'VENDOR_DLKM/',
Yifan Hongf496f1b2020-07-15 16:52:59 -0700255 'ODM_DLKM/',
Ramji Jiyani13a41372022-01-27 07:05:08 +0000256 'SYSTEM_DLKM/',
Daniel Normanedf12472019-05-22 10:47:08 -0700257)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800258
259
Chris Grossfabf50a2019-05-02 12:42:09 -0700260def write_sorted_data(data, path):
Tao Bao2ad4b822019-06-27 16:52:12 -0700261 """Writes the sorted contents of either a list or dict to file.
Chris Grossfabf50a2019-05-02 12:42:09 -0700262
Tao Bao2ad4b822019-06-27 16:52:12 -0700263 This function sorts the contents of the list or dict and then writes the
264 resulting sorted contents to a file specified by path.
Chris Grossfabf50a2019-05-02 12:42:09 -0700265
266 Args:
267 data: The list or dict to sort and write.
268 path: Path to the file to write the sorted values to. The file at path will
269 be overridden if it exists.
270 """
271 with open(path, 'w') as output:
Daniel Normand5d70ea2019-06-05 15:13:43 -0700272 for entry in sorted(data):
Chris Grossfabf50a2019-05-02 12:42:09 -0700273 out_str = '{}={}\n'.format(entry, data[entry]) if isinstance(
274 data, dict) else '{}\n'.format(entry)
275 output.write(out_str)
276
277
Daniel Norman03747412022-02-25 10:38:37 -0800278def extract_items(input_zip, output_dir, extract_item_list):
279 """Extracts items in extra_item_list from a zip to a dir."""
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800280
Daniel Norman03747412022-02-25 10:38:37 -0800281 logger.info('extracting from %s', input_zip)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800282
283 # Filter the extract_item_list to remove any items that do not exist in the
284 # zip file. Otherwise, the extraction step will fail.
285
Daniel Norman03747412022-02-25 10:38:37 -0800286 with zipfile.ZipFile(input_zip, allowZip64=True) as input_zipfile:
287 input_namelist = input_zipfile.namelist()
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800288
289 filtered_extract_item_list = []
290 for pattern in extract_item_list:
Daniel Norman03747412022-02-25 10:38:37 -0800291 matching_namelist = fnmatch.filter(input_namelist, pattern)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800292 if not matching_namelist:
293 logger.warning('no match for %s', pattern)
294 else:
295 filtered_extract_item_list.append(pattern)
296
Daniel Norman03747412022-02-25 10:38:37 -0800297 common.UnzipToDir(input_zip, output_dir, filtered_extract_item_list)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800298
299
Daniel Normanfdb38812019-04-15 09:47:24 -0700300def copy_items(from_dir, to_dir, patterns):
301 """Similar to extract_items() except uses an input dir instead of zip."""
302 file_paths = []
303 for dirpath, _, filenames in os.walk(from_dir):
Daniel Normane5b134a2019-04-17 14:54:06 -0700304 file_paths.extend(
305 os.path.relpath(path=os.path.join(dirpath, filename), start=from_dir)
306 for filename in filenames)
Daniel Normanfdb38812019-04-15 09:47:24 -0700307
308 filtered_file_paths = set()
309 for pattern in patterns:
310 filtered_file_paths.update(fnmatch.filter(file_paths, pattern))
311
312 for file_path in filtered_file_paths:
313 original_file_path = os.path.join(from_dir, file_path)
314 copied_file_path = os.path.join(to_dir, file_path)
315 copied_file_dir = os.path.dirname(copied_file_path)
316 if not os.path.exists(copied_file_dir):
317 os.makedirs(copied_file_dir)
318 if os.path.islink(original_file_path):
319 os.symlink(os.readlink(original_file_path), copied_file_path)
320 else:
321 shutil.copyfile(original_file_path, copied_file_path)
322
323
Daniel Norman03747412022-02-25 10:38:37 -0800324def validate_config_lists():
Daniel Normane5964522019-03-19 10:32:03 -0700325 """Performs validations on the merge config lists.
326
Daniel Normane5964522019-03-19 10:32:03 -0700327 Returns:
328 False if a validation fails, otherwise true.
329 """
Daniel Normanedf12472019-05-22 10:47:08 -0700330 has_error = False
331
Daniel Normand5d70ea2019-06-05 15:13:43 -0700332 default_combined_item_set = set(DEFAULT_FRAMEWORK_ITEM_LIST)
333 default_combined_item_set.update(DEFAULT_VENDOR_ITEM_LIST)
Daniel Normane5964522019-03-19 10:32:03 -0700334
Daniel Norman03747412022-02-25 10:38:37 -0800335 combined_item_set = set(OPTIONS.framework_item_list)
336 combined_item_set.update(OPTIONS.vendor_item_list)
Daniel Normane5964522019-03-19 10:32:03 -0700337
338 # Check that the merge config lists are not missing any item specified
339 # by the default config lists.
340 difference = default_combined_item_set.difference(combined_item_set)
341 if difference:
Daniel Normane5b134a2019-04-17 14:54:06 -0700342 logger.error('Missing merge config items: %s', list(difference))
Daniel Normane5964522019-03-19 10:32:03 -0700343 logger.error('Please ensure missing items are in either the '
Daniel Normand5d70ea2019-06-05 15:13:43 -0700344 'framework-item-list or vendor-item-list files provided to '
Daniel Normane5964522019-03-19 10:32:03 -0700345 'this script.')
Daniel Normanedf12472019-05-22 10:47:08 -0700346 has_error = True
347
Daniel Normandbbf5a32020-10-22 16:03:32 -0700348 # Check that partitions only come from one input.
Daniel Normanedf12472019-05-22 10:47:08 -0700349 for partition in SINGLE_BUILD_PARTITIONS:
Daniel Normandbbf5a32020-10-22 16:03:32 -0700350 image_path = 'IMAGES/{}.img'.format(partition.lower().replace('/', ''))
351 in_framework = (
Daniel Norman03747412022-02-25 10:38:37 -0800352 any(item.startswith(partition) for item in OPTIONS.framework_item_list)
353 or image_path in OPTIONS.framework_item_list)
Daniel Normandbbf5a32020-10-22 16:03:32 -0700354 in_vendor = (
Daniel Norman03747412022-02-25 10:38:37 -0800355 any(item.startswith(partition) for item in OPTIONS.vendor_item_list) or
356 image_path in OPTIONS.vendor_item_list)
Daniel Normand5d70ea2019-06-05 15:13:43 -0700357 if in_framework and in_vendor:
Daniel Normanedf12472019-05-22 10:47:08 -0700358 logger.error(
Tao Bao2ad4b822019-06-27 16:52:12 -0700359 'Cannot extract items from %s for both the framework and vendor'
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +0900360 ' builds. Please ensure only one merge config item list'
Tao Bao2ad4b822019-06-27 16:52:12 -0700361 ' includes %s.', partition, partition)
Daniel Normanedf12472019-05-22 10:47:08 -0700362 has_error = True
Daniel Normane5964522019-03-19 10:32:03 -0700363
Daniel Norman03747412022-02-25 10:38:37 -0800364 if ('dynamic_partition_list' in OPTIONS.framework_misc_info_keys) or (
365 'super_partition_groups' in OPTIONS.framework_misc_info_keys):
Daniel Norman19b9fe92019-03-19 14:48:02 -0700366 logger.error('Dynamic partition misc info keys should come from '
Daniel Normand5d70ea2019-06-05 15:13:43 -0700367 'the vendor instance of META/misc_info.txt.')
Daniel Normanedf12472019-05-22 10:47:08 -0700368 has_error = True
Daniel Norman19b9fe92019-03-19 14:48:02 -0700369
Daniel Normanedf12472019-05-22 10:47:08 -0700370 return not has_error
Daniel Normane5964522019-03-19 10:32:03 -0700371
372
Daniel Norman03747412022-02-25 10:38:37 -0800373def merge_ab_partitions_txt(framework_meta_dir, vendor_meta_dir,
374 merged_meta_dir):
375 """Merges META/ab_partitions.txt.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800376
Daniel Norman03747412022-02-25 10:38:37 -0800377 The output contains the union of the partition names.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800378 """
Daniel Norman03747412022-02-25 10:38:37 -0800379 with open(os.path.join(framework_meta_dir, 'ab_partitions.txt')) as f:
Daniel Normand5d70ea2019-06-05 15:13:43 -0700380 framework_ab_partitions = f.read().splitlines()
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800381
Daniel Norman03747412022-02-25 10:38:37 -0800382 with open(os.path.join(vendor_meta_dir, 'ab_partitions.txt')) as f:
Daniel Normand5d70ea2019-06-05 15:13:43 -0700383 vendor_ab_partitions = f.read().splitlines()
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800384
Daniel Norman03747412022-02-25 10:38:37 -0800385 write_sorted_data(
386 data=set(framework_ab_partitions + vendor_ab_partitions),
387 path=os.path.join(merged_meta_dir, 'ab_partitions.txt'))
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800388
389
Daniel Norman03747412022-02-25 10:38:37 -0800390def merge_misc_info_txt(framework_meta_dir, vendor_meta_dir, merged_meta_dir):
391 """Merges META/misc_info.txt.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800392
Daniel Norman03747412022-02-25 10:38:37 -0800393 The output contains a combination of key=value pairs from both inputs.
394 Most pairs are taken from the vendor input, while some are taken from
395 the framework input.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800396 """
397
Daniel Norman03747412022-02-25 10:38:37 -0800398 OPTIONS.framework_misc_info = common.LoadDictionaryFromFile(
399 os.path.join(framework_meta_dir, 'misc_info.txt'))
400 OPTIONS.vendor_misc_info = common.LoadDictionaryFromFile(
401 os.path.join(vendor_meta_dir, 'misc_info.txt'))
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800402
Daniel Norman03747412022-02-25 10:38:37 -0800403 # Merged misc info is a combination of vendor misc info plus certain values
404 # from the framework misc info.
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800405
Daniel Norman03747412022-02-25 10:38:37 -0800406 merged_dict = OPTIONS.vendor_misc_info
407 for key in OPTIONS.framework_misc_info_keys:
408 merged_dict[key] = OPTIONS.framework_misc_info[key]
Daniel Norman19b9fe92019-03-19 14:48:02 -0700409
Daniel Norman38888d32020-11-19 14:51:15 -0800410 # If AVB is enabled then ensure that we build vbmeta.img.
411 # Partial builds with AVB enabled may set PRODUCT_BUILD_VBMETA_IMAGE=false to
412 # skip building an incomplete vbmeta.img.
413 if merged_dict.get('avb_enable') == 'true':
414 merged_dict['avb_building_vbmeta_image'] = 'true'
415
Daniel Norman03747412022-02-25 10:38:37 -0800416 return merged_dict
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800417
418
Daniel Norman03747412022-02-25 10:38:37 -0800419def merge_dynamic_partitions_info_txt(framework_meta_dir, vendor_meta_dir,
420 merged_meta_dir):
421 """Merge META/dynamic_partitions_info.txt."""
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +0900422 framework_dynamic_partitions_dict = common.LoadDictionaryFromFile(
Daniel Norman03747412022-02-25 10:38:37 -0800423 os.path.join(framework_meta_dir, 'dynamic_partitions_info.txt'))
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +0900424 vendor_dynamic_partitions_dict = common.LoadDictionaryFromFile(
Daniel Norman03747412022-02-25 10:38:37 -0800425 os.path.join(vendor_meta_dir, 'dynamic_partitions_info.txt'))
Daniel Normana61cde02019-05-03 14:19:13 -0700426
Daniel Normanbfc51ef2019-07-24 14:34:54 -0700427 merged_dynamic_partitions_dict = common.MergeDynamicPartitionInfoDicts(
Daniel Normand5d70ea2019-06-05 15:13:43 -0700428 framework_dict=framework_dynamic_partitions_dict,
Daniel Norman55417142019-11-25 16:04:36 -0800429 vendor_dict=vendor_dynamic_partitions_dict)
Daniel Normana61cde02019-05-03 14:19:13 -0700430
Chris Grossfabf50a2019-05-02 12:42:09 -0700431 write_sorted_data(
432 data=merged_dynamic_partitions_dict,
Daniel Norman03747412022-02-25 10:38:37 -0800433 path=os.path.join(merged_meta_dir, 'dynamic_partitions_info.txt'))
434
435 # Merge misc info keys used for Dynamic Partitions.
436 OPTIONS.merged_misc_info.update(merged_dynamic_partitions_dict)
437 # Ensure that add_img_to_target_files rebuilds super split images for
438 # devices that retrofit dynamic partitions. This flag may have been set to
439 # false in the partial builds to prevent duplicate building of super.img.
440 OPTIONS.merged_misc_info['build_super_partition'] = 'true'
Chris Grossfabf50a2019-05-02 12:42:09 -0700441
442
Bill Peckham19c3feb2020-03-20 18:31:43 -0700443def item_list_to_partition_set(item_list):
444 """Converts a target files item list to a partition set.
445
446 The item list contains items that might look like 'SYSTEM/*' or 'VENDOR/*' or
447 'OTA/android-info.txt'. Items that end in '/*' are assumed to match entire
448 directories where 'SYSTEM' or 'VENDOR' is a directory name that identifies the
449 contents of a partition of the same name. Other items in the list, such as the
450 'OTA' example contain metadata. This function iterates such a list, returning
451 a set that contains the partition entries.
452
453 Args:
454 item_list: A list of items in a target files package.
Daniel Normanb0c75912020-09-24 14:30:21 -0700455
Bill Peckham19c3feb2020-03-20 18:31:43 -0700456 Returns:
457 A set of partitions extracted from the list of items.
458 """
459
460 partition_set = set()
461
462 for item in item_list:
463 match = PARTITION_ITEM_PATTERN.search(item.strip())
464 partition_tag = match.group(1).lower() if match else None
465
466 if partition_tag:
467 partition_set.add(partition_tag)
468
469 return partition_set
470
471
Daniel Norman03747412022-02-25 10:38:37 -0800472def merge_package_keys_txt(framework_meta_dir, vendor_meta_dir, merged_meta_dir,
473 file_name):
474 """Merges APK/APEX key list files."""
Chris Grossfabf50a2019-05-02 12:42:09 -0700475
Daniel Norman03747412022-02-25 10:38:37 -0800476 if file_name not in ('apkcerts.txt', 'apexkeys.txt'):
477 raise ExternalError(
478 'Unexpected file_name provided to merge_package_keys_txt: %s',
479 file_name)
Chris Grossfabf50a2019-05-02 12:42:09 -0700480
481 def read_helper(d):
482 temp = {}
Daniel Norman03747412022-02-25 10:38:37 -0800483 with open(os.path.join(d, file_name)) as f:
484 for line in f.read().splitlines():
485 line = line.strip()
486 if line:
487 name_search = MODULE_KEY_PATTERN.search(line.split()[0])
488 temp[name_search.group(1)] = line
Chris Grossfabf50a2019-05-02 12:42:09 -0700489 return temp
490
Daniel Norman03747412022-02-25 10:38:37 -0800491 framework_dict = read_helper(framework_meta_dir)
492 vendor_dict = read_helper(vendor_meta_dir)
Bill Peckham19c3feb2020-03-20 18:31:43 -0700493 merged_dict = {}
Chris Grossfabf50a2019-05-02 12:42:09 -0700494
Bill Peckham19c3feb2020-03-20 18:31:43 -0700495 def filter_into_merged_dict(item_dict, partition_set):
496 for key, value in item_dict.items():
Daniel Norman03747412022-02-25 10:38:37 -0800497 tag_search = PARTITION_TAG_PATTERN.search(value)
Bill Peckham19c3feb2020-03-20 18:31:43 -0700498
Daniel Norman03747412022-02-25 10:38:37 -0800499 if tag_search is None:
Bill Peckham19c3feb2020-03-20 18:31:43 -0700500 raise ValueError('Entry missing partition tag: %s' % value)
501
Daniel Norman03747412022-02-25 10:38:37 -0800502 partition_tag = tag_search.group(1)
Bill Peckham19c3feb2020-03-20 18:31:43 -0700503
504 if partition_tag in partition_set:
505 if key in merged_dict:
Daniel Normanb0c75912020-09-24 14:30:21 -0700506 if OPTIONS.allow_duplicate_apkapex_keys:
507 # TODO(b/150582573) Always raise on duplicates.
508 logger.warning('Duplicate key %s' % key)
509 continue
510 else:
511 raise ValueError('Duplicate key %s' % key)
Bill Peckham19c3feb2020-03-20 18:31:43 -0700512
513 merged_dict[key] = value
514
Daniel Norman03747412022-02-25 10:38:37 -0800515 # Prioritize framework keys first.
516 # Duplicate keys from vendor are an error, or ignored.
517 filter_into_merged_dict(framework_dict, OPTIONS.framework_partition_set)
518 filter_into_merged_dict(vendor_dict, OPTIONS.vendor_partition_set)
Chris Grossfabf50a2019-05-02 12:42:09 -0700519
Bill Peckham19c3feb2020-03-20 18:31:43 -0700520 # The following code is similar to write_sorted_data, but different enough
521 # that we couldn't use that function. We need the output to be sorted by the
522 # basename of the apex/apk (without the ".apex" or ".apk" suffix). This
523 # allows the sort to be consistent with the framework/vendor input data and
524 # eases comparison of input data with merged data.
Daniel Norman03747412022-02-25 10:38:37 -0800525 with open(os.path.join(merged_meta_dir, file_name), 'w') as output:
526 for key, value in sorted(merged_dict.items()):
527 output.write(value + '\n')
Daniel Normana61cde02019-05-03 14:19:13 -0700528
529
Daniel Norman03747412022-02-25 10:38:37 -0800530def create_file_contexts_copies(framework_meta_dir, vendor_meta_dir,
531 merged_meta_dir):
532 """Creates named copies of each partial build's file_contexts.bin.
Daniel Normand5d70ea2019-06-05 15:13:43 -0700533
Daniel Norman03747412022-02-25 10:38:37 -0800534 Used when regenerating images from the partial build.
535 """
536
537 def copy_fc_file(source_dir, file_name):
538 for name in (file_name, 'file_contexts.bin'):
539 fc_path = os.path.join(source_dir, name)
540 if os.path.exists(fc_path):
541 shutil.copyfile(fc_path, os.path.join(merged_meta_dir, file_name))
542 return
543 raise ValueError('Missing file_contexts file from %s: %s', source_dir,
544 file_name)
545
546 copy_fc_file(framework_meta_dir, 'framework_file_contexts.bin')
547 copy_fc_file(vendor_meta_dir, 'vendor_file_contexts.bin')
548
549 # Replace <image>_selinux_fc values with framework or vendor file_contexts.bin
550 # depending on which dictionary the key came from.
551 # Only the file basename is required because all selinux_fc properties are
552 # replaced with the full path to the file under META/ when misc_info.txt is
553 # loaded from target files for repacking. See common.py LoadInfoDict().
554 for key in OPTIONS.vendor_misc_info:
555 if key.endswith('_selinux_fc'):
556 OPTIONS.merged_misc_info[key] = 'vendor_file_contexts.bin'
557 for key in OPTIONS.framework_misc_info:
558 if key.endswith('_selinux_fc'):
559 OPTIONS.merged_misc_info[key] = 'framework_file_contexts.bin'
Daniel Norman72c626f2019-05-13 15:58:14 -0700560
561
Daniel Norman03747412022-02-25 10:38:37 -0800562def compile_split_sepolicy(target_files_dir, partition_map):
Daniel Norman48603ff2021-02-22 15:15:24 -0800563 """Uses secilc to compile a split sepolicy file.
564
565 Depends on various */etc/selinux/* and */etc/vintf/* files within partitions.
566
567 Args:
Daniel Norman03747412022-02-25 10:38:37 -0800568 target_files_dir: Extracted directory of target_files, containing partition
569 directories.
570 partition_map: A map of partition name -> relative path within
571 target_files_dir.
Daniel Norman48603ff2021-02-22 15:15:24 -0800572
573 Returns:
574 A command list that can be executed to create the compiled sepolicy.
575 """
576
577 def get_file(partition, path):
578 if partition not in partition_map:
579 logger.warning('Cannot load SEPolicy files for missing partition %s',
580 partition)
581 return None
Daniel Norman03747412022-02-25 10:38:37 -0800582 return os.path.join(target_files_dir, partition_map[partition], path)
Daniel Norman48603ff2021-02-22 15:15:24 -0800583
584 # Load the kernel sepolicy version from the FCM. This is normally provided
585 # directly to selinux.cpp as a build flag, but is also available in this file.
586 fcm_file = get_file('system', 'etc/vintf/compatibility_matrix.device.xml')
587 if not fcm_file or not os.path.exists(fcm_file):
588 raise ExternalError('Missing required file for loading sepolicy: %s', fcm)
589 kernel_sepolicy_version = ElementTree.parse(fcm_file).getroot().find(
590 'sepolicy/kernel-sepolicy-version').text
591
592 # Load the vendor's plat sepolicy version. This is the version used for
593 # locating sepolicy mapping files.
594 vendor_plat_version_file = get_file('vendor',
595 'etc/selinux/plat_sepolicy_vers.txt')
596 if not vendor_plat_version_file or not os.path.exists(
Daniel Norman2d7989a2021-04-05 17:40:47 +0000597 vendor_plat_version_file):
Daniel Norman48603ff2021-02-22 15:15:24 -0800598 raise ExternalError('Missing required sepolicy file %s',
599 vendor_plat_version_file)
600 with open(vendor_plat_version_file) as f:
601 vendor_plat_version = f.read().strip()
602
603 # Use the same flags and arguments as selinux.cpp OpenSplitPolicy().
604 cmd = ['secilc', '-m', '-M', 'true', '-G', '-N']
605 cmd.extend(['-c', kernel_sepolicy_version])
Daniel Norman03747412022-02-25 10:38:37 -0800606 cmd.extend(['-o', os.path.join(target_files_dir, 'META/combined_sepolicy')])
Daniel Norman48603ff2021-02-22 15:15:24 -0800607 cmd.extend(['-f', '/dev/null'])
608
609 required_policy_files = (
610 ('system', 'etc/selinux/plat_sepolicy.cil'),
611 ('system', 'etc/selinux/mapping/%s.cil' % vendor_plat_version),
612 ('vendor', 'etc/selinux/vendor_sepolicy.cil'),
613 ('vendor', 'etc/selinux/plat_pub_versioned.cil'),
614 )
615 for policy in (map(lambda partition_and_path: get_file(*partition_and_path),
616 required_policy_files)):
617 if not policy or not os.path.exists(policy):
618 raise ExternalError('Missing required sepolicy file %s', policy)
619 cmd.append(policy)
620
621 optional_policy_files = (
622 ('system', 'etc/selinux/mapping/%s.compat.cil' % vendor_plat_version),
623 ('system_ext', 'etc/selinux/system_ext_sepolicy.cil'),
624 ('system_ext', 'etc/selinux/mapping/%s.cil' % vendor_plat_version),
625 ('product', 'etc/selinux/product_sepolicy.cil'),
626 ('product', 'etc/selinux/mapping/%s.cil' % vendor_plat_version),
627 ('odm', 'etc/selinux/odm_sepolicy.cil'),
628 )
629 for policy in (map(lambda partition_and_path: get_file(*partition_and_path),
630 optional_policy_files)):
631 if policy and os.path.exists(policy):
632 cmd.append(policy)
633
634 return cmd
635
636
Daniel Norman03747412022-02-25 10:38:37 -0800637def validate_merged_apex_info(target_files_dir, partitions):
Daniel Normane9af70a2021-04-15 16:39:22 -0700638 """Validates the APEX files in the merged target files directory.
639
640 Checks the APEX files in all possible preinstalled APEX directories.
641 Depends on the <partition>/apex/* APEX files within partitions.
642
643 Args:
Daniel Norman03747412022-02-25 10:38:37 -0800644 target_files_dir: Extracted directory of target_files, containing partition
Daniel Norman571e1822021-06-25 17:18:25 -0700645 directories.
Daniel Normane9af70a2021-04-15 16:39:22 -0700646 partitions: A list of all the partitions in the output directory.
647
648 Raises:
649 RuntimeError: if apex_utils fails to parse any APEX file.
650 ExternalError: if the same APEX package is provided by multiple partitions.
651 """
652 apex_packages = set()
653
Daniel Norman03747412022-02-25 10:38:37 -0800654 apex_partitions = ('system', 'system_ext', 'product', 'vendor', 'odm')
Daniel Normane9af70a2021-04-15 16:39:22 -0700655 for partition in filter(lambda p: p in apex_partitions, partitions):
656 apex_info = apex_utils.GetApexInfoFromTargetFiles(
Daniel Norman03747412022-02-25 10:38:37 -0800657 target_files_dir, partition, compressed_only=False)
Daniel Normane9af70a2021-04-15 16:39:22 -0700658 partition_apex_packages = set([info.package_name for info in apex_info])
659 duplicates = apex_packages.intersection(partition_apex_packages)
660 if duplicates:
661 raise ExternalError(
662 'Duplicate APEX packages found in multiple partitions: %s' %
663 ' '.join(duplicates))
664 apex_packages.update(partition_apex_packages)
665
666
Daniel Norman799a18c2022-03-04 15:48:47 -0800667def update_care_map_image_size_props(images_dir):
668 """Sets <partition>_image_size props in misc_info.
Daniel Normandb8cacc2021-04-09 15:34:43 -0700669
Daniel Norman799a18c2022-03-04 15:48:47 -0800670 add_images_to_target_files uses these props to generate META/care_map.pb.
671 Regenerated images will have this property set during regeneration.
Daniel Normandb8cacc2021-04-09 15:34:43 -0700672
Daniel Norman799a18c2022-03-04 15:48:47 -0800673 However, images copied directly from input partial target files packages
674 need this value calculated here.
Daniel Normandb8cacc2021-04-09 15:34:43 -0700675 """
Daniel Norman799a18c2022-03-04 15:48:47 -0800676 for partition in common.PARTITIONS_WITH_CARE_MAP:
677 image_path = os.path.join(images_dir, '{}.img'.format(partition))
Daniel Normandb8cacc2021-04-09 15:34:43 -0700678 if os.path.exists(image_path):
Daniel Norman799a18c2022-03-04 15:48:47 -0800679 partition_size = sparse_img.GetImagePartitionSize(image_path)
680 image_props = build_image.ImagePropFromGlobalDict(
681 OPTIONS.merged_misc_info, partition)
682 verity_image_builder = verity_utils.CreateVerityImageBuilder(image_props)
683 image_size = verity_image_builder.CalculateMaxImageSize(partition_size)
684 OPTIONS.merged_misc_info['{}_image_size'.format(partition)] = image_size
Daniel Normandb8cacc2021-04-09 15:34:43 -0700685
Daniel Normandb8cacc2021-04-09 15:34:43 -0700686
Daniel Norman03747412022-02-25 10:38:37 -0800687def merge_meta_files(temp_dir, merged_dir):
688 """Merges various files in META/*."""
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800689
Daniel Norman03747412022-02-25 10:38:37 -0800690 framework_meta_dir = os.path.join(temp_dir, 'framework_meta', 'META')
691 extract_items(
692 input_zip=OPTIONS.framework_target_files,
693 output_dir=os.path.dirname(framework_meta_dir),
694 extract_item_list=('META/*',))
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800695
Daniel Norman03747412022-02-25 10:38:37 -0800696 vendor_meta_dir = os.path.join(temp_dir, 'vendor_meta', 'META')
697 extract_items(
698 input_zip=OPTIONS.vendor_target_files,
699 output_dir=os.path.dirname(vendor_meta_dir),
700 extract_item_list=('META/*',))
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800701
Daniel Norman03747412022-02-25 10:38:37 -0800702 merged_meta_dir = os.path.join(merged_dir, 'META')
Bill Peckham364c1cc2019-03-29 18:27:23 -0700703
Daniel Norman03747412022-02-25 10:38:37 -0800704 # Merge META/misc_info.txt into OPTIONS.merged_misc_info,
705 # but do not write it yet. The following functions may further
706 # modify this dict.
707 OPTIONS.merged_misc_info = merge_misc_info_txt(
708 framework_meta_dir=framework_meta_dir,
709 vendor_meta_dir=vendor_meta_dir,
710 merged_meta_dir=merged_meta_dir)
Daniel Norman72c626f2019-05-13 15:58:14 -0700711
Daniel Norman03747412022-02-25 10:38:37 -0800712 create_file_contexts_copies(
713 framework_meta_dir=framework_meta_dir,
714 vendor_meta_dir=vendor_meta_dir,
715 merged_meta_dir=merged_meta_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -0800716
Daniel Normanc44f9642022-03-03 19:41:13 +0000717 if OPTIONS.merged_misc_info.get('use_dynamic_partitions') == 'true':
Daniel Norman03747412022-02-25 10:38:37 -0800718 merge_dynamic_partitions_info_txt(
719 framework_meta_dir=framework_meta_dir,
720 vendor_meta_dir=vendor_meta_dir,
721 merged_meta_dir=merged_meta_dir)
Daniel Normana61cde02019-05-03 14:19:13 -0700722
Daniel Normanc44f9642022-03-03 19:41:13 +0000723 if OPTIONS.merged_misc_info.get('ab_update') == 'true':
Daniel Norman03747412022-02-25 10:38:37 -0800724 merge_ab_partitions_txt(
725 framework_meta_dir=framework_meta_dir,
726 vendor_meta_dir=vendor_meta_dir,
727 merged_meta_dir=merged_meta_dir)
Daniel Norman799a18c2022-03-04 15:48:47 -0800728 update_care_map_image_size_props(images_dir=os.path.join(merged_dir, 'IMAGES'))
Chris Grossfabf50a2019-05-02 12:42:09 -0700729
Daniel Norman03747412022-02-25 10:38:37 -0800730 for file_name in ('apkcerts.txt', 'apexkeys.txt'):
731 merge_package_keys_txt(
732 framework_meta_dir=framework_meta_dir,
733 vendor_meta_dir=vendor_meta_dir,
734 merged_meta_dir=merged_meta_dir,
735 file_name=file_name)
Chris Grossfabf50a2019-05-02 12:42:09 -0700736
Daniel Norman03747412022-02-25 10:38:37 -0800737 # Write the now-finalized OPTIONS.merged_misc_info.
738 write_sorted_data(
739 data=OPTIONS.merged_misc_info,
740 path=os.path.join(merged_meta_dir, 'misc_info.txt'))
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700741
742
Daniel Norman03747412022-02-25 10:38:37 -0800743def process_dexopt(temp_dir, output_target_files_dir):
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700744 """If needed, generates dexopt files for vendor apps.
745
746 Args:
747 temp_dir: Location containing an 'output' directory where target files have
Daniel Normane292f5f2022-02-17 14:16:10 -0800748 been extracted, e.g. <temp_dir>/output/SYSTEM, <temp_dir>/output/IMAGES,
749 etc.
Daniel Norman03747412022-02-25 10:38:37 -0800750 output_target_files_dir: The name of a directory that will be used to create
751 the output target files package after all the special cases are processed.
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700752 """
753 # Load vendor and framework META/misc_info.txt.
Daniel Norman03747412022-02-25 10:38:37 -0800754 if (OPTIONS.vendor_misc_info.get('building_with_vsdk') != 'true' or
755 OPTIONS.framework_dexpreopt_tools is None or
756 OPTIONS.framework_dexpreopt_config is None or
757 OPTIONS.vendor_dexpreopt_config is None):
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700758 return
759
760 logger.info('applying dexpreopt')
761
762 # The directory structure to apply dexpreopt is:
763 #
764 # <temp_dir>/
765 # framework_meta/
766 # META/
767 # vendor_meta/
768 # META/
769 # output/
770 # SYSTEM/
771 # VENDOR/
772 # IMAGES/
773 # <other items extracted from system and vendor target files>
774 # tools/
775 # <contents of dexpreopt_tools.zip>
776 # system_config/
777 # <contents of system dexpreopt_config.zip>
778 # vendor_config/
779 # <contents of vendor dexpreopt_config.zip>
780 # system -> output/SYSTEM
781 # vendor -> output/VENDOR
782 # apex -> output/SYSTEM/apex (only for flattened APEX builds)
783 # apex/ (extracted updatable APEX)
784 # <apex 1>/
785 # ...
786 # <apex 2>/
787 # ...
788 # ...
789 # out/dex2oat_result/vendor/
790 # <app>
791 # oat/arm64/
792 # package.vdex
793 # package.odex
794 # <priv-app>
795 # oat/arm64/
796 # package.vdex
797 # package.odex
798 dexpreopt_tools_files_temp_dir = os.path.join(temp_dir, 'tools')
Daniel Normane292f5f2022-02-17 14:16:10 -0800799 dexpreopt_framework_config_files_temp_dir = os.path.join(
800 temp_dir, 'system_config')
801 dexpreopt_vendor_config_files_temp_dir = os.path.join(temp_dir,
802 'vendor_config')
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700803
804 extract_items(
Daniel Norman03747412022-02-25 10:38:37 -0800805 input_zip=OPTIONS.framework_dexpreopt_tools,
806 output_dir=dexpreopt_tools_files_temp_dir,
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700807 extract_item_list=('*',))
808 extract_items(
Daniel Norman03747412022-02-25 10:38:37 -0800809 input_zip=OPTIONS.framework_dexpreopt_config,
810 output_dir=dexpreopt_framework_config_files_temp_dir,
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700811 extract_item_list=('*',))
812 extract_items(
Daniel Norman03747412022-02-25 10:38:37 -0800813 input_zip=OPTIONS.vendor_dexpreopt_config,
814 output_dir=dexpreopt_vendor_config_files_temp_dir,
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700815 extract_item_list=('*',))
816
Daniel Normane292f5f2022-02-17 14:16:10 -0800817 os.symlink(
Daniel Norman03747412022-02-25 10:38:37 -0800818 os.path.join(output_target_files_dir, 'SYSTEM'),
Daniel Normane292f5f2022-02-17 14:16:10 -0800819 os.path.join(temp_dir, 'system'))
820 os.symlink(
Daniel Norman03747412022-02-25 10:38:37 -0800821 os.path.join(output_target_files_dir, 'VENDOR'),
Daniel Normane292f5f2022-02-17 14:16:10 -0800822 os.path.join(temp_dir, 'vendor'))
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700823
824 # The directory structure for flatteded APEXes is:
825 #
826 # SYSTEM
827 # apex
828 # <APEX name, e.g., com.android.wifi>
829 # apex_manifest.pb
830 # apex_pubkey
831 # etc/
832 # javalib/
833 # lib/
834 # lib64/
835 # priv-app/
836 #
837 # The directory structure for updatable APEXes is:
838 #
839 # SYSTEM
840 # apex
841 # com.android.adbd.apex
842 # com.android.appsearch.apex
843 # com.android.art.apex
844 # ...
Daniel Norman03747412022-02-25 10:38:37 -0800845 apex_root = os.path.join(output_target_files_dir, 'SYSTEM', 'apex')
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700846
847 # Check for flattended versus updatable APEX.
Daniel Norman03747412022-02-25 10:38:37 -0800848 if OPTIONS.framework_misc_info.get('target_flatten_apex') == 'false':
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700849 # Extract APEX.
850 logging.info('extracting APEX')
851
852 apex_extract_root_dir = os.path.join(temp_dir, 'apex')
853 os.makedirs(apex_extract_root_dir)
854
855 for apex in (glob.glob(os.path.join(apex_root, '*.apex')) +
856 glob.glob(os.path.join(apex_root, '*.capex'))):
857 logging.info(' apex: %s', apex)
858 # deapexer is in the same directory as the merge_target_files binary extracted
859 # from otatools.zip.
860 apex_json_info = subprocess.check_output(['deapexer', 'info', apex])
861 logging.info(' info: %s', apex_json_info)
862 apex_info = json.loads(apex_json_info)
863 apex_name = apex_info['name']
864 logging.info(' name: %s', apex_name)
865
866 apex_extract_dir = os.path.join(apex_extract_root_dir, apex_name)
867 os.makedirs(apex_extract_dir)
868
869 # deapexer uses debugfs_static, which is part of otatools.zip.
870 command = [
871 'deapexer',
872 '--debugfs_path',
873 'debugfs_static',
874 'extract',
875 apex,
876 apex_extract_dir,
877 ]
878 logging.info(' running %s', command)
879 subprocess.check_call(command)
880 else:
881 # Flattened APEXes don't need to be extracted since they have the necessary
882 # directory structure.
883 os.symlink(os.path.join(apex_root), os.path.join(temp_dir, 'apex'))
884
885 # Modify system config to point to the tools that have been extracted.
886 # Absolute or .. paths are not allowed by the dexpreopt_gen tool in
887 # dexpreopt_soong.config.
888 dexpreopt_framework_soon_config = os.path.join(
889 dexpreopt_framework_config_files_temp_dir, 'dexpreopt_soong.config')
890 with open(dexpreopt_framework_soon_config, 'w') as f:
891 dexpreopt_soong_config = {
892 'Profman': 'tools/profman',
893 'Dex2oat': 'tools/dex2oatd',
894 'Aapt': 'tools/aapt2',
895 'SoongZip': 'tools/soong_zip',
896 'Zip2zip': 'tools/zip2zip',
897 'ManifestCheck': 'tools/manifest_check',
898 'ConstructContext': 'tools/construct_context',
899 }
900 json.dump(dexpreopt_soong_config, f)
901
902 # TODO(b/188179859): Make *dex location configurable to vendor or system_other.
903 use_system_other_odex = False
904
905 if use_system_other_odex:
906 dex_img = 'SYSTEM_OTHER'
907 else:
908 dex_img = 'VENDOR'
909 # Open vendor_filesystem_config to append the items generated by dexopt.
910 vendor_file_system_config = open(
Daniel Normane292f5f2022-02-17 14:16:10 -0800911 os.path.join(temp_dir, 'output', 'META',
912 'vendor_filesystem_config.txt'), 'a')
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700913
914 # Dexpreopt vendor apps.
915 dexpreopt_config_suffix = '_dexpreopt.config'
Daniel Normane292f5f2022-02-17 14:16:10 -0800916 for config in glob.glob(
917 os.path.join(dexpreopt_vendor_config_files_temp_dir,
918 '*' + dexpreopt_config_suffix)):
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700919 app = os.path.basename(config)[:-len(dexpreopt_config_suffix)]
920 logging.info('dexpreopt config: %s %s', config, app)
921
922 apk_dir = 'app'
923 apk_path = os.path.join(temp_dir, 'vendor', apk_dir, app, app + '.apk')
924 if not os.path.exists(apk_path):
925 apk_dir = 'priv-app'
926 apk_path = os.path.join(temp_dir, 'vendor', apk_dir, app, app + '.apk')
927 if not os.path.exists(apk_path):
Daniel Normane292f5f2022-02-17 14:16:10 -0800928 logging.warning(
929 'skipping dexpreopt for %s, no apk found in vendor/app '
930 'or vendor/priv-app', app)
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700931 continue
932
933 # Generate dexpreopting script. Note 'out_dir' is not the output directory
934 # where the script is generated, but the OUT_DIR at build time referenced
935 # in the dexpreot config files, e.g., "out/.../core-oj.jar", so the tool knows
936 # how to adjust the path.
937 command = [
938 os.path.join(dexpreopt_tools_files_temp_dir, 'dexpreopt_gen'),
939 '-global',
Daniel Normane292f5f2022-02-17 14:16:10 -0800940 os.path.join(dexpreopt_framework_config_files_temp_dir,
941 'dexpreopt.config'),
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700942 '-global_soong',
Daniel Normane292f5f2022-02-17 14:16:10 -0800943 os.path.join(dexpreopt_framework_config_files_temp_dir,
944 'dexpreopt_soong.config'),
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700945 '-module',
946 config,
947 '-dexpreopt_script',
948 'dexpreopt_app.sh',
949 '-out_dir',
950 'out',
951 '-base_path',
952 '.',
953 '--uses_target_files',
954 ]
955
956 # Run the command from temp_dir so all tool paths are its descendants.
Daniel Normane292f5f2022-02-17 14:16:10 -0800957 logging.info('running %s', command)
958 subprocess.check_call(command, cwd=temp_dir)
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700959
960 # Call the generated script.
961 command = ['sh', 'dexpreopt_app.sh', apk_path]
Daniel Normane292f5f2022-02-17 14:16:10 -0800962 logging.info('running %s', command)
963 subprocess.check_call(command, cwd=temp_dir)
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700964
965 # Output files are in:
966 #
967 # <temp_dir>/out/dex2oat_result/vendor/priv-app/<app>/oat/arm64/package.vdex
968 # <temp_dir>/out/dex2oat_result/vendor/priv-app/<app>/oat/arm64/package.odex
969 # <temp_dir>/out/dex2oat_result/vendor/app/<app>/oat/arm64/package.vdex
970 # <temp_dir>/out/dex2oat_result/vendor/app/<app>/oat/arm64/package.odex
971 #
972 # Copy the files to their destination. The structure of system_other is:
973 #
974 # system_other/
975 # system-other-odex-marker
976 # system/
977 # app/
978 # <app>/oat/arm64/
979 # <app>.odex
980 # <app>.vdex
981 # ...
982 # priv-app/
983 # <app>/oat/arm64/
984 # <app>.odex
985 # <app>.vdex
986 # ...
987
988 # TODO(b/188179859): Support for other architectures.
989 arch = 'arm64'
990
Daniel Normane292f5f2022-02-17 14:16:10 -0800991 dex_destination = os.path.join(temp_dir, 'output', dex_img, apk_dir, app,
992 'oat', arch)
Jose Galmes9c8f6eb2021-07-21 09:34:08 -0700993 os.makedirs(dex_destination)
Daniel Normane292f5f2022-02-17 14:16:10 -0800994 dex2oat_path = os.path.join(temp_dir, 'out', 'dex2oat_result', 'vendor',
995 apk_dir, app, 'oat', arch)
996 shutil.copy(
997 os.path.join(dex2oat_path, 'package.vdex'),
998 os.path.join(dex_destination, app + '.vdex'))
999 shutil.copy(
1000 os.path.join(dex2oat_path, 'package.odex'),
1001 os.path.join(dex_destination, app + '.odex'))
Jose Galmes9c8f6eb2021-07-21 09:34:08 -07001002
1003 # Append entries to vendor_file_system_config.txt, such as:
1004 #
1005 # vendor/app/<app>/oat 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
1006 # vendor/app/<app>/oat/arm64 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
1007 # vendor/app/<app>/oat/arm64/<app>.odex 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
1008 # vendor/app/<app>/oat/arm64/<app>.vdex 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
1009 if not use_system_other_odex:
1010 vendor_app_prefix = 'vendor/' + apk_dir + '/' + app + '/oat'
1011 selabel = 'selabel=u:object_r:vendor_app_file:s0 capabilities=0x0'
1012 vendor_file_system_config.writelines([
1013 vendor_app_prefix + ' 0 2000 755 ' + selabel + '\n',
1014 vendor_app_prefix + '/' + arch + ' 0 2000 755 ' + selabel + '\n',
Daniel Normane292f5f2022-02-17 14:16:10 -08001015 vendor_app_prefix + '/' + arch + '/' + app + '.odex 0 0 644 ' +
1016 selabel + '\n',
1017 vendor_app_prefix + '/' + arch + '/' + app + '.vdex 0 0 644 ' +
1018 selabel + '\n',
Jose Galmes9c8f6eb2021-07-21 09:34:08 -07001019 ])
1020
1021 if not use_system_other_odex:
1022 vendor_file_system_config.close()
1023 # Delete vendor.img so that it will be regenerated.
1024 # TODO(b/188179859): Rebuilding a vendor image in GRF mode (e.g., T(framework)
1025 # and S(vendor) may require logic similar to that in
1026 # rebuild_image_with_sepolicy.
Daniel Norman03747412022-02-25 10:38:37 -08001027 vendor_img = os.path.join(output_target_files_dir, 'IMAGES', 'vendor.img')
Jose Galmes9c8f6eb2021-07-21 09:34:08 -07001028 if os.path.exists(vendor_img):
1029 logging.info('Deleting %s', vendor_img)
1030 os.remove(vendor_img)
1031
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001032
Daniel Norman03747412022-02-25 10:38:37 -08001033def create_merged_package(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -07001034 """Merges two target files packages into one target files structure.
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001035
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001036 Returns:
1037 Path to merged package under temp directory.
1038 """
Daniel Normandbbf5a32020-10-22 16:03:32 -07001039 # Extract "as is" items from the input framework and vendor partial target
1040 # files packages directly into the output temporary directory, since these items
1041 # do not need special case processing.
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001042
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001043 output_target_files_temp_dir = os.path.join(temp_dir, 'output')
Bill Peckham889b0c62019-02-21 18:53:37 -08001044 extract_items(
Daniel Norman03747412022-02-25 10:38:37 -08001045 input_zip=OPTIONS.framework_target_files,
1046 output_dir=output_target_files_temp_dir,
1047 extract_item_list=OPTIONS.framework_item_list)
Bill Peckham889b0c62019-02-21 18:53:37 -08001048 extract_items(
Daniel Norman03747412022-02-25 10:38:37 -08001049 input_zip=OPTIONS.vendor_target_files,
1050 output_dir=output_target_files_temp_dir,
1051 extract_item_list=OPTIONS.vendor_item_list)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001052
Daniel Normandbbf5a32020-10-22 16:03:32 -07001053 # Perform special case processing on META/* items.
1054 # After this function completes successfully, all the files we need to create
1055 # the output target files package are in place.
Daniel Norman03747412022-02-25 10:38:37 -08001056 merge_meta_files(temp_dir=temp_dir, merged_dir=output_target_files_temp_dir)
1057
1058 process_dexopt(
1059 temp_dir=temp_dir, output_target_files_dir=output_target_files_temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001060
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001061 return output_target_files_temp_dir
1062
1063
Daniel Norman03747412022-02-25 10:38:37 -08001064def generate_missing_images(target_files_dir):
1065 """Generate any missing images from target files."""
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001066
1067 # Regenerate IMAGES in the target directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001068
Daniel Normandbbf5a32020-10-22 16:03:32 -07001069 add_img_args = [
1070 '--verbose',
1071 '--add_missing',
1072 ]
Daniel Norman03747412022-02-25 10:38:37 -08001073 if OPTIONS.rebuild_recovery:
Daniel Normana4911da2019-03-15 14:36:21 -07001074 add_img_args.append('--rebuild_recovery')
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001075 add_img_args.append(target_files_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001076
1077 add_img_to_target_files.main(add_img_args)
1078
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001079
Daniel Norman03747412022-02-25 10:38:37 -08001080def rebuild_image_with_sepolicy(target_files_dir):
Daniel Norman571e1822021-06-25 17:18:25 -07001081 """Rebuilds odm.img or vendor.img to include merged sepolicy files.
1082
1083 If odm is present then odm is preferred -- otherwise vendor is used.
Daniel Norman571e1822021-06-25 17:18:25 -07001084 """
1085 partition = 'vendor'
1086 if os.path.exists(os.path.join(target_files_dir, 'ODM')) or os.path.exists(
1087 os.path.join(target_files_dir, 'IMAGES/odm.img')):
1088 partition = 'odm'
1089 partition_img = '{}.img'.format(partition)
Po Hua6c59122022-02-16 08:41:29 +00001090 partition_map = '{}.map'.format(partition)
Daniel Norman571e1822021-06-25 17:18:25 -07001091
1092 logger.info('Recompiling %s using the merged sepolicy files.', partition_img)
1093
1094 # Copy the combined SEPolicy file and framework hashes to the image that is
1095 # being rebuilt.
1096 def copy_selinux_file(input_path, output_filename):
Po Hu0e4403e2021-07-06 17:05:56 +08001097 input_filename = os.path.join(target_files_dir, input_path)
1098 if not os.path.exists(input_filename):
1099 input_filename = input_filename.replace('SYSTEM_EXT/', 'SYSTEM/system_ext/') \
1100 .replace('PRODUCT/', 'SYSTEM/product/')
1101 if not os.path.exists(input_filename):
1102 logger.info('Skipping copy_selinux_file for %s', input_filename)
1103 return
Daniel Norman571e1822021-06-25 17:18:25 -07001104 shutil.copy(
Po Hu0e4403e2021-07-06 17:05:56 +08001105 input_filename,
Daniel Norman571e1822021-06-25 17:18:25 -07001106 os.path.join(target_files_dir, partition.upper(), 'etc/selinux',
1107 output_filename))
1108
1109 copy_selinux_file('META/combined_sepolicy', 'precompiled_sepolicy')
1110 copy_selinux_file('SYSTEM/etc/selinux/plat_sepolicy_and_mapping.sha256',
1111 'precompiled_sepolicy.plat_sepolicy_and_mapping.sha256')
1112 copy_selinux_file(
1113 'SYSTEM_EXT/etc/selinux/system_ext_sepolicy_and_mapping.sha256',
1114 'precompiled_sepolicy.system_ext_sepolicy_and_mapping.sha256')
1115 copy_selinux_file('PRODUCT/etc/selinux/product_sepolicy_and_mapping.sha256',
1116 'precompiled_sepolicy.product_sepolicy_and_mapping.sha256')
1117
Daniel Norman03747412022-02-25 10:38:37 -08001118 if not OPTIONS.vendor_otatools:
Daniel Norman571e1822021-06-25 17:18:25 -07001119 # Remove the partition from the merged target-files archive. It will be
Daniel Norman03747412022-02-25 10:38:37 -08001120 # rebuilt later automatically by generate_missing_images().
Daniel Norman571e1822021-06-25 17:18:25 -07001121 os.remove(os.path.join(target_files_dir, 'IMAGES', partition_img))
Daniel Norman03747412022-02-25 10:38:37 -08001122 return
Daniel Norman571e1822021-06-25 17:18:25 -07001123
Daniel Norman03747412022-02-25 10:38:37 -08001124 # TODO(b/192253131): Remove the need for vendor_otatools by fixing
1125 # backwards-compatibility issues when compiling images across releases.
1126 if not OPTIONS.vendor_target_files:
1127 raise ValueError(
1128 'Expected vendor_target_files if vendor_otatools is not None.')
1129 logger.info(
1130 '%s recompilation will be performed using the vendor otatools.zip',
1131 partition_img)
Daniel Norman571e1822021-06-25 17:18:25 -07001132
Daniel Norman03747412022-02-25 10:38:37 -08001133 # Unzip the vendor build's otatools.zip and target-files archive.
1134 vendor_otatools_dir = common.MakeTempDir(
1135 prefix='merge_target_files_vendor_otatools_')
1136 vendor_target_files_dir = common.MakeTempDir(
1137 prefix='merge_target_files_vendor_target_files_')
1138 common.UnzipToDir(OPTIONS.vendor_otatools, vendor_otatools_dir)
1139 common.UnzipToDir(OPTIONS.vendor_target_files, vendor_target_files_dir)
Daniel Norman571e1822021-06-25 17:18:25 -07001140
Daniel Norman03747412022-02-25 10:38:37 -08001141 # Copy the partition contents from the merged target-files archive to the
1142 # vendor target-files archive.
1143 shutil.rmtree(os.path.join(vendor_target_files_dir, partition.upper()))
1144 shutil.copytree(
1145 os.path.join(target_files_dir, partition.upper()),
1146 os.path.join(vendor_target_files_dir, partition.upper()),
1147 symlinks=True)
Daniel Norman571e1822021-06-25 17:18:25 -07001148
Daniel Norman03747412022-02-25 10:38:37 -08001149 # Delete then rebuild the partition.
1150 os.remove(os.path.join(vendor_target_files_dir, 'IMAGES', partition_img))
1151 rebuild_partition_command = [
1152 os.path.join(vendor_otatools_dir, 'bin', 'add_img_to_target_files'),
1153 '--verbose',
1154 '--add_missing',
1155 ]
1156 if OPTIONS.rebuild_recovery:
1157 rebuild_partition_command.append('--rebuild_recovery')
1158 rebuild_partition_command.append(vendor_target_files_dir)
1159 logger.info('Recompiling %s: %s', partition_img,
1160 ' '.join(rebuild_partition_command))
1161 common.RunAndCheckOutput(rebuild_partition_command, verbose=True)
Po Hua6c59122022-02-16 08:41:29 +00001162
Daniel Norman03747412022-02-25 10:38:37 -08001163 # Move the newly-created image to the merged target files dir.
1164 if not os.path.exists(os.path.join(target_files_dir, 'IMAGES')):
1165 os.makedirs(os.path.join(target_files_dir, 'IMAGES'))
1166 shutil.move(
1167 os.path.join(vendor_target_files_dir, 'IMAGES', partition_img),
1168 os.path.join(target_files_dir, 'IMAGES', partition_img))
1169 shutil.move(
1170 os.path.join(vendor_target_files_dir, 'IMAGES', partition_map),
1171 os.path.join(target_files_dir, 'IMAGES', partition_map))
Po Hua6c59122022-02-16 08:41:29 +00001172
Daniel Norman03747412022-02-25 10:38:37 -08001173 def copy_recovery_file(filename):
1174 for subdir in ('VENDOR', 'SYSTEM/vendor'):
1175 source = os.path.join(vendor_target_files_dir, subdir, filename)
1176 if os.path.exists(source):
1177 dest = os.path.join(target_files_dir, subdir, filename)
1178 shutil.copy(source, dest)
1179 return
1180 logger.info('Skipping copy_recovery_file for %s, file not found', filename)
1181
1182 if OPTIONS.rebuild_recovery:
1183 copy_recovery_file('etc/recovery.img')
1184 copy_recovery_file('bin/install-recovery.sh')
1185 copy_recovery_file('recovery-from-boot.p')
Daniel Norman571e1822021-06-25 17:18:25 -07001186
1187
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001188def generate_super_empty_image(target_dir, output_super_empty):
Tao Bao2ad4b822019-06-27 16:52:12 -07001189 """Generates super_empty image from target package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001190
1191 Args:
1192 target_dir: Path to the target file package which contains misc_info.txt for
1193 detailed information for super image.
1194 output_super_empty: If provided, copies a super_empty.img file from the
1195 target files package to this path.
1196 """
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001197 # Create super_empty.img using the merged misc_info.txt.
1198
Daniel Norman4cc9df62019-07-18 10:11:07 -07001199 misc_info_txt = os.path.join(target_dir, 'META', 'misc_info.txt')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001200
Kiyoung Kimebe7c9c2019-06-25 17:09:55 +09001201 use_dynamic_partitions = common.LoadDictionaryFromFile(misc_info_txt).get(
1202 'use_dynamic_partitions')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001203
1204 if use_dynamic_partitions != 'true' and output_super_empty:
1205 raise ValueError(
1206 'Building super_empty.img requires use_dynamic_partitions=true.')
1207 elif use_dynamic_partitions == 'true':
Daniel Norman4cc9df62019-07-18 10:11:07 -07001208 super_empty_img = os.path.join(target_dir, 'IMAGES', 'super_empty.img')
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001209 build_super_image_args = [
1210 misc_info_txt,
1211 super_empty_img,
1212 ]
1213 build_super_image.main(build_super_image_args)
1214
1215 # Copy super_empty.img to the user-provided output_super_empty location.
1216 if output_super_empty:
1217 shutil.copyfile(super_empty_img, output_super_empty)
1218
Daniel Normanb8a2f9d2019-04-24 12:55:51 -07001219
Daniel Norman03747412022-02-25 10:38:37 -08001220def create_target_files_archive(output_zip, source_dir, temp_dir):
1221 """Creates a target_files zip archive from the input source dir.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001222
1223 Args:
Daniel Norman03747412022-02-25 10:38:37 -08001224 output_zip: The name of the zip archive target files package.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001225 source_dir: The target directory contains package to be archived.
1226 temp_dir: Path to temporary directory for any intermediate files.
1227 """
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001228 output_target_files_list = os.path.join(temp_dir, 'output.list')
Daniel Norman4cc9df62019-07-18 10:11:07 -07001229 output_target_files_meta_dir = os.path.join(source_dir, 'META')
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001230
Daniel Normandbbf5a32020-10-22 16:03:32 -07001231 def files_from_path(target_path, extra_args=None):
1232 """Gets files under the given path and return a sorted list."""
1233 find_command = ['find', target_path] + (extra_args or [])
1234 find_process = common.Run(
1235 find_command, stdout=subprocess.PIPE, verbose=False)
1236 return common.RunAndCheckOutput(['sort'],
1237 stdin=find_process.stdout,
1238 verbose=False)
1239
Daniel Norman03747412022-02-25 10:38:37 -08001240 # META content appears first in the zip. This is done by the
1241 # standard build system for optimized extraction of those files,
1242 # so we do the same step for merged target_files.zips here too.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001243 meta_content = files_from_path(output_target_files_meta_dir)
Daniel Norman4cc9df62019-07-18 10:11:07 -07001244 other_content = files_from_path(
1245 source_dir,
1246 ['-path', output_target_files_meta_dir, '-prune', '-o', '-print'])
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001247
Tao Bao2ad4b822019-06-27 16:52:12 -07001248 with open(output_target_files_list, 'w') as f:
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001249 f.write(meta_content)
1250 f.write(other_content)
1251
1252 command = [
Bill Peckhamf753e152019-02-19 18:02:46 -08001253 'soong_zip',
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001254 '-d',
Daniel Normane5b134a2019-04-17 14:54:06 -07001255 '-o',
Daniel Norman03747412022-02-25 10:38:37 -08001256 os.path.abspath(output_zip),
Daniel Normane5b134a2019-04-17 14:54:06 -07001257 '-C',
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001258 source_dir,
Daniel Normaneaf5c1d2021-02-09 11:01:42 -08001259 '-r',
Daniel Normane5b134a2019-04-17 14:54:06 -07001260 output_target_files_list,
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001261 ]
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001262
Daniel Norman03747412022-02-25 10:38:37 -08001263 logger.info('creating %s', output_zip)
Daniel Normaneaf5c1d2021-02-09 11:01:42 -08001264 common.RunAndCheckOutput(command, verbose=True)
Daniel Norman03747412022-02-25 10:38:37 -08001265 logger.info('finished creating %s', output_zip)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001266
1267
Daniel Norman03747412022-02-25 10:38:37 -08001268def merge_target_files(temp_dir):
Tao Bao2ad4b822019-06-27 16:52:12 -07001269 """Merges two target files packages together.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001270
Daniel Norman03747412022-02-25 10:38:37 -08001271 This function uses framework and vendor target files packages as input,
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001272 performs various file extractions, special case processing, and finally
1273 creates a merged zip archive as output.
1274
1275 Args:
1276 temp_dir: The name of a directory we use when we extract items from the
1277 input target files packages, and also a scratch directory that we use for
1278 temporary files.
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001279 """
1280
1281 logger.info('starting: merge framework %s and vendor %s into output %s',
Daniel Norman03747412022-02-25 10:38:37 -08001282 OPTIONS.framework_target_files, OPTIONS.vendor_target_files,
1283 OPTIONS.output_target_files)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001284
Daniel Norman03747412022-02-25 10:38:37 -08001285 output_target_files_temp_dir = create_merged_package(temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001286
Yifan Hongade0d3f2019-08-21 16:37:11 -07001287 if not check_target_files_vintf.CheckVintf(output_target_files_temp_dir):
Daniel Normanb0c75912020-09-24 14:30:21 -07001288 raise RuntimeError('Incompatible VINTF metadata')
Yifan Hongade0d3f2019-08-21 16:37:11 -07001289
Daniel Norman21c34f72020-11-11 17:25:50 -08001290 partition_map = common.PartitionMapFromTargetFiles(
1291 output_target_files_temp_dir)
1292
Daniel Normand3351562020-10-29 12:33:11 -07001293 # Generate and check for cross-partition violations of sharedUserId
1294 # values in APKs. This requires the input target-files packages to contain
1295 # *.apk files.
Daniel Normanb8d52a22020-10-26 17:55:00 -07001296 shareduid_violation_modules = os.path.join(
1297 output_target_files_temp_dir, 'META', 'shareduid_violation_modules.json')
1298 with open(shareduid_violation_modules, 'w') as f:
Daniel Normanb8d52a22020-10-26 17:55:00 -07001299 violation = find_shareduid_violation.FindShareduidViolation(
1300 output_target_files_temp_dir, partition_map)
Daniel Normand3351562020-10-29 12:33:11 -07001301
1302 # Write the output to a file to enable debugging.
Daniel Normanb8d52a22020-10-26 17:55:00 -07001303 f.write(violation)
Daniel Normand3351562020-10-29 12:33:11 -07001304
1305 # Check for violations across the input builds' partition groups.
1306 shareduid_errors = common.SharedUidPartitionViolations(
Daniel Norman03747412022-02-25 10:38:37 -08001307 json.loads(violation),
1308 [OPTIONS.framework_partition_set, OPTIONS.vendor_partition_set])
Daniel Normand3351562020-10-29 12:33:11 -07001309 if shareduid_errors:
1310 for error in shareduid_errors:
1311 logger.error(error)
1312 raise ValueError('sharedUserId APK error. See %s' %
1313 shareduid_violation_modules)
Daniel Normanb8d52a22020-10-26 17:55:00 -07001314
Daniel Norman48603ff2021-02-22 15:15:24 -08001315 # host_init_verifier and secilc check only the following partitions:
Daniel Norman21c34f72020-11-11 17:25:50 -08001316 filtered_partitions = {
1317 partition: path
1318 for partition, path in partition_map.items()
Daniel Norman21c34f72020-11-11 17:25:50 -08001319 if partition in ['system', 'system_ext', 'product', 'vendor', 'odm']
1320 }
Daniel Norman48603ff2021-02-22 15:15:24 -08001321
1322 # Run host_init_verifier on the combined init rc files.
Daniel Norman21c34f72020-11-11 17:25:50 -08001323 common.RunHostInitVerifier(
1324 product_out=output_target_files_temp_dir,
1325 partition_map=filtered_partitions)
1326
Daniel Norman48603ff2021-02-22 15:15:24 -08001327 # Check that the split sepolicy from the multiple builds can compile.
Daniel Norman571e1822021-06-25 17:18:25 -07001328 split_sepolicy_cmd = compile_split_sepolicy(output_target_files_temp_dir,
1329 filtered_partitions)
Daniel Norman48603ff2021-02-22 15:15:24 -08001330 logger.info('Compiling split sepolicy: %s', ' '.join(split_sepolicy_cmd))
1331 common.RunAndCheckOutput(split_sepolicy_cmd)
Daniel Norman571e1822021-06-25 17:18:25 -07001332 # Include the compiled policy in an image if requested.
Daniel Norman03747412022-02-25 10:38:37 -08001333 if OPTIONS.rebuild_sepolicy:
1334 rebuild_image_with_sepolicy(output_target_files_temp_dir)
Daniel Norman48603ff2021-02-22 15:15:24 -08001335
Daniel Normane9af70a2021-04-15 16:39:22 -07001336 # Run validation checks on the pre-installed APEX files.
1337 validate_merged_apex_info(output_target_files_temp_dir, partition_map.keys())
1338
Daniel Norman03747412022-02-25 10:38:37 -08001339 generate_missing_images(output_target_files_temp_dir)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001340
Daniel Norman03747412022-02-25 10:38:37 -08001341 generate_super_empty_image(output_target_files_temp_dir,
1342 OPTIONS.output_super_empty)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001343
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001344 # Finally, create the output target files zip archive and/or copy the
1345 # output items to the output target files directory.
1346
Daniel Norman03747412022-02-25 10:38:37 -08001347 if OPTIONS.output_dir:
1348 copy_items(output_target_files_temp_dir, OPTIONS.output_dir,
1349 OPTIONS.output_item_list)
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001350
Daniel Norman03747412022-02-25 10:38:37 -08001351 if not OPTIONS.output_target_files:
Kiyoung Kim7cbeda72019-06-28 13:26:04 +09001352 return
1353
Daniel Norman03747412022-02-25 10:38:37 -08001354 create_target_files_archive(OPTIONS.output_target_files,
1355 output_target_files_temp_dir, temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001356
Daniel Norman74eb74b2019-09-18 14:01:48 -07001357 # Create the IMG package from the merged target files package.
Daniel Norman03747412022-02-25 10:38:37 -08001358 if OPTIONS.output_img:
1359 img_from_target_files.main(
1360 [OPTIONS.output_target_files, OPTIONS.output_img])
Daniel Norman74eb74b2019-09-18 14:01:48 -07001361
Daniel Norman3b64ce12019-04-16 16:11:35 -07001362 # Create the OTA package from the merged target files package.
1363
Daniel Norman03747412022-02-25 10:38:37 -08001364 if OPTIONS.output_ota:
1365 ota_from_target_files.main(
1366 [OPTIONS.output_target_files, OPTIONS.output_ota])
Daniel Norman3b64ce12019-04-16 16:11:35 -07001367
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001368
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001369def call_func_with_temp_dir(func, keep_tmp):
Tao Bao2ad4b822019-06-27 16:52:12 -07001370 """Manages the creation and cleanup of the temporary directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001371
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001372 This function calls the given function after first creating a temporary
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001373 directory. It also cleans up the temporary directory.
1374
1375 Args:
Daniel Normane5b134a2019-04-17 14:54:06 -07001376 func: The function to call. Should accept one parameter, the path to the
1377 temporary directory.
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001378 keep_tmp: Keep the temporary directory after processing is complete.
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001379 """
1380
1381 # Create a temporary directory. This will serve as the parent of directories
1382 # we use when we extract items from the input target files packages, and also
1383 # a scratch directory that we use for temporary files.
1384
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001385 temp_dir = common.MakeTempDir(prefix='merge_target_files_')
1386
1387 try:
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001388 func(temp_dir)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001389 finally:
1390 if keep_tmp:
1391 logger.info('keeping %s', temp_dir)
1392 else:
1393 common.Cleanup()
1394
1395
1396def main():
1397 """The main function.
1398
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001399 Process command line arguments, then call merge_target_files to
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001400 perform the heavy lifting.
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001401 """
1402
1403 common.InitLogging()
1404
Bill Peckhamf753e152019-02-19 18:02:46 -08001405 def option_handler(o, a):
1406 if o == '--system-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -07001407 logger.warning(
1408 '--system-target-files has been renamed to --framework-target-files')
1409 OPTIONS.framework_target_files = a
1410 elif o == '--framework-target-files':
1411 OPTIONS.framework_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001412 elif o == '--system-item-list':
Daniel Normand5d70ea2019-06-05 15:13:43 -07001413 logger.warning(
1414 '--system-item-list has been renamed to --framework-item-list')
1415 OPTIONS.framework_item_list = a
1416 elif o == '--framework-item-list':
1417 OPTIONS.framework_item_list = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001418 elif o == '--system-misc-info-keys':
Daniel Norman4cc9df62019-07-18 10:11:07 -07001419 logger.warning('--system-misc-info-keys has been renamed to '
1420 '--framework-misc-info-keys')
Daniel Normand5d70ea2019-06-05 15:13:43 -07001421 OPTIONS.framework_misc_info_keys = a
1422 elif o == '--framework-misc-info-keys':
1423 OPTIONS.framework_misc_info_keys = a
Bill Peckhamf753e152019-02-19 18:02:46 -08001424 elif o == '--other-target-files':
Daniel Normand5d70ea2019-06-05 15:13:43 -07001425 logger.warning(
1426 '--other-target-files has been renamed to --vendor-target-files')
1427 OPTIONS.vendor_target_files = a
1428 elif o == '--vendor-target-files':
1429 OPTIONS.vendor_target_files = a
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001430 elif o == '--other-item-list':
Daniel Norman2d7989a2021-04-05 17:40:47 +00001431 logger.warning('--other-item-list has been renamed to --vendor-item-list')
Daniel Normand5d70ea2019-06-05 15:13:43 -07001432 OPTIONS.vendor_item_list = a
1433 elif o == '--vendor-item-list':
1434 OPTIONS.vendor_item_list = a
Bill Peckhamf753e152019-02-19 18:02:46 -08001435 elif o == '--output-target-files':
1436 OPTIONS.output_target_files = a
Daniel Normanfdb38812019-04-15 09:47:24 -07001437 elif o == '--output-dir':
1438 OPTIONS.output_dir = a
1439 elif o == '--output-item-list':
1440 OPTIONS.output_item_list = a
Daniel Norman3b64ce12019-04-16 16:11:35 -07001441 elif o == '--output-ota':
1442 OPTIONS.output_ota = a
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001443 elif o == '--output-img':
1444 OPTIONS.output_img = a
Daniel Normanf0318252019-04-15 11:34:56 -07001445 elif o == '--output-super-empty':
1446 OPTIONS.output_super_empty = a
Po Hua6c59122022-02-16 08:41:29 +00001447 elif o == '--rebuild_recovery':
Daniel Normana4911da2019-03-15 14:36:21 -07001448 OPTIONS.rebuild_recovery = True
Daniel Normanb0c75912020-09-24 14:30:21 -07001449 elif o == '--allow-duplicate-apkapex-keys':
1450 OPTIONS.allow_duplicate_apkapex_keys = True
Daniel Norman571e1822021-06-25 17:18:25 -07001451 elif o == '--vendor-otatools':
1452 OPTIONS.vendor_otatools = a
1453 elif o == '--rebuild-sepolicy':
1454 OPTIONS.rebuild_sepolicy = True
Bill Peckham364c1cc2019-03-29 18:27:23 -07001455 elif o == '--keep-tmp':
Bill Peckhamf753e152019-02-19 18:02:46 -08001456 OPTIONS.keep_tmp = True
Jose Galmes9c8f6eb2021-07-21 09:34:08 -07001457 elif o == '--framework-dexpreopt-config':
1458 OPTIONS.framework_dexpreopt_config = a
1459 elif o == '--framework-dexpreopt-tools':
1460 OPTIONS.framework_dexpreopt_tools = a
1461 elif o == '--vendor-dexpreopt-config':
1462 OPTIONS.vendor_dexpreopt_config = a
Bill Peckhamf753e152019-02-19 18:02:46 -08001463 else:
1464 return False
1465 return True
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001466
Bill Peckhamf753e152019-02-19 18:02:46 -08001467 args = common.ParseOptions(
Daniel Normane5b134a2019-04-17 14:54:06 -07001468 sys.argv[1:],
1469 __doc__,
Bill Peckhamf753e152019-02-19 18:02:46 -08001470 extra_long_opts=[
1471 'system-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -07001472 'framework-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001473 'system-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -07001474 'framework-item-list=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001475 'system-misc-info-keys=',
Daniel Normand5d70ea2019-06-05 15:13:43 -07001476 'framework-misc-info-keys=',
Bill Peckhamf753e152019-02-19 18:02:46 -08001477 'other-target-files=',
Daniel Normand5d70ea2019-06-05 15:13:43 -07001478 'vendor-target-files=',
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001479 'other-item-list=',
Daniel Normand5d70ea2019-06-05 15:13:43 -07001480 'vendor-item-list=',
Bill Peckhamf753e152019-02-19 18:02:46 -08001481 'output-target-files=',
Daniel Normanfdb38812019-04-15 09:47:24 -07001482 'output-dir=',
1483 'output-item-list=',
Daniel Norman3b64ce12019-04-16 16:11:35 -07001484 'output-ota=',
Daniel Norman1bd2a1d2019-04-18 12:32:18 -07001485 'output-img=',
Daniel Normanf0318252019-04-15 11:34:56 -07001486 'output-super-empty=',
Jose Galmes9c8f6eb2021-07-21 09:34:08 -07001487 'framework-dexpreopt-config=',
1488 'framework-dexpreopt-tools=',
1489 'vendor-dexpreopt-config=',
Daniel Normana4911da2019-03-15 14:36:21 -07001490 'rebuild_recovery',
Daniel Normanb0c75912020-09-24 14:30:21 -07001491 'allow-duplicate-apkapex-keys',
Daniel Norman571e1822021-06-25 17:18:25 -07001492 'vendor-otatools=',
1493 'rebuild-sepolicy',
Bill Peckham364c1cc2019-03-29 18:27:23 -07001494 'keep-tmp',
Bill Peckhamf753e152019-02-19 18:02:46 -08001495 ],
1496 extra_option_handler=option_handler)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001497
Tao Bao2ad4b822019-06-27 16:52:12 -07001498 # pylint: disable=too-many-boolean-expressions
Daniel Normand5d70ea2019-06-05 15:13:43 -07001499 if (args or OPTIONS.framework_target_files is None or
1500 OPTIONS.vendor_target_files is None or
Daniel Normane5b134a2019-04-17 14:54:06 -07001501 (OPTIONS.output_target_files is None and OPTIONS.output_dir is None) or
Po Hua6c59122022-02-16 08:41:29 +00001502 (OPTIONS.output_dir is not None and OPTIONS.output_item_list is None) or
1503 (OPTIONS.rebuild_recovery and not OPTIONS.rebuild_sepolicy)):
Bill Peckhamf753e152019-02-19 18:02:46 -08001504 common.Usage(__doc__)
Bill Peckham889b0c62019-02-21 18:53:37 -08001505 sys.exit(1)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001506
Daniel Normand5d70ea2019-06-05 15:13:43 -07001507 if OPTIONS.framework_item_list:
Daniel Norman03747412022-02-25 10:38:37 -08001508 OPTIONS.framework_item_list = common.LoadListFromFile(
1509 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001510 else:
Daniel Norman03747412022-02-25 10:38:37 -08001511 OPTIONS.framework_item_list = DEFAULT_FRAMEWORK_ITEM_LIST
1512 OPTIONS.framework_partition_set = item_list_to_partition_set(
1513 OPTIONS.framework_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001514
Daniel Normand5d70ea2019-06-05 15:13:43 -07001515 if OPTIONS.framework_misc_info_keys:
Daniel Norman03747412022-02-25 10:38:37 -08001516 OPTIONS.framework_misc_info_keys = common.LoadListFromFile(
Daniel Normand5d70ea2019-06-05 15:13:43 -07001517 OPTIONS.framework_misc_info_keys)
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001518 else:
Daniel Norman03747412022-02-25 10:38:37 -08001519 OPTIONS.framework_misc_info_keys = DEFAULT_FRAMEWORK_MISC_INFO_KEYS
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001520
Daniel Normand5d70ea2019-06-05 15:13:43 -07001521 if OPTIONS.vendor_item_list:
Daniel Norman03747412022-02-25 10:38:37 -08001522 OPTIONS.vendor_item_list = common.LoadListFromFile(OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001523 else:
Daniel Norman03747412022-02-25 10:38:37 -08001524 OPTIONS.vendor_item_list = DEFAULT_VENDOR_ITEM_LIST
1525 OPTIONS.vendor_partition_set = item_list_to_partition_set(
1526 OPTIONS.vendor_item_list)
Daniel Norman2c99c5b2019-03-07 13:01:48 -08001527
Daniel Normanfdb38812019-04-15 09:47:24 -07001528 if OPTIONS.output_item_list:
Daniel Norman03747412022-02-25 10:38:37 -08001529 OPTIONS.output_item_list = common.LoadListFromFile(OPTIONS.output_item_list)
Daniel Normanfdb38812019-04-15 09:47:24 -07001530 else:
Daniel Norman03747412022-02-25 10:38:37 -08001531 OPTIONS.output_item_list = None
Daniel Normanfdb38812019-04-15 09:47:24 -07001532
Daniel Norman03747412022-02-25 10:38:37 -08001533 if not validate_config_lists():
Daniel Normane5964522019-03-19 10:32:03 -07001534 sys.exit(1)
1535
Daniel Norman03747412022-02-25 10:38:37 -08001536 call_func_with_temp_dir(lambda temp_dir: merge_target_files(temp_dir),
1537 OPTIONS.keep_tmp)
Bill Peckhame9eb5f92019-02-01 15:52:10 -08001538
1539
1540if __name__ == '__main__':
Bill Peckham889b0c62019-02-21 18:53:37 -08001541 main()