+++ /dev/null
-/*\r
- * \r
- * Copyright 2002-2004 The Ant-Contrib project\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package net.sf.antcontrib.cpptasks.compiler;\r
-\r
-import java.io.File;\r
-import java.io.FileWriter;\r
-import java.io.IOException;\r
-import java.util.Enumeration;\r
-import java.util.Vector;\r
-\r
-import net.sf.antcontrib.cpptasks.CCTask;\r
-import net.sf.antcontrib.cpptasks.CUtil;\r
-import net.sf.antcontrib.cpptasks.LinkerDef;\r
-import net.sf.antcontrib.cpptasks.ProcessorDef;\r
-import net.sf.antcontrib.cpptasks.ProcessorParam;\r
-import net.sf.antcontrib.cpptasks.types.CommandLineArgument;\r
-import net.sf.antcontrib.cpptasks.types.LibrarySet;\r
-import net.sf.antcontrib.cpptasks.TargetDef;\r
-\r
-import org.apache.tools.ant.BuildException;\r
-import org.apache.tools.ant.types.Environment;\r
-\r
-\r
-/**\r
- * An abstract Linker implementation that performs the link via an external\r
- * command.\r
- *\r
- * @author Adam Murdoch\r
- */\r
-public abstract class CommandLineLinker extends AbstractLinker\r
-{\r
- private String command;\r
- private Environment env = null;\r
- private String identifier;\r
- private String identifierArg;\r
- private boolean isLibtool;\r
- private String[] librarySets;\r
- private CommandLineLinker libtoolLinker;\r
- private boolean newEnvironment = false;\r
- private String outputSuffix;\r
-\r
-\r
- /** Creates a comand line linker invocation */\r
- public CommandLineLinker(String command,\r
- String identifierArg,\r
- String[] extensions,\r
- String[] ignoredExtensions, String outputSuffix,\r
- boolean isLibtool, CommandLineLinker libtoolLinker)\r
- {\r
- super(extensions, ignoredExtensions);\r
- this.command = command;\r
- this.identifierArg = identifierArg;\r
- this.outputSuffix = outputSuffix;\r
- this.isLibtool = isLibtool;\r
- this.libtoolLinker = libtoolLinker;\r
- }\r
- protected abstract void addBase(long base, Vector args);\r
-\r
- protected abstract void addFixed(Boolean fixed, Vector args);\r
-\r
- abstract protected void addImpliedArgs(boolean debug,\r
- LinkType linkType, Vector args, Boolean defaultflag);\r
- protected abstract void addIncremental(boolean incremental, Vector args);\r
-\r
- //\r
- // Windows processors handle these through file list\r
- //\r
- protected String[] addLibrarySets(CCTask task, LibrarySet[] libsets, Vector preargs,\r
- Vector midargs, Vector endargs) {\r
- return null;\r
- }\r
- protected abstract void addMap(boolean map, Vector args);\r
- protected abstract void addStack(int stack, Vector args);\r
- protected abstract void addEntry(String entry, Vector args);\r
- \r
- protected LinkerConfiguration createConfiguration(\r
- CCTask task,\r
- LinkType linkType,\r
- ProcessorDef[] baseDefs, LinkerDef specificDef, TargetDef targetPlatform) {\r
-\r
- Vector preargs = new Vector();\r
- Vector midargs = new Vector();\r
- Vector endargs = new Vector();\r
- Vector[] args = new Vector[] { preargs, midargs, endargs };\r
-\r
- LinkerDef[] defaultProviders = new LinkerDef[baseDefs.length+1];\r
- defaultProviders[0] = specificDef;\r
- for(int i = 0; i < baseDefs.length; i++) {\r
- defaultProviders[i+1] = (LinkerDef) baseDefs[i];\r
- }\r
- //\r
- // add command line arguments inherited from <cc> element\r
- // any "extends" and finally the specific CompilerDef\r
- CommandLineArgument[] commandArgs;\r
- for(int i = defaultProviders.length-1; i >= 0; i--) {\r
- commandArgs = defaultProviders[i].getActiveProcessorArgs();\r
- for(int j = 0; j < commandArgs.length; j++) {\r
- args[commandArgs[j].getLocation()].\r
- addElement(commandArgs[j].getValue());\r
- }\r
- }\r
-\r
- Vector params = new Vector();\r
- //\r
- // add command line arguments inherited from <cc> element\r
- // any "extends" and finally the specific CompilerDef\r
- ProcessorParam[] paramArray;\r
- for (int i = defaultProviders.length - 1; i >= 0; i--) {\r
- paramArray = defaultProviders[i].getActiveProcessorParams();\r
- for (int j = 0; j < paramArray.length; j++) {\r
- params.add(paramArray[j]);\r
- }\r
- }\r
-\r
- paramArray = (ProcessorParam[])(params.toArray(new ProcessorParam[params.size()]));\r
-\r
- boolean debug = specificDef.getDebug(baseDefs,0);\r
-\r
- \r
- String startupObject = getStartupObject(linkType);\r
- Boolean defaultflag = specificDef.getDefaultflag(defaultProviders, 1);\r
- addImpliedArgs(debug, linkType, preargs, defaultflag);\r
- addIncremental(specificDef.getIncremental(defaultProviders,1), preargs);\r
- addFixed(specificDef.getFixed(defaultProviders,1), preargs);\r
- addMap(specificDef.getMap(defaultProviders,1), preargs);\r
- addBase(specificDef.getBase(defaultProviders,1), preargs);\r
- addStack(specificDef.getStack(defaultProviders,1), preargs);\r
- addEntry(specificDef.getEntry(defaultProviders, 1), preargs);\r
-\r
- String[] libnames = null;\r
- LibrarySet[] libsets = specificDef.getActiveLibrarySets(defaultProviders,1);\r
- if (libsets.length > 0) {\r
- libnames = addLibrarySets(task, libsets, preargs, midargs, endargs);\r
- }\r
-\r
- StringBuffer buf = new StringBuffer(getIdentifier());\r
- for (int i = 0; i < 3; i++) {\r
- Enumeration argenum = args[i].elements();\r
- while (argenum.hasMoreElements()) {\r
- buf.append(' ');\r
- buf.append(argenum.nextElement().toString());\r
- }\r
- }\r
- String configId = buf.toString();\r
-\r
- String[][] options = new String[][] {\r
- new String[args[0].size() + args[1].size()],\r
- new String[args[2].size()] };\r
- args[0].copyInto(options[0]);\r
- int offset = args[0].size();\r
- for (int i = 0; i < args[1].size(); i++) {\r
- options[0][i+offset] = (String) args[1].elementAt(i);\r
- }\r
- args[2].copyInto(options[1]);\r
-\r
-\r
- boolean rebuild = specificDef.getRebuild(baseDefs,0);\r
- boolean map = specificDef.getMap(defaultProviders,1);\r
-\r
- //task.log("libnames:"+libnames.length, Project.MSG_VERBOSE);\r
- return new CommandLineLinkerConfiguration(this,configId,options,\r
- paramArray,\r
- rebuild,map,libnames, startupObject);\r
- }\r
-\r
- /**\r
- * Allows drived linker to decorate linker option.\r
- * Override by GccLinker to prepend a "-Wl," to\r
- * pass option to through gcc to linker.\r
- *\r
- * @param buf buffer that may be used and abused in the decoration process,\r
- * must not be null.\r
- * @param arg linker argument\r
- */\r
- protected String decorateLinkerOption(StringBuffer buf, String arg) {\r
- return arg;\r
- }\r
-\r
- protected final String getCommand() {\r
- return command;\r
- }\r
- protected abstract String getCommandFileSwitch(String commandFile);\r
-\r
-\r
- public String getIdentifier() {\r
- if(identifier == null) {\r
- if (identifierArg == null) {\r
- identifier = getIdentifier(new String[] { command }, command);\r
- } else {\r
- identifier = getIdentifier(new String[] { command, identifierArg },\r
- command);\r
- }\r
- }\r
- return identifier;\r
- }\r
- public final CommandLineLinker getLibtoolLinker() {\r
- if (libtoolLinker != null) {\r
- return libtoolLinker;\r
- }\r
- return this;\r
- }\r
- protected abstract int getMaximumCommandLength();\r
-\r
- public String getOutputFileName(String baseName) {\r
- return baseName + outputSuffix;\r
- }\r
-\r
- protected String[] getOutputFileSwitch(CCTask task, String outputFile) {\r
- return getOutputFileSwitch(outputFile);\r
- }\r
- protected abstract String[] getOutputFileSwitch(String outputFile);\r
- protected String getStartupObject(LinkType linkType) {\r
- return null;\r
- }\r
-\r
- /**\r
- * Performs a link using a command line linker\r
- *\r
- */\r
- public void link(CCTask task,\r
- File outputFile,\r
- String[] sourceFiles,\r
- CommandLineLinkerConfiguration config)\r
- throws BuildException\r
- {\r
- File parentDir = new File(outputFile.getParent());\r
- String parentPath;\r
- try {\r
- parentPath = parentDir.getCanonicalPath();\r
- } catch(IOException ex) {\r
- parentPath = parentDir.getAbsolutePath();\r
- }\r
- String[] execArgs = prepareArguments(task, parentPath,outputFile.getName(),\r
- sourceFiles, config);\r
- int commandLength = 0;\r
- for(int i = 0; i < execArgs.length; i++) {\r
- commandLength += execArgs[i].length() + 1;\r
- }\r
-\r
- //\r
- // if command length exceeds maximum\r
- // (1024 for Windows) then create a temporary\r
- // file containing everything but the command name\r
- if(commandLength >= this.getMaximumCommandLength()) {\r
- try {\r
- execArgs = prepareResponseFile(outputFile,execArgs);\r
- }\r
- catch(IOException ex) {\r
- throw new BuildException(ex);\r
- }\r
- }\r
- \r
- int retval = runCommand(task,parentDir,execArgs); \r
- //\r
- // if the process returned a failure code then\r
- // throw an BuildException\r
- //\r
- if(retval != 0) {\r
- //\r
- // construct the exception\r
- //\r
- throw new BuildException(this.getCommand() + " failed with return code " + retval, task.getLocation());\r
- }\r
- \r
- }\r
-\r
-\r
- /**\r
- * Prepares argument list for exec command. Will return null\r
- * if command line would exceed allowable command line buffer.\r
- *\r
- * @param outputFile linker output file\r
- * @param sourceFiles linker input files (.obj, .o, .res)\r
- * @param args linker arguments\r
- * @return arguments for runTask\r
- */\r
- protected String[] prepareArguments(\r
- CCTask task,\r
- String outputDir,\r
- String outputFile,\r
- String[] sourceFiles,\r
- CommandLineLinkerConfiguration config) {\r
- \r
- String[] preargs = config.getPreArguments();\r
- String[] endargs = config.getEndArguments();\r
- String outputSwitch[] = getOutputFileSwitch(task, outputFile);\r
- int allArgsCount = preargs.length + 1 + outputSwitch.length +\r
- sourceFiles.length + endargs.length;\r
- if (isLibtool) {\r
- allArgsCount++;\r
- }\r
- String[] allArgs = new String[allArgsCount];\r
- int index = 0;\r
- if (isLibtool) {\r
- allArgs[index++] = "libtool";\r
- }\r
- allArgs[index++] = this.getCommand();\r
- StringBuffer buf = new StringBuffer();\r
- for (int i = 0; i < preargs.length; i++) {\r
- allArgs[index++] = decorateLinkerOption(buf, preargs[i]);\r
- }\r
- for (int i = 0; i < outputSwitch.length; i++) {\r
- allArgs[index++] = outputSwitch[i];\r
- }\r
- for (int i = 0; i < sourceFiles.length; i++) {\r
- allArgs[index++] = prepareFilename(buf,outputDir,sourceFiles[i]);\r
- }\r
- for (int i = 0; i < endargs.length; i++) {\r
- allArgs[index++] = decorateLinkerOption(buf, endargs[i]);\r
- }\r
- return allArgs;\r
- }\r
-\r
- /**\r
- * Processes filename into argument form\r
- *\r
- */\r
- protected String prepareFilename(StringBuffer buf,\r
- String outputDir, String sourceFile) {\r
- String relativePath = CUtil.getRelativePath(outputDir,\r
- new File(sourceFile));\r
- return quoteFilename(buf,relativePath);\r
- }\r
-\r
- /**\r
- * Prepares argument list to execute the linker using a\r
- * response file.\r
- *\r
- * @param outputFile linker output file\r
- * @param args output of prepareArguments\r
- * @return arguments for runTask\r
- */\r
- protected String[] prepareResponseFile(File outputFile,String[] args) throws IOException\r
- {\r
- String baseName = outputFile.getName();\r
- File commandFile = new File(outputFile.getParent(),baseName + ".rsp");\r
- FileWriter writer = new FileWriter(commandFile);\r
- int execArgCount = 1;\r
- if (isLibtool) {\r
- execArgCount++;\r
- }\r
- String[] execArgs = new String[execArgCount+1];\r
- for (int i = 0; i < execArgCount; i++) {\r
- execArgs[i] = args[i];\r
- }\r
- execArgs[execArgCount] = getCommandFileSwitch(commandFile.toString());\r
- for(int i = execArgCount; i < args.length; i++) {\r
- //\r
- // if embedded space and not quoted then\r
- // quote argument\r
- if (args[i].indexOf(" ") >= 0 && args[i].charAt(0) != '\"') {\r
- writer.write('\"');\r
- writer.write(args[i]);\r
- writer.write("\"\n");\r
- } else {\r
- writer.write(args[i]);\r
- writer.write('\n');\r
- }\r
- }\r
- writer.close();\r
- return execArgs;\r
- }\r
-\r
-\r
- protected String quoteFilename(StringBuffer buf,String filename) {\r
- if(filename.indexOf(' ') >= 0) {\r
- buf.setLength(0);\r
- buf.append('\"');\r
- buf.append(filename);\r
- buf.append('\"');\r
- return buf.toString();\r
- }\r
- return filename;\r
- }\r
-\r
- /**\r
- * This method is exposed so test classes can overload\r
- * and test the arguments without actually spawning the\r
- * compiler\r
- */\r
- protected int runCommand(CCTask task, File workingDir,String[] cmdline)\r
- throws BuildException {\r
- return CUtil.runCommand(task,workingDir,cmdline, newEnvironment, env);\r
- }\r
-\r
- protected final void setCommand(String command) {\r
- this.command = command;\r
- }\r
-\r
-}\r