1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.github.maven_nar;
21
22 import java.io.File;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37
38 import com.github.maven_nar.cpptasks.CCTask;
39 import com.github.maven_nar.cpptasks.CUtil;
40 import com.github.maven_nar.cpptasks.CompilerDef;
41 import com.github.maven_nar.cpptasks.LinkerDef;
42 import com.github.maven_nar.cpptasks.OutputTypeEnum;
43 import com.github.maven_nar.cpptasks.RuntimeType;
44 import com.github.maven_nar.cpptasks.SubsystemEnum;
45 import com.github.maven_nar.cpptasks.types.LibrarySet;
46 import com.github.maven_nar.cpptasks.types.LibraryTypeEnum;
47 import com.github.maven_nar.cpptasks.types.LinkerArgument;
48 import com.github.maven_nar.cpptasks.types.SystemLibrarySet;
49
50
51
52
53
54
55 @Mojo(name = "nar-testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE,
56 requiresDependencyResolution = ResolutionScope.TEST)
57 public class NarTestCompileMojo extends AbstractCompileMojo {
58
59
60
61 @Parameter(property = "skipNar")
62 protected boolean skipNar;
63
64 private void createTest(final Project antProject, final Test test)
65 throws MojoExecutionException, MojoFailureException {
66 final String type = "test";
67
68
69 final CCTask task = new CCTask();
70 task.setProject(antProject);
71
72
73 final SubsystemEnum subSystem = new SubsystemEnum();
74 subSystem.setValue("console");
75 task.setSubsystem(subSystem);
76
77
78 final OutputTypeEnum outTypeEnum = new OutputTypeEnum();
79 outTypeEnum.setValue(test.getType());
80 task.setOuttype(outTypeEnum);
81
82
83 File outDir = new File(getTestTargetDirectory(), "bin");
84 outDir = new File(outDir, getAOL().toString());
85 outDir.mkdirs();
86
87
88 final File outFile = new File(outDir, test.getName());
89 getLog().debug("NAR - output: '" + outFile + "'");
90 task.setOutfile(outFile);
91
92
93 File objDir = new File(getTestTargetDirectory(), "obj");
94 objDir = new File(objDir, getAOL().toString());
95 objDir.mkdirs();
96 task.setObjdir(objDir);
97
98
99 task.setFailonerror(failOnError(getAOL()));
100 task.setLibtool(useLibtool(getAOL()));
101
102
103 final RuntimeType runtimeType = new RuntimeType();
104 runtimeType.setValue(getRuntime(getAOL()));
105 task.setRuntime(runtimeType);
106
107
108 final Cpp cpp = getCpp();
109 if (cpp != null) {
110 final CompilerDef cppCompiler = getCpp().getTestCompiler(type, test.getName());
111 if (cppCompiler != null) {
112 task.addConfiguredCompiler(cppCompiler);
113 }
114 }
115
116
117 final C c = getC();
118 if (c != null) {
119 final CompilerDef cCompiler = c.getTestCompiler(type, test.getName());
120 if (cCompiler != null) {
121 task.addConfiguredCompiler(cCompiler);
122 }
123 }
124
125
126 final Fortran fortran = getFortran();
127 if (fortran != null) {
128 final CompilerDef fortranCompiler = getFortran().getTestCompiler(type, test.getName());
129 if (fortranCompiler != null) {
130 task.addConfiguredCompiler(fortranCompiler);
131 }
132 }
133
134
135 getJava().addIncludePaths(task, type);
136
137 getMsvc().configureCCTask(task);
138
139 List depLibs = getNarArtifacts();
140
141
142 for (final Object depLib1 : depLibs) {
143 final Artifact artifact = (Artifact) depLib1;
144
145
146 File include = getLayout()
147 .getIncludeDirectory(getUnpackDirectory(), artifact.getArtifactId(), artifact.getBaseVersion());
148 if (!include.exists()) {
149
150 include = getLayout()
151 .getIncludeDirectory(getTestUnpackDirectory(), artifact.getArtifactId(), artifact.getBaseVersion());
152 }
153 if (include.exists()) {
154 task.createIncludePath().setPath(include.getPath());
155 }
156 }
157
158
159 final File jniIncludeDir = getJavah().getJniDirectory();
160 if (jniIncludeDir.exists()) {
161 task.createIncludePath().setPath(jniIncludeDir.getPath());
162 }
163
164
165 final LinkerDef linkerDefinition = getLinker().getTestLinker(this, task, getOS(), getAOL().getKey() + ".linker.",
166 type);
167 task.addConfiguredLinker(linkerDefinition);
168
169 final File includeDir = getLayout().getIncludeDirectory(getTargetDirectory(), getMavenProject().getArtifactId(),
170 getMavenProject().getVersion());
171
172 String linkType = test.getLink( getLibraries() );
173 final File libDir = getLayout().getLibDirectory(getTargetDirectory(), getMavenProject().getArtifactId(),
174 getMavenProject().getVersion(), getAOL().toString(), linkType);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 if (includeDir.exists()) {
197 task.createIncludePath().setLocation(includeDir);
198 }
199
200
201 if (libDir.exists()) {
202 final LibrarySet libSet = new LibrarySet();
203 libSet.setProject(antProject);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 final String libs = getNarInfo().getProperty(getAOL(), "libs.names", getOutput(true));
222
223 getLog().debug("Searching for parent to link with " + libs);
224 libSet.setLibs(new CUtil.StringArrayBuilder(libs));
225 final LibraryTypeEnum libType = new LibraryTypeEnum();
226 libType.setValue(linkType);
227 libSet.setType(libType);
228 libSet.setDir(libDir);
229 task.addLibset(libSet);
230 }
231
232
233 final List depLibOrder = getDependencyLibOrder();
234
235
236
237 if (depLibOrder != null && !depLibOrder.isEmpty()) {
238
239 final List tmp = new LinkedList();
240
241 for (final Object aDepLibOrder : depLibOrder) {
242
243 final String depToOrderName = (String) aDepLibOrder;
244
245 for (final Iterator j = depLibs.iterator(); j.hasNext(); ) {
246
247 final NarArtifact dep = (NarArtifact) j.next();
248 final String depName = dep.getGroupId() + ":" + dep.getArtifactId();
249
250 if (depName.equals(depToOrderName)) {
251
252 tmp.add(dep);
253 j.remove();
254 }
255 }
256 }
257
258 tmp.addAll(depLibs);
259 depLibs = tmp;
260 }
261
262 for (final Object depLib : depLibs) {
263 final NarArtifact dependency = (NarArtifact) depLib;
264
265
266
267 final String binding = getBinding(test, dependency);
268 getLog().debug("Using Binding: " + binding);
269 AOL aol = getAOL();
270 aol = dependency.getNarInfo().getAOL(getAOL());
271 getLog().debug("Using Library AOL: " + aol.toString());
272
273
274
275
276
277
278
279
280 if (!binding.equals(Library.JNI) && !binding.equals(Library.NONE) && !binding.equals(Library.EXECUTABLE)) {
281
282 File dir = getLayout()
283 .getLibDirectory(getUnpackDirectory(), dependency.getArtifactId(), dependency.getBaseVersion(),
284 aol.toString(), binding);
285 getLog().debug("Looking for Library Directory: " + dir);
286 if (!dir.exists()) {
287 getLog().debug("Library Directory " + dir + " does NOT exist.");
288
289
290 dir = getLayout()
291 .getLibDirectory(getTestUnpackDirectory(), dependency.getArtifactId(), dependency.getBaseVersion(),
292 aol.toString(), binding);
293 getLog().debug("Looking for Library Directory: " + dir);
294 }
295 if (dir.exists()) {
296 final LibrarySet libSet = new LibrarySet();
297 libSet.setProject(antProject);
298
299
300 final String libs = dependency.getNarInfo().getLibs(getAOL());
301 if (libs != null && !libs.equals("")) {
302 getLog().debug("Using LIBS = " + libs);
303 libSet.setLibs(new CUtil.StringArrayBuilder(libs));
304 libSet.setDir(dir);
305 task.addLibset(libSet);
306 }
307 } else {
308 getLog().debug("Library Directory " + dir + " does NOT exist.");
309 }
310
311
312
313 final String options = dependency.getNarInfo().getOptions(getAOL());
314 if (options != null && !options.equals("")) {
315 getLog().debug("Using OPTIONS = " + options);
316 final LinkerArgument arg = new LinkerArgument();
317 arg.setValue(options);
318 linkerDefinition.addConfiguredLinkerArg(arg);
319 }
320
321 final String sysLibs = dependency.getNarInfo().getSysLibs(getAOL());
322 if (sysLibs != null && !sysLibs.equals("")) {
323 getLog().debug("Using SYSLIBS = " + sysLibs);
324 final SystemLibrarySet sysLibSet = new SystemLibrarySet();
325 sysLibSet.setProject(antProject);
326
327 sysLibSet.setLibs(new CUtil.StringArrayBuilder(sysLibs));
328 task.addSyslibset(sysLibSet);
329 }
330 }
331 }
332
333
334 getJava().addRuntime(task, getJavaHome(getAOL()), getOS(), getAOL().getKey() + ".java.");
335
336
337 try {
338 task.execute();
339 } catch (final BuildException e) {
340 throw new MojoExecutionException("NAR: Test-Compile failed", e);
341 }
342 }
343
344
345
346
347
348
349
350 @Override
351 protected ScopeFilter getArtifactScopeFilter() {
352
353 return new ScopeFilter( Artifact.SCOPE_TEST, null );
354 }
355
356 @Override
357 protected File getUnpackDirectory() {
358 return getTestUnpackDirectory() == null ? super.getUnpackDirectory() : getTestUnpackDirectory();
359 }
360
361 @Override
362 public final void narExecute() throws MojoExecutionException, MojoFailureException {
363 if (this.skipTests) {
364 getLog().info("Not compiling test sources");
365 } else {
366
367
368 getTestTargetDirectory().mkdirs();
369
370 for (final Object o : getTests()) {
371 createTest(getAntProject(), (Test) o);
372 }
373 }
374 }
375
376 }