summaryrefslogtreecommitdiffstats
path: root/src/net/sf/antcontrib/cpptasks/mozilla/XpidlCompiler.java
blob: 32eb9ec14d66da4636ae2beb9a83d4d22e939dbb (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 *
 * Copyright 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.mozilla;

import java.io.File;
import java.util.Vector;

import net.sf.antcontrib.cpptasks.CCTask;
import net.sf.antcontrib.cpptasks.CUtil;
import net.sf.antcontrib.cpptasks.OptimizationEnum;
import net.sf.antcontrib.cpptasks.compiler.CommandLineCompiler;
import net.sf.antcontrib.cpptasks.compiler.CommandLineCompilerConfiguration;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
import net.sf.antcontrib.cpptasks.compiler.Linker;
import net.sf.antcontrib.cpptasks.compiler.Processor;
import net.sf.antcontrib.cpptasks.compiler.ProgressMonitor;
import net.sf.antcontrib.cpptasks.gcc.LdLinker;
import net.sf.antcontrib.cpptasks.parser.CParser;
import net.sf.antcontrib.cpptasks.parser.Parser;
import net.sf.antcontrib.cpptasks.VersionInfo;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Environment;

/**
 * Adapter for the Mozilla Xpidl Compiler.
 *
 * @author Curt Arnold
 */
public final class XpidlCompiler
    extends CommandLineCompiler {
  /**
   * Singleton instance.
   */
  private static final XpidlCompiler INSTANCE = new XpidlCompiler(
      false, null);
  /**
   * Gets singleton instance of compiler.
   * @return XpidlCompiler singleton instance
   */
  public static XpidlCompiler getInstance() {
    return INSTANCE;
  }

  /**
   * Constructor.
   * @param newEnvironment boolean establish an new environment.
   * @param env Environment environment.
   */
  private XpidlCompiler(final boolean newEnvironment,
                        final Environment env) {
    super("xpidl", null, new String[] {".idl", ".xpidl"}
          , new String[0], ".xpt", false, null, newEnvironment, env);
  }

  /**
   * Add arguments for debug, etc.
   * @param args Vector command argument list
   * @param debug boolean build for debug if true
   * @param multithreaded boolean build for multithreading if true
   * @param exceptions boolean enable exceptions if true
   * @param linkType LinkType output and runtime type
   * @param rtti Boolean enable run-time type identification if true
   * @param optimization OptimizationEnum optimization
   */
  protected void addImpliedArgs(final Vector args,
                                final boolean debug,
                                final boolean multithreaded,
                                final boolean exceptions,
                                final LinkType linkType,
                                final Boolean rtti,
                                final OptimizationEnum optimization) {
  }

  /**
   * Add arguments for specified warning level.
   * @param args Vector command line arguments
   * @param level int warning level value
   */
  protected void addWarningSwitch(final Vector args,
                                  final int level) {
  }

  /**
   * Change enviroment (deprecated).
   * @param newEnvironment boolean use new environment.
   * @param env Environment environment
   * @return Processor modified processor
   */
  public Processor changeEnvironment(final boolean newEnvironment,
                                     final Environment env) {
    return this;
  }

  /**
   * Gets dependency parser.
   * @param source source file
   * @return parser
   */
  protected Parser createParser(final File source) {
    return new CParser();
  }

  /**
   * Gets number of command line arguments per input file.
   * @return int number of command line arguments per input file.
   */
  protected int getArgumentCountPerInputFile() {
    return 3;
  }

  /**
   * Gets output file names.
   * @param inputFile String input file name
   * @param versionInfo version info, not used by this compiler.
   * @return String[] output file names
   */
  public String[] getOutputFileNames(final String inputFile,
                                     final VersionInfo versionInfo) {
    //
    //  if a recognized input file
    //
    String baseName = getBaseOutputName(inputFile);
    return new String[] {
        baseName + ".xpt",
        baseName + ".h"};
  }

  /**
   * Gets input file arguments.
   * @param outputDir File output directory
   * @param filename String input file name.
   * @param index int argument index,
   *         0 to getNumberOfArgumentsPerInputFile() -1
   * @return String input file argument
   */
  protected String getInputFileArgument(final File outputDir,
                                        final String filename,
                                        final int index) {
    return "";
  }

  /**
   * Gets maximum length of command line.
   * @return int maximum length of command line
   */
  public int getMaximumCommandLength() {
    return 1024;
  }

  /**
   * Gets maximum number of input files processed per command.
   * @return int maximum number of input files processed per command.
   */
  protected int getMaximumInputFilesPerCommand() {
    return 1;
  }

  /**
   * Adds command line arguments for include paths.
   *
   * @param baseDirPath String base directory
   * @param includeDirs File[] include directories
   * @param args Vector command line arguments
   * @param relativeArgs Vector arguments for configuration identification
   * @param includePathId StringBuffer buffer for configuration identification
   */
  protected void addIncludes(final String baseDirPath,
                             final File[] includeDirs,
                             final Vector args,
                             final Vector relativeArgs,
                             final StringBuffer includePathId) {
    //
    //   requires space between switch and path
    //
    for (int i = 0; i < includeDirs.length; i++) {
      args.addElement("-I");
      args.addElement(includeDirs[i].getAbsolutePath());
      if (relativeArgs != null) {
        String relative = CUtil.getRelativePath(baseDirPath,
                                                includeDirs[i]);
        relativeArgs.addElement("-I");
        relativeArgs.addElement(relative);
        if (includePathId != null) {
          if (includePathId.length() == 0) {
            includePathId.append("-I ");
          } else {
            includePathId.append(" -I ");
          }
          includePathId.append(relative);
        }
      }
    }
  }

  /**
   * Gets include directory switch.
   * @param includeDir String include directory
   * @return String command switch to add specified directory to search path
   */
  protected String getIncludeDirSwitch(final String includeDir) {
    return "-I" + includeDir;
  }

  /**
   * Gets switch to define preprocessor macro.
   * @param buffer StringBuffer command line argument
   * @param define String macro name
   * @param value String macro value, may be null.
   */
  protected void getDefineSwitch(final StringBuffer buffer,
                                 final String define,
                                 final String value) {
  }

  /**
   * Gets switch to undefine preprocessor macro.
   * @param buffer StringBuffer command line argument
   * @param define String macro name
   */
  protected void getUndefineSwitch(final StringBuffer buffer,
                                   final String define) {
  }

  /**
   * Gets standard include paths.
   * @return File[] standard include paths
   */
  protected File[] getEnvironmentIncludePath() {
    return new File[0];
  }

  /**
   * Gets linker associated with this type.
   * @param type LinkType linker, returns ld.
   * @return Linker
   */
  public Linker getLinker(final LinkType type) {
    return LdLinker.getInstance();
  }

  /**
   * Compiles an .idl file into the corresponding .h and .xpt files.
   * @param task current cc task
   * @param outputDir output directory
   * @param sourceFiles source files
   * @param args command line arguments that appear before input files
   * @param endArgs command line arguments that appear after input files
   * @param relentless if true, do not stop at first compilation error
   * @param config compiler configuration
   * @param monitor progress monitor
   */
  public void compile(final CCTask task,
                      final File outputDir,
                      final String[] sourceFiles,
                      final String[] args,
                      final String[] endArgs,
                      final boolean relentless,
                      final CommandLineCompilerConfiguration config,
                      final ProgressMonitor monitor)  {

    BuildException exc = null;
    String[] thisSource = new String[1];
    String[] tlbCommand = new String[args.length + endArgs.length + 6];
    tlbCommand[0] = "xpidl";
    tlbCommand[1] = "-m";
    tlbCommand[2] = "typelib";
    String[] headerCommand = new String[args.length + endArgs.length + 6];
    headerCommand[0] = "xpidl";
    headerCommand[1] = "-m";
    headerCommand[2] = "header";
    for (int i = 0; i < args.length; i++) {
      tlbCommand[i + 3] = args[i];
      headerCommand[i + 3] = args[i];
    }
    tlbCommand[args.length + 3] = "-e";
    headerCommand[args.length + 3] = "-e";

    int tlbIndex = args.length + 6;
    int headerIndex = args.length + 6;
    for (int i = 0; i < endArgs.length; i++) {
      tlbCommand[tlbIndex++] = endArgs[i];
      headerCommand[headerIndex++] = endArgs[i];
    }
    for (int j = 0; j < sourceFiles.length; j++) {
      tlbIndex = args.length + 4;
      headerIndex = args.length + 4;
      String[] outputFileNames = getOutputFileNames(sourceFiles[j], null);

      tlbCommand[tlbIndex++] = outputFileNames[0];
      tlbCommand[tlbIndex++] = sourceFiles[j];

      headerCommand[headerIndex++] = outputFileNames[1];
      headerCommand[headerIndex++] = sourceFiles[j];

      int retval = runCommand(task, outputDir, tlbCommand);
      if (retval == 0) {
        retval = runCommand(task, outputDir, headerCommand);
      }
      if (monitor != null) {
        thisSource[0] = sourceFiles[j];
        monitor.progress(thisSource);
      }
      //
      //   if the process returned a failure code and
      //      we aren't holding an exception from an earlier
      //      interation
      if (retval != 0 && exc == null) {
        //
        //   construct the exception
        //
        exc = new BuildException(this.getCommand()
                                 + " failed with return code " + retval, task
                                 .getLocation());
        //
        //   and throw it now unless we are relentless
        //
        if (!relentless) {
          throw exc;
        }
      }
    }
    //
    //   if the compiler returned a failure value earlier
    //      then throw an exception
    if (exc != null) {
      throw exc;
    }
  }

  /**
   * Get total command line length due to the input file.
   * @param outputDir File output directory
   * @param inputFile String input file
   * @return int characters added to command line for the input file.
   */
  protected int getTotalArgumentLengthForInputFile(
      final File outputDir,
      final String inputFile) {
    return 0;
  }

  /**
   * Gets compiler identifier.
   * @return String compiler identification string
   */
  public String getIdentifier() {
    return "Mozilla xpidl";
  }

}