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