summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/sf/antcontrib/cpptasks/gcc/GppLinker.java
blob: 1ca34b5b4186ced77416f7894506823b44c35e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 *
 * Copyright 2003-2004 The Ant-Contrib project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sf.antcontrib.cpptasks.gcc;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Vector;

import net.sf.antcontrib.cpptasks.CCTask;
import net.sf.antcontrib.cpptasks.CUtil;
import net.sf.antcontrib.cpptasks.compiler.CaptureStreamHandler;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
import net.sf.antcontrib.cpptasks.compiler.Linker;
import net.sf.antcontrib.cpptasks.types.LibrarySet;
/**
 * Adapter for the g++ variant of the GCC linker
 *
 * @author Stephen M. Webb <stephen.webb@bregmasoft.com>, et.al.
 */
public class GppLinker extends GnuLinker {
    protected static final String[] discardFiles = new String[0];
    protected static final String[] objFiles = new String[]{".o", ".a", ".lib",
            ".dll", ".so", ".sl"};
    private final static String libPrefix = "libraries: =";
    private static final String[] libtoolObjFiles = new String[]{".fo", ".a",
            ".lib", ".dll", ".so", ".sl"};
    private static String[] linkerOptions = new String[]{"-bundle", "-dylib",
            "-dynamic", "-dynamiclib", "-nostartfiles", "-nostdlib",
            "-prebind", "-s", "-static", "-shared", "-symbolic", "-Xlinker"};

    private static final GppLinker dllLinker = new GppLinker("gcc", objFiles,
            discardFiles, "lib", ".so", false, false, new GppLinker("gcc", objFiles,
                    discardFiles, "lib", ".so", false, true, null));
    private static final GppLinker dllClangLinker = new GppLinker("clang", objFiles,
            discardFiles, "lib", ".so", false, false, new GppLinker("clang", objFiles,
                    discardFiles, "lib", ".so", false, true, null));

    private static final GppLinker instance = new GppLinker("gcc", objFiles,
            discardFiles, "", "", false, false, null);
    private static final GppLinker clangInstance = new GppLinker("clang", objFiles,
            discardFiles, "", "", false, false, null);
    private static final GppLinker xcodeClangInstance = new GppLinker(clangInstance, true);

    private static final GppLinker machBundleLinker = new GppLinker("gcc",
            objFiles, discardFiles, "lib", ".bundle", false, false, null);
    private static final GppLinker machClangBundleLinker = new GppLinker("clang",
            objFiles, discardFiles, "lib", ".bundle", false, false, null);
    private static final GppLinker xcodeMachClangBundleLinker = new GppLinker(machClangBundleLinker, true);

    private static final GppLinker machDllLinker = new GppLinker("gcc",
            objFiles, discardFiles, "lib", ".dylib", false, false, null);
    private static final GppLinker machDllClangLinker = new GppLinker("clang",
            objFiles, discardFiles, "lib", ".dylib", false, false, null);
    private static final GppLinker xcodeMachDllClangLinker = new GppLinker(machDllClangLinker, true);

    public static GppLinker getInstance() {
        return instance;
    }
    public static GppLinker getClangInstance() {
        return clangInstance;
    }
    public static GppLinker getXcodeClangInstance() {
        return xcodeClangInstance;
    }
    private String runtimeLibrary;
    protected GppLinker(String command, String[] extensions,
            String[] ignoredExtensions, String outputPrefix,
            String outputSuffix, boolean isXCoderun, boolean isLibtool, GppLinker libtoolLinker) {
        super(command, "-dumpversion", extensions, ignoredExtensions,
                outputPrefix, outputSuffix, false, isLibtool, libtoolLinker);
    }
    protected GppLinker(GppLinker ld, boolean isXCoderun) {
        super(ld, isXCoderun);
    }

    @Override
    protected final String[] getStaticLinkerOptions() { return  linkerOptions; }

    @Override
    protected final GnuLinker getStaticDllLinker() {
        return dllLinker;
    }
    @Override
    protected final GnuLinker getStaticDllClangLinker() {
        return dllClangLinker;
    }
    @Override
    protected final GnuLinker getStaticClangInstance() {
        return clangInstance;
    }
    @Override
    protected final GnuLinker getStaticXcodeClangInstance() {
        return xcodeClangInstance;
    }
    @Override
    protected final GnuLinker getStaticMachBundleLinker() {
        return machBundleLinker;
    }
    @Override
    protected final GnuLinker getStaticMachClangBundleLinker() {
        return machClangBundleLinker;
    }
    @Override
    protected final GnuLinker getStaticXcodeMachClangBundleLinker() {
        return xcodeMachClangBundleLinker;
    }
    @Override
    protected final GnuLinker getStaticMachDllLinker() {
        return machDllLinker;
    }
    @Override
    protected final GnuLinker getStaticMachDllClangLinker() {
        return machDllClangLinker;
    }
    @Override
    protected final GnuLinker getStaticXcodeMachDllClangLinker() {
        return xcodeMachDllClangLinker;
    }
    @Override
    protected final GnuLinker getStaticInstance() {
        return instance;
    }

    @Override
    protected void addImpliedArgs(boolean debug, LinkType linkType, Vector args) {
        super.addImpliedArgs(debug, linkType, args);
        if (linkType.isStaticRuntime()) {
            String[] cmdin = new String[]{"g++", "-print-file-name=libstdc++.a"};
            String[] cmdout = CaptureStreamHandler.run(cmdin);
            if (cmdout.length > 0) {
                runtimeLibrary = cmdout[0];
            } else {
                runtimeLibrary = null;
            }
        } else {
            runtimeLibrary = "-lstdc++";
        }
    }
    @Override
    public String[] addLibrarySets(CCTask task, LibrarySet[] libsets,
            Vector preargs, Vector midargs, Vector endargs) {
        String[] rs = super.addLibrarySets(task, libsets, preargs, midargs,
                endargs);
        if (runtimeLibrary != null) {
            endargs.addElement(runtimeLibrary);
        }
        return rs;
    }
    /**
     * Returns library path.
     *
     */
    @Override
    public File[] getLibraryPath() {
        if (libDirs == null) {
            Vector dirs = new Vector();
            // Ask GCC where it will look for its libraries.
            String[] args = new String[]{"g++", "-print-search-dirs"};
            String[] cmdout = CaptureStreamHandler.run(args);
            for (int i = 0; i < cmdout.length; ++i) {
                int prefixIndex = cmdout[i].indexOf(libPrefix);
                if (prefixIndex >= 0) {
                    // Special case DOS-type GCCs like MinGW or Cygwin
                    int s = prefixIndex + libPrefix.length();
                    int t = cmdout[i].indexOf(';', s);
                    while (t > 0) {
                        dirs.addElement(cmdout[i].substring(s, t));
                        s = t + 1;
                        t = cmdout[i].indexOf(';', s);
                    }
                    dirs.addElement(cmdout[i].substring(s));
                    ++i;
                    for (; i < cmdout.length; ++i) {
                        dirs.addElement(cmdout[i]);
                    }
                }
            }
            // Eliminate all but actual directories.
            String[] libpath = new String[dirs.size()];
            dirs.copyInto(libpath);
            int count = CUtil.checkDirectoryArray(libpath);
            // Build return array.
            libDirs = new File[count];
            int index = 0;
            for (int i = 0; i < libpath.length; ++i) {
                if (libpath[i] != null) {
                    libDirs[index++] = new File(libpath[i]);
                }
            }
        }
        return libDirs;
    }
}