]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/borland/BorlandProcessor.java
Initial import.
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / borland / BorlandProcessor.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.BufferedReader;
19 import java.io.File;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.Vector;
24 import java.io.FileWriter;
25
26 import net.sf.antcontrib.cpptasks.CUtil;
27 import net.sf.antcontrib.cpptasks.types.LibraryTypeEnum;
28 /**
29 * A add-in class for Borland(r) processor adapters
30 *
31 *
32 */
33 public final class BorlandProcessor {
34 public static void addWarningSwitch(Vector args, int level) {
35 switch (level) {
36 case 0 :
37 args.addElement("-w-");
38 break;
39 case 5 :
40 args.addElement("-w!");
41 break;
42 default :
43 args.addElement("-w");
44 break;
45 }
46 }
47 public static String getCommandFileSwitch(String cmdFile) {
48 StringBuffer buf = new StringBuffer("@");
49 quoteFile(buf, cmdFile);
50 return buf.toString();
51 }
52 public static void getDefineSwitch(StringBuffer buffer, String define,
53 String value) {
54 buffer.append("-D");
55 buffer.append(define);
56 if (value != null && value.length() > 0) {
57 buffer.append('=');
58 buffer.append(value);
59 }
60 }
61 /**
62 * This method extracts path information from the appropriate .cfg file in
63 * the install directory.
64 *
65 * @param toolName
66 * Tool name, for example, "bcc32", "brc32", "ilink32"
67 * @param switchChar
68 * Command line switch character, for example "L" for libraries
69 * @param defaultRelativePaths
70 * default paths relative to executable directory
71 * @return path
72 */
73 public static File[] getEnvironmentPath(String toolName, char switchChar,
74 String[] defaultRelativePath) {
75 if (toolName == null) {
76 throw new NullPointerException("toolName");
77 }
78 if (defaultRelativePath == null) {
79 throw new NullPointerException("defaultRelativePath");
80 }
81 String[] path = defaultRelativePath;
82 File exeDir = CUtil.getExecutableLocation(toolName + ".exe");
83 if (exeDir != null) {
84 File cfgFile = new File(exeDir, toolName + ".cfg");
85 if (cfgFile.exists()) {
86 try {
87 Reader reader = new BufferedReader(new FileReader(cfgFile));
88 BorlandCfgParser cfgParser = new BorlandCfgParser(
89 switchChar);
90 path = cfgParser.parsePath(reader);
91 reader.close();
92 } catch (IOException ex) {
93 //
94 // could be logged
95 //
96 }
97 }
98 } else {
99 //
100 // if can't find the executable,
101 // assume current directory to resolve relative paths
102 //
103 exeDir = new File(System.getProperty("user.dir"));
104 }
105 int nonExistant = 0;
106 File[] resourcePath = new File[path.length];
107 for (int i = 0; i < path.length; i++) {
108 resourcePath[i] = new File(path[i]);
109 if (!resourcePath[i].isAbsolute()) {
110 resourcePath[i] = new File(exeDir, path[i]);
111 }
112 //
113 // if any of the entries do not exist or are
114 // not directories, null them out
115 if (!(resourcePath[i].exists() && resourcePath[i].isDirectory())) {
116 resourcePath[i] = null;
117 nonExistant++;
118 }
119 }
120 //
121 // if there were some non-existant or non-directory
122 // entries in the configuration file then
123 // create a shorter array
124 if (nonExistant > 0) {
125 File[] culled = new File[resourcePath.length - nonExistant];
126 int index = 0;
127 for (int i = 0; i < resourcePath.length; i++) {
128 if (resourcePath[i] != null) {
129 culled[index++] = resourcePath[i];
130 }
131 }
132 resourcePath = culled;
133 }
134 return resourcePath;
135 }
136 public static String getIncludeDirSwitch(String includeOption,
137 String includeDir) {
138 StringBuffer buf = new StringBuffer(includeOption);
139 quoteFile(buf, includeDir);
140 return buf.toString();
141 }
142 public static String[] getLibraryPatterns(String[] libnames, LibraryTypeEnum libType) {
143 StringBuffer buf = new StringBuffer();
144 String[] patterns = new String[libnames.length];
145 for (int i = 0; i < libnames.length; i++) {
146 buf.setLength(0);
147 buf.append(libnames[i]);
148 buf.append(".lib");
149 patterns[i] = buf.toString();
150 }
151 return patterns;
152 }
153 public static String[] getOutputFileSwitch(String outFile) {
154 return new String[0];
155 }
156 public static void getUndefineSwitch(StringBuffer buffer, String define) {
157 buffer.append("-U");
158 buffer.append(define);
159 }
160 public static boolean isCaseSensitive() {
161 return false;
162 }
163 private static void quoteFile(StringBuffer buf, String outPath) {
164 if (outPath.indexOf(' ') >= 0) {
165 buf.append('\"');
166 buf.append(outPath);
167 buf.append('\"');
168 } else {
169 buf.append(outPath);
170 }
171 }
172
173 /**
174 * Prepares argument list to execute the linker using a response file.
175 *
176 * @param outputFile
177 * linker output file
178 * @param args
179 * output of prepareArguments
180 * @return arguments for runTask
181 */
182 public static String[] prepareResponseFile(File outputFile,
183 String[] args,
184 String continuation)
185 throws IOException {
186 String baseName = outputFile.getName();
187 File commandFile = new File(outputFile.getParent(), baseName + ".lnk");
188 FileWriter writer = new FileWriter(commandFile);
189 for (int i = 1; i < args.length - 1; i++) {
190 writer.write(args[i]);
191 //
192 // if either the current argument ends with
193 // or next argument starts with a comma then
194 // don't split the line
195 if (args[i].endsWith(",") || args[i + 1].startsWith(",")) {
196 writer.write(' ');
197 } else {
198 //
199 // split the line to make it more readable
200 //
201 writer.write(continuation);
202 }
203 }
204 //
205 // write the last argument
206 //
207 if (args.length > 1) {
208 writer.write(args[args.length - 1]);
209 }
210 writer.close();
211 String[] execArgs = new String[2];
212 execArgs[0] = args[0];
213 execArgs[1] = getCommandFileSwitch(commandFile.toString());
214 return execArgs;
215 }
216
217 private BorlandProcessor() {
218 }
219 }