Alan Leung | 816c55f | 2017-10-19 19:00:29 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package util.build; |
| 18 | |
| 19 | import com.android.tools.r8.CompilationMode; |
| 20 | import com.android.tools.r8.D8; |
| 21 | import com.android.tools.r8.D8Command; |
| 22 | import com.android.tools.r8.utils.OutputMode; |
| 23 | import java.nio.file.Files; |
| 24 | import java.nio.file.Path; |
| 25 | import java.nio.file.Paths; |
| 26 | import java.nio.file.attribute.BasicFileAttributes; |
| 27 | |
| 28 | public class D8BuildStep extends BuildStep { |
| 29 | |
| 30 | private final boolean deleteInputFileAfterBuild; |
| 31 | private final D8Command.Builder builder; |
| 32 | |
| 33 | D8BuildStep(BuildFile inputFile, BuildFile outputFile, boolean deleteInputFileAfterBuild) { |
| 34 | super(inputFile, outputFile); |
| 35 | this.deleteInputFileAfterBuild = deleteInputFileAfterBuild; |
| 36 | this.builder = |
| 37 | D8Command.builder() |
| 38 | .setMode(CompilationMode.DEBUG) |
| 39 | .setMinApiLevel(1000) |
| 40 | .setEnableDesugaring(false) |
| 41 | .setOutputMode(OutputMode.Indexed); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | boolean build() { |
| 46 | |
| 47 | if (super.build()) { |
| 48 | try { |
| 49 | builder.setOutputPath(Paths.get(outputFile.fileName.getAbsolutePath())); |
| 50 | Files.find( |
| 51 | Paths.get(inputFile.fileName.getAbsolutePath()), |
| 52 | 1000, |
| 53 | D8BuildStep::isJarOrClassFile) |
| 54 | .forEach( |
| 55 | p -> { |
| 56 | try { |
| 57 | builder.addProgramFiles(p); |
| 58 | } catch (Throwable e) { |
| 59 | e.printStackTrace(); |
| 60 | } |
| 61 | }); |
| 62 | D8.run(builder.build()); |
| 63 | } catch (Throwable e) { |
| 64 | e.printStackTrace(); |
| 65 | return false; |
| 66 | } |
| 67 | if (deleteInputFileAfterBuild) { |
| 68 | inputFile.fileName.delete(); |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int hashCode() { |
| 77 | return inputFile.hashCode() ^ outputFile.hashCode(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object obj) { |
| 82 | if (super.equals(obj)) { |
| 83 | D8BuildStep other = (D8BuildStep) obj; |
| 84 | |
| 85 | return inputFile.equals(other.inputFile) && outputFile.equals(other.outputFile); |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | private static boolean isJarOrClassFile(Path file, BasicFileAttributes attrs) { |
| 91 | if (!attrs.isRegularFile()) { |
| 92 | return false; |
| 93 | } |
| 94 | String name = file.getFileName().toString().toLowerCase(); |
| 95 | return name.endsWith(".jar") || name.endsWith(".class"); |
| 96 | } |
| 97 | } |