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.trolltech;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.Reader;
27 import java.util.Vector;
28
29 import org.apache.tools.ant.types.Environment;
30
31 import com.github.maven_nar.cpptasks.OptimizationEnum;
32 import com.github.maven_nar.cpptasks.VersionInfo;
33 import com.github.maven_nar.cpptasks.compiler.CommandLineCompiler;
34 import com.github.maven_nar.cpptasks.compiler.LinkType;
35 import com.github.maven_nar.cpptasks.compiler.Linker;
36 import com.github.maven_nar.cpptasks.compiler.Processor;
37 import com.github.maven_nar.cpptasks.gcc.LdLinker;
38 import com.github.maven_nar.cpptasks.parser.CParser;
39 import com.github.maven_nar.cpptasks.parser.Parser;
40
41 /**
42 * Adapter for the Trolltech Qt MOC Compiler.
43 *
44 * @author Curt Arnold
45 */
46 public final class MetaObjectCompiler extends CommandLineCompiler {
47 /**
48 * Singleton instance.
49 */
50 private static final MetaObjectCompiler INSTANCE = new MetaObjectCompiler(false, null);
51
52 /**
53 * Gets singleton instance of compiler.
54 *
55 * @return MetaObjectCompiler singleton instance
56 */
57 public static MetaObjectCompiler getInstance() {
58 return INSTANCE;
59 }
60
61 /**
62 * Constructor.
63 *
64 * @param newEnvironment
65 * boolean establish an new environment.
66 * @param env
67 * Environment environment.
68 */
69 private MetaObjectCompiler(final boolean newEnvironment, final Environment env) {
70 super("moc", "-version", new String[] {
71 ".h", ".cpp"
72 }, new String[0], ".moc", false, null, newEnvironment, env);
73 }
74
75 /**
76 * Add arguments for debug, etc.
77 *
78 * @param args
79 * Vector command argument list
80 * @param debug
81 * boolean build for debug if true
82 * @param multithreaded
83 * boolean build for multithreading if true
84 * @param exceptions
85 * boolean enable exceptions if true
86 * @param linkType
87 * LinkType output and runtime type
88 * @param rtti
89 * Boolean enable run-time type identification if true
90 * @param optimization
91 * OptimizationEnum optimization
92 */
93 @Override
94 protected void addImpliedArgs(final Vector<String> args, final boolean debug, final boolean multithreaded,
95 final boolean exceptions, final LinkType linkType, final Boolean rtti, final OptimizationEnum optimization) {
96 }
97
98 /**
99 * Add arguments for specified warning level.
100 *
101 * @param args
102 * Vector command line arguments
103 * @param level
104 * int warning level value
105 */
106 @Override
107 protected void addWarningSwitch(final Vector<String> args, final int level) {
108 }
109
110 /**
111 * Returns the bid of the processor for the file.
112 *
113 * @param inputFile
114 * filename of input file
115 * @return bid for the file, 0 indicates no interest, 1 indicates that the
116 * processor recognizes the file but doesn't process it (header
117 * files, for example), 100 indicates strong interest
118 */
119 @Override
120 public int bid(final String inputFile) {
121 //
122 // get base bid
123 final int baseBid = super.bid(inputFile);
124 //
125 // if the base bid was non-zero (.h or .cpp extension)
126 //
127 if (baseBid > 0) {
128 //
129 // scan the file for Q_OBJECT
130 // skip file if not present
131 //
132 try {
133 final Reader reader = new BufferedReader(new FileReader(inputFile));
134 final boolean hasQObject = MetaObjectParser.hasQObject(reader);
135 reader.close();
136 if (hasQObject) {
137 return baseBid;
138 }
139 } catch (final IOException ex) {
140 return 0;
141 }
142 }
143 return 0;
144 }
145
146 /**
147 * Change enviroment (deprecated).
148 *
149 * @param newEnvironment
150 * boolean use new environment.
151 * @param env
152 * Environment environment
153 * @return Processor modified processor
154 */
155 @Override
156 public Processor changeEnvironment(final boolean newEnvironment, final Environment env) {
157 return this;
158 }
159
160 /**
161 * Gets a parser to scan source file for dependencies.
162 *
163 * @param source
164 * source file
165 * @return parser
166 */
167 @Override
168 protected Parser createParser(final File source) {
169 return new CParser();
170 }
171
172 /**
173 * Gets number of command line arguments per input file.
174 *
175 * @return int number of command line arguments per input file.
176 */
177 @Override
178 protected int getArgumentCountPerInputFile() {
179 return 3;
180 }
181
182 /**
183 * Gets switch to define preprocessor macro.
184 *
185 * @param buffer
186 * StringBuffer command line argument
187 * @param define
188 * String macro name
189 * @param value
190 * String macro value, may be null.
191 */
192 @Override
193 protected void getDefineSwitch(final StringBuffer buffer, final String define, final String value) {
194 }
195
196 /**
197 * Gets standard include paths.
198 *
199 * @return File[] standard include paths
200 */
201 @Override
202 protected File[] getEnvironmentIncludePath() {
203 return new File[0];
204 }
205
206 /**
207 * Gets include directory switch.
208 *
209 * @param includeDir
210 * String include directory
211 * @return String command switch to add specified directory to search path
212 */
213 @Override
214 protected String getIncludeDirSwitch(final String includeDir) {
215 return "";
216 }
217
218 /**
219 * Gets input file arguments.
220 *
221 * @param outputDir
222 * File output directory
223 * @param filename
224 * String input file name.
225 * @param index
226 * int argument index,
227 * 0 to getNumberOfArgumentsPerInputFile() -1
228 * @return String input file argument
229 */
230 @Override
231 protected String getInputFileArgument(final File outputDir, final String filename, final int index) {
232 switch (index) {
233 case 0:
234 return "-o";
235 case 1:
236 final String outputFileName = getOutputFileNames(filename, null)[0];
237 return new File(outputDir, outputFileName).toString();
238
239 case 2:
240 return filename;
241
242 default:
243 return null;
244 }
245 }
246
247 /**
248 * Gets linker associated with this type.
249 *
250 * @param type
251 * LinkType linker, returns ld.
252 * @return Linker
253 */
254 @Override
255 public Linker getLinker(final LinkType type) {
256 return LdLinker.getInstance();
257 }
258
259 /**
260 * Gets maximum length of command line.
261 *
262 * @return int maximum length of command line
263 */
264 @Override
265 public int getMaximumCommandLength() {
266 return 1024;
267 }
268
269 /**
270 * Gets maximum number of input files processed per command.
271 *
272 * @return int maximum number of input files processed per command.
273 */
274 @Override
275 protected int getMaximumInputFilesPerCommand() {
276 return 1;
277 }
278
279 /**
280 * Gets output file names.
281 *
282 * @param inputFile
283 * String input file name
284 * @param versionInfo
285 * version info, not used by this compiler.
286 * @return String[] output file names
287 */
288 @Override
289 public String[] getOutputFileNames(final String inputFile, final VersionInfo versionInfo) {
290 if (inputFile.endsWith(".cpp")) {
291 return super.getOutputFileNames(inputFile, versionInfo);
292 }
293 //
294 // if a recognized input file
295 //
296 final String baseName = getBaseOutputName(inputFile);
297 return new String[] {
298 "moc_" + baseName + ".cpp"
299 };
300 }
301
302 /**
303 * Gets switch to undefine preprocessor macro.
304 *
305 * @param buffer
306 * StringBuffer command line argument
307 * @param define
308 * String macro name
309 */
310 @Override
311 protected void getUndefineSwitch(final StringBuffer buffer, final String define) {
312 }
313 }