1 /*
2  * Copyright (C) 2013 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.android.jack.Jack;
20 import com.android.jack.Main;
21 import com.android.jack.Options;
22 
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 
29 public class JackBuildStep extends SourceBuildStep {
30 
31     private final String destPath;
32     private final String classPath;
33     private final Set<String> sourceFiles = new HashSet<String>();
34 
JackBuildStep(String destPath, String classPath)35     public JackBuildStep(String destPath, String classPath) {
36         this.destPath = destPath;
37         this.classPath = classPath;
38     }
39 
40     @Override
addSourceFile(String sourceFile)41     public void addSourceFile(String sourceFile) {
42         sourceFiles.add(sourceFile);
43     }
44 
45     @Override
build()46     boolean build() {
47         if (super.build()) {
48             if (sourceFiles.isEmpty()) {
49                 return true;
50             }
51 
52             File outDir = new File(destPath).getParentFile();
53             if (!outDir.exists() && !outDir.mkdirs()) {
54                 System.err.println("failed to create output dir: "
55                         + outDir.getAbsolutePath());
56                 return false;
57             }
58             List<String> commandLine = new ArrayList(4 + sourceFiles.size());
59             commandLine.add("--verbose");
60             commandLine.add("error");
61             commandLine.add("--classpath");
62             commandLine.add(classPath);
63             commandLine.add("--output-jack");
64             commandLine.add(destPath);
65             commandLine.addAll(sourceFiles);
66 
67             try {
68                 Options options = Main.parseCommandLine(commandLine);
69                 Jack.checkAndRun(options);
70             } catch (Throwable ex) {
71                 ex.printStackTrace();
72                 return false;
73             }
74             return true;
75         }
76         return false;
77     }
78 
79     @Override
equals(Object obj)80     public boolean equals(Object obj) {
81         if (super.equals(obj)) {
82             JackBuildStep other = (JackBuildStep) obj;
83             return destPath.equals(other.destPath) && classPath.equals(other.classPath)
84                     && sourceFiles.equals(other.sourceFiles);
85         }
86         return false;
87     }
88 
89     @Override
hashCode()90     public int hashCode() {
91         return destPath.hashCode() ^ classPath.hashCode() ^ sourceFiles.hashCode();
92     }
93 }
94