3 * Copyright 2002-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
.borland
;
18 import java
.io
.BufferedReader
;
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
;
26 import net
.sf
.antcontrib
.cpptasks
.CUtil
;
27 import net
.sf
.antcontrib
.cpptasks
.types
.LibraryTypeEnum
;
29 * A add-in class for Borland(r) processor adapters
33 public final class BorlandProcessor
{
34 public static void addWarningSwitch(Vector args
, int level
) {
37 args
.addElement("-w-");
40 args
.addElement("-w!");
43 args
.addElement("-w");
47 public static String
getCommandFileSwitch(String cmdFile
) {
48 StringBuffer buf
= new StringBuffer("@");
49 quoteFile(buf
, cmdFile
);
50 return buf
.toString();
52 public static void getDefineSwitch(StringBuffer buffer
, String define
,
55 buffer
.append(define
);
56 if (value
!= null && value
.length() > 0) {
62 * This method extracts path information from the appropriate .cfg file in
63 * the install directory.
66 * Tool name, for example, "bcc32", "brc32", "ilink32"
68 * Command line switch character, for example "L" for libraries
69 * @param defaultRelativePaths
70 * default paths relative to executable directory
73 public static File
[] getEnvironmentPath(String toolName
, char switchChar
,
74 String
[] defaultRelativePath
) {
75 if (toolName
== null) {
76 throw new NullPointerException("toolName");
78 if (defaultRelativePath
== null) {
79 throw new NullPointerException("defaultRelativePath");
81 String
[] path
= defaultRelativePath
;
82 File exeDir
= CUtil
.getExecutableLocation(toolName
+ ".exe");
84 File cfgFile
= new File(exeDir
, toolName
+ ".cfg");
85 if (cfgFile
.exists()) {
87 Reader reader
= new BufferedReader(new FileReader(cfgFile
));
88 BorlandCfgParser cfgParser
= new BorlandCfgParser(
90 path
= cfgParser
.parsePath(reader
);
92 } catch (IOException ex
) {
100 // if can't find the executable,
101 // assume current directory to resolve relative paths
103 exeDir
= new File(System
.getProperty("user.dir"));
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
]);
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;
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
];
127 for (int i
= 0; i
< resourcePath
.length
; i
++) {
128 if (resourcePath
[i
] != null) {
129 culled
[index
++] = resourcePath
[i
];
132 resourcePath
= culled
;
136 public static String
getIncludeDirSwitch(String includeOption
,
138 StringBuffer buf
= new StringBuffer(includeOption
);
139 quoteFile(buf
, includeDir
);
140 return buf
.toString();
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
++) {
147 buf
.append(libnames
[i
]);
149 patterns
[i
] = buf
.toString();
153 public static String
[] getOutputFileSwitch(String outFile
) {
154 return new String
[0];
156 public static void getUndefineSwitch(StringBuffer buffer
, String define
) {
158 buffer
.append(define
);
160 public static boolean isCaseSensitive() {
163 private static void quoteFile(StringBuffer buf
, String outPath
) {
164 if (outPath
.indexOf(' ') >= 0) {
174 * Prepares argument list to execute the linker using a response file.
179 * output of prepareArguments
180 * @return arguments for runTask
182 public static String
[] prepareResponseFile(File outputFile
,
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
]);
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(",")) {
199 // split the line to make it more readable
201 writer
.write(continuation
);
205 // write the last argument
207 if (args
.length
> 1) {
208 writer
.write(args
[args
.length
- 1]);
211 String
[] execArgs
= new String
[2];
212 execArgs
[0] = args
[0];
213 execArgs
[1] = getCommandFileSwitch(commandFile
.toString());
217 private BorlandProcessor() {