Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.sun.tools.javac.Main; |
| 20 | |
| 21 | import java.io.File; |
| 22 | import java.io.PrintWriter; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.Set; |
| 25 | |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 26 | public class JavacBuildStep extends SourceBuildStep { |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 27 | |
| 28 | private final String destPath; |
| 29 | private final String classPath; |
| 30 | private final Set<String> sourceFiles = new HashSet<String>(); |
| 31 | public JavacBuildStep(String destPath, String classPath) { |
Yohann Roussel | e5b72e2 | 2015-10-01 17:35:37 +0200 | [diff] [blame] | 32 | super(new File(destPath)); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 33 | this.destPath = destPath; |
| 34 | this.classPath = classPath; |
| 35 | } |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 36 | |
| 37 | @Override |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 38 | public void addSourceFile(String sourceFile) |
| 39 | { |
| 40 | sourceFiles.add(sourceFile); |
| 41 | } |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 42 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 43 | @Override |
| 44 | boolean build() { |
| 45 | if (super.build()) |
| 46 | { |
| 47 | if (sourceFiles.isEmpty()) |
| 48 | { |
| 49 | return true; |
| 50 | } |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 51 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 52 | File destFile = new File(destPath); |
| 53 | if (!destFile.exists() && !destFile.mkdirs()) |
| 54 | { |
| 55 | System.err.println("failed to create destination dir"); |
| 56 | return false; |
| 57 | } |
Neil Fuller | ece00bc | 2015-08-12 14:31:43 +0100 | [diff] [blame] | 58 | int args = 8; |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 59 | String[] commandLine = new String[sourceFiles.size()+args]; |
| 60 | commandLine[0] = "-classpath"; |
| 61 | commandLine[1] = classPath; |
| 62 | commandLine[2] = "-d"; |
| 63 | commandLine[3] = destPath; |
Neil Fuller | ece00bc | 2015-08-12 14:31:43 +0100 | [diff] [blame] | 64 | commandLine[4] = "-source"; |
| 65 | commandLine[5] = "1.7"; |
| 66 | commandLine[6] = "-target"; |
| 67 | commandLine[7] = "1.7"; |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 68 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 69 | String[] files = new String[sourceFiles.size()]; |
| 70 | sourceFiles.toArray(files); |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 71 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 72 | System.arraycopy(files, 0, commandLine, args, files.length); |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 73 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 74 | return Main.compile(commandLine, new PrintWriter(System.err)) == 0; |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean equals(Object obj) { |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 81 | if (super.equals(obj)) |
| 82 | { |
| 83 | JavacBuildStep other = (JavacBuildStep) obj; |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 84 | return destPath.equals(other.destPath) |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 85 | && classPath.equals(other.classPath) |
| 86 | && sourceFiles.equals(other.sourceFiles); |
| 87 | } |
| 88 | return false; |
| 89 | } |
Yohann Roussel | 1046142 | 2014-09-25 10:13:32 +0200 | [diff] [blame] | 90 | |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public int hashCode() { |
| 93 | return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode(); |
| 94 | } |
| 95 | } |