3 * Copyright 2001-2004 The Ant-Contrib project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package net
.sf
.antcontrib
.cpptasks
.gcc
.cross
.sparc_sun_solaris2
;
19 import java
.util
.Vector
;
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
;
26 * Adapter for the GCC linker
28 * @author Adam Murdoch
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
, "", "",
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() {
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
);
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");
69 if (linkType
.isSubsystemGUI()) {
70 args
.addElement("-mwindows");
75 * Allows drived linker to decorate linker option. Override by GccLinker to
76 * prepend a "-Wl," to pass option to through gcc to linker.
79 * buffer that may be used and abused in the decoration process,
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)) {
89 // passed automatically by GCC
104 boolean known
= false;
105 for (int i
= 0; i
< linkerOptions
.length
; i
++) {
106 if (linkerOptions
[i
].equals(arg
)) {
115 decoratedArg
= buf
.toString();
123 * Returns library path.
126 public File
[] getLibraryPath() {
127 if (libDirs
== null) {
129 // construct gcc lib path from machine and version
131 StringBuffer buf
= new StringBuffer("/lib/gcc-lib/");
132 buf
.append(GccProcessor
.getMachine());
134 buf
.append(GccProcessor
.getVersion());
136 // build default path from gcc and system /lib and /lib/w32api
138 String
[] impliedLibPath
= new String
[]{buf
.toString(),
139 "/lib/w32api", "/lib"};
141 // read gcc specs file for other library paths
143 String
[] specs
= GccProcessor
.getSpecs();
144 String
[][] libpaths
= GccProcessor
.parseSpecs(specs
, "*link:",
147 if (libpaths
[0].length
> 0) {
148 libpath
= new String
[libpaths
[0].length
+ 3];
150 for (; i
< libpaths
[0].length
; i
++) {
151 libpath
[i
] = libpaths
[0][i
];
153 libpath
[i
++] = buf
.toString();
154 libpath
[i
++] = "/lib/w32api";
155 libpath
[i
++] = "/lib";
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"};
164 for (int i
= 0; i
< libpath
.length
; i
++) {
165 if (libpath
[i
].indexOf("mingw") >= 0) {
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
);
179 // check that remaining entries are actual directories
181 int count
= CUtil
.checkDirectoryArray(libpath
);
183 // populate return array with remaining entries
185 libDirs
= new File
[count
];
187 for (int i
= 0; i
< libpath
.length
; i
++) {
188 if (libpath
[i
] != null) {
189 libDirs
[index
++] = new File(libpath
[i
]);
195 public Linker
getLinker(LinkType type
) {
196 if (type
.isStaticLibrary()) {
197 return GccLibrarian
.getInstance();
199 if (type
.isPluginModule()) {
201 return machBundleLinker
;
206 if (type
.isSharedLibrary()) {
208 return machDllLinker
;