JavacBuildStep.java revision 3570d4dcf7edea7e8db9a389df596df2925e971e
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
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
26public class JavacBuildStep extends SourceBuildStep {
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    @Override
37    public void addSourceFile(String sourceFile)
38    {
39        sourceFiles.add(sourceFile);
40    }
41
42    @Override
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
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
87    public int hashCode() {
88        return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode();
89    }
90}
91