summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/sf/antcontrib/cpptasks/gcc/GnuLinker.java
blob: cbd30ea2c7e01b7f2a650a72be61c1002b2dc2ed (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
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.CUtil;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
import net.sf.antcontrib.cpptasks.compiler.Linker;

public abstract class GnuLinker extends AbstractLdLinker {

    static String[] darwinLinkerOptions = new String[]{"-arch", "-weak_framework", "-lazy_framework", "-weak_library" };

    protected final boolean isGCC;
    protected File[] libDirs;

    public GnuLinker(String command, String identifierArg, String[] extensions,
            String[] ignoredExtensions, String outputPrefix,
            String outputSuffix, boolean isXCoderun, boolean isLibtool,
            AbstractLdLinker libtoolLinker) {
        super(command, identifierArg, extensions, ignoredExtensions,
                outputPrefix, outputSuffix, isXCoderun, isLibtool,
                libtoolLinker);
        isGCC = "gcc".equals(command);
    }

    public GnuLinker(AbstractLdLinker ld, boolean isXCoderun) {
        super(ld, isXCoderun);
        isGCC = "gcc".equals(getCommand());
    }

    protected abstract String[] getStaticLinkerOptions();
    protected abstract GnuLinker getStaticDllLinker();
    protected abstract GnuLinker getStaticDllClangLinker();
    protected abstract GnuLinker getStaticClangInstance();
    protected abstract GnuLinker getStaticXcodeClangInstance();
    protected abstract GnuLinker getStaticMachBundleLinker();
    protected abstract GnuLinker getStaticMachClangBundleLinker();
    protected abstract GnuLinker getStaticXcodeMachClangBundleLinker();
    protected abstract GnuLinker getStaticMachDllLinker();
    protected abstract GnuLinker getStaticMachDllClangLinker();
    protected abstract GnuLinker getStaticXcodeMachDllClangLinker();
    protected abstract GnuLinker getStaticInstance();

    @Override
    protected void addImpliedArgs(boolean debug, LinkType linkType, Vector args) {
        super.addImpliedArgs(debug, linkType, args);
        if (getIdentifier().indexOf("mingw") >= 0) {
            if (linkType.isSubsystemConsole()) {
                args.addElement("-mconsole");
            }
            if (linkType.isSubsystemGUI()) {
                args.addElement("-mwindows");
            }
        }
    }

    /**
     * Allows drived linker to decorate linker option. Override by GccLinker to
     * prepend a "-Wl," to pass option to through gcc to linker.
     *
     * @param buf
     *            buffer that may be used and abused in the decoration process,
     *            must not be null.
     * @param arg
     *            linker argument
     */
    @Override
    public String decorateLinkerOption(StringBuffer buf, String arg) {
        if (arg.startsWith("--sysroot")) {
          return arg;
        }
        if (arg.startsWith("-nostdlib")) {
          return arg;
        }
        String decoratedArg = arg;
        if (arg.length() > 1 && arg.charAt(0) == '-') {
            switch (arg.charAt(1)) {
                //
                //   passed automatically by GCC
                //
                case 'g' :
                case 'f' :
                case 'F' :
                /* Darwin */
                case 'm' :
                case 'O' :
                case 'W' :
                case 'l' :
                case 'L' :
                case 'u' :
                case 'v' :
                    break;
                default :
                    boolean known = false;
                    HashSet<String> allLinkerOptions = new HashSet<String>();
                    allLinkerOptions.addAll(Arrays.asList(getStaticLinkerOptions()));
                    if (isDarwin()) {
                        allLinkerOptions.addAll(Arrays.asList(darwinLinkerOptions));
                    }
                    known = allLinkerOptions.contains(arg);

                    if (!known) {
                        buf.setLength(0);
                        buf.append("-Wl,");
                        buf.append(arg);
                        decoratedArg = buf.toString();
                    }
                    break;
            }
        }
        return decoratedArg;
    }

    @Override
    public Linker getLinker(LinkType type) {
        if (type.isStaticLibrary()) {
            return GccLibrarian.getInstance(); // uses 'ar', which is 'gcc' agnostic
        }
        if (type.isPluginModule()) {
            if (isDarwin()) {
                return isGCC ? getStaticMachBundleLinker() : ( getXcodeRun() ? getStaticXcodeMachClangBundleLinker() : getStaticMachClangBundleLinker() );
            } else {
                return isGCC ? getStaticDllLinker() : getStaticDllClangLinker();
            }
        }
        if (type.isSharedLibrary()) {
            if (isDarwin()) {
                return isGCC ? getStaticMachDllLinker() : ( getXcodeRun() ? getStaticXcodeMachDllClangLinker() : getStaticMachDllClangLinker() );
            } else {
                return isGCC ? getStaticDllLinker() : getStaticDllClangLinker();
            }
        }
        return isGCC ? getStaticInstance() : ( getXcodeRun() ? getStaticXcodeClangInstance() : getStaticClangInstance() ) ;
    }

}