summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/borland/BorlandProcessor.java
blob: d53f00cc12cf2740b9e0d6370e5be281611c9802 (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
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * 
 * Copyright 2002-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.borland;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Vector;
import java.io.FileWriter;

import net.sf.antcontrib.cpptasks.CUtil;
import net.sf.antcontrib.cpptasks.types.LibraryTypeEnum;
/**
 * A add-in class for Borland(r) processor adapters
 * 
 *  
 */
public final class BorlandProcessor {
    public static void addWarningSwitch(Vector args, int level) {
        switch (level) {
            case 0 :
                args.addElement("-w-");
                break;
            case 5 :
                args.addElement("-w!");
                break;
            default :
                args.addElement("-w");
                break;
        }
    }
    public static String getCommandFileSwitch(String cmdFile) {
        StringBuffer buf = new StringBuffer("@");
        quoteFile(buf, cmdFile);
        return buf.toString();
    }
    public static void getDefineSwitch(StringBuffer buffer, String define,
            String value) {
        buffer.append("-D");
        buffer.append(define);
        if (value != null && value.length() > 0) {
            buffer.append('=');
            buffer.append(value);
        }
    }
    /**
     * This method extracts path information from the appropriate .cfg file in
     * the install directory.
     * 
     * @param toolName
     *            Tool name, for example, "bcc32", "brc32", "ilink32"
     * @param switchChar
     *            Command line switch character, for example "L" for libraries
     * @param defaultRelativePaths
     *            default paths relative to executable directory
     * @return path
     */
    public static File[] getEnvironmentPath(String toolName, char switchChar,
            String[] defaultRelativePath) {
        if (toolName == null) {
            throw new NullPointerException("toolName");
        }
        if (defaultRelativePath == null) {
            throw new NullPointerException("defaultRelativePath");
        }
        String[] path = defaultRelativePath;
        File exeDir = CUtil.getExecutableLocation(toolName + ".exe");
        if (exeDir != null) {
            File cfgFile = new File(exeDir, toolName + ".cfg");
            if (cfgFile.exists()) {
                try {
                    Reader reader = new BufferedReader(new FileReader(cfgFile));
                    BorlandCfgParser cfgParser = new BorlandCfgParser(
                            switchChar);
                    path = cfgParser.parsePath(reader);
                    reader.close();
                } catch (IOException ex) {
                    //
                    //  could be logged
                    //
                }
            }
        } else {
            //
            //  if can't find the executable,
            //     assume current directory to resolve relative paths
            //
            exeDir = new File(System.getProperty("user.dir"));
        }
        int nonExistant = 0;
        File[] resourcePath = new File[path.length];
        for (int i = 0; i < path.length; i++) {
            resourcePath[i] = new File(path[i]);
            if (!resourcePath[i].isAbsolute()) {
                resourcePath[i] = new File(exeDir, path[i]);
            }
            //
            //  if any of the entries do not exist or are
            //     not directories, null them out
            if (!(resourcePath[i].exists() && resourcePath[i].isDirectory())) {
                resourcePath[i] = null;
                nonExistant++;
            }
        }
        //
        //  if there were some non-existant or non-directory
        //    entries in the configuration file then
        //    create a shorter array
        if (nonExistant > 0) {
            File[] culled = new File[resourcePath.length - nonExistant];
            int index = 0;
            for (int i = 0; i < resourcePath.length; i++) {
                if (resourcePath[i] != null) {
                    culled[index++] = resourcePath[i];
                }
            }
            resourcePath = culled;
        }
        return resourcePath;
    }
    public static String getIncludeDirSwitch(String includeOption,
            String includeDir) {
        StringBuffer buf = new StringBuffer(includeOption);
        quoteFile(buf, includeDir);
        return buf.toString();
    }
    public static String[] getLibraryPatterns(String[] libnames, LibraryTypeEnum libType) {
        StringBuffer buf = new StringBuffer();
        String[] patterns = new String[libnames.length];
        for (int i = 0; i < libnames.length; i++) {
            buf.setLength(0);
            buf.append(libnames[i]);
            buf.append(".lib");
            patterns[i] = buf.toString();
        }
        return patterns;
    }
    public static String[] getOutputFileSwitch(String outFile) {
        return new String[0];
    }
    public static void getUndefineSwitch(StringBuffer buffer, String define) {
        buffer.append("-U");
        buffer.append(define);
    }
    public static boolean isCaseSensitive() {
        return false;
    }
    private static void quoteFile(StringBuffer buf, String outPath) {
        if (outPath.indexOf(' ') >= 0) {
            buf.append('\"');
            buf.append(outPath);
            buf.append('\"');
        } else {
            buf.append(outPath);
        }
    }
    
    /**
     * Prepares argument list to execute the linker using a response file.
     * 
     * @param outputFile
     *            linker output file
     * @param args
     *            output of prepareArguments
     * @return arguments for runTask
     */
    public static String[] prepareResponseFile(File outputFile, 
    		String[] args,
			String continuation)
            throws IOException {
        String baseName = outputFile.getName();
        File commandFile = new File(outputFile.getParent(), baseName + ".lnk");
        FileWriter writer = new FileWriter(commandFile);
        for (int i = 1; i < args.length - 1; i++) {
            writer.write(args[i]);
            //
            //  if either the current argument ends with
            //     or next argument starts with a comma then
            //      don't split the line
            if (args[i].endsWith(",") || args[i + 1].startsWith(",")) {
                writer.write(' ');
            } else {
                //
                //  split the line to make it more readable
                //
                writer.write(continuation);
            }
        }
        //
        //  write the last argument
        //
        if (args.length > 1) {
            writer.write(args[args.length - 1]);
        }
        writer.close();
        String[] execArgs = new String[2];
        execArgs[0] = args[0];
        execArgs[1] = getCommandFileSwitch(commandFile.toString());
        return execArgs;
    }
    
    private BorlandProcessor() {
    }
}