View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.borland;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.FileWriter;
26  import java.io.IOException;
27  import java.io.Reader;
28  import java.util.Vector;
29  
30  import com.github.maven_nar.cpptasks.CUtil;
31  import com.github.maven_nar.cpptasks.types.LibraryTypeEnum;
32  
33  /**
34   * A add-in class for Borland(r) processor adapters
35   *
36   * 
37   */
38  public final class BorlandProcessor {
39    public static void addWarningSwitch(final Vector<String> args, final int level) {
40      switch (level) {
41        case 0:
42          args.addElement("-w-");
43          break;
44        case 5:
45          args.addElement("-w!");
46          break;
47        default:
48          args.addElement("-w");
49          break;
50      }
51    }
52  
53    public static void getDefineSwitch(final StringBuffer buffer, final String define, final 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    /**
63     * This method extracts path information from the appropriate .cfg file in
64     * the install directory.
65     * 
66     * @param toolName
67     *          Tool name, for example, "bcc32", "brc32", "ilink32"
68     * @param switchChar
69     *          Command line switch character, for example "L" for libraries
70     * @param defaultRelativePath
71     *          default path relative to executable directory
72     * @return path
73     */
74    public static File[] getEnvironmentPath(final String toolName, final char switchChar,
75        final String[] defaultRelativePath) {
76      if (toolName == null) {
77        throw new NullPointerException("toolName");
78      }
79      if (defaultRelativePath == null) {
80        throw new NullPointerException("defaultRelativePath");
81      }
82      String[] path = defaultRelativePath;
83      File exeDir = CUtil.getExecutableLocation(toolName + ".exe");
84      if (exeDir != null) {
85        final File cfgFile = new File(exeDir, toolName + ".cfg");
86        if (cfgFile.exists()) {
87          try {
88            final Reader reader = new BufferedReader(new FileReader(cfgFile));
89            final BorlandCfgParser cfgParser = new BorlandCfgParser(switchChar);
90            path = cfgParser.parsePath(reader);
91            reader.close();
92          } catch (final 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       final File[] culled = new File[resourcePath.length - nonExistant];
126       int index = 0;
127       for (final File element : resourcePath) {
128         if (element != null) {
129           culled[index++] = element;
130         }
131       }
132       resourcePath = culled;
133     }
134     return resourcePath;
135   }
136 
137   public static String getIncludeDirSwitch(final String includeOption, final String includeDir) {
138     final StringBuffer buf = new StringBuffer(includeOption);
139     quoteFile(buf, includeDir);
140     return buf.toString();
141   }
142 
143   public static String[] getLibraryPatterns(final String[] libnames, final LibraryTypeEnum libType) {
144     final StringBuffer buf = new StringBuffer();
145     final String[] patterns = new String[libnames.length];
146     for (int i = 0; i < libnames.length; i++) {
147       buf.setLength(0);
148       buf.append(libnames[i]);
149       buf.append(".lib");
150       patterns[i] = buf.toString();
151     }
152     return patterns;
153   }
154 
155   public static String[] getOutputFileSwitch(final String outFile) {
156     return new String[0];
157   }
158 
159   public static void getUndefineSwitch(final StringBuffer buffer, final String define) {
160     buffer.append("-U");
161     buffer.append(define);
162   }
163 
164   public static boolean isCaseSensitive() {
165     return false;
166   }
167 
168   /**
169    * Prepares argument list to execute the linker using a response file.
170    * 
171    * @param outputFile
172    *          linker output file
173    * @param args
174    *          output of prepareArguments
175    * @return arguments for runTask
176    */
177   public static String[] prepareResponseFile(final File outputFile, final String[] args, final String continuation)
178       throws IOException {
179     final String baseName = outputFile.getName();
180     final File commandFile = new File(outputFile.getParent(), baseName + ".rsp");
181     final FileWriter writer = new FileWriter(commandFile);
182     for (int i = 1; i < args.length - 1; i++) {
183       writer.write(args[i]);
184       //
185       // if either the current argument ends with
186       // or next argument starts with a comma then
187       // don't split the line
188       if (args[i].endsWith(",") || args[i + 1].startsWith(",")) {
189         writer.write(' ');
190       } else {
191         //
192         // split the line to make it more readable
193         //
194         writer.write(continuation);
195       }
196     }
197     //
198     // write the last argument
199     //
200     if (args.length > 1) {
201       writer.write(args[args.length - 1]);
202     }
203     writer.close();
204     final String[] execArgs = new String[2];
205     execArgs[0] = args[0];
206     //
207     // left for the caller to decorate
208     execArgs[1] = commandFile.toString();
209     return execArgs;
210   }
211 
212   public static void quoteFile(final StringBuffer buf, final String outPath) {
213     if (outPath.charAt(0) != '\"'
214         && (outPath.indexOf(' ') >= 0 || outPath.indexOf('-') >= 0 || outPath.indexOf('/') >= 0)) {
215       buf.append('\"');
216       buf.append(outPath);
217       buf.append('\"');
218     } else {
219       buf.append(outPath);
220     }
221   }
222 
223   private BorlandProcessor() {
224   }
225 }