blob: d08a2c6160cfd0dda1808ca8ccc05c75c8323b3b [file] [log] [blame]
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -07001/*
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
17package util.build;
18
19import com.sun.tools.javac.Main;
20
21import java.io.File;
22import java.io.PrintWriter;
23import java.util.HashSet;
24import java.util.Set;
25
Yohann Roussel3570d4d2014-09-25 10:13:32 +020026public class JavacBuildStep extends SourceBuildStep {
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070027
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 }
Yohann Roussel3570d4d2014-09-25 10:13:32 +020035
36 @Override
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070037 public void addSourceFile(String sourceFile)
38 {
39 sourceFiles.add(sourceFile);
40 }
Yohann Roussel3570d4d2014-09-25 10:13:32 +020041
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070042 @Override
43 boolean build() {
44 if (super.build())
45 {
46 if (sourceFiles.isEmpty())
47 {
48 return true;
49 }
Yohann Roussel3570d4d2014-09-25 10:13:32 +020050
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070051 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;
Yohann Roussel3570d4d2014-09-25 10:13:32 +020063
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070064 String[] files = new String[sourceFiles.size()];
65 sourceFiles.toArray(files);
Yohann Roussel3570d4d2014-09-25 10:13:32 +020066
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070067 System.arraycopy(files, 0, commandLine, args, files.length);
Yohann Roussel3570d4d2014-09-25 10:13:32 +020068
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070069 return Main.compile(commandLine, new PrintWriter(System.err)) == 0;
70 }
71 return false;
72 }
73
74 @Override
75 public boolean equals(Object obj) {
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070076 if (super.equals(obj))
77 {
78 JavacBuildStep other = (JavacBuildStep) obj;
Yohann Roussel3570d4d2014-09-25 10:13:32 +020079 return destPath.equals(other.destPath)
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070080 && classPath.equals(other.classPath)
81 && sourceFiles.equals(other.sourceFiles);
82 }
83 return false;
84 }
Yohann Roussel3570d4d2014-09-25 10:13:32 +020085
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070086 @Override
87 public int hashCode() {
88 return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode();
89 }
90}