/
NdkBuildMojo.java
924 lines (828 loc) · 37 KB
/
NdkBuildMojo.java
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*
* 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 com.jayway.maven.plugins.android.phase05compile;
import com.jayway.maven.plugins.android.AbstractAndroidMojo;
import com.jayway.maven.plugins.android.CommandExecutor;
import com.jayway.maven.plugins.android.ExecutionException;
import com.jayway.maven.plugins.android.common.AetherHelper;
import com.jayway.maven.plugins.android.common.NativeHelper;
import com.jayway.maven.plugins.android.config.ConfigPojo;
import com.jayway.maven.plugins.android.config.PullParameter;
import com.jayway.maven.plugins.android.configuration.HeaderFilesDirective;
import com.jayway.maven.plugins.android.configuration.Ndk;
import org.apache.commons.io.FileUtils;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.util.IOUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Johan Lindquist <johanlindquist@gmail.com>
* @goal ndk-build
* @phase compile
* @requiresProject true
*/
public class NdkBuildMojo extends AbstractAndroidMojo
{
/**
* <p>The Android NDK to use.</p>
* <p>Looks like this:</p>
* <pre>
* <ndk>
* <path>/opt/android-ndk-r4</path>
* </ndk>
* </pre>
* <p>The <code><path></code> parameter is optional. The default is the setting of the ANDROID_NDK_HOME
* environment variable. The parameter can be used to override this setting with a different environment variable
* like this:</p>
* <pre>
* <ndk>
* <path>${env.ANDROID_NDK_HOME}</path>
* </ndk>
* </pre>
* <p>or just with a hardcoded absolute path. The parameters can also be configured from command-line with parameter
* <code>-Dandroid.ndk.path</code>.</p>
*
* @parameter
*/
@ConfigPojo( prefix = "ndk" )
private Ndk ndk;
/**
* Allows for overriding the default ndk-build executable.
*
* @parameter expression="${android.ndk.ndk-build-executable}"
*/
@PullParameter
private String ndkBuildExecutable;
/**
* @parameter expression="${android.ndk.ndk-build-directory}"
*/
@PullParameter
private String ndkBuildDirectory;
/**
* Specifies the classifier with which the artifact should be stored in the repository
*
* @parameter expression="${android.ndk.build.native-classifier}"
*/
@PullParameter
private String ndkClassifier;
/**
* Specifies additional command line parameters to pass to ndk-build
*
* @parameter expression="${android.ndk.build.command-line}"
*/
@PullParameter
protected String ndkBuildAdditionalCommandline;
/**
* Flag indicating whether the NDK output directory (libs/<architecture>) should be cleared after build. This
* will essentially 'move' all the native artifacts (.so) to
* the ${project.build.directory}/libs/<architecture>.
* If an APK is built as part of the invocation, the libraries will be included from here.
*
* @parameter expression="${android.ndk.build.clear-native-artifacts}" default-value="false"
*/
@PullParameter( defaultValue = "false" )
private Boolean clearNativeArtifacts;
/**
* Flag indicating whether the resulting native library should be attached as an artifact to the build. This
* means the resulting .so is installed into the repository as well as being included in the final APK.
*
* @parameter expression="${android.ndk.build.attach-native-artifact}" default-value="false"
*/
@PullParameter( defaultValue = "false" )
private Boolean attachNativeArtifacts;
/**
* Build folder to place built native libraries into
*
* @parameter expression="${android.ndk.build.ndk-output-directory}"
* default-value="${project.build.directory}/ndk-libs"
*/
private File ndkOutputDirectory;
/**
* <p>Folder containing native, static libraries compiled and linked by the NDK.</p>
*
* @parameter expression="${android.nativeLibrariesOutputDirectory}" default-value="${project.basedir}/obj/local"
*/
private File nativeLibrariesOutputDirectory;
/**
* <p>Target to invoke on the native makefile.</p>
*
* @parameter expression="${android.nativeTarget}"
*/
@PullParameter
private String target;
/**
* Defines the architecture for the NDK build
*
* @parameter expression="${android.ndk.build.architecture}" default-value="armeabi"
*/
@PullParameter( defaultValue = "armeabi" )
private String ndkArchitecture;
/**
* @component
* @readonly
* @required
*/
protected ArtifactFactory artifactFactory;
/**
* Flag indicating whether the header files used in the build should be included and attached to the build as
* an additional artifact.
*
* @parameter expression="${android.ndk.build.attach-header-files}" default-value="true"
*/
@PullParameter( defaultValue = "true" )
private Boolean attachHeaderFiles;
/**
* Flag indicating whether the make files last LOCAL_SRC_INCLUDES should be used for determing what header
* files to include. Setting this flag to true, overrides any defined header files directives.
* <strong>Note: </strong> By setting this flag to true, all header files used in the project will be
* added to the resulting header archive. This may be undesirable in most cases and is therefore turned off by
* default.
*
* @parameter expression="${android.ndk.build.use-local-src-include-paths}" default-value="false"
*/
@PullParameter( defaultValue = "false" )
private Boolean useLocalSrcIncludePaths;
/**
* Specifies the set of header files includes/excludes which should be used for bundling the exported header
* files. The below shows an example of how this can be used.
* <p/>
* <pre>
* <headerFilesDirectives>
* <headerFilesDirective>
* <directory>${basedir}/jni/include</directory>
* <includes>
* <includes>**\/*.h</include>
* </includes>
* <headerFilesDirective>
* </headerFilesDirectives>
* </pre>
* <br/>
* If no <code>headerFilesDirectives</code> is specified, the default includes will be defined as shown below:
* <br/>
* <pre>
* <headerFilesDirectives>
* <headerFilesDirective>
* <directory>${basedir}/jni</directory>
* <includes>
* <includes>**\/*.h</include>
* </includes>
* <excludes>
* <exclude>**\/*.c</exclude>
* </excludes>
* <headerFilesDirective>
* [..]
* </headerFilesDirectives>
* </pre>
*
* @parameter
*/
@PullParameter
private List<HeaderFilesDirective> headerFilesDirectives;
/**
* The Jar archiver.
*
* @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
*/
private JarArchiver jarArchiver;
/**
* Flag indicating whether the header files for native, static library dependencies should be used. If true,
* the header archive for each statically linked dependency will be resolved.
*
* @parameter expression="${android.ndk.build.use-header-archives}" default-value="true"
*/
@PullParameter( defaultValue = "true" )
private Boolean useHeaderArchives;
/**
* Defines additional system properties which should be exported to the ndk-build script. This
* <br/>
* <pre>
* <systemProperties>
* <propertyName>propertyValue</propertyName>
* <build-target>android</build-target>
* [..]
* </systemProperties>
* </pre> *
*
* @parameter
*/
@PullParameter
private Map<String, String> systemProperties;
/**
* Flag indicating whether warnings should be ignored while compiling. If true,
* the build will not fail if warning are found during compile.
*
* @parameter expression="${android.ndk.build.ignore-build-warnings}" default-value="true"
*/
@PullParameter( defaultValue = "true" )
private Boolean ignoreBuildWarnings;
/**
* Defines the regular expression used to detect whether error/warning output from ndk-build is a minor compile
* warning or is actually an error which should cause the build to fail.
* <p/>
* If the pattern matches, the output from the compiler will <strong>not</strong> be considered an error and compile
* will be successful.
*
* @parameter expression="${android.ndk.build.build-warnings-regular-expression}"
* default-value=".*[warning|note]: .*"
*/
@PullParameter( defaultValue = ".*[warning|note]: .*" )
private String buildWarningsRegularExpression;
/**
* @parameter expression="${android.ndk.build.skip-native-library-stripping}" default-value="false"
*/
@PullParameter( defaultValue = "false" )
private Boolean skipStripping;
/**
* @parameter expression="${android.ndk.build.ndk-toolchain}"
*/
@PullParameter
private String ndkToolchain;
/**
* Specifies the final name of the library output by the build (this allows
*
* @parameter expression="${android.ndk.build.build.final-library.name}"
*/
@PullParameter
private String ndkFinalLibraryName;
/**
* Specifies the makefile to use for the build (if other than the default Android.mk).
*
* @parameter
*/
@PullParameter
private String makefile;
/**
* Specifies the application makefile to use for the build (if other than the default Application.mk).
*
* @parameter
*/
@PullParameter
private String applicationMakefile;
/**
* Flag indicating whether to use the max available jobs for the host machine
*
* @parameter expression="${android.ndk.build.maxJobs}" default-value="false"
*/
@PullParameter( defaultValue = "false" )
private Boolean maxJobs;
/**
*
* @throws MojoExecutionException
* @throws MojoFailureException
*/
public void execute() throws MojoExecutionException, MojoFailureException
{
//ConfigHandler cfh = new ConfigHandler( this );
//cfh.parseConfiguration();
try
{
// Validate the NDK
final File ndkBuildFile = new File( getAndroidNdk().getNdkBuildPath() );
NativeHelper.validateNDKVersion( ndkBuildFile.getParentFile() );
// Validate the makefile - if our packaging type is so (for example) and there are
// dependencies on .a files (or shared files for that matter) the makefile should include
// the include of our Android Maven plugin generated makefile.
validateMakefile( project, makefile );
Preparation preparation = new Preparation().invoke();
File nativeLibDirectory = preparation.getNativeLibDirectory();
boolean libsDirectoryExists = preparation.isLibsDirectoryExists();
File directoryToRemove = preparation.getDirectoryToRemove();
// Start setting up the command line to be executed
final CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
// Add an error listener to the build - this allows the build to conditionally fail
// depending on a) the output of the build b) whether or not build errors (output on stderr) should be
// ignored and c) whether the pattern matches or not
executor.setErrorListener( getNdkErrorListener() );
final Set<Artifact> nativeLibraryArtifacts = findNativeLibraryDependencies();
// If there are any static libraries the code needs to link to, include those in the make file
final Set<Artifact> resolveNativeLibraryArtifacts = AetherHelper
.resolveArtifacts( nativeLibraryArtifacts, repoSystem, repoSession, projectRepos );
final File androidMavenMakefile = File.createTempFile( "android_maven_plugin_makefile", ".mk" );
androidMavenMakefile.deleteOnExit();
final MakefileHelper.MakefileHolder makefileHolder = MakefileHelper
.createMakefileFromArtifacts( androidMavenMakefile.getParentFile(), resolveNativeLibraryArtifacts,
useHeaderArchives, repoSession, projectRepos, repoSystem );
IOUtil.copy( makefileHolder.getMakeFile(), new FileOutputStream( androidMavenMakefile ) );
// Add the path to the generated makefile - this is picked up by the build (by an include from the user)
executor.addEnvironment( "ANDROID_MAVEN_PLUGIN_MAKEFILE", androidMavenMakefile.getAbsolutePath() );
// Only add the LOCAL_STATIC_LIBRARIES
if ( NativeHelper.hasStaticNativeLibraryArtifact( resolveNativeLibraryArtifacts ) )
{
executor.addEnvironment( "ANDROID_MAVEN_PLUGIN_LOCAL_STATIC_LIBRARIES",
MakefileHelper.createLibraryList( resolveNativeLibraryArtifacts, true ) );
}
// Only add the LOCAL_SHARED_LIBRARIES
if ( NativeHelper.hasSharedNativeLibraryArtifact( resolveNativeLibraryArtifacts ) )
{
executor.addEnvironment( "ANDROID_MAVEN_PLUGIN_LOCAL_SHARED_LIBRARIES",
MakefileHelper.createLibraryList( resolveNativeLibraryArtifacts, false ) );
}
// Adds the location of the Makefile capturer file - this file will after the build include
// things like header files, flags etc. It is processed after the build to retrieve the headers
// and also capture flags etc ...
final File makefileCaptureFile = File.createTempFile( "android_maven_plugin_makefile_captures", ".tmp" );
makefileCaptureFile.deleteOnExit();
executor.addEnvironment( MakefileHelper.MAKEFILE_CAPTURE_FILE, makefileCaptureFile.getAbsolutePath() );
// Add any defined system properties
if ( systemProperties != null && ! systemProperties.isEmpty() )
{
for ( Map.Entry<String, String> entry : systemProperties.entrySet() )
{
executor.addEnvironment( entry.getKey(), entry.getValue() );
}
}
executor.setLogger( this.getLog() );
// Setup the command line for the make
final List<String> commands = new ArrayList<String>();
// Setup the build directory (defaults to the current directory) but may be different depending
// on user configuration
commands.add( "-C" );
if ( ndkBuildDirectory == null )
{
ndkBuildDirectory = project.getBasedir().getAbsolutePath();
}
commands.add( ndkBuildDirectory );
// If the build should use a custom makefile or not - some validation is done to ensure
// this exists and all
if ( makefile != null )
{
File makeFile = new File( project.getBasedir(), makefile );
if ( ! makeFile.exists() )
{
getLog().error( "Specified makefile " + makeFile + " does not exist" );
throw new MojoExecutionException( "Specified makefile " + makeFile + " does not exist" );
}
commands.add( "-f" );
commands.add( makefile );
}
configureApplicationMakefile( commands );
configureMaxJobs( commands );
configureNdkToolchain( commands );
// Anything else on the command line the user wants to add - simply splice it up and
// add it one by one to the command line
if ( ndkBuildAdditionalCommandline != null )
{
String[] additionalCommands = ndkBuildAdditionalCommandline.split( " " );
for ( final String command : additionalCommands )
{
commands.add( command );
}
}
// If a build target is specified, tag that onto the command line as the
// very last of the parameters
if ( target != null )
{
commands.add( target );
}
else /*if ( "a".equals( project.getPackaging() ) )*/
{
commands.add( project.getArtifactId() );
}
final String ndkBuildPath = resolveNdkBuildExecutable();
getLog().info( ndkBuildPath + " " + commands.toString() );
executor.executeCommand( ndkBuildPath, commands, project.getBasedir(), true );
cleanUp( nativeLibDirectory, libsDirectoryExists, directoryToRemove, makefileHolder, makefileCaptureFile );
}
catch ( MojoExecutionException e )
{
getLog().error( "Error during build: " + e.getMessage(), e );
throw e;
}
catch ( Exception e )
{
getLog().error( "Error while executing: " + e.getMessage() );
throw new MojoExecutionException( e.getMessage(), e );
}
}
private void configureApplicationMakefile( List<String> commands )
throws MojoExecutionException
{
if ( applicationMakefile != null )
{
File appMK = new File( project.getBasedir(), applicationMakefile );
if ( ! appMK.exists() )
{
getLog().error( "Specified application makefile " + appMK + " does not exist" );
throw new MojoExecutionException( "Specified application makefile " + appMK + " does not exist" );
}
commands.add( "NDK_APPLICATION_MK=" + applicationMakefile );
}
}
private void configureMaxJobs( List<String> commands )
{
if ( maxJobs )
{
String jobs = String.valueOf( Runtime.getRuntime().availableProcessors() );
getLog().info( "executing " + jobs + " parallel jobs" );
commands.add( "-j" );
commands.add( jobs );
}
}
private void configureNdkToolchain( List<String> commands )
{
if ( ndkToolchain != null )
{
// Setup the correct toolchain to use
// FIXME: perform a validation that this toolchain exists in the NDK
commands.add( "NDK_TOOLCHAIN=" + ndkToolchain );
}
}
private void cleanUp( File nativeLibDirectory, boolean libsDirectoryExists, File directoryToRemove,
MakefileHelper.MakefileHolder makefileHolder, File makefileCaptureFile )
throws IOException, MojoExecutionException
{
try
{
// Cleanup libs/armeabi directory if needed - this implies moving any native artifacts into target/libs
if ( clearNativeArtifacts )
{
nativeLibDirectory = cleanUpNativeArtifacts( nativeLibDirectory, libsDirectoryExists );
}
// Attempt to attach the native library if the project is defined as a "pure" native Android library
// (packaging is 'so' or 'a') or if the plugin has been configured to attach the native library to the
// build
if ( "so".equals( project.getPackaging() ) || "a".equals( project.getPackaging() )
|| attachNativeArtifacts )
{
final File nativeArtifactFile;
if ( ndkFinalLibraryName == null )
{
File[] files = nativeLibDirectory.listFiles( new FilenameFilter()
{
public boolean accept( final File dir, final String name )
{
if ( "a".equals( project.getPackaging() ) )
{
return name.startsWith(
"lib" + ( target != null ? target : project.getArtifactId() ) ) && name
.endsWith( ".a" );
}
else
{
return name.startsWith(
"lib" + ( target != null ? target : project.getArtifactId() ) ) && name
.endsWith( ".so" );
}
}
} );
// slight limitation at this stage - we only handle a single .so artifact
if ( files == null || files.length != 1 )
{
getLog().warn( "Error while detecting native compile artifacts: "
+ ( files == null || files.length == 0
? "None found"
: "Found more than 1 artifact" ) );
if ( files != null && files.length > 1 )
{
getLog().debug( "List of files found: " + Arrays.asList( files ) );
getLog().error(
"Currently, only a single, final native library is supported by the build" );
throw new MojoExecutionException(
"Currently, only a single, final native library is supported by the build" );
}
else
{
getLog().error( "No native compiled library found, did the native compile complete "
+ "successfully?" );
throw new MojoExecutionException( "No native compiled library found, did the native "
+ "compile complete successfully?" );
}
}
nativeArtifactFile = files[ 0 ];
}
else
{
// Find the nativeArtifactFile in the nativeLibDirectory/ndkFinalLibraryName
nativeArtifactFile = new File( nativeLibDirectory,
ndkFinalLibraryName + "." + project.getPackaging() );
if ( ! nativeArtifactFile.exists() )
{
getLog().error(
"Could not locate final native library using the provided ndkFinalLibraryName "
+ ndkFinalLibraryName + " (tried " + nativeArtifactFile.getAbsolutePath()
+ ")" );
throw new MojoExecutionException(
"Could not locate final native library using the provided ndkFinalLibraryName "
+ ndkFinalLibraryName + " (tried " + nativeArtifactFile.getAbsolutePath()
+ ")" );
}
}
final String artifactType = resolveArtifactType( nativeArtifactFile );
if ( "so".equals( artifactType ) && ! skipStripping )
{
getLog().debug( "Post processing (stripping) native compiled artifact: " + nativeArtifactFile );
invokeNDKStripper( nativeArtifactFile );
}
getLog().debug( "Adding native compiled artifact: " + nativeArtifactFile );
File fileToAttach = nativeArtifactFile;
if ( ! libsDirectoryExists )
{
getLog().debug( "Moving native compiled artifact to target directory for preservation" );
// This indicates the output directory was created by the build (us) and that we should really
// move it to the target (needed to preserve the attached artifact once install is invoked)
final File destFile = new File( project.getBuild().getDirectory(),
nativeArtifactFile.getName() );
FileUtils.moveFile( nativeArtifactFile, destFile );
fileToAttach = destFile;
}
projectHelper.attachArtifact( this.project, artifactType,
( ndkClassifier != null ? ndkClassifier : ndkArchitecture ), fileToAttach );
}
// Process conditionally any of the headers to include into the header archive file
processMakefileCapture( makefileCaptureFile );
}
finally
{
// If we created any directories as part of the build, blow those away after we're done
if ( ! libsDirectoryExists )
{
getLog().info( "Cleaning up native library output directory after build" );
getLog().debug( "Removing directory: " + directoryToRemove );
FileUtils.deleteDirectory( directoryToRemove );
}
// If we created a makefile for the build we should be polite and remove any extracted include
// directories after we're done
if ( makefileHolder != null )
{
getLog().info( "Cleaning up extracted include directories used for build" );
MakefileHelper.cleanupAfterBuild( makefileHolder );
}
}
}
private CommandExecutor.ErrorListener getNdkErrorListener()
{
return new CommandExecutor.ErrorListener()
{
@Override
public boolean isError( String error )
{
// Unconditionally ignore *All* build warning if configured to
if ( ignoreBuildWarnings )
{
return false;
}
final Pattern pattern = Pattern.compile( buildWarningsRegularExpression );
final Matcher matcher = pattern.matcher( error );
// If the the reg.exp actually matches, we can safely say this is not an error
// since in theory the user told us so
if ( matcher.matches() )
{
return false;
}
// Otherwise, it is just another error
return true;
}
};
}
private File cleanUpNativeArtifacts( File nativeLibDirectory, boolean libsDirectoryExists ) throws IOException
{
final File destinationDirectory = new File( ndkOutputDirectory.getAbsolutePath(),
"/" + ndkArchitecture );
if ( ! libsDirectoryExists )
{
FileUtils.moveDirectory( nativeLibDirectory, destinationDirectory );
}
else
{
FileUtils.copyDirectory( nativeLibDirectory, destinationDirectory );
FileUtils.cleanDirectory( nativeLibDirectory );
}
nativeLibDirectory = destinationDirectory;
return nativeLibDirectory;
}
private void validateMakefile( MavenProject project, String makefile )
{
// TODO: actually perform validation
}
private void invokeNDKStripper( File file ) throws MojoExecutionException
{
try
{
getLog().debug( "Detected shared library artifact, will now strip it" );
// Execute the strip command
final CommandExecutor stripCommandExecutor = CommandExecutor.Factory.createDefaultCommmandExecutor();
stripCommandExecutor.setErrorListener( new CommandExecutor.ErrorListener()
{
public boolean isError( String error )
{
getLog().error( "Error while stripping binary: " + error );
return true;
}
} );
stripCommandExecutor.setLogger( getLog() );
stripCommandExecutor
.executeCommand( resolveNdkStripper().getAbsolutePath(), Arrays.asList( file.getAbsolutePath() ) );
}
catch ( ExecutionException e )
{
getLog().error( "Error while attempting to strip shared library", e );
throw new MojoExecutionException( "Error while attempting to strip shared library" );
}
}
private String resolveNdkBuildExecutable() throws MojoExecutionException
{
if ( ndkBuildExecutable != null )
{
getLog().debug( "ndk-build overriden, using " + ndkBuildExecutable );
return ndkBuildExecutable;
}
return getAndroidNdk().getNdkBuildPath();
}
private File resolveNdkStripper() throws MojoExecutionException
{
return getAndroidNdk().getStripper( ndkToolchain );
}
private void processMakefileCapture( File localCIncludesFile ) throws MojoExecutionException
{
try
{
if ( attachHeaderFiles )
{
final List<HeaderFilesDirective> finalHeaderFilesDirectives = new ArrayList<HeaderFilesDirective>();
if ( useLocalSrcIncludePaths )
{
Properties props = new Properties();
props.load( new FileInputStream( localCIncludesFile ) );
String localCIncludes = props.getProperty( "LOCAL_C_INCLUDES" );
if ( localCIncludes != null && ! localCIncludes.trim().isEmpty() )
{
String[] includes = localCIncludes.split( " " );
for ( String include : includes )
{
final HeaderFilesDirective headerFilesDirective = new HeaderFilesDirective();
File includeDir = new File( project.getBasedir(), include );
headerFilesDirective.setDirectory( includeDir.getAbsolutePath() );
headerFilesDirective.setIncludes( new String[]{ "**/*.h" } );
finalHeaderFilesDirectives.add( headerFilesDirective );
}
}
}
else
{
if ( headerFilesDirectives != null )
{
finalHeaderFilesDirectives.addAll( headerFilesDirectives );
}
}
if ( finalHeaderFilesDirectives.isEmpty() )
{
getLog().debug( "No header files included, will add default set" );
final HeaderFilesDirective e = new HeaderFilesDirective();
e.setDirectory( new File( project.getBasedir() + "/jni" ).getAbsolutePath() );
e.setIncludes( new String[]{ "**/*.h" } );
finalHeaderFilesDirectives.add( e );
}
createHeaderArchive( finalHeaderFilesDirectives );
}
}
catch ( Exception e )
{
throw new MojoExecutionException( "Error while processing headers to include: " + e.getMessage(), e );
}
}
private void createHeaderArchive( List<HeaderFilesDirective> finalHeaderFilesDirectives )
throws MojoExecutionException
{
try
{
MavenArchiver mavenArchiver = new MavenArchiver();
mavenArchiver.setArchiver( jarArchiver );
final File jarFile = new File( new File( project.getBuild().getDirectory() ),
project.getBuild().getFinalName() + ".har" );
mavenArchiver.setOutputFile( jarFile );
for ( HeaderFilesDirective headerFilesDirective : finalHeaderFilesDirectives )
{
mavenArchiver.getArchiver().addDirectory( new File( headerFilesDirective.getDirectory() ),
headerFilesDirective.getIncludes(), headerFilesDirective.getExcludes() );
}
final MavenArchiveConfiguration mavenArchiveConfiguration = new MavenArchiveConfiguration();
mavenArchiveConfiguration.setAddMavenDescriptor( false );
mavenArchiver.createArchive( project, mavenArchiveConfiguration );
projectHelper.attachArtifact( project, "har", ( ndkClassifier != null ? ndkClassifier : ndkArchitecture ),
jarFile );
}
catch ( Exception e )
{
throw new MojoExecutionException( e.getMessage() );
}
}
private Set<Artifact> findNativeLibraryDependencies() throws MojoExecutionException
{
NativeHelper nativeHelper = new NativeHelper( project, projectRepos, repoSession, repoSystem, artifactFactory,
getLog() );
final Set<Artifact> staticLibraryArtifacts = nativeHelper
.getNativeDependenciesArtifacts( unpackedApkLibsDirectory, false );
final Set<Artifact> sharedLibraryArtifacts = nativeHelper
.getNativeDependenciesArtifacts( unpackedApkLibsDirectory, true );
final Set<Artifact> mergedArtifacts = new LinkedHashSet<Artifact>( staticLibraryArtifacts );
mergedArtifacts.addAll( sharedLibraryArtifacts );
return mergedArtifacts;
}
/**
* Resolve the artifact type from the current project and the specified file. If the project packaging is
* either 'a' or 'so' it will use the packaging, otherwise it checks the file for the extension
*
* @param file The file being added as an artifact
* @return The artifact type (so or a)
*/
private String resolveArtifactType( File file )
{
if ( "so".equals( project.getPackaging() ) || "a".equals( project.getPackaging() ) )
{
return project.getPackaging();
}
else
{
// At this point, the file (as found by our filtering previously will end with either 'so' or 'a'
return file.getName().endsWith( "so" ) ? "so" : "a";
}
}
private class Preparation
{
private File nativeLibDirectory;
private boolean libsDirectoryExists;
private File directoryToRemove;
public File getNativeLibDirectory()
{
return nativeLibDirectory;
}
public boolean isLibsDirectoryExists()
{
return libsDirectoryExists;
}
public File getDirectoryToRemove()
{
return directoryToRemove;
}
public Preparation invoke()
{
// This usually points to ${basedir}/obj/local
nativeLibDirectory = new File( nativeLibrariesOutputDirectory, ndkArchitecture );
libsDirectoryExists = nativeLibDirectory.exists();
// Determine how much of the output directory structure (most likely obj/...) does not exist
// and based on what we find, determine how much of it we delete after the build
directoryToRemove = nativeLibDirectory;
if ( ! libsDirectoryExists )
{
getLog().info( "Creating native output directory " + nativeLibDirectory );
// This simply checks how much of the structure already exists - nothing (e.g. we make all the dirs)
// or just a partial part (the architecture part)?
if ( ! nativeLibrariesOutputDirectory.exists() )
{
if ( nativeLibrariesOutputDirectory.getParentFile().exists() )
{
nativeLibDirectory.mkdir();
}
else
{
nativeLibDirectory.mkdirs();
directoryToRemove = nativeLibrariesOutputDirectory.getParentFile();
}
}
else
{
if ( nativeLibDirectory.getParentFile().exists() )
{
nativeLibDirectory.mkdir();
}
else
{
nativeLibDirectory.mkdirs();
directoryToRemove = nativeLibDirectory.getParentFile();
}
}
}
return this;
}
}
}