587
|
1 |
/*
|
628
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
587
|
17 |
package com.nokia.helium.imaker.ant.engines;
|
|
18 |
|
|
19 |
import java.io.File;
|
628
|
20 |
import java.io.FileNotFoundException;
|
587
|
21 |
import java.io.FileOutputStream;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.io.OutputStreamWriter;
|
|
24 |
import java.io.StringWriter;
|
|
25 |
import java.net.MalformedURLException;
|
|
26 |
import java.net.URL;
|
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.Hashtable;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.Map;
|
|
31 |
|
|
32 |
import org.apache.log4j.Logger;
|
|
33 |
import org.apache.tools.ant.Project;
|
|
34 |
import org.apache.tools.ant.types.DataType;
|
|
35 |
|
|
36 |
import com.nokia.helium.core.plexus.AntStreamConsumer;
|
|
37 |
import com.nokia.helium.core.plexus.FileStreamConsumer;
|
|
38 |
import com.nokia.helium.imaker.IMakerException;
|
|
39 |
import com.nokia.helium.imaker.ant.Command;
|
|
40 |
import com.nokia.helium.imaker.ant.Engine;
|
|
41 |
import com.nokia.helium.imaker.ant.taskdefs.IMakerTask;
|
|
42 |
|
|
43 |
import freemarker.cache.ClassTemplateLoader;
|
|
44 |
import freemarker.cache.FileTemplateLoader;
|
|
45 |
import freemarker.template.Configuration;
|
|
46 |
import freemarker.template.Template;
|
|
47 |
import freemarker.template.TemplateException;
|
|
48 |
|
|
49 |
/**
|
628
|
50 |
*
|
587
|
51 |
* Simplest possible definition of the type, e.g:
|
628
|
52 |
*
|
587
|
53 |
* <pre>
|
|
54 |
* <emakeEngine id="imaker.ec" />
|
|
55 |
* </pre>
|
|
56 |
*
|
|
57 |
* Emake engine with some custom configuration.
|
628
|
58 |
*
|
|
59 |
* <pre>
|
587
|
60 |
* <emakeEngine id="imaker.ec" >
|
|
61 |
* <arg value="--emake-annofile=imaker.anno.xml" />
|
|
62 |
* </emakeEngine>
|
|
63 |
* </pre>
|
|
64 |
*
|
|
65 |
* @ant.type name=emakeEngine category="imaker"
|
|
66 |
*/
|
|
67 |
public class EmakeEngine extends DataType implements Engine {
|
|
68 |
private Logger log = Logger.getLogger(getClass());
|
|
69 |
private IMakerTask task;
|
628
|
70 |
private List<Arg> customArgs = new ArrayList<Arg>();
|
587
|
71 |
private File template;
|
628
|
72 |
|
587
|
73 |
/**
|
628
|
74 |
* Holder for emake custom args.
|
587
|
75 |
*/
|
|
76 |
public class Arg {
|
|
77 |
private String value;
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Get the value of the argument.
|
628
|
81 |
*
|
587
|
82 |
* @return the argument
|
|
83 |
*/
|
|
84 |
public String getValue() {
|
|
85 |
return value;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
628
|
89 |
* Define the additional command line parameter you want to add to emake invocation.
|
|
90 |
*
|
587
|
91 |
* @param value the additional command line parameter
|
|
92 |
* @ant.required
|
|
93 |
*/
|
|
94 |
public void setValue(String value) {
|
|
95 |
this.value = value;
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* {@inheritDoc}
|
|
101 |
*/
|
|
102 |
public void build(List<List<Command>> cmdSet) throws IMakerException {
|
|
103 |
File makefile = null;
|
|
104 |
try {
|
|
105 |
// Writing the makefile.
|
|
106 |
makefile = writeMakefile(cmdSet);
|
628
|
107 |
|
587
|
108 |
// Running Emake
|
|
109 |
runEmake(makefile);
|
628
|
110 |
}
|
|
111 |
finally {
|
587
|
112 |
if (makefile != null) {
|
|
113 |
makefile.delete();
|
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Returns the jar file name containing this class
|
628
|
120 |
*
|
587
|
121 |
* @return a File object or null if not found.
|
|
122 |
* @throws IMakerException
|
|
123 |
*/
|
|
124 |
protected File getJarFile() throws IMakerException {
|
628
|
125 |
URL url = this.getClass().getClassLoader().getResource(this.getClass().getName().replace('.', '/')
|
|
126 |
+ ".class");
|
587
|
127 |
if (url.getProtocol().equals("jar") && url.getPath().contains("!/")) {
|
|
128 |
String fileUrl = url.getPath().split("!/")[0];
|
|
129 |
try {
|
|
130 |
return new File(new URL(fileUrl).getPath());
|
628
|
131 |
}
|
|
132 |
catch (MalformedURLException e) {
|
587
|
133 |
throw new IMakerException("Error determining the jar file where "
|
628
|
134 |
+ this.getClass().getName() + " is located.", e);
|
587
|
135 |
}
|
|
136 |
}
|
|
137 |
return null;
|
|
138 |
}
|
628
|
139 |
|
587
|
140 |
/**
|
|
141 |
* Run emake using defined makefile.
|
628
|
142 |
*
|
587
|
143 |
* @param makefile the makefile to build
|
|
144 |
* @throws IMakerException
|
|
145 |
*/
|
|
146 |
private void runEmake(File makefile) throws IMakerException {
|
|
147 |
FileStreamConsumer output = null;
|
|
148 |
if (task.getOutput() != null) {
|
|
149 |
try {
|
|
150 |
output = new FileStreamConsumer(task.getOutput());
|
628
|
151 |
}
|
|
152 |
catch (FileNotFoundException e) {
|
587
|
153 |
throw new IMakerException("Error creating the stream recorder: " + e.getMessage(), e);
|
|
154 |
}
|
|
155 |
}
|
|
156 |
try {
|
|
157 |
Emake emake = new Emake();
|
|
158 |
emake.setWorkingDir(task.getEpocroot());
|
|
159 |
List<String> args = new ArrayList<String>();
|
|
160 |
for (Arg arg : customArgs) {
|
|
161 |
if (arg.getValue() != null) {
|
|
162 |
args.add(arg.getValue());
|
|
163 |
}
|
|
164 |
}
|
|
165 |
args.add("-f");
|
|
166 |
args.add(makefile.getAbsolutePath());
|
|
167 |
args.add("all");
|
|
168 |
if (task.isVerbose()) {
|
|
169 |
emake.addOutputLineHandler(new AntStreamConsumer(task));
|
|
170 |
}
|
|
171 |
emake.addErrorLineHandler(new AntStreamConsumer(task, Project.MSG_ERR));
|
|
172 |
if (output != null) {
|
|
173 |
emake.addOutputLineHandler(output);
|
|
174 |
emake.addErrorLineHandler(output);
|
|
175 |
}
|
|
176 |
emake.execute(args.toArray(new String[args.size()]));
|
628
|
177 |
}
|
|
178 |
catch (IMakerException e) {
|
587
|
179 |
throw new IMakerException("Error executing emake: " + e.getMessage(), e);
|
628
|
180 |
}
|
|
181 |
finally {
|
587
|
182 |
if (output != null) {
|
|
183 |
output.close();
|
|
184 |
}
|
|
185 |
}
|
|
186 |
}
|
628
|
187 |
|
587
|
188 |
/**
|
628
|
189 |
* Create the Makefile based on the cmdSet build sequence.
|
|
190 |
*
|
587
|
191 |
* @param cmdSet
|
|
192 |
* @return
|
628
|
193 |
* @throws IMakerException
|
587
|
194 |
* @throws IOException
|
|
195 |
*/
|
|
196 |
private File writeMakefile(List<List<Command>> cmdSet) throws IMakerException {
|
|
197 |
try {
|
|
198 |
Configuration cfg = new Configuration();
|
|
199 |
Template template = null;
|
|
200 |
if (this.template != null) {
|
|
201 |
if (!this.template.exists()) {
|
628
|
202 |
throw new IMakerException("Could not find template file: "
|
|
203 |
+ this.template.getAbsolutePath());
|
587
|
204 |
}
|
|
205 |
task.log("Loading template: " + this.template.getAbsolutePath());
|
|
206 |
cfg.setTemplateLoader(new FileTemplateLoader(this.template.getParentFile()));
|
|
207 |
template = cfg.getTemplate(this.template.getName());
|
628
|
208 |
}
|
|
209 |
else {
|
587
|
210 |
cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), ""));
|
|
211 |
template = cfg.getTemplate("build_imaker_roms_signing.mk.ftl");
|
|
212 |
}
|
|
213 |
File makefile = File.createTempFile("helium-imaker", ".mk", task.getEpocroot());
|
|
214 |
makefile.deleteOnExit();
|
|
215 |
StringWriter out = new StringWriter();
|
|
216 |
Map<String, Object> data = new Hashtable<String, Object>();
|
|
217 |
data.put("cmdSets", cmdSet);
|
|
218 |
data.put("makefile", makefile.getAbsoluteFile());
|
|
219 |
data.put("java_home", System.getProperty("java.home"));
|
|
220 |
File jar = getJarFile();
|
|
221 |
if (jar != null) {
|
628
|
222 |
task.log("Using "
|
|
223 |
+ jar
|
|
224 |
+ " as the utility container, make sure the file is available under an emake root.");
|
587
|
225 |
data.put("java_utils_classpath", jar.getAbsolutePath());
|
|
226 |
}
|
|
227 |
template.process(data, out);
|
|
228 |
log.debug(out.getBuffer().toString());
|
628
|
229 |
|
587
|
230 |
OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(makefile));
|
|
231 |
output.append(out.getBuffer().toString());
|
|
232 |
output.close();
|
|
233 |
return makefile;
|
628
|
234 |
}
|
|
235 |
catch (IOException e) {
|
|
236 |
throw new IMakerException("Error generating the makefile: " + e.getMessage(), e);
|
|
237 |
}
|
|
238 |
catch (TemplateException e) {
|
|
239 |
throw new IMakerException("Error while rendering the makefile template: "
|
|
240 |
+ e.getMessage(), e);
|
587
|
241 |
}
|
|
242 |
}
|
628
|
243 |
|
587
|
244 |
/**
|
|
245 |
* Add custom parameters for the emake invocation.
|
628
|
246 |
*
|
587
|
247 |
* @return a new Arg object.
|
|
248 |
*/
|
|
249 |
public Arg createArg() {
|
|
250 |
Arg arg = new Arg();
|
|
251 |
customArgs.add(arg);
|
|
252 |
return arg;
|
|
253 |
}
|
628
|
254 |
|
587
|
255 |
/**
|
|
256 |
* {@inheritDoc}
|
|
257 |
*/
|
|
258 |
@Override
|
|
259 |
public void setTask(IMakerTask task) {
|
|
260 |
this.task = task;
|
|
261 |
}
|
628
|
262 |
|
587
|
263 |
/**
|
|
264 |
* Defines an alternate template to use to generate the build sequence for emake.
|
628
|
265 |
*
|
587
|
266 |
* @ant.not-required
|
|
267 |
*/
|
|
268 |
public void setTemplate(File template) {
|
|
269 |
this.template = template;
|
|
270 |
}
|
|
271 |
}
|