]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/compiler/CommandLineLinker.java
Initial import.
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / compiler / CommandLineLinker.java
CommitLineData
878ddf1f 1/*\r
2 * \r
3 * Copyright 2002-2004 The Ant-Contrib project\r
4 *\r
5 * Licensed under the Apache License, Version 2.0 (the "License");\r
6 * you may not use this file except in compliance with the License.\r
7 * You may obtain a copy of the License at\r
8 *\r
9 * http://www.apache.org/licenses/LICENSE-2.0\r
10 *\r
11 * Unless required by applicable law or agreed to in writing, software\r
12 * distributed under the License is distributed on an "AS IS" BASIS,\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 * See the License for the specific language governing permissions and\r
15 * limitations under the License.\r
16 */\r
17package net.sf.antcontrib.cpptasks.compiler;\r
18\r
19import java.io.File;\r
20import java.io.FileWriter;\r
21import java.io.IOException;\r
22import java.util.Enumeration;\r
23import java.util.Vector;\r
24\r
25import net.sf.antcontrib.cpptasks.CCTask;\r
26import net.sf.antcontrib.cpptasks.CUtil;\r
27import net.sf.antcontrib.cpptasks.LinkerDef;\r
28import net.sf.antcontrib.cpptasks.ProcessorDef;\r
29import net.sf.antcontrib.cpptasks.ProcessorParam;\r
30import net.sf.antcontrib.cpptasks.types.CommandLineArgument;\r
31import net.sf.antcontrib.cpptasks.types.LibrarySet;\r
32import net.sf.antcontrib.cpptasks.TargetDef;\r
33\r
34import org.apache.tools.ant.BuildException;\r
35import org.apache.tools.ant.types.Environment;\r
36\r
37\r
38/**\r
39 * An abstract Linker implementation that performs the link via an external\r
40 * command.\r
41 *\r
42 * @author Adam Murdoch\r
43 */\r
44public abstract class CommandLineLinker extends AbstractLinker\r
45{\r
46 private String command;\r
47 private Environment env = null;\r
48 private String identifier;\r
49 private String identifierArg;\r
50 private boolean isLibtool;\r
51 private String[] librarySets;\r
52 private CommandLineLinker libtoolLinker;\r
53 private boolean newEnvironment = false;\r
54 private String outputSuffix;\r
55\r
56\r
57 /** Creates a comand line linker invocation */\r
58 public CommandLineLinker(String command,\r
59 String identifierArg,\r
60 String[] extensions,\r
61 String[] ignoredExtensions, String outputSuffix,\r
62 boolean isLibtool, CommandLineLinker libtoolLinker)\r
63 {\r
64 super(extensions, ignoredExtensions);\r
65 this.command = command;\r
66 this.identifierArg = identifierArg;\r
67 this.outputSuffix = outputSuffix;\r
68 this.isLibtool = isLibtool;\r
69 this.libtoolLinker = libtoolLinker;\r
70 }\r
71 protected abstract void addBase(long base, Vector args);\r
72\r
73 protected abstract void addFixed(Boolean fixed, Vector args);\r
74\r
75 abstract protected void addImpliedArgs(boolean debug,\r
76 LinkType linkType, Vector args, Boolean defaultflag);\r
77 protected abstract void addIncremental(boolean incremental, Vector args);\r
78\r
79 //\r
80 // Windows processors handle these through file list\r
81 //\r
82 protected String[] addLibrarySets(CCTask task, LibrarySet[] libsets, Vector preargs,\r
83 Vector midargs, Vector endargs) {\r
84 return null;\r
85 }\r
86 protected abstract void addMap(boolean map, Vector args);\r
87 protected abstract void addStack(int stack, Vector args);\r
88 protected abstract void addEntry(String entry, Vector args);\r
89 \r
90 protected LinkerConfiguration createConfiguration(\r
91 CCTask task,\r
92 LinkType linkType,\r
93 ProcessorDef[] baseDefs, LinkerDef specificDef, TargetDef targetPlatform) {\r
94\r
95 Vector preargs = new Vector();\r
96 Vector midargs = new Vector();\r
97 Vector endargs = new Vector();\r
98 Vector[] args = new Vector[] { preargs, midargs, endargs };\r
99\r
100 LinkerDef[] defaultProviders = new LinkerDef[baseDefs.length+1];\r
101 defaultProviders[0] = specificDef;\r
102 for(int i = 0; i < baseDefs.length; i++) {\r
103 defaultProviders[i+1] = (LinkerDef) baseDefs[i];\r
104 }\r
105 //\r
106 // add command line arguments inherited from <cc> element\r
107 // any "extends" and finally the specific CompilerDef\r
108 CommandLineArgument[] commandArgs;\r
109 for(int i = defaultProviders.length-1; i >= 0; i--) {\r
110 commandArgs = defaultProviders[i].getActiveProcessorArgs();\r
111 for(int j = 0; j < commandArgs.length; j++) {\r
112 args[commandArgs[j].getLocation()].\r
113 addElement(commandArgs[j].getValue());\r
114 }\r
115 }\r
116\r
117 Vector params = new Vector();\r
118 //\r
119 // add command line arguments inherited from <cc> element\r
120 // any "extends" and finally the specific CompilerDef\r
121 ProcessorParam[] paramArray;\r
122 for (int i = defaultProviders.length - 1; i >= 0; i--) {\r
123 paramArray = defaultProviders[i].getActiveProcessorParams();\r
124 for (int j = 0; j < paramArray.length; j++) {\r
125 params.add(paramArray[j]);\r
126 }\r
127 }\r
128\r
129 paramArray = (ProcessorParam[])(params.toArray(new ProcessorParam[params.size()]));\r
130\r
131 boolean debug = specificDef.getDebug(baseDefs,0);\r
132\r
133 \r
134 String startupObject = getStartupObject(linkType);\r
135 Boolean defaultflag = specificDef.getDefaultflag(defaultProviders, 1);\r
136 addImpliedArgs(debug, linkType, preargs, defaultflag);\r
137 addIncremental(specificDef.getIncremental(defaultProviders,1), preargs);\r
138 addFixed(specificDef.getFixed(defaultProviders,1), preargs);\r
139 addMap(specificDef.getMap(defaultProviders,1), preargs);\r
140 addBase(specificDef.getBase(defaultProviders,1), preargs);\r
141 addStack(specificDef.getStack(defaultProviders,1), preargs);\r
142 addEntry(specificDef.getEntry(defaultProviders, 1), preargs);\r
143\r
144 String[] libnames = null;\r
145 LibrarySet[] libsets = specificDef.getActiveLibrarySets(defaultProviders,1);\r
146 if (libsets.length > 0) {\r
147 libnames = addLibrarySets(task, libsets, preargs, midargs, endargs);\r
148 }\r
149\r
150 StringBuffer buf = new StringBuffer(getIdentifier());\r
151 for (int i = 0; i < 3; i++) {\r
152 Enumeration argenum = args[i].elements();\r
153 while (argenum.hasMoreElements()) {\r
154 buf.append(' ');\r
155 buf.append(argenum.nextElement().toString());\r
156 }\r
157 }\r
158 String configId = buf.toString();\r
159\r
160 String[][] options = new String[][] {\r
161 new String[args[0].size() + args[1].size()],\r
162 new String[args[2].size()] };\r
163 args[0].copyInto(options[0]);\r
164 int offset = args[0].size();\r
165 for (int i = 0; i < args[1].size(); i++) {\r
166 options[0][i+offset] = (String) args[1].elementAt(i);\r
167 }\r
168 args[2].copyInto(options[1]);\r
169\r
170\r
171 boolean rebuild = specificDef.getRebuild(baseDefs,0);\r
172 boolean map = specificDef.getMap(defaultProviders,1);\r
173\r
174 //task.log("libnames:"+libnames.length, Project.MSG_VERBOSE);\r
175 return new CommandLineLinkerConfiguration(this,configId,options,\r
176 paramArray,\r
177 rebuild,map,libnames, startupObject);\r
178 }\r
179\r
180 /**\r
181 * Allows drived linker to decorate linker option.\r
182 * Override by GccLinker to prepend a "-Wl," to\r
183 * pass option to through gcc to linker.\r
184 *\r
185 * @param buf buffer that may be used and abused in the decoration process,\r
186 * must not be null.\r
187 * @param arg linker argument\r
188 */\r
189 protected String decorateLinkerOption(StringBuffer buf, String arg) {\r
190 return arg;\r
191 }\r
192\r
193 protected final String getCommand() {\r
194 return command;\r
195 }\r
196 protected abstract String getCommandFileSwitch(String commandFile);\r
197\r
198\r
199 public String getIdentifier() {\r
200 if(identifier == null) {\r
201 if (identifierArg == null) {\r
202 identifier = getIdentifier(new String[] { command }, command);\r
203 } else {\r
204 identifier = getIdentifier(new String[] { command, identifierArg },\r
205 command);\r
206 }\r
207 }\r
208 return identifier;\r
209 }\r
210 public final CommandLineLinker getLibtoolLinker() {\r
211 if (libtoolLinker != null) {\r
212 return libtoolLinker;\r
213 }\r
214 return this;\r
215 }\r
216 protected abstract int getMaximumCommandLength();\r
217\r
218 public String getOutputFileName(String baseName) {\r
219 return baseName + outputSuffix;\r
220 }\r
221\r
222 protected String[] getOutputFileSwitch(CCTask task, String outputFile) {\r
223 return getOutputFileSwitch(outputFile);\r
224 }\r
225 protected abstract String[] getOutputFileSwitch(String outputFile);\r
226 protected String getStartupObject(LinkType linkType) {\r
227 return null;\r
228 }\r
229\r
230 /**\r
231 * Performs a link using a command line linker\r
232 *\r
233 */\r
234 public void link(CCTask task,\r
235 File outputFile,\r
236 String[] sourceFiles,\r
237 CommandLineLinkerConfiguration config)\r
238 throws BuildException\r
239 {\r
240 File parentDir = new File(outputFile.getParent());\r
241 String parentPath;\r
242 try {\r
243 parentPath = parentDir.getCanonicalPath();\r
244 } catch(IOException ex) {\r
245 parentPath = parentDir.getAbsolutePath();\r
246 }\r
247 String[] execArgs = prepareArguments(task, parentPath,outputFile.getName(),\r
248 sourceFiles, config);\r
249 int commandLength = 0;\r
250 for(int i = 0; i < execArgs.length; i++) {\r
251 commandLength += execArgs[i].length() + 1;\r
252 }\r
253\r
254 //\r
255 // if command length exceeds maximum\r
256 // (1024 for Windows) then create a temporary\r
257 // file containing everything but the command name\r
258 if(commandLength >= this.getMaximumCommandLength()) {\r
259 try {\r
260 execArgs = prepareResponseFile(outputFile,execArgs);\r
261 }\r
262 catch(IOException ex) {\r
263 throw new BuildException(ex);\r
264 }\r
265 }\r
266 \r
267 int retval = runCommand(task,parentDir,execArgs); \r
268 //\r
269 // if the process returned a failure code then\r
270 // throw an BuildException\r
271 //\r
272 if(retval != 0) {\r
273 //\r
274 // construct the exception\r
275 //\r
276 throw new BuildException(this.getCommand() + " failed with return code " + retval, task.getLocation());\r
277 }\r
278 \r
279 }\r
280\r
281\r
282 /**\r
283 * Prepares argument list for exec command. Will return null\r
284 * if command line would exceed allowable command line buffer.\r
285 *\r
286 * @param outputFile linker output file\r
287 * @param sourceFiles linker input files (.obj, .o, .res)\r
288 * @param args linker arguments\r
289 * @return arguments for runTask\r
290 */\r
291 protected String[] prepareArguments(\r
292 CCTask task,\r
293 String outputDir,\r
294 String outputFile,\r
295 String[] sourceFiles,\r
296 CommandLineLinkerConfiguration config) {\r
297 \r
298 String[] preargs = config.getPreArguments();\r
299 String[] endargs = config.getEndArguments();\r
300 String outputSwitch[] = getOutputFileSwitch(task, outputFile);\r
301 int allArgsCount = preargs.length + 1 + outputSwitch.length +\r
302 sourceFiles.length + endargs.length;\r
303 if (isLibtool) {\r
304 allArgsCount++;\r
305 }\r
306 String[] allArgs = new String[allArgsCount];\r
307 int index = 0;\r
308 if (isLibtool) {\r
309 allArgs[index++] = "libtool";\r
310 }\r
311 allArgs[index++] = this.getCommand();\r
312 StringBuffer buf = new StringBuffer();\r
313 for (int i = 0; i < preargs.length; i++) {\r
314 allArgs[index++] = decorateLinkerOption(buf, preargs[i]);\r
315 }\r
316 for (int i = 0; i < outputSwitch.length; i++) {\r
317 allArgs[index++] = outputSwitch[i];\r
318 }\r
319 for (int i = 0; i < sourceFiles.length; i++) {\r
320 allArgs[index++] = prepareFilename(buf,outputDir,sourceFiles[i]);\r
321 }\r
322 for (int i = 0; i < endargs.length; i++) {\r
323 allArgs[index++] = decorateLinkerOption(buf, endargs[i]);\r
324 }\r
325 return allArgs;\r
326 }\r
327\r
328 /**\r
329 * Processes filename into argument form\r
330 *\r
331 */\r
332 protected String prepareFilename(StringBuffer buf,\r
333 String outputDir, String sourceFile) {\r
334 String relativePath = CUtil.getRelativePath(outputDir,\r
335 new File(sourceFile));\r
336 return quoteFilename(buf,relativePath);\r
337 }\r
338\r
339 /**\r
340 * Prepares argument list to execute the linker using a\r
341 * response file.\r
342 *\r
343 * @param outputFile linker output file\r
344 * @param args output of prepareArguments\r
345 * @return arguments for runTask\r
346 */\r
347 protected String[] prepareResponseFile(File outputFile,String[] args) throws IOException\r
348 {\r
349 String baseName = outputFile.getName();\r
350 File commandFile = new File(outputFile.getParent(),baseName + ".rsp");\r
351 FileWriter writer = new FileWriter(commandFile);\r
352 int execArgCount = 1;\r
353 if (isLibtool) {\r
354 execArgCount++;\r
355 }\r
356 String[] execArgs = new String[execArgCount+1];\r
357 for (int i = 0; i < execArgCount; i++) {\r
358 execArgs[i] = args[i];\r
359 }\r
360 execArgs[execArgCount] = getCommandFileSwitch(commandFile.toString());\r
361 for(int i = execArgCount; i < args.length; i++) {\r
362 //\r
363 // if embedded space and not quoted then\r
364 // quote argument\r
365 if (args[i].indexOf(" ") >= 0 && args[i].charAt(0) != '\"') {\r
366 writer.write('\"');\r
367 writer.write(args[i]);\r
368 writer.write("\"\n");\r
369 } else {\r
370 writer.write(args[i]);\r
371 writer.write('\n');\r
372 }\r
373 }\r
374 writer.close();\r
375 return execArgs;\r
376 }\r
377\r
378\r
379 protected String quoteFilename(StringBuffer buf,String filename) {\r
380 if(filename.indexOf(' ') >= 0) {\r
381 buf.setLength(0);\r
382 buf.append('\"');\r
383 buf.append(filename);\r
384 buf.append('\"');\r
385 return buf.toString();\r
386 }\r
387 return filename;\r
388 }\r
389\r
390 /**\r
391 * This method is exposed so test classes can overload\r
392 * and test the arguments without actually spawning the\r
393 * compiler\r
394 */\r
395 protected int runCommand(CCTask task, File workingDir,String[] cmdline)\r
396 throws BuildException {\r
397 return CUtil.runCommand(task,workingDir,cmdline, newEnvironment, env);\r
398 }\r
399\r
400 protected final void setCommand(String command) {\r
401 this.command = command;\r
402 }\r
403\r
404}\r