]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/cross/sparc_sun_solaris2/GppLinker.java
Initial import.
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / gcc / cross / sparc_sun_solaris2 / GppLinker.java
CommitLineData
878ddf1f 1/*\r
2 * \r
3 * Copyright 2003-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.gcc.cross.sparc_sun_solaris2;\r
18import java.io.File;\r
19import java.util.Vector;\r
20\r
21import net.sf.antcontrib.cpptasks.CCTask;\r
22import net.sf.antcontrib.cpptasks.CUtil;\r
23import net.sf.antcontrib.cpptasks.compiler.CaptureStreamHandler;\r
24import net.sf.antcontrib.cpptasks.compiler.LinkType;\r
25import net.sf.antcontrib.cpptasks.compiler.Linker;\r
26import net.sf.antcontrib.cpptasks.gcc.AbstractLdLinker;\r
27import net.sf.antcontrib.cpptasks.types.LibrarySet;\r
28/**\r
29 * Adapter for the g++ variant of the GCC linker\r
30 * \r
31 * @author Stephen M. Webb <stephen.webb@bregmasoft.com>\r
32 */\r
33public class GppLinker extends AbstractLdLinker {\r
34 protected static final String[] discardFiles = new String[0];\r
35 protected static final String[] objFiles = new String[]{".o", ".a", ".lib",\r
36 ".dll", ".so", ".sl"};\r
37 private final static String libPrefix = "libraries: =";\r
38 protected static final String[] libtoolObjFiles = new String[]{".fo", ".a",\r
39 ".lib", ".dll", ".so", ".sl"};\r
40 private static String[] linkerOptions = new String[]{"-bundle", "-dylib",\r
41 "-dynamic", "-dynamiclib", "-nostartfiles", "-nostdlib",\r
42 "-prebind", "-s", "-static", "-shared", "-symbolic", "-Xlinker"};\r
43 private static final GppLinker dllLinker = new GppLinker(\r
44 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "lib",\r
45 ".so", false, new GppLinker(GccCCompiler.CMD_PREFIX + "gcc",\r
46 objFiles, discardFiles, "lib", ".so", true, null));\r
47 private static final GppLinker instance = new GppLinker(\r
48 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "", "",\r
49 false, null);\r
50 private static final GppLinker machDllLinker = new GppLinker(\r
51 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "lib",\r
52 ".dylib", false, null);\r
53 private static final GppLinker machPluginLinker = new GppLinker(\r
54 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "lib",\r
55 ".bundle", false, null);\r
56 public static GppLinker getInstance() {\r
57 return instance;\r
58 }\r
59 private File[] libDirs;\r
60 private String runtimeLibrary;\r
61 protected GppLinker(String command, String[] extensions,\r
62 String[] ignoredExtensions, String outputPrefix,\r
63 String outputSuffix, boolean isLibtool, GppLinker libtoolLinker) {\r
64 super(command, "-dumpversion", extensions, ignoredExtensions,\r
65 outputPrefix, outputSuffix, isLibtool, libtoolLinker);\r
66 }\r
67 protected void addImpliedArgs(boolean debug, LinkType linkType, Vector args, Boolean defaultflag) {\r
68 super.addImpliedArgs(debug, linkType, args, defaultflag);\r
69 if (getIdentifier().indexOf("mingw") >= 0) {\r
70 if (linkType.isSubsystemConsole()) {\r
71 args.addElement("-mconsole");\r
72 }\r
73 if (linkType.isSubsystemGUI()) {\r
74 args.addElement("-mwindows");\r
75 }\r
76 }\r
77 if (linkType.isStaticRuntime()) {\r
78 String[] cmdin = new String[]{GccCCompiler.CMD_PREFIX + "g++",\r
79 "-print-file-name=libstdc++.a"};\r
80 String[] cmdout = CaptureStreamHandler.run(cmdin);\r
81 if (cmdout.length > 0) {\r
82 runtimeLibrary = cmdout[0];\r
83 } else {\r
84 runtimeLibrary = null;\r
85 }\r
86 } else {\r
87 runtimeLibrary = "-lstdc++";\r
88 }\r
89 }\r
90 public String[] addLibrarySets(CCTask task, LibrarySet[] libsets,\r
91 Vector preargs, Vector midargs, Vector endargs) {\r
92 String[] rs = super.addLibrarySets(task, libsets, preargs, midargs,\r
93 endargs);\r
94 if (runtimeLibrary != null) {\r
95 endargs.addElement(runtimeLibrary);\r
96 }\r
97 return rs;\r
98 }\r
99 /**\r
100 * Allows drived linker to decorate linker option. Override by GppLinker to\r
101 * prepend a "-Wl," to pass option to through gcc to linker.\r
102 * \r
103 * @param buf\r
104 * buffer that may be used and abused in the decoration process,\r
105 * must not be null.\r
106 * @param arg\r
107 * linker argument\r
108 */\r
109 public String decorateLinkerOption(StringBuffer buf, String arg) {\r
110 String decoratedArg = arg;\r
111 if (arg.length() > 1 && arg.charAt(0) == '-') {\r
112 switch (arg.charAt(1)) {\r
113 //\r
114 // passed automatically by GCC\r
115 //\r
116 case 'g' :\r
117 case 'f' :\r
118 case 'F' :\r
119 /* Darwin */\r
120 case 'm' :\r
121 case 'O' :\r
122 case 'W' :\r
123 case 'l' :\r
124 case 'L' :\r
125 case 'u' :\r
126 break;\r
127 default :\r
128 boolean known = false;\r
129 for (int i = 0; i < linkerOptions.length; i++) {\r
130 if (linkerOptions[i].equals(arg)) {\r
131 known = true;\r
132 break;\r
133 }\r
134 }\r
135 if (!known) {\r
136 buf.setLength(0);\r
137 buf.append("-Wl,");\r
138 buf.append(arg);\r
139 decoratedArg = buf.toString();\r
140 }\r
141 break;\r
142 }\r
143 }\r
144 return decoratedArg;\r
145 }\r
146 /**\r
147 * Returns library path.\r
148 * \r
149 */\r
150 public File[] getLibraryPath() {\r
151 if (libDirs == null) {\r
152 Vector dirs = new Vector();\r
153 // Ask GCC where it will look for its libraries.\r
154 String[] args = new String[]{GccCCompiler.CMD_PREFIX + "g++",\r
155 "-print-search-dirs"};\r
156 String[] cmdout = CaptureStreamHandler.run(args);\r
157 for (int i = 0; i < cmdout.length; ++i) {\r
158 int prefixIndex = cmdout[i].indexOf(libPrefix);\r
159 if (prefixIndex >= 0) {\r
160 // Special case DOS-type GCCs like MinGW or Cygwin\r
161 int s = prefixIndex + libPrefix.length();\r
162 int t = cmdout[i].indexOf(';', s);\r
163 while (t > 0) {\r
164 dirs.addElement(cmdout[i].substring(s, t));\r
165 s = t + 1;\r
166 t = cmdout[i].indexOf(';', s);\r
167 }\r
168 dirs.addElement(cmdout[i].substring(s));\r
169 ++i;\r
170 for (; i < cmdout.length; ++i) {\r
171 dirs.addElement(cmdout[i]);\r
172 }\r
173 }\r
174 }\r
175 // Eliminate all but actual directories.\r
176 String[] libpath = new String[dirs.size()];\r
177 dirs.copyInto(libpath);\r
178 int count = CUtil.checkDirectoryArray(libpath);\r
179 // Build return array.\r
180 libDirs = new File[count];\r
181 int index = 0;\r
182 for (int i = 0; i < libpath.length; ++i) {\r
183 if (libpath[i] != null) {\r
184 libDirs[index++] = new File(libpath[i]);\r
185 }\r
186 }\r
187 }\r
188 return libDirs;\r
189 }\r
190 public Linker getLinker(LinkType type) {\r
191 if (type.isStaticLibrary()) {\r
192 return GccLibrarian.getInstance();\r
193 }\r
194 if (type.isPluginModule()) {\r
195 if (GccProcessor.getMachine().indexOf("darwin") >= 0) {\r
196 return machPluginLinker;\r
197 } else {\r
198 return dllLinker;\r
199 }\r
200 }\r
201 if (type.isSharedLibrary()) {\r
202 if (GccProcessor.getMachine().indexOf("darwin") >= 0) {\r
203 return machDllLinker;\r
204 } else {\r
205 return dllLinker;\r
206 }\r
207 }\r
208 return instance;\r
209 }\r
210}\r