--- /dev/null
+/*\r
+ * \r
+ * Copyright 2003-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.gcc.cross;\r
+import java.io.File;\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.LinkerParam;\r
+import net.sf.antcontrib.cpptasks.compiler.CaptureStreamHandler;\r
+import net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration;\r
+import net.sf.antcontrib.cpptasks.compiler.LinkType;\r
+import net.sf.antcontrib.cpptasks.compiler.Linker;\r
+import net.sf.antcontrib.cpptasks.gcc.AbstractLdLinker;\r
+import net.sf.antcontrib.cpptasks.types.LibrarySet;\r
+\r
+import org.apache.tools.ant.BuildException;\r
+/**\r
+ * Adapter for the g++ variant of the GCC linker\r
+ * \r
+ * @author Stephen M. Webb <stephen.webb@bregmasoft.com>\r
+ */\r
+public class GppLinker extends AbstractLdLinker {\r
+ protected static final String[] discardFiles = new String[0];\r
+ protected static final String[] objFiles = new String[]{".o", ".a", ".lib",\r
+ ".dll", ".so", ".sl"};\r
+ private static final GppLinker dllLinker = new GppLinker("gcc", objFiles,\r
+ discardFiles, "lib", ".so", false, new GppLinker("gcc", objFiles,\r
+ discardFiles, "lib", ".so", true, null));\r
+ private final static String libPrefix = "libraries: =";\r
+ protected static final String[] libtoolObjFiles = new String[]{".fo", ".a",\r
+ ".lib", ".dll", ".so", ".sl"};\r
+ private static String[] linkerOptions = new String[]{"-bundle", "-dylib",\r
+ "-dynamic", "-dynamiclib", "-nostartfiles", "-nostdlib",\r
+ "-prebind", "-s", "-static", "-shared", "-symbolic", "-Xlinker"};\r
+ private static final GppLinker instance = new GppLinker("gcc", objFiles,\r
+ discardFiles, "", "", false, null);\r
+ private static final GppLinker machDllLinker = new GppLinker("gcc",\r
+ objFiles, discardFiles, "lib", ".dylib", false, null);\r
+ private static final GppLinker machPluginLinker = new GppLinker("gcc",\r
+ objFiles, discardFiles, "lib", ".bundle", false, null);\r
+ public static GppLinker getInstance() {\r
+ return instance;\r
+ }\r
+ private File[] libDirs;\r
+ private String runtimeLibrary;\r
+ protected GppLinker(String command, String[] extensions,\r
+ String[] ignoredExtensions, String outputPrefix,\r
+ String outputSuffix, boolean isLibtool, GppLinker libtoolLinker) {\r
+ super(command, "-dumpversion", extensions, ignoredExtensions,\r
+ outputPrefix, outputSuffix, isLibtool, libtoolLinker);\r
+ }\r
+ protected void addImpliedArgs(boolean debug, LinkType linkType, Vector args, Boolean defaultflag) {\r
+ super.addImpliedArgs(debug, linkType, args, defaultflag);\r
+ if (getIdentifier().indexOf("mingw") >= 0) {\r
+ if (linkType.isSubsystemConsole()) {\r
+ args.addElement("-mconsole");\r
+ }\r
+ if (linkType.isSubsystemGUI()) {\r
+ args.addElement("-mwindows");\r
+ }\r
+ }\r
+ if (linkType.isStaticRuntime()) {\r
+ String[] cmdin = new String[]{"g++", "-print-file-name=libstdc++.a"};\r
+ String[] cmdout = CaptureStreamHandler.run(cmdin);\r
+ if (cmdout.length > 0) {\r
+ runtimeLibrary = cmdout[0];\r
+ } else {\r
+ runtimeLibrary = null;\r
+ }\r
+ } else {\r
+ runtimeLibrary = "-lstdc++";\r
+ }\r
+ }\r
+ public String[] addLibrarySets(CCTask task, LibrarySet[] libsets,\r
+ Vector preargs, Vector midargs, Vector endargs) {\r
+ String[] rs = super.addLibrarySets(task, libsets, preargs, midargs,\r
+ endargs);\r
+ if (runtimeLibrary != null) {\r
+ endargs.addElement(runtimeLibrary);\r
+ }\r
+ return rs;\r
+ }\r
+ protected Object clone() throws CloneNotSupportedException {\r
+ GppLinker clone = (GppLinker) super.clone();\r
+ return clone;\r
+ }\r
+ /**\r
+ * Allows drived linker to decorate linker option. Override by GppLinker to\r
+ * prepend a "-Wl," to pass option to through gcc to linker.\r
+ * \r
+ * @param buf\r
+ * buffer that may be used and abused in the decoration process,\r
+ * must not be null.\r
+ * @param arg\r
+ * linker argument\r
+ */\r
+ public String decorateLinkerOption(StringBuffer buf, String arg) {\r
+ String decoratedArg = arg;\r
+ if (arg.length() > 1 && arg.charAt(0) == '-') {\r
+ switch (arg.charAt(1)) {\r
+ //\r
+ // passed automatically by GCC\r
+ //\r
+ case 'g' :\r
+ case 'f' :\r
+ case 'F' :\r
+ /* Darwin */\r
+ case 'm' :\r
+ case 'O' :\r
+ case 'W' :\r
+ case 'l' :\r
+ case 'L' :\r
+ case 'u' :\r
+ break;\r
+ default :\r
+ boolean known = false;\r
+ for (int i = 0; i < linkerOptions.length; i++) {\r
+ if (linkerOptions[i].equals(arg)) {\r
+ known = true;\r
+ break;\r
+ }\r
+ }\r
+ if (!known) {\r
+ buf.setLength(0);\r
+ buf.append("-Wl,");\r
+ buf.append(arg);\r
+ decoratedArg = buf.toString();\r
+ }\r
+ break;\r
+ }\r
+ }\r
+ return decoratedArg;\r
+ }\r
+ /**\r
+ * Returns library path.\r
+ * \r
+ */\r
+ public File[] getLibraryPath() {\r
+ if (libDirs == null) {\r
+ Vector dirs = new Vector();\r
+ // Ask GCC where it will look for its libraries.\r
+ String[] args = new String[]{"g++", "-print-search-dirs"};\r
+ String[] cmdout = CaptureStreamHandler.run(args);\r
+ for (int i = 0; i < cmdout.length; ++i) {\r
+ int prefixIndex = cmdout[i].indexOf(libPrefix);\r
+ if (prefixIndex >= 0) {\r
+ // Special case DOS-type GCCs like MinGW or Cygwin\r
+ int s = prefixIndex + libPrefix.length();\r
+ int t = cmdout[i].indexOf(';', s);\r
+ while (t > 0) {\r
+ dirs.addElement(cmdout[i].substring(s, t));\r
+ s = t + 1;\r
+ t = cmdout[i].indexOf(';', s);\r
+ }\r
+ dirs.addElement(cmdout[i].substring(s));\r
+ ++i;\r
+ for (; i < cmdout.length; ++i) {\r
+ dirs.addElement(cmdout[i]);\r
+ }\r
+ }\r
+ }\r
+ // Eliminate all but actual directories.\r
+ String[] libpath = new String[dirs.size()];\r
+ dirs.copyInto(libpath);\r
+ int count = CUtil.checkDirectoryArray(libpath);\r
+ // Build return array.\r
+ libDirs = new File[count];\r
+ int index = 0;\r
+ for (int i = 0; i < libpath.length; ++i) {\r
+ if (libpath[i] != null) {\r
+ libDirs[index++] = new File(libpath[i]);\r
+ }\r
+ }\r
+ }\r
+ return libDirs;\r
+ }\r
+ public Linker getLinker(LinkType type) {\r
+ if (type.isStaticLibrary()) {\r
+ return GccLibrarian.getInstance();\r
+ }\r
+ if (type.isPluginModule()) {\r
+ if (GccProcessor.getMachine().indexOf("darwin") >= 0) {\r
+ return machPluginLinker;\r
+ } else {\r
+ return dllLinker;\r
+ }\r
+ }\r
+ if (type.isSharedLibrary()) {\r
+ if (GccProcessor.getMachine().indexOf("darwin") >= 0) {\r
+ return machDllLinker;\r
+ } else {\r
+ return dllLinker;\r
+ }\r
+ }\r
+ return instance;\r
+ }\r
+ public void link(CCTask task, File outputFile, String[] sourceFiles,\r
+ CommandLineLinkerConfiguration config) throws BuildException {\r
+ try {\r
+ GppLinker clone = (GppLinker) this.clone();\r
+ LinkerParam param = config.getParam("target");\r
+ if (param != null)\r
+ clone.setCommand(param.getValue() + "-" + this.getCommand());\r
+ clone.superlink(task, outputFile, sourceFiles, config);\r
+ } catch (CloneNotSupportedException e) {\r
+ superlink(task, outputFile, sourceFiles, config);\r
+ }\r
+ }\r
+ private void superlink(CCTask task, File outputFile, String[] sourceFiles,\r
+ CommandLineLinkerConfiguration config) throws BuildException {\r
+ super.link(task, outputFile, sourceFiles, config);\r
+ }\r
+}\r