blob: a1cde1f683ae5a9c08b62463884b777b98082305 [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 Roussel10461422014-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) {
Yohann Roussele5b72e22015-10-01 17:35:37 +020032 super(new File(destPath));
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070033 this.destPath = destPath;
34 this.classPath = classPath;
35 }
Yohann Roussel10461422014-09-25 10:13:32 +020036
37 @Override
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070038 public void addSourceFile(String sourceFile)
39 {
40 sourceFiles.add(sourceFile);
41 }
Yohann Roussel10461422014-09-25 10:13:32 +020042
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070043 @Override
44 boolean build() {
45 if (super.build())
46 {
47 if (sourceFiles.isEmpty())
48 {
49 return true;
50 }
Yohann Roussel10461422014-09-25 10:13:32 +020051
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070052 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 Fullerece00bc2015-08-12 14:31:43 +010058 int args = 8;
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070059 String[] commandLine = new String[sourceFiles.size()+args];
60 commandLine[0] = "-classpath";
61 commandLine[1] = classPath;
62 commandLine[2] = "-d";
63 commandLine[3] = destPath;
Neil Fullerece00bc2015-08-12 14:31:43 +010064 commandLine[4] = "-source";
65 commandLine[5] = "1.7";
66 commandLine[6] = "-target";
67 commandLine[7] = "1.7";
Yohann Roussel10461422014-09-25 10:13:32 +020068
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070069 String[] files = new String[sourceFiles.size()];
70 sourceFiles.toArray(files);
Yohann Roussel10461422014-09-25 10:13:32 +020071
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070072 System.arraycopy(files, 0, commandLine, args, files.length);
Yohann Roussel10461422014-09-25 10:13:32 +020073
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070074 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 Chuang9a223d72011-04-27 17:19:46 -070081 if (super.equals(obj))
82 {
83 JavacBuildStep other = (JavacBuildStep) obj;
Yohann Roussel10461422014-09-25 10:13:32 +020084 return destPath.equals(other.destPath)
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070085 && classPath.equals(other.classPath)
86 && sourceFiles.equals(other.sourceFiles);
87 }
88 return false;
89 }
Yohann Roussel10461422014-09-25 10:13:32 +020090
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070091 @Override
92 public int hashCode() {
93 return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode();
94 }
95}