]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/gcc/cross/sparc_sun_solaris2/GccLinker.java
Initial import.
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / gcc / cross / sparc_sun_solaris2 / GccLinker.java
1 /*
2 *
3 * Copyright 2001-2004 The Ant-Contrib project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package net.sf.antcontrib.cpptasks.gcc.cross.sparc_sun_solaris2;
18 import java.io.File;
19 import java.util.Vector;
20
21 import net.sf.antcontrib.cpptasks.CUtil;
22 import net.sf.antcontrib.cpptasks.compiler.LinkType;
23 import net.sf.antcontrib.cpptasks.compiler.Linker;
24 import net.sf.antcontrib.cpptasks.gcc.AbstractLdLinker;
25 /**
26 * Adapter for the GCC linker
27 *
28 * @author Adam Murdoch
29 */
30 public class GccLinker extends AbstractLdLinker {
31 private static final String[] discardFiles = new String[0];
32 private static final String[] objFiles = new String[]{".o", ".a", ".lib",
33 ".dll", ".so", ".sl"};
34 private static final String[] libtoolObjFiles = new String[]{".fo", ".a",
35 ".lib", ".dll", ".so", ".sl"};
36 private static String[] linkerOptions = new String[]{"-bundle",
37 "-dynamiclib", "-nostartfiles", "-nostdlib", "-prebind", "-s",
38 "-static", "-shared", "-symbolic", "-Xlinker",
39 "--export-all-symbols", "-static-libgcc",};
40 private static final GccLinker dllLinker = new GccLinker(
41 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "lib",
42 ".so", false, new GccLinker(GccCCompiler.CMD_PREFIX + "gcc",
43 objFiles, discardFiles, "lib", ".so", true, null));
44 private static final GccLinker instance = new GccLinker(
45 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "", "",
46 false, null);
47 private static final GccLinker machBundleLinker = new GccLinker(
48 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "lib",
49 ".bundle", false, null);
50 private static final GccLinker machDllLinker = new GccLinker(
51 GccCCompiler.CMD_PREFIX + "gcc", objFiles, discardFiles, "lib",
52 ".dylib", false, null);
53 public static GccLinker getInstance() {
54 return instance;
55 }
56 private File[] libDirs;
57 protected GccLinker(String command, String[] extensions,
58 String[] ignoredExtensions, String outputPrefix,
59 String outputSuffix, boolean isLibtool, GccLinker libtoolLinker) {
60 super(command, "-dumpversion", extensions, ignoredExtensions,
61 outputPrefix, outputSuffix, isLibtool, libtoolLinker);
62 }
63 protected void addImpliedArgs(boolean debug, LinkType linkType, Vector args, Boolean defaultflag) {
64 super.addImpliedArgs(debug, linkType, args, defaultflag);
65 if (getIdentifier().indexOf("mingw") >= 0) {
66 if (linkType.isSubsystemConsole()) {
67 args.addElement("-mconsole");
68 }
69 if (linkType.isSubsystemGUI()) {
70 args.addElement("-mwindows");
71 }
72 }
73 }
74 /**
75 * Allows drived linker to decorate linker option. Override by GccLinker to
76 * prepend a "-Wl," to pass option to through gcc to linker.
77 *
78 * @param buf
79 * buffer that may be used and abused in the decoration process,
80 * must not be null.
81 * @param arg
82 * linker argument
83 */
84 public String decorateLinkerOption(StringBuffer buf, String arg) {
85 String decoratedArg = arg;
86 if (arg.length() > 1 && arg.charAt(0) == '-') {
87 switch (arg.charAt(1)) {
88 //
89 // passed automatically by GCC
90 //
91 case 'g' :
92 case 'f' :
93 case 'F' :
94 /* Darwin */
95 case 'm' :
96 case 'O' :
97 case 'W' :
98 case 'l' :
99 case 'L' :
100 case 'u' :
101 case 'v' :
102 break;
103 default :
104 boolean known = false;
105 for (int i = 0; i < linkerOptions.length; i++) {
106 if (linkerOptions[i].equals(arg)) {
107 known = true;
108 break;
109 }
110 }
111 if (!known) {
112 buf.setLength(0);
113 buf.append("-Wl,");
114 buf.append(arg);
115 decoratedArg = buf.toString();
116 }
117 break;
118 }
119 }
120 return decoratedArg;
121 }
122 /**
123 * Returns library path.
124 *
125 */
126 public File[] getLibraryPath() {
127 if (libDirs == null) {
128 //
129 // construct gcc lib path from machine and version
130 //
131 StringBuffer buf = new StringBuffer("/lib/gcc-lib/");
132 buf.append(GccProcessor.getMachine());
133 buf.append('/');
134 buf.append(GccProcessor.getVersion());
135 //
136 // build default path from gcc and system /lib and /lib/w32api
137 //
138 String[] impliedLibPath = new String[]{buf.toString(),
139 "/lib/w32api", "/lib"};
140 //
141 // read gcc specs file for other library paths
142 //
143 String[] specs = GccProcessor.getSpecs();
144 String[][] libpaths = GccProcessor.parseSpecs(specs, "*link:",
145 new String[]{"%q"});
146 String[] libpath;
147 if (libpaths[0].length > 0) {
148 libpath = new String[libpaths[0].length + 3];
149 int i = 0;
150 for (; i < libpaths[0].length; i++) {
151 libpath[i] = libpaths[0][i];
152 }
153 libpath[i++] = buf.toString();
154 libpath[i++] = "/lib/w32api";
155 libpath[i++] = "/lib";
156 } else {
157 //
158 // if a failure to find any matches then
159 // use some default values for lib path entries
160 libpath = new String[]{"/usr/local/lib/mingw",
161 "/usr/local/lib", "/usr/lib/w32api", "/usr/lib/mingw",
162 "/usr/lib", buf.toString(), "/lib/w32api", "/lib"};
163 }
164 for (int i = 0; i < libpath.length; i++) {
165 if (libpath[i].indexOf("mingw") >= 0) {
166 libpath[i] = null;
167 }
168 }
169 //
170 // if cygwin then
171 // we have to prepend location of gcc32
172 // and .. to start of absolute filenames to
173 // have something that will exist in the
174 // windows filesystem
175 if (GccProcessor.isCygwin()) {
176 GccProcessor.convertCygwinFilenames(libpath);
177 }
178 //
179 // check that remaining entries are actual directories
180 //
181 int count = CUtil.checkDirectoryArray(libpath);
182 //
183 // populate return array with remaining entries
184 //
185 libDirs = new File[count];
186 int index = 0;
187 for (int i = 0; i < libpath.length; i++) {
188 if (libpath[i] != null) {
189 libDirs[index++] = new File(libpath[i]);
190 }
191 }
192 }
193 return libDirs;
194 }
195 public Linker getLinker(LinkType type) {
196 if (type.isStaticLibrary()) {
197 return GccLibrarian.getInstance();
198 }
199 if (type.isPluginModule()) {
200 if (isDarwin()) {
201 return machBundleLinker;
202 } else {
203 return dllLinker;
204 }
205 }
206 if (type.isSharedLibrary()) {
207 if (isDarwin()) {
208 return machDllLinker;
209 } else {
210 return dllLinker;
211 }
212 }
213 return instance;
214 }
215 }