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 | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 87 | C_RED = "\033[1;31m" |
| 88 | C_GREEN = "\033[1;32m" |
| 89 | C_BLUE = "\033[1;34m" |
| 90 | C_OFF = "\033[0m" |
| 91 | C_BOLD = "\033[1m" |
| 92 | |
| 93 | |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 94 | def enforce_uses_libraries(manifest, required, optional, relax, is_apk, path): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 95 | """Verify that the <uses-library> tags in the manifest match those provided |
| 96 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 97 | by the build system. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 98 | |
| 99 | Args: |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 100 | manifest: manifest (either parsed XML or aapt dump of APK) |
| 101 | required: required libs known to the build system |
| 102 | optional: optional libs known to the build system |
| 103 | relax: if true, suppress error on mismatch and just write it to file |
| 104 | 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] | 105 | """ |
| 106 | if is_apk: |
| 107 | manifest_required, manifest_optional, tags = extract_uses_libs_apk( |
| 108 | manifest) |
| 109 | else: |
| 110 | manifest_required, manifest_optional, tags = extract_uses_libs_xml( |
| 111 | manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 112 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 113 | # Trim namespace component. Normally Soong does that automatically when it |
| 114 | # handles module names specified in Android.bp properties. However not all |
| 115 | # <uses-library> entries in the manifest correspond to real modules: some of |
| 116 | # the optional libraries may be missing at build time. Therefor this script |
| 117 | # accepts raw module names as spelled in Android.bp/Amdroid.mk and trims the |
| 118 | # optional namespace part manually. |
| 119 | required = trim_namespace_parts(required) |
| 120 | optional = trim_namespace_parts(optional) |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 121 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 122 | if manifest_required == required and manifest_optional == optional: |
| 123 | return None |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 124 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 125 | #pylint: disable=line-too-long |
| 126 | errmsg = ''.join([ |
| 127 | 'mismatch in the <uses-library> tags between the build system and the ' |
| 128 | 'manifest:\n', |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 129 | '\t- required libraries in build system: %s[%s]%s\n' % (C_RED, ', '.join(required), C_OFF), |
| 130 | '\t vs. in the manifest: %s[%s]%s\n' % (C_RED, ', '.join(manifest_required), C_OFF), |
| 131 | '\t- optional libraries in build system: %s[%s]%s\n' % (C_RED, ', '.join(optional), C_OFF), |
| 132 | '\t vs. in the manifest: %s[%s]%s\n' % (C_RED, ', '.join(manifest_optional), C_OFF), |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 133 | '\t- tags in the manifest (%s):\n' % path, |
| 134 | '\t\t%s\n' % '\t\t'.join(tags), |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 135 | '%snote:%s the following options are available:\n' % (C_BLUE, C_OFF), |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 136 | '\t- to temporarily disable the check on command line, rebuild with ', |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 137 | '%sRELAX_USES_LIBRARY_CHECK=true%s' % (C_BOLD, C_OFF), |
| 138 | ' (this will set compiler filter "verify" and disable AOT-compilation in dexpreopt)\n', |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 139 | '\t- to temporarily disable the check for the whole product, set ', |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 140 | '%sPRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true%s in the product makefiles\n' % (C_BOLD, C_OFF), |
| 141 | '\t- to fix the check, make build system properties coherent with the manifest\n', |
| 142 | '\t- for details, see %sbuild/make/Changes.md%s' % (C_GREEN, C_OFF), |
| 143 | ' and %shttps://source.android.com/devices/tech/dalvik/art-class-loader-context%s\n' % (C_GREEN, C_OFF) |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 144 | ]) |
| 145 | #pylint: enable=line-too-long |
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 | if not relax: |
| 148 | raise ManifestMismatchError(errmsg) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 149 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 150 | return errmsg |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 151 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 152 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 153 | MODULE_NAMESPACE = re.compile('^//[^:]+:') |
| 154 | |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 155 | |
| 156 | def trim_namespace_parts(modules): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 157 | """Trim the namespace part of each module, if present. |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 158 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 159 | Leave only the name. |
| 160 | """ |
| 161 | |
| 162 | trimmed = [] |
| 163 | for module in modules: |
| 164 | trimmed.append(MODULE_NAMESPACE.sub('', module)) |
| 165 | return trimmed |
Ulya Trafimovich | 1b51345 | 2021-07-20 14:27:32 +0100 | [diff] [blame] | 166 | |
| 167 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 168 | def extract_uses_libs_apk(badging): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 169 | """Extract <uses-library> tags from the manifest of an APK.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 170 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 171 | pattern = re.compile("^uses-library(-not-required)?:'(.*)'$", re.MULTILINE) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 172 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 173 | required = [] |
| 174 | optional = [] |
| 175 | lines = [] |
| 176 | for match in re.finditer(pattern, badging): |
| 177 | lines.append(match.group(0)) |
| 178 | libname = match.group(2) |
| 179 | if match.group(1) is None: |
| 180 | required.append(libname) |
| 181 | else: |
| 182 | optional.append(libname) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 183 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 184 | required = first_unique_elements(required) |
| 185 | optional = first_unique_elements(optional) |
| 186 | tags = first_unique_elements(lines) |
| 187 | return required, optional, tags |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 188 | |
| 189 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 190 | def extract_uses_libs_xml(xml): #pylint: disable=inconsistent-return-statements |
| 191 | """Extract <uses-library> tags from the manifest.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 192 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 193 | manifest = parse_manifest(xml) |
| 194 | elems = get_children_with_tag(manifest, 'application') |
| 195 | application = elems[0] if len(elems) == 1 else None |
| 196 | if len(elems) > 1: #pylint: disable=no-else-raise |
| 197 | raise RuntimeError('found multiple <application> tags') |
| 198 | elif not elems: |
| 199 | if uses_libraries or optional_uses_libraries: #pylint: disable=undefined-variable |
| 200 | raise ManifestMismatchError('no <application> tag found') |
| 201 | return |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 202 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 203 | libs = get_children_with_tag(application, 'uses-library') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 204 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 205 | required = [uses_library_name(x) for x in libs if uses_library_required(x)] |
| 206 | optional = [ |
| 207 | uses_library_name(x) for x in libs if not uses_library_required(x) |
| 208 | ] |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 209 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 210 | # render <uses-library> tags as XML for a pretty error message |
| 211 | tags = [] |
| 212 | for lib in libs: |
| 213 | tags.append(lib.toprettyxml()) |
Ulya Trafimovich | bb7513d | 2021-03-30 17:15:16 +0100 | [diff] [blame] | 214 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 215 | required = first_unique_elements(required) |
| 216 | optional = first_unique_elements(optional) |
| 217 | tags = first_unique_elements(tags) |
| 218 | return required, optional, tags |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 219 | |
| 220 | |
| 221 | def first_unique_elements(l): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 222 | result = [] |
| 223 | for x in l: |
| 224 | if x not in result: |
| 225 | result.append(x) |
| 226 | return result |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 227 | |
| 228 | |
| 229 | def uses_library_name(lib): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 230 | """Extract the name attribute of a uses-library tag. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 231 | |
| 232 | Args: |
| 233 | lib: a <uses-library> tag. |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 234 | """ |
| 235 | name = lib.getAttributeNodeNS(android_ns, 'name') |
| 236 | return name.value if name is not None else '' |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def uses_library_required(lib): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 240 | """Extract the required attribute of a uses-library tag. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 241 | |
| 242 | Args: |
| 243 | lib: a <uses-library> tag. |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 244 | """ |
| 245 | required = lib.getAttributeNodeNS(android_ns, 'required') |
| 246 | return (required.value == 'true') if required is not None else True |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 247 | |
| 248 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 249 | def extract_target_sdk_version(manifest, is_apk=False): |
| 250 | """Returns the targetSdkVersion from the manifest. |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 251 | |
| 252 | Args: |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 253 | manifest: manifest (either parsed XML or aapt dump of APK) |
| 254 | 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] | 255 | """ |
| 256 | if is_apk: #pylint: disable=no-else-return |
| 257 | return extract_target_sdk_version_apk(manifest) |
| 258 | else: |
| 259 | return extract_target_sdk_version_xml(manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 260 | |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 261 | |
| 262 | def extract_target_sdk_version_apk(badging): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 263 | """Extract targetSdkVersion tags from the manifest of an APK.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 264 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 265 | pattern = re.compile("^targetSdkVersion?:'(.*)'$", re.MULTILINE) |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 266 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 267 | for match in re.finditer(pattern, badging): |
| 268 | return match.group(1) |
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 | raise RuntimeError('cannot find targetSdkVersion in the manifest') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | def extract_target_sdk_version_xml(xml): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 274 | """Extract targetSdkVersion tags from the manifest.""" |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 275 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 276 | manifest = parse_manifest(xml) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 277 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 278 | # Get or insert the uses-sdk element |
| 279 | uses_sdk = get_children_with_tag(manifest, 'uses-sdk') |
| 280 | if len(uses_sdk) > 1: #pylint: disable=no-else-raise |
| 281 | raise RuntimeError('found multiple uses-sdk elements') |
| 282 | elif len(uses_sdk) == 0: |
| 283 | raise RuntimeError('missing uses-sdk element') |
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 | uses_sdk = uses_sdk[0] |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 286 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 287 | min_attr = uses_sdk.getAttributeNodeNS(android_ns, 'minSdkVersion') |
| 288 | if min_attr is None: |
| 289 | raise RuntimeError('minSdkVersion is not specified') |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 290 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 291 | target_attr = uses_sdk.getAttributeNodeNS(android_ns, 'targetSdkVersion') |
| 292 | if target_attr is None: |
| 293 | target_attr = min_attr |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 294 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 295 | return target_attr.value |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 296 | |
| 297 | |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 298 | def load_dexpreopt_configs(configs): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 299 | """Load dexpreopt.config files and map module names to library names.""" |
| 300 | module_to_libname = {} |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 301 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 302 | if configs is None: |
| 303 | configs = [] |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 304 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 305 | for config in configs: |
| 306 | with open(config, 'r') as f: |
| 307 | contents = json.load(f) |
| 308 | module_to_libname[contents['Name']] = contents['ProvidesUsesLibrary'] |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 309 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 310 | return module_to_libname |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 311 | |
| 312 | |
| 313 | def translate_libnames(modules, module_to_libname): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 314 | """Translate module names into library names using the mapping.""" |
| 315 | if modules is None: |
| 316 | modules = [] |
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 | libnames = [] |
| 319 | for name in modules: |
| 320 | if name in module_to_libname: |
| 321 | name = module_to_libname[name] |
| 322 | libnames.append(name) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 323 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 324 | return libnames |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 325 | |
| 326 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 327 | def main(): |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 328 | """Program entry point.""" |
| 329 | try: |
| 330 | args = parse_args() |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 331 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 332 | # The input can be either an XML manifest or an APK, they are parsed and |
| 333 | # processed in different ways. |
| 334 | is_apk = args.input.endswith('.apk') |
| 335 | if is_apk: |
| 336 | aapt = args.aapt if args.aapt is not None else 'aapt' |
| 337 | manifest = subprocess.check_output( |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 338 | [aapt, 'dump', 'badging', args.input]).decode('utf-8') |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 339 | else: |
| 340 | manifest = minidom.parse(args.input) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 341 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 342 | if args.enforce_uses_libraries: |
| 343 | # Load dexpreopt.config files and build a mapping from module |
| 344 | # names to library names. This is necessary because build system |
| 345 | # addresses libraries by their module name (`uses_libs`, |
| 346 | # `optional_uses_libs`, `LOCAL_USES_LIBRARIES`, |
| 347 | # `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain module names), while |
| 348 | # the manifest addresses libraries by their name. |
| 349 | mod_to_lib = load_dexpreopt_configs(args.dexpreopt_configs) |
| 350 | required = translate_libnames(args.uses_libraries, mod_to_lib) |
| 351 | optional = translate_libnames(args.optional_uses_libraries, |
| 352 | mod_to_lib) |
Ulya Trafimovich | 3c902e7 | 2021-03-04 18:06:27 +0000 | [diff] [blame] | 353 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 354 | # Check if the <uses-library> lists in the build system agree with |
| 355 | # those in the manifest. Raise an exception on mismatch, unless the |
| 356 | # script was passed a special parameter to suppress exceptions. |
| 357 | errmsg = enforce_uses_libraries(manifest, required, optional, |
| 358 | args.enforce_uses_libraries_relax, |
| 359 | is_apk, args.input) |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 360 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 361 | # Create a status file that is empty on success, or contains an |
| 362 | # error message on failure. When exceptions are suppressed, |
| 363 | # dexpreopt command command will check file size to determine if |
| 364 | # the check has failed. |
| 365 | if args.enforce_uses_libraries_status: |
| 366 | with open(args.enforce_uses_libraries_status, 'w') as f: |
Spandan Das | 3d5cd4d | 2021-09-20 18:24:56 +0000 | [diff] [blame] | 367 | if errmsg is not None: |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 368 | f.write('%s\n' % errmsg) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 369 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 370 | if args.extract_target_sdk_version: |
| 371 | try: |
| 372 | print(extract_target_sdk_version(manifest, is_apk)) |
| 373 | except: #pylint: disable=bare-except |
| 374 | # Failed; don't crash, return "any" SDK version. This will |
| 375 | # result in dexpreopt not adding any compatibility libraries. |
| 376 | print(10000) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 377 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 378 | if args.output: |
| 379 | # XML output is supposed to be written only when this script is |
| 380 | # invoked with XML input manifest, not with an APK. |
| 381 | if is_apk: |
| 382 | raise RuntimeError('cannot save APK manifest as XML') |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 383 | |
Cole Faust | c41dd72 | 2021-11-09 15:08:26 -0800 | [diff] [blame] | 384 | with open(args.output, 'w') as f: |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 385 | write_xml(f, manifest) |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 386 | |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 387 | # pylint: disable=broad-except |
| 388 | except Exception as err: |
Ulya Trafimovich | b4c19f8 | 2021-11-01 12:57:59 +0000 | [diff] [blame] | 389 | print('%serror:%s ' % (C_RED, C_OFF) + str(err), file=sys.stderr) |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 390 | sys.exit(-1) |
| 391 | |
Colin Cross | 7211910 | 2019-05-20 13:14:18 -0700 | [diff] [blame] | 392 | |
| 393 | if __name__ == '__main__': |
Spandan Das | f880742 | 2021-08-25 20:01:17 +0000 | [diff] [blame] | 394 | main() |