Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package com.android.tools.apiusagesample; |
| 6 | |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 7 | import com.android.tools.r8.CompilationMode; |
| 8 | import com.android.tools.r8.D8; |
| 9 | import com.android.tools.r8.D8Command; |
Yohann Roussel | 76f1c54 | 2017-11-22 22:36:16 +0100 | [diff] [blame] | 10 | import com.android.tools.r8.origin.PathOrigin; |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 11 | import com.android.tools.r8.CompilationFailedException; |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 12 | import com.android.tools.r8.utils.OutputMode; |
| 13 | import java.io.IOException; |
| 14 | import java.nio.file.FileVisitResult; |
| 15 | import java.nio.file.Files; |
| 16 | import java.nio.file.Path; |
| 17 | import java.nio.file.Paths; |
| 18 | import java.nio.file.SimpleFileVisitor; |
| 19 | import java.nio.file.attribute.BasicFileAttributes; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | import java.util.concurrent.ExecutorService; |
| 23 | import java.util.concurrent.Executors; |
| 24 | |
| 25 | public class D8Compiler { |
| 26 | private int minSdkVersion; |
| 27 | private Path bootclasspath; |
| 28 | private List<Path> classpath; |
| 29 | private static ExecutorService pool = Executors.newFixedThreadPool(4); |
| 30 | |
| 31 | private D8Compiler(int minSdkVersion, Path bootclasspath, List<Path> classpath) { |
| 32 | this.minSdkVersion = minSdkVersion; |
| 33 | this.bootclasspath = bootclasspath; |
| 34 | this.classpath = classpath; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * java ...Compiler output input minSdkVersion mainDexClasses bootclasspath [classpathEntries]+ |
| 39 | */ |
| 40 | public static void main(String[] args) throws Throwable { |
| 41 | try { |
| 42 | int argIndex = 0; |
| 43 | Path outputDir = Paths.get(args[argIndex++]); |
| 44 | Path input = Paths.get(args[argIndex++]); |
| 45 | int minSdkVersion = Integer.parseInt(args[argIndex++]); |
| 46 | Path mainDexClasses = Paths.get(args[argIndex++]); |
| 47 | Path bootclasspath = Paths.get(args[argIndex++]); |
| 48 | |
| 49 | List<Path> classpath = new ArrayList<>(args.length - argIndex); |
| 50 | while (argIndex < args.length) { |
| 51 | classpath.add(Paths.get(args[argIndex++])); |
| 52 | } |
| 53 | |
| 54 | D8Compiler compiler = new D8Compiler(minSdkVersion, bootclasspath, classpath); |
| 55 | |
| 56 | List<Path> toMerge = new ArrayList<>(3); |
| 57 | |
| 58 | int intermediateIndex = 0; |
| 59 | for (Path entry : classpath) { |
| 60 | Path output = outputDir.resolve(entry.getFileName() + "." + (intermediateIndex++)); |
| 61 | Files.createDirectory(output); |
| 62 | toMerge.add(output); |
| 63 | compiler.compile(output, entry); |
| 64 | } |
| 65 | |
| 66 | Path output = outputDir.resolve("main." + (intermediateIndex++)); |
| 67 | Files.createDirectory(output); |
| 68 | toMerge.add(output); |
| 69 | compiler.compile(output, input); |
| 70 | |
| 71 | compiler.merge(outputDir, mainDexClasses, toMerge); |
| 72 | } finally { |
| 73 | // Terminate pool threads to prevent the VM to wait on then before exiting. |
| 74 | pool.shutdown(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private void compile(Path output, Path input) throws Throwable { |
| 79 | D8Command.Builder builder = |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 80 | D8Command.builder(new D8DiagnosticsHandler()) |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 81 | // Compile in debug and merge in release to assert access to both modes |
| 82 | .setMode(CompilationMode.DEBUG) |
| 83 | .setMinApiLevel(minSdkVersion) |
| 84 | .setIntermediate(true) |
| 85 | .setEnableDesugaring(true) |
| 86 | .setOutputPath(output); |
| 87 | |
| 88 | builder.addLibraryResourceProvider(CachingArchiveClassFileProvider.getProvider(bootclasspath)); |
| 89 | |
| 90 | for (Path entry : classpath) { |
| 91 | builder.addClasspathResourceProvider(CachingArchiveClassFileProvider.getProvider(entry)); |
| 92 | } |
| 93 | |
| 94 | if (Files.isRegularFile(input)) { |
| 95 | builder.setOutputMode(OutputMode.Indexed); |
| 96 | builder.addProgramFiles(input); |
| 97 | } else { |
| 98 | builder.setOutputMode(OutputMode.FilePerInputClass); |
| 99 | Files.walkFileTree(input, new SimpleFileVisitor<Path>() { |
| 100 | @Override |
| 101 | public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) |
| 102 | throws IOException { |
Yohann Roussel | 76f1c54 | 2017-11-22 22:36:16 +0100 | [diff] [blame] | 103 | builder.addClassProgramData(Files.readAllBytes(path), new PathOrigin(path)); |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 104 | return FileVisitResult.CONTINUE; |
| 105 | } |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | D8.run(builder.build(), pool); |
| 110 | } |
| 111 | |
| 112 | private void merge(Path outputDir, Path mainDexClasses, |
Yohann Roussel | 9a8d437 | 2017-11-21 14:36:44 +0100 | [diff] [blame] | 113 | List<Path> toMerge) throws IOException, CompilationFailedException { |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 114 | D8Command.Builder merger = D8Command.builder(new D8DiagnosticsHandler()); |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 115 | merger.setEnableDesugaring(false); |
| 116 | |
| 117 | for (Path mergeInput : toMerge) { |
| 118 | Files.walkFileTree(mergeInput, new SimpleFileVisitor<Path>() { |
| 119 | @Override |
| 120 | public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) |
| 121 | throws IOException { |
Yohann Roussel | 76f1c54 | 2017-11-22 22:36:16 +0100 | [diff] [blame] | 122 | merger.addDexProgramData(Files.readAllBytes(path), new PathOrigin(path)); |
Yohann Roussel | bb57162 | 2017-11-09 10:47:36 +0100 | [diff] [blame] | 123 | return FileVisitResult.CONTINUE; |
| 124 | } |
| 125 | }); |
| 126 | } |
| 127 | if (mainDexClasses != null) { |
| 128 | merger.addMainDexListFiles(mainDexClasses); |
| 129 | } |
| 130 | merger.setMinApiLevel(minSdkVersion) |
| 131 | .setMode(CompilationMode.RELEASE) |
| 132 | .setOutputPath(outputDir) |
| 133 | .setEnableDesugaring(false) |
| 134 | .setIntermediate(false); |
| 135 | D8.run(merger.build()); |
| 136 | } |
| 137 | } |