Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 2 | # Copyright (c) 2018, 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 | |
| 6 | ''' |
| 7 | Build r8lib.jar using src/main/keep.txt and test that d8_api_usage_sample.jar |
| 8 | works with the minified R8. |
| 9 | ''' |
| 10 | |
| 11 | import argparse |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 12 | import gradle |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 13 | import jdk |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 14 | import os |
| 15 | import subprocess |
| 16 | import toolhelper |
| 17 | import utils |
| 18 | |
| 19 | parser = argparse.ArgumentParser(description=__doc__.strip(), |
| 20 | formatter_class=argparse.RawTextHelpFormatter) |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 21 | parser.add_argument('-e', '--exclude_deps', action='store_true', |
| 22 | help='Create lib jar without dependencies') |
| 23 | parser.add_argument('-k', '--keep', default=utils.R8LIB_KEEP_RULES, |
| 24 | help='Keep rules file for lib') |
| 25 | parser.add_argument('-n', '--no_relocate', action='store_true', |
| 26 | help='Create lib jar without relocating libraries') |
| 27 | parser.add_argument('-o', '--out', default=None, |
| 28 | help='Output for built library') |
| 29 | parser.add_argument('-t', '--target', default='r8', |
| 30 | help='Compile target for library') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 31 | |
| 32 | API_LEVEL = 26 |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 33 | DEPS_JAR = os.path.join(utils.LIBS, 'deps.jar') |
| 34 | SAMPLE_JAR = os.path.join(utils.REPO_ROOT, 'tests', 'd8_api_usage_sample.jar') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 35 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 36 | def build_r8lib(target, exclude_deps, no_relocate, keep_rules_path, |
| 37 | output_path, **kwargs): |
| 38 | # Clean the build directory to ensure no repackaging of any existing |
| 39 | # lib or deps. |
| 40 | gradle.RunGradle(['clean']) |
| 41 | lib_args = [target] |
| 42 | deps_args = ['repackageDeps'] |
| 43 | if exclude_deps: |
| 44 | lib_args.append('-Pexclude_deps') |
| 45 | if no_relocate: |
| 46 | lib_args.append('-Plib_no_relocate') |
| 47 | deps_args.append('-Plib_no_relocate') |
| 48 | # Produce the r8lib target to be processed later. |
| 49 | gradle.RunGradle(lib_args) |
| 50 | target_lib = os.path.join(utils.LIBS, target + '.jar') |
| 51 | temp_lib = os.path.join(utils.LIBS, target + '_to_process.jar') |
| 52 | os.rename(target_lib, temp_lib) |
| 53 | # Produce the dependencies needed for running r8 on lib.jar. |
| 54 | gradle.RunGradle(deps_args) |
| 55 | temp_deps = os.path.join(utils.LIBS, target + 'lib_deps.jar') |
| 56 | os.rename(DEPS_JAR, temp_deps) |
| 57 | # Produce R8 for compiling lib |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 58 | if output_path is None: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 59 | output_path = target + 'lib.jar' |
Tamas Kenez | 54b939a | 2018-12-07 15:55:26 +0100 | [diff] [blame] | 60 | output_map_path = output_path + '.map' |
Morten Krogh-Jespersen | eda5581 | 2023-04-25 09:26:07 +0200 | [diff] [blame] | 61 | r8_output_map_path = output_path + '_map.zip' |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 62 | toolhelper.run( |
| 63 | 'r8', |
| 64 | ('--release', |
| 65 | '--classfile', |
| 66 | '--lib', utils.RT_JAR, |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 67 | '--lib', temp_deps, |
| 68 | temp_lib, |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 69 | '--output', output_path, |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 70 | '--pg-conf', keep_rules_path, |
Morten Krogh-Jespersen | eda5581 | 2023-04-25 09:26:07 +0200 | [diff] [blame] | 71 | '--pg-map-output', output_map_path, |
| 72 | '--partition-map-output', r8_output_map_path), |
Mathias Rav | 56df69d | 2018-06-07 12:38:21 +0200 | [diff] [blame] | 73 | **kwargs) |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 74 | if exclude_deps: |
| 75 | return [output_path, temp_deps] |
| 76 | else: |
| 77 | return [output_path] |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 78 | |
| 79 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 80 | def test_d8sample(paths): |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 81 | with utils.TempDir() as path: |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 82 | args = [jdk.GetJavaExecutable(), |
| 83 | '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 84 | 'com.android.tools.apiusagesample.D8ApiUsageSample', |
| 85 | '--output', path, |
| 86 | '--min-api', str(API_LEVEL), |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 87 | '--lib', utils.get_android_jar(API_LEVEL), |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 88 | '--classpath', utils.R8_JAR, |
| 89 | '--main-dex-list', '/dev/null', |
| 90 | os.path.join(utils.BUILD, 'test/examples/hello.jar')] |
| 91 | utils.PrintCmd(args) |
| 92 | subprocess.check_call(args) |
| 93 | |
| 94 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 95 | def test_r8command(paths): |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 96 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 97 | # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g. |
| 98 | # R8CommandParser should have been minified in LIB_JAR. |
| 99 | # Just in case R8CommandParser is also present in LIB_JAR, we put |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 100 | # SAMPLE_JAR first on the classpath to use its version of R8CommandParser. |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 101 | args = [jdk.GetJavaExecutable(), |
| 102 | '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 103 | 'com.android.tools.r8.R8CommandParser', |
| 104 | '--output', path + "/output.zip", |
| 105 | '--min-api', str(API_LEVEL), |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 106 | '--lib', utils.get_android_jar(API_LEVEL), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 107 | '--main-dex-list', '/dev/null', |
| 108 | os.path.join(utils.BUILD, 'test/examples/hello.jar')] |
| 109 | utils.PrintCmd(args) |
| 110 | subprocess.check_call(args) |
| 111 | |
| 112 | |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 113 | def test_r8cfcommand(paths): |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 114 | with utils.TempDir() as path: |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 115 | # SAMPLE_JAR and LIB_JAR should not have any classes in common, since e.g. |
| 116 | # R8CommandParser should have been minified in LIB_JAR. |
| 117 | # Just in case R8CommandParser is also present in LIB_JAR, we put |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 118 | # SAMPLE_JAR first on the classpath to use its version of R8CommandParser. |
Ian Zerny | 3f54e22 | 2019-02-12 10:51:17 +0100 | [diff] [blame] | 119 | args = [jdk.GetJavaExecutable(), |
| 120 | '-cp', '%s:%s' % (SAMPLE_JAR, ":".join(paths)), |
Mathias Rav | e3f3c52 | 2018-05-30 08:22:17 +0200 | [diff] [blame] | 121 | 'com.android.tools.r8.R8CommandParser', |
| 122 | '--classfile', |
| 123 | '--output', path + "/output.jar", |
| 124 | '--lib', utils.RT_JAR, |
| 125 | os.path.join(utils.BUILD, 'test/examples/hello.jar')] |
| 126 | utils.PrintCmd(args) |
| 127 | subprocess.check_call(args) |
| 128 | |
| 129 | |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 130 | def main(): |
| 131 | # Handle --help |
Morten Krogh-Jespersen | 00699af | 2018-10-09 10:54:42 +0200 | [diff] [blame] | 132 | args = parser.parse_args() |
| 133 | output_paths = build_r8lib( |
| 134 | args.target, args.exclude_deps, args.no_relocate, args.keep, args.out) |
| 135 | if args.target == 'r8': |
| 136 | gradle.RunGradle(['buildExampleJars']) |
| 137 | test_r8command(output_paths) |
| 138 | test_r8cfcommand(output_paths) |
| 139 | if args.target == 'd8': |
| 140 | gradle.RunGradle(['buildExampleJars']) |
| 141 | test_d8sample(output_paths) |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | main() |