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