Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of 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, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """A tool for checking that a manifest agrees with the build system.""" |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import argparse |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 22 | import json |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 23 | import re |
| 24 | import subprocess |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 25 | import sys |
| 26 | from xml.dom import minidom |
| 27 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 28 | from manifest import android_ns |
| 29 | from manifest import get_children_with_tag |
| 30 | from manifest import parse_manifest |
| 31 | from manifest import write_xml |
| 32 | |
| 33 | |
| 34 | class ManifestMismatchError(Exception): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 35 | pass |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def parse_args(): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 39 | """Parse commandline arguments.""" |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 40 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 41 | parser = argparse.ArgumentParser() |
| 42 | parser.add_argument( |
| 43 | '--uses-library', |
| 44 | dest='uses_libraries', |
| 45 | action='append', |
| 46 | help='specify uses-library entries known to the build system') |
| 47 | parser.add_argument( |
| 48 | '--optional-uses-library', |
| 49 | dest='optional_uses_libraries', |
| 50 | action='append', |
| 51 | help='specify uses-library entries known to the build system with ' |
| 52 | 'required:false' |
| 53 | ) |
| 54 | parser.add_argument( |
| 55 | '--enforce-uses-libraries', |
| 56 | dest='enforce_uses_libraries', |
| 57 | action='store_true', |
| 58 | help='check the uses-library entries known to the build system against ' |
| 59 | 'the manifest' |
| 60 | ) |
| 61 | parser.add_argument( |
| 62 | '--enforce-uses-libraries-relax', |
| 63 | dest='enforce_uses_libraries_relax', |
| 64 | action='store_true', |
| 65 | help='do not fail immediately, just save the error message to file') |
| 66 | parser.add_argument( |
| 67 | '--enforce-uses-libraries-status', |
| 68 | dest='enforce_uses_libraries_status', |
| 69 | help='output file to store check status (error message)') |
| 70 | parser.add_argument( |
| 71 | '--extract-target-sdk-version', |
| 72 | dest='extract_target_sdk_version', |
| 73 | action='store_true', |
| 74 | help='print the targetSdkVersion from the manifest') |
| 75 | parser.add_argument( |
| 76 | '--dexpreopt-config', |
| 77 | dest='dexpreopt_configs', |
| 78 | action='append', |
| 79 | help='a paths to a dexpreopt.config of some library') |
| 80 | parser.add_argument('--aapt', dest='aapt', help='path to aapt executable') |
| 81 | parser.add_argument( |
| 82 | '--output', '-o', dest='output', help='output AndroidManifest.xml file') |
| 83 | parser.add_argument('input', help='input AndroidManifest.xml file') |
| 84 | return parser.parse_args() |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 85 | |
| 86 | |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 87 | def enforce_uses_libraries(manifest, required, optional, relax, is_apk, path): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 88 | """Verify that the <uses-library> tags in the manifest match those provided |
| 89 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 90 | by the build system. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 91 | |
| 92 | Args: |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 93 | manifest: manifest (either parsed XML or aapt dump of APK) |
| 94 | required: required libs known to the build system |
| 95 | optional: optional libs known to the build system |
| 96 | relax: if true, suppress error on mismatch and just write it to file |
| 97 | is_apk: if the manifest comes from an APK or an XML file |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 98 | """ |
| 99 | if is_apk: |
| 100 | manifest_required, manifest_optional, tags = extract_uses_libs_apk( |
| 101 | manifest) |
| 102 | else: |
| 103 | manifest_required, manifest_optional, tags = extract_uses_libs_xml( |
| 104 | manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 105 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 106 | # Trim namespace component. Normally Soong does that automatically when it |
| 107 | # handles module names specified in Android.bp properties. However not all |
| 108 | # <uses-library> entries in the manifest correspond to real modules: some of |
| 109 | # the optional libraries may be missing at build time. Therefor this script |
| 110 | # accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the |
| 111 | # optional namespace part manually. |
| 112 | required = trim_namespace_parts(required) |
| 113 | optional = trim_namespace_parts(optional) |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 114 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 115 | if manifest_required == required and manifest_optional == optional: |
| 116 | return None |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 117 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 118 | #pylint: disable=line-too-long |
| 119 | errmsg = ''.join([ |
| 120 | 'mismatch in the <uses-library> tags between the build system and the ' |
| 121 | 'manifest:\n', |
| 122 | '\t- required libraries in build system: [%s]\n' % ', '.join(required), |
| 123 | '\t vs. in the manifest: [%s]\n' % |
| 124 | ', '.join(manifest_required), |
| 125 | '\t- optional libraries in build system: [%s]\n' % ', '.join(optional), |
| 126 | '\t vs. in the manifest: [%s]\n' % |
| 127 | ', '.join(manifest_optional), |
| 128 | '\t- tags in the manifest (%s):\n' % path, |
| 129 | '\t\t%s\n' % '\t\t'.join(tags), |
| 130 | 'note: the following options are available:\n', |
| 131 | '\t- to temporarily disable the check on command line, rebuild with ', |
| 132 | 'RELAX_USES_LIBRARY_CHECK=true (this will set compiler filter "verify" ', |
| 133 | 'and disable AOT-compilation in dexpreopt)\n', |
| 134 | '\t- to temporarily disable the check for the whole product, set ', |
| 135 | 'PRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true in the product makefiles\n', |
| 136 | '\t- to fix the check, make build system properties coherent with the ' |
| 137 | 'manifest\n', '\t- see build/make/Changes.md for details\n' |
| 138 | ]) |
| 139 | #pylint: enable=line-too-long |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 140 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 141 | if not relax: |
| 142 | raise ManifestMismatchError(errmsg) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 143 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 144 | return errmsg |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 145 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 146 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 147 | MODULE_NAMESPACE = re.compile('^//[^:]+:') |
| 148 | |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 149 | |
| 150 | def trim_namespace_parts(modules): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 151 | """Trim the namespace part of each module, if present. |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 152 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 153 | Leave only the name. |
| 154 | """ |
| 155 | |
| 156 | trimmed = [] |
| 157 | for module in modules: |
| 158 | trimmed.append(MODULE_NAMESPACE.sub('', module)) |
| 159 | return trimmed |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 160 | |
| 161 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 162 | def extract_uses_libs_apk(badging): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 163 | """Extract <uses-library> tags from the manifest of an APK.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 164 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 165 | pattern = re.compile("^uses-library(-not-required)?:'(.*)'$", re.MULTILINE) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 166 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 167 | required = [] |
| 168 | optional = [] |
| 169 | lines = [] |
| 170 | for match in re.finditer(pattern, badging): |
| 171 | lines.append(match.group(0)) |
| 172 | libname = match.group(2) |
| 173 | if match.group(1) is None: |
| 174 | required.append(libname) |
| 175 | else: |
| 176 | optional.append(libname) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 177 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 178 | required = first_unique_elements(required) |
| 179 | optional = first_unique_elements(optional) |
| 180 | tags = first_unique_elements(lines) |
| 181 | return required, optional, tags |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 182 | |
| 183 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 184 | def extract_uses_libs_xml(xml): #pylint: disable=inconsistent-return-statements |
| 185 | """Extract <uses-library> tags from the manifest.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 186 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 187 | manifest = parse_manifest(xml) |
| 188 | elems = get_children_with_tag(manifest, 'application') |
| 189 | application = elems[0] if len(elems) == 1 else None |
| 190 | if len(elems) > 1: #pylint: disable=no-else-raise |
| 191 | raise RuntimeError('found multiple <application> tags') |
| 192 | elif not elems: |
| 193 | if uses_libraries or optional_uses_libraries: #pylint: disable=undefined-variable |
| 194 | raise ManifestMismatchError('no <application> tag found') |
| 195 | return |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 196 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 197 | libs = get_children_with_tag(application, 'uses-library') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 198 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 199 | required = [uses_library_name(x) for x in libs if uses_library_required(x)] |
| 200 | optional = [ |
| 201 | uses_library_name(x) for x in libs if not uses_library_required(x) |
| 202 | ] |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 203 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 204 | # render <uses-library> tags as XML for a pretty error message |
| 205 | tags = [] |
| 206 | for lib in libs: |
| 207 | tags.append(lib.toprettyxml()) |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 208 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 209 | required = first_unique_elements(required) |
| 210 | optional = first_unique_elements(optional) |
| 211 | tags = first_unique_elements(tags) |
| 212 | return required, optional, tags |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 213 | |
| 214 | |
| 215 | def first_unique_elements(l): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 216 | result = [] |
| 217 | for x in l: |
| 218 | if x not in result: |
| 219 | result.append(x) |
| 220 | return result |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 221 | |
| 222 | |
| 223 | def uses_library_name(lib): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 224 | """Extract the name attribute of a uses-library tag. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 225 | |
| 226 | Args: |
| 227 | lib: a <uses-library> tag. |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 228 | """ |
| 229 | name = lib.getAttributeNodeNS(android_ns, 'name') |
| 230 | return name.value if name is not None else '' |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 231 | |
| 232 | |
| 233 | def uses_library_required(lib): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 234 | """Extract the required attribute of a uses-library tag. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 235 | |
| 236 | Args: |
| 237 | lib: a <uses-library> tag. |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 238 | """ |
| 239 | required = lib.getAttributeNodeNS(android_ns, 'required') |
| 240 | return (required.value == 'true') if required is not None else True |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 241 | |
| 242 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 243 | def extract_target_sdk_version(manifest, is_apk=False): |
| 244 | """Returns the targetSdkVersion from the manifest. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 245 | |
| 246 | Args: |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 247 | manifest: manifest (either parsed XML or aapt dump of APK) |
| 248 | is_apk: if the manifest comes from an APK or an XML file |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 249 | """ |
| 250 | if is_apk: #pylint: disable=no-else-return |
| 251 | return extract_target_sdk_version_apk(manifest) |
| 252 | else: |
| 253 | return extract_target_sdk_version_xml(manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 254 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 255 | |
| 256 | def extract_target_sdk_version_apk(badging): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 257 | """Extract targetSdkVersion tags from the manifest of an APK.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 258 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 259 | pattern = re.compile("^targetSdkVersion?:'(.*)'$", re.MULTILINE) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 260 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 261 | for match in re.finditer(pattern, badging): |
| 262 | return match.group(1) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 263 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 264 | raise RuntimeError('cannot find targetSdkVersion in the manifest') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 265 | |
| 266 | |
| 267 | def extract_target_sdk_version_xml(xml): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 268 | """Extract targetSdkVersion tags from the manifest.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 269 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 270 | manifest = parse_manifest(xml) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 271 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 272 | # Get or insert the uses-sdk element |
| 273 | uses_sdk = get_children_with_tag(manifest, 'uses-sdk') |
| 274 | if len(uses_sdk) > 1: #pylint: disable=no-else-raise |
| 275 | raise RuntimeError('found multiple uses-sdk elements') |
| 276 | elif len(uses_sdk) == 0: |
| 277 | raise RuntimeError('missing uses-sdk element') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 278 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 279 | uses_sdk = uses_sdk[0] |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 280 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 281 | min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion') |
| 282 | if min_attr is None: |
| 283 | raise RuntimeError('minSdkVersion is not specified') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 284 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 285 | target_attr = uses_sdk.getAttributeNodeNS(android_ns, 'targetSdkVersion') |
| 286 | if target_attr is None: |
| 287 | target_attr = min_attr |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 288 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 289 | return target_attr.value |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 290 | |
| 291 | |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 292 | def load_dexpreopt_configs(configs): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 293 | """Load dexpreopt.config files and map module names to library names.""" |
| 294 | module_to_libname = {} |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 295 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 296 | if configs is None: |
| 297 | configs = [] |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 298 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 299 | for config in configs: |
| 300 | with open(config, 'r') as f: |
| 301 | contents = json.load(f) |
| 302 | module_to_libname[contents['Name']] = contents['ProvidesUsesLibrary'] |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 303 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 304 | return module_to_libname |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 305 | |
| 306 | |
| 307 | def translate_libnames(modules, module_to_libname): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 308 | """Translate module names into library names using the mapping.""" |
| 309 | if modules is None: |
| 310 | modules = [] |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 311 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 312 | libnames = [] |
| 313 | for name in modules: |
| 314 | if name in module_to_libname: |
| 315 | name = module_to_libname[name] |
| 316 | libnames.append(name) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 317 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 318 | return libnames |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 319 | |
| 320 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 321 | def main(): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 322 | """Program entry point.""" |
| 323 | try: |
| 324 | args = parse_args() |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 325 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 326 | # The input can be either an XML manifest or an APK, they are parsed and |
| 327 | # processed in different ways. |
| 328 | is_apk = args.input.endswith('.apk') |
| 329 | if is_apk: |
| 330 | aapt = args.aapt if args.aapt is not None else 'aapt' |
| 331 | manifest = subprocess.check_output( |
| 332 | [aapt, 'dump', 'badging', args.input]) |
| 333 | else: |
| 334 | manifest = minidom.parse(args.input) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 335 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 336 | if args.enforce_uses_libraries: |
| 337 | # Load dexpreopt.config files and build a mapping from module |
| 338 | # names to library names. This is necessary because build system |
| 339 | # addresses libraries by their module name (`uses_libs`, |
| 340 | # `optional_uses_libs`, `LOCAL_USES_LIBRARIES`, |
| 341 | # `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain module names), while |
| 342 | # the manifest addresses libraries by their name. |
| 343 | mod_to_lib = load_dexpreopt_configs(args.dexpreopt_configs) |
| 344 | required = translate_libnames(args.uses_libraries, mod_to_lib) |
| 345 | optional = translate_libnames(args.optional_uses_libraries, |
| 346 | mod_to_lib) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 347 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 348 | # Check if the <uses-library> lists in the build system agree with |
| 349 | # those in the manifest. Raise an exception on mismatch, unless the |
| 350 | # script was passed a special parameter to suppress exceptions. |
| 351 | errmsg = enforce_uses_libraries(manifest, required, optional, |
| 352 | args.enforce_uses_libraries_relax, |
| 353 | is_apk, args.input) |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 354 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 355 | # Create a status file that is empty on success, or contains an |
| 356 | # error message on failure. When exceptions are suppressed, |
| 357 | # dexpreopt command command will check file size to determine if |
| 358 | # the check has failed. |
| 359 | if args.enforce_uses_libraries_status: |
| 360 | with open(args.enforce_uses_libraries_status, 'w') as f: |
Spandan Das | 3d5cd4d | 2021-09-20 18:24:56 +0000 | [diff] [blame] | 361 | if errmsg is not None: |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 362 | f.write('%s\n' % errmsg) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 363 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 364 | if args.extract_target_sdk_version: |
| 365 | try: |
| 366 | print(extract_target_sdk_version(manifest, is_apk)) |
| 367 | except: #pylint: disable=bare-except |
| 368 | # Failed; don't crash, return "any" SDK version. This will |
| 369 | # result in dexpreopt not adding any compatibility libraries. |
| 370 | print(10000) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 371 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 372 | if args.output: |
| 373 | # XML output is supposed to be written only when this script is |
| 374 | # invoked with XML input manifest, not with an APK. |
| 375 | if is_apk: |
| 376 | raise RuntimeError('cannot save APK manifest as XML') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 377 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 378 | with open(args.output, 'wb') as f: |
| 379 | write_xml(f, manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 380 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 381 | # pylint: disable=broad-except |
| 382 | except Exception as err: |
| 383 | print('error: ' + str(err), file=sys.stderr) |
| 384 | sys.exit(-1) |
| 385 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 386 | |
| 387 | if __name__ == '__main__': |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 388 | main() |