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