blob: bc61d50c68da3967d9b8a43def5273d829e0e752 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Søren Gjessecbeae782019-05-21 14:14:25 +02002# 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.
5from __future__ import print_function
6import os
7import sys
8import 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 Gjesse889e09d2019-11-07 16:33:51 +010016def SanitizeLibrariesInPgconf(
Christoffer Quist Adamsen0bd90752019-11-11 12:16:29 +010017 sanitized_lib_path,
18 sanitized_pgconf_path,
19 pgconfs,
20 injars = None,
21 libraryjars = None):
Søren Gjessecbeae782019-05-21 14:14:25 +020022 with open(sanitized_pgconf_path, 'w') as sanitized_pgconf:
Christoffer Quist Adamsen0bd90752019-11-11 12:16:29 +010023 injars = [] if injars is None else injars
24 libraryjars = [] if libraryjars is None else libraryjars
Søren Gjessecbeae782019-05-21 14:14:25 +020025 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 Gjesse889e09d2019-11-07 16:33:51 +010050 SanitizeLibraries(sanitized_lib_path, libraryjars, injars)
51
52
53def SanitizeLibraries(sanitized_lib_path, libraryjars, injars):
Søren Gjessecbeae782019-05-21 14:14:25 +020054 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 Gjessecbeae782019-05-21 14:14:25 +020071
Christoffer Quist Adamsen0bd90752019-11-11 12:16:29 +010072def 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 Gjessecbeae782019-05-21 14:14:25 +020081def main(argv):
Christoffer Quist Adamsen0bd90752019-11-11 12:16:29 +010082 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 Gjessecbeae782019-05-21 14:14:25 +0200106
107if __name__ == '__main__':
108 sys.exit(main(sys.argv[1:]))