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.openwatcom;
21  
22  import java.io.File;
23  import java.util.Vector;
24  
25  import com.github.maven_nar.cpptasks.CCTask;
26  import com.github.maven_nar.cpptasks.CUtil;
27  import com.github.maven_nar.cpptasks.compiler.CommandLineLinker;
28  import com.github.maven_nar.cpptasks.compiler.CommandLineLinkerConfiguration;
29  import com.github.maven_nar.cpptasks.compiler.LinkType;
30  import com.github.maven_nar.cpptasks.compiler.Linker;
31  import com.github.maven_nar.cpptasks.types.LibraryTypeEnum;
32  
33  /**
34   * Adapter for the OpenWatcom Librarian.
35   *
36   * @author Curt Arnold
37   */
38  public final class OpenWatcomLibrarian extends CommandLineLinker {
39    /**
40     * Singleton.
41     */
42    private static final OpenWatcomLibrarian INSTANCE = new OpenWatcomLibrarian();
43  
44    /**
45     * Singleton accessor.
46     * 
47     * @return OpenWatcomLibrarian librarian instance
48     */
49    public static OpenWatcomLibrarian getInstance() {
50      return INSTANCE;
51    }
52  
53    /**
54     * Constructor.
55     */
56    private OpenWatcomLibrarian() {
57      super("wlib", null, new String[] {
58        ".obj"
59      }, new String[0], ".lib", false, null);
60    }
61  
62    /**
63     * Add base address.
64     * 
65     * @param base
66     *          long base address
67     * @param args
68     *          Vector command line arguments
69     */
70    protected void addBase(final long base, final Vector<String> args) {
71    }
72  
73    /**
74     * Add alternative entry point.
75     * 
76     * @param entry
77     *          String entry point
78     * @param args
79     *          Vector command line arguments
80     */
81    protected void addEntry(final String entry, final Vector<String> args) {
82    }
83  
84    /**
85     * Add fixed parameter.
86     * 
87     * @param fixed
88     *          Boolean true if fixed
89     * @param args
90     *          Vector command line arguments
91     */
92    protected void addFixed(final Boolean fixed, final Vector<String> args) {
93    }
94  
95    /**
96     * Add implied arguments.
97     * 
98     * @param debug
99     *          boolean true if debugging
100    * @param linkType
101    *          LinkType link type
102    * @param args
103    *          Vector command line arguments
104    */
105   protected void addImpliedArgs(final boolean debug, final LinkType linkType, final Vector<String> args) {
106   }
107 
108   /**
109    * Add incremental option.
110    * 
111    * @param incremental
112    *          boolean true if incremental
113    * @param args
114    *          Vector command line arguments
115    */
116   protected void addIncremental(final boolean incremental, final Vector<String> args) {
117   }
118 
119   /**
120    * Add map option.
121    * 
122    * @param map
123    *          boolean true to create map file
124    * @param args
125    *          Vector command line argument
126    */
127   protected void addMap(final boolean map, final Vector<String> args) {
128   }
129 
130   /**
131    * Add stack size option.
132    * 
133    * @param stack
134    *          int stack size
135    * @param args
136    *          Vector command line arguments
137    */
138   protected void addStack(final int stack, final Vector<String> args) {
139   }
140 
141   /**
142    * Get command file switch.
143    * 
144    * @param cmdFile
145    *          String command file
146    * @return String command file switch
147    */
148   @Override
149   protected String getCommandFileSwitch(final String cmdFile) {
150     return OpenWatcomProcessor.getCommandFileSwitch(cmdFile);
151   }
152 
153   /**
154    * Get library search path.
155    * 
156    * @return File[] library search path
157    */
158   @Override
159   public File[] getLibraryPath() {
160     return CUtil.getPathFromEnvironment("LIB", ";");
161   }
162 
163   /**
164    * Get file selectors for specified library names.
165    * 
166    * @param libnames
167    *          String[] library names
168    * @param libType
169    *          LibraryTypeEnum library type enum
170    * @return String[] file selection patterns
171    */
172   @Override
173   public String[] getLibraryPatterns(final String[] libnames, final LibraryTypeEnum libType) {
174     return OpenWatcomProcessor.getLibraryPatterns(libnames, libType);
175   }
176 
177   /**
178    * Get linker.
179    * 
180    * @param type
181    *          LinkType link type
182    * @return Linker linker
183    */
184   @Override
185   public Linker getLinker(final LinkType type) {
186     return OpenWatcomCLinker.getInstance().getLinker(type);
187   }
188 
189   /**
190    * Gets maximum command line.
191    * 
192    * @return int maximum command line
193    */
194   @Override
195   public int getMaximumCommandLength() {
196     return 1024;
197   }
198 
199   /**
200    * Create output file switch.
201    * 
202    * @param outFile
203    *          String output file switch
204    * @return String[] output file switch
205    */
206   @Override
207   public String[] getOutputFileSwitch(final String outFile) {
208     return OpenWatcomProcessor.getOutputFileSwitch(outFile);
209   }
210 
211   /**
212    * Gets case-sensisitivity of processor.
213    * 
214    * @return boolean true if case sensitive
215    */
216   @Override
217   public boolean isCaseSensitive() {
218     return OpenWatcomProcessor.isCaseSensitive();
219   }
220 
221   /**
222    * Builds a library.
223    * 
224    * @param task
225    *          task
226    * @param outputFile
227    *          generated library
228    * @param sourceFiles
229    *          object files
230    * @param config
231    *          linker configuration
232    */
233   @Override
234   public void link(final CCTask task, final File outputFile, final String[] sourceFiles,
235       final CommandLineLinkerConfiguration config) {
236     //
237     // delete any existing library
238     outputFile.delete();
239     //
240     // build a new library
241     super.link(task, outputFile, sourceFiles, config);
242   }
243 
244   /**
245    * Prepares argument list for exec command.
246    * 
247    * @param task
248    *          task
249    * @param outputDir
250    *          output directory
251    * @param outputName
252    *          output file name
253    * @param sourceFiles
254    *          object files
255    * @param config
256    *          linker configuration
257    * @return arguments for runTask
258    */
259   @Override
260   protected String[] prepareArguments(final CCTask task, final String outputDir, final String outputName,
261       final String[] sourceFiles, final CommandLineLinkerConfiguration config) {
262     final String[] preargs = config.getPreArguments();
263     final String[] endargs = config.getEndArguments();
264     final StringBuffer buf = new StringBuffer();
265     final Vector<String> execArgs = new Vector<>(preargs.length + endargs.length + 10 + sourceFiles.length);
266 
267     execArgs.addElement(this.getCommand());
268     final String outputFileName = new File(outputDir, outputName).toString();
269     execArgs.addElement(quoteFilename(buf, outputFileName));
270 
271     for (final String prearg : preargs) {
272       execArgs.addElement(prearg);
273     }
274 
275     int objBytes = 0;
276 
277     for (final String sourceFile : sourceFiles) {
278       final String last4 = sourceFile.substring(sourceFile.length() - 4).toLowerCase();
279       if (!last4.equals(".def") && !last4.equals(".res") && !last4.equals(".lib")) {
280         execArgs.addElement("+" + quoteFilename(buf, sourceFile));
281         objBytes += new File(sourceFile).length();
282       }
283     }
284 
285     for (final String endarg : endargs) {
286       execArgs.addElement(endarg);
287     }
288 
289     final String[] execArguments = new String[execArgs.size()];
290     execArgs.copyInto(execArguments);
291 
292     return execArguments;
293   }
294 
295 }