]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/borland/BorlandLinker.java
Restructuring for better separation of Tool packages.
[mirror_edk2.git] / Tools / Java / Source / Cpptasks / net / sf / antcontrib / cpptasks / borland / BorlandLinker.java
1 /*
2 *
3 * Copyright 2002-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.borland;
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Enumeration;
21 import java.util.Vector;
22
23 import net.sf.antcontrib.cpptasks.CCTask;
24 import net.sf.antcontrib.cpptasks.compiler.CommandLineLinker;
25 import net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration;
26 import net.sf.antcontrib.cpptasks.compiler.LinkType;
27 import net.sf.antcontrib.cpptasks.compiler.Linker;
28 import net.sf.antcontrib.cpptasks.types.LibraryTypeEnum;
29
30 /**
31 * Adapter for the Borland(r) ilink32 linker
32 *
33 * @author Curt Arnold
34 */
35 public final class BorlandLinker extends CommandLineLinker {
36 private static final BorlandLinker dllLinker = new BorlandLinker(".dll");
37 private static final BorlandLinker instance = new BorlandLinker(".exe");
38 public static BorlandLinker getInstance() {
39 return instance;
40 }
41 private BorlandLinker(String outputSuffix) {
42 super("ilink32", "-r", new String[]{".obj", ".lib", ".res"},
43 new String[]{".map", ".pdb", ".lnk"}, outputSuffix, false, null);
44 }
45 protected void addBase(long base, Vector args) {
46 if (base >= 0) {
47 String baseAddr = Long.toHexString(base);
48 args.addElement("-b:" + baseAddr);
49 }
50 }
51 protected void addFixed(Boolean fixed, Vector args) {
52 }
53 protected void addImpliedArgs(boolean debug, LinkType linkType, Vector args, Boolean defaultflag) {
54 if (linkType.isExecutable()) {
55 if (linkType.isSubsystemConsole()) {
56 args.addElement("/ap");
57 } else {
58 if (linkType.isSubsystemGUI()) {
59 args.addElement("/Tpe");
60 }
61 }
62 }
63 if (linkType.isSharedLibrary()) {
64 args.addElement("/Tpd");
65 args.addElement("/Gi");
66 }
67 }
68 protected void addIncremental(boolean incremental, Vector args) {
69 }
70 protected void addMap(boolean map, Vector args) {
71 if (!map) {
72 args.addElement("-x");
73 }
74 }
75 protected void addStack(int stack, Vector args) {
76 if (stack >= 0) {
77 String stackStr = Integer.toHexString(stack);
78 args.addElement("-S:" + stackStr);
79 }
80 }
81 /* (non-Javadoc)
82 * @see net.sf.antcontrib.cpptasks.compiler.CommandLineLinker#addEntry(int, java.util.Vector)
83 */
84 protected void addEntry(String entry, Vector args) {
85 }
86
87 public String getCommandFileSwitch(String commandFile) {
88 return "@" + commandFile;
89 }
90 public String getIdentifier() {
91 return "Borland Linker";
92 }
93 public File[] getLibraryPath() {
94 return BorlandProcessor.getEnvironmentPath("ilink32", 'L',
95 new String[]{"..\\lib"});
96 }
97 public String[] getLibraryPatterns(String[] libnames, LibraryTypeEnum libType) {
98 return BorlandProcessor.getLibraryPatterns(libnames, libType);
99 }
100 public Linker getLinker(LinkType type) {
101 if (type.isStaticLibrary()) {
102 return BorlandLibrarian.getInstance();
103 }
104 if (type.isSharedLibrary()) {
105 return dllLinker;
106 }
107 return instance;
108 }
109 public int getMaximumCommandLength() {
110 return 1024;
111 }
112 public String[] getOutputFileSwitch(String outFile) {
113 return BorlandProcessor.getOutputFileSwitch(outFile);
114 }
115 protected String getStartupObject(LinkType linkType) {
116 if (linkType.isSharedLibrary()) {
117 return "c0d32.obj";
118 }
119 if (linkType.isSubsystemGUI()) {
120 return "c0w32.obj";
121 }
122 if (linkType.isSubsystemConsole()) {
123 return "c0x32.obj";
124 }
125 return null;
126 }
127 public boolean isCaseSensitive() {
128 return BorlandProcessor.isCaseSensitive();
129 }
130 /**
131 * Prepares argument list for exec command.
132 *
133 * @param outputFile
134 * linker output file
135 * @param sourceFiles
136 * linker input files (.obj, .o, .res)
137 * @param args
138 * linker arguments
139 * @return arguments for runTask
140 */
141 protected String[] prepareArguments(
142 CCTask task,
143 String outputDir,
144 String outputName,
145 String[] sourceFiles,
146 CommandLineLinkerConfiguration config) {
147 String[] preargs = config.getPreArguments();
148 String[] endargs = config.getEndArguments();
149 Vector execArgs = new Vector(preargs.length + endargs.length + 10
150 + sourceFiles.length);
151 execArgs.addElement(this.getCommand());
152 for (int i = 0; i < preargs.length; i++) {
153 execArgs.addElement(preargs[i]);
154 }
155 for (int i = 0; i < endargs.length; i++) {
156 execArgs.addElement(endargs[i]);
157 }
158 //
159 // see if the input files have any known startup obj files
160 //
161 String startup = null;
162 for (int i = 0; i < sourceFiles.length; i++) {
163 String filename = new File(sourceFiles[i]).getName().toLowerCase();
164 if (startup != null && filename.substring(0, 2).equals("c0")
165 && filename.substring(3, 5).equals("32")
166 && filename.substring(filename.length() - 4).equals(".obj")) {
167 startup = sourceFiles[i];
168 }
169 }
170 //
171 // c0w32.obj, c0x32.obj or c0d32.obj depending on
172 // link type
173 if (startup == null) {
174 startup = config.getStartupObject();
175 }
176 execArgs.addElement(startup);
177 Vector resFiles = new Vector();
178 Vector libFiles = new Vector();
179 String defFile = null;
180 StringBuffer buf = new StringBuffer();
181 for (int i = 0; i < sourceFiles.length; i++) {
182 String last4 = sourceFiles[i]
183 .substring(sourceFiles[i].length() - 4).toLowerCase();
184 if (last4.equals(".def")) {
185 defFile = quoteFilename(buf, sourceFiles[i]);
186 } else {
187 if (last4.equals(".res")) {
188 resFiles.addElement(quoteFilename(buf, sourceFiles[i]));
189 } else {
190 if (last4.equals(".lib")) {
191 libFiles.addElement(quoteFilename(buf, sourceFiles[i]));
192 } else {
193 execArgs.addElement(quoteFilename(buf, sourceFiles[i]));
194 }
195 }
196 }
197 }
198 //
199 // output file name
200 //
201 String outputFileName = new File(outputDir, outputName).toString();
202 execArgs.addElement("," + quoteFilename(buf, outputFileName));
203 if (config.getMap()) {
204 int lastPeriod = outputFileName.lastIndexOf('.');
205 String mapName;
206 if (lastPeriod < outputFileName.length() - 4) {
207 mapName = outputFileName + ".map";
208 } else {
209 mapName = outputFileName.substring(0, lastPeriod) + ".map";
210 }
211 execArgs.addElement("," + quoteFilename(buf, mapName) + ",");
212 } else {
213 execArgs.addElement(",,");
214 }
215 //
216 // add all the libraries
217 //
218 Enumeration libEnum = libFiles.elements();
219 boolean hasImport32 = false;
220 boolean hasCw32 = false;
221 while (libEnum.hasMoreElements()) {
222 String libName = (String) libEnum.nextElement();
223 if (libName.equalsIgnoreCase("import32.lib")) {
224 hasImport32 = true;
225 }
226 if (libName.equalsIgnoreCase("cw32.lib")) {
227 hasImport32 = true;
228 }
229 execArgs.addElement(quoteFilename(buf, libName));
230 }
231 if (!hasCw32) {
232 execArgs.addElement(quoteFilename(buf, "cw32.lib"));
233 }
234 if (!hasImport32) {
235 execArgs.addElement(quoteFilename(buf, "import32.lib"));
236 }
237 if (defFile == null) {
238 execArgs.addElement(",,");
239 } else {
240 execArgs.addElement("," + quoteFilename(buf, defFile) + ",");
241 }
242 Enumeration resEnum = resFiles.elements();
243 while (resEnum.hasMoreElements()) {
244 String resName = (String) resEnum.nextElement();
245 execArgs.addElement(quoteFilename(buf, resName));
246 }
247 String[] execArguments = new String[execArgs.size()];
248 execArgs.copyInto(execArguments);
249 return execArguments;
250 }
251 /**
252 * Prepares argument list to execute the linker using a response file.
253 *
254 * @param outputFile
255 * linker output file
256 * @param args
257 * output of prepareArguments
258 * @return arguments for runTask
259 */
260 protected String[] prepareResponseFile(File outputFile, String[] args)
261 throws IOException {
262 return BorlandProcessor.prepareResponseFile(outputFile, args, " + \n");
263 }
264 }