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 java.io.File;
20 import java.io.IOException;
21 
22 public class JillBuildStep extends BuildStep {
23 
JillBuildStep(BuildFile inputFile, BuildFile outputFile)24     JillBuildStep(BuildFile inputFile, BuildFile outputFile) {
25         super(inputFile, outputFile);
26     }
27 
28     @Override
build()29     boolean build() {
30         if (super.build()) {
31             File tmpInputJar = new File(inputFile.fileName.getPath() + ".jar");
32             try {
33 
34                 File outDir = outputFile.fileName.getParentFile();
35                 if (!outDir.exists() && !outDir.mkdirs()) {
36                     System.err.println("failed to create output dir: "
37                             + outDir.getAbsolutePath());
38                     return false;
39                 }
40 
41                 // input file is a class file but jack supports only jar
42                 JarBuildStep jarStep = new JarBuildStep(
43                     inputFile,
44                     inputFile.fileName.getName(),
45                     new BuildFile(tmpInputJar),
46                     /* deleteInputFileAfterBuild = */ false);
47                 if (!jarStep.build()) {
48                   throw new IOException("Failed to make jar: " + outputFile.getPath());
49                 }
50 
51 
52                 String[] commandLine = new String[] {
53                     "--verbose",
54                     "error",
55                     "--import",
56                     tmpInputJar.getAbsolutePath(),
57                     "--output-jack",
58                     outputFile.fileName.getAbsolutePath(),
59                   };
60 
61                 ExecuteFile exec = new ExecuteFile(JackBuildDalvikSuite.JACK, commandLine);
62                 exec.setErr(System.err);
63                 exec.setOut(System.out);
64                 if (!exec.run()) {
65                     return false;
66                 }
67 
68                 return true;
69             } catch (Throwable ex) {
70                 System.err.println("exception while transforming jack file from jar "
71                         + inputFile.fileName.getAbsolutePath() + " to "
72                         + outputFile.fileName.getAbsolutePath());
73                 ex.printStackTrace();
74             } finally {
75                 tmpInputJar.delete();
76             }
77         }
78         return false;
79     }
80 
81     @Override
equals(Object obj)82     public boolean equals(Object obj) {
83         if (super.equals(obj)) {
84             JillBuildStep other = (JillBuildStep) obj;
85 
86             return inputFile.equals(other.inputFile) && outputFile.equals(other.outputFile);
87         }
88         return false;
89     }
90 
91     @Override
hashCode()92     public int hashCode() {
93         return inputFile.hashCode() ^ outputFile.hashCode();
94     }
95 }
96