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 26 public class JavacBuildStep extends SourceBuildStep { 27 28 private final String destPath; 29 private final String classPath; 30 private final Set<String> sourceFiles = new HashSet<String>(); JavacBuildStep(String destPath, String classPath)31 public JavacBuildStep(String destPath, String classPath) { 32 this.destPath = destPath; 33 this.classPath = classPath; 34 } 35 36 @Override addSourceFile(String sourceFile)37 public void addSourceFile(String sourceFile) 38 { 39 sourceFiles.add(sourceFile); 40 } 41 42 @Override build()43 boolean build() { 44 if (super.build()) 45 { 46 if (sourceFiles.isEmpty()) 47 { 48 return true; 49 } 50 51 File destFile = new File(destPath); 52 if (!destFile.exists() && !destFile.mkdirs()) 53 { 54 System.err.println("failed to create destination dir"); 55 return false; 56 } 57 int args = 4; 58 String[] commandLine = new String[sourceFiles.size()+args]; 59 commandLine[0] = "-classpath"; 60 commandLine[1] = classPath; 61 commandLine[2] = "-d"; 62 commandLine[3] = destPath; 63 64 String[] files = new String[sourceFiles.size()]; 65 sourceFiles.toArray(files); 66 67 System.arraycopy(files, 0, commandLine, args, files.length); 68 69 return Main.compile(commandLine, new PrintWriter(System.err)) == 0; 70 } 71 return false; 72 } 73 74 @Override equals(Object obj)75 public boolean equals(Object obj) { 76 if (super.equals(obj)) 77 { 78 JavacBuildStep other = (JavacBuildStep) obj; 79 return destPath.equals(other.destPath) 80 && classPath.equals(other.classPath) 81 && sourceFiles.equals(other.sourceFiles); 82 } 83 return false; 84 } 85 86 @Override hashCode()87 public int hashCode() { 88 return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode(); 89 } 90 } 91