Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 2 | # Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | from __future__ import print_function |
| 6 | import os |
| 7 | import sys |
| 8 | import zipfile |
| 9 | |
| 10 | # Proguard lookup program classes before library classes. In R8 this is |
| 11 | # not the behaviour (it used to be) R8 will check library classes before |
| 12 | # program classes. Some apps have duplicate classes in the library and program. |
| 13 | # To make these apps work with R8 simulate program classes before library |
| 14 | # classes by creating a new library jar which have all the provided library |
| 15 | # classes which are not also in program classes. |
Søren Gjesse | 889e09d | 2019-11-07 16:33:51 +0100 | [diff] [blame] | 16 | def SanitizeLibrariesInPgconf( |
Christoffer Quist Adamsen | 0bd9075 | 2019-11-11 12:16:29 +0100 | [diff] [blame] | 17 | sanitized_lib_path, |
| 18 | sanitized_pgconf_path, |
| 19 | pgconfs, |
| 20 | injars = None, |
| 21 | libraryjars = None): |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 22 | with open(sanitized_pgconf_path, 'w') as sanitized_pgconf: |
Christoffer Quist Adamsen | 0bd9075 | 2019-11-11 12:16:29 +0100 | [diff] [blame] | 23 | injars = [] if injars is None else injars |
| 24 | libraryjars = [] if libraryjars is None else libraryjars |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 25 | for pgconf in pgconfs: |
| 26 | pgconf_dirname = os.path.abspath(os.path.dirname(pgconf)) |
| 27 | first_library_jar = True |
| 28 | with open(pgconf) as pgconf_file: |
| 29 | for line in pgconf_file: |
| 30 | trimmed = line.strip() |
| 31 | if trimmed.startswith('-injars'): |
| 32 | # Collect -injars and leave them in the configuration. |
| 33 | injar = os.path.join( |
| 34 | pgconf_dirname, trimmed[len('-injars'):].strip()) |
| 35 | injars.append(injar) |
| 36 | sanitized_pgconf.write('-injars {}\n'.format(injar)) |
| 37 | elif trimmed.startswith('-libraryjars'): |
| 38 | # Collect -libraryjars and replace them with the sanitized library. |
| 39 | libraryjar = os.path.join( |
| 40 | pgconf_dirname, trimmed[len('-libraryjars'):].strip()) |
| 41 | libraryjars.append(libraryjar) |
| 42 | if first_library_jar: |
| 43 | sanitized_pgconf.write( |
| 44 | '-libraryjars {}\n'.format(sanitized_lib_path)) |
| 45 | first_library_jar = False |
| 46 | sanitized_pgconf.write('# {}'.format(line)) |
| 47 | else: |
| 48 | sanitized_pgconf.write(line) |
| 49 | |
Søren Gjesse | 889e09d | 2019-11-07 16:33:51 +0100 | [diff] [blame] | 50 | SanitizeLibraries(sanitized_lib_path, libraryjars, injars) |
| 51 | |
| 52 | |
| 53 | def SanitizeLibraries(sanitized_lib_path, libraryjars, injars): |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 54 | program_entries = set() |
| 55 | library_entries = set() |
| 56 | |
| 57 | for injar in injars: |
| 58 | with zipfile.ZipFile(injar, 'r') as injar_zf: |
| 59 | for zipinfo in injar_zf.infolist(): |
| 60 | program_entries.add(zipinfo.filename) |
| 61 | |
| 62 | with zipfile.ZipFile(sanitized_lib_path, 'w') as output_zf: |
| 63 | for libraryjar in libraryjars: |
| 64 | with zipfile.ZipFile(libraryjar, 'r') as input_zf: |
| 65 | for zipinfo in input_zf.infolist(): |
| 66 | if (not zipinfo.filename in program_entries |
| 67 | and not zipinfo.filename in library_entries): |
| 68 | library_entries.add(zipinfo.filename) |
| 69 | output_zf.writestr(zipinfo, input_zf.read(zipinfo)) |
| 70 | |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 71 | |
Christoffer Quist Adamsen | 0bd9075 | 2019-11-11 12:16:29 +0100 | [diff] [blame] | 72 | def usage(argv, error): |
| 73 | print(error) |
| 74 | print("Usage: sanitize_libraries.py <sanitized_lib> <sanitized_pgconf> (" |
| 75 | + "--injar <existing_injar>" |
| 76 | + "|--libraryjar <existing_library_jar>" |
| 77 | + "|--pgconf <existing_pgconf>)+") |
| 78 | return 1 |
| 79 | |
| 80 | |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 81 | def main(argv): |
Christoffer Quist Adamsen | 0bd9075 | 2019-11-11 12:16:29 +0100 | [diff] [blame] | 82 | if (len(argv) < 4): |
| 83 | return usage(argv, "Wrong number of arguments!") |
| 84 | pgconfs = [] |
| 85 | injars = [] |
| 86 | libraryjars = [] |
| 87 | i = 2 |
| 88 | while i < len(argv): |
| 89 | directive = argv[i] |
| 90 | if directive not in ['--pgconf', '--injar', '--libraryjar']: |
| 91 | return usage( |
| 92 | argv, |
| 93 | 'Unexpected argument, expected one of --pgconf, --injar, and ' |
| 94 | + '--libraryjar.') |
| 95 | if i + 1 >= len(argv): |
| 96 | return usage(argv, 'Expected argument after ' + directive + '.') |
| 97 | file = argv[i + 1] |
| 98 | if directive == '--pgconf': |
| 99 | pgconfs.append(file) |
| 100 | elif directive == '--injar': |
| 101 | injars.append(file) |
| 102 | elif directive == '--libraryjar': |
| 103 | libraryjars.append(file) |
| 104 | i = i + 2 |
| 105 | SanitizeLibrariesInPgconf(argv[0], argv[1], pgconfs, injars, libraryjars) |
Søren Gjesse | cbeae78 | 2019-05-21 14:14:25 +0200 | [diff] [blame] | 106 | |
| 107 | if __name__ == '__main__': |
| 108 | sys.exit(main(sys.argv[1:])) |