Subversion Repositories DevTools

Rev

Rev 6455 | Rev 7056 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4937 dpurdie 1
########################################################################
2
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
3
#
4
# Module name   : androidBuilder.pl
5
# Module type   : Makefile system
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : This program is invoked by the JATS Makefile System
10
#                 to 'build' an Android project from an AndroidStudio based
11
#                 Android project. It does this by:
12
#                   Creating a build.xml file from the Eclispe files
13
#                   Injecting properties into the build
14
#                       Create gradle.properties (Must not be version controlled)
15
#                       Create local.properties (Must not be version controlled)
16
#                   Insert external dependencies
17
#                       Jar files
18
#                       Aar files
19
#                       JNI Libraries
20
#                   Invoking 'gradle' to perform the build 
21
#
22
#                 This process requires external tool - delivered in packages
23
#                 These are:
24
#                   gradle      - Provides the core of gradle
25
#                   androidSdk  - From package rather than installed
26
#                                 This provides flexability when a new Sdk
27
#                                 is required
28
#
29
# Usage:        The utility is invoked in a controlled manner from a Jats
30
#               makefile. The call is generated by the Android Toolset
31
#               Arguments:
32
#                -verbose                   - Increase debugging
33
#                -verbose=n                 - Increase debugging
34
#                -f=manifestFile.xml        - project Manifest file
35
#                -i=path                    - Path to the interface directory
36
#                -t=[P|D]"                  - Build Type. Production or Debug
37
#                -pn=PackageName            - Package Name
38
#                -pv=PackageVersion         - Package Version
39
#                -clean                     - Will clean the build
40
#                -populate                  - Test Env and Populate 'libs'
5412 dpurdie 41
#                -autotest                  - Run Unit Tests
5444 dpurdie 42
#                -hasTests                  - Build unit tests too    
4937 dpurdie 43
#               Aguments that can be provided by the user
44
#                -Jar=name                  - Name of a Jar to include
45
#                -Aar=name                  - Name of an Aar to include
46
#                -lname                     - Name of a Jats library to include
47
#                -Lname                     - Name of a 3rd party library to include
48
#
49
# Note: This function may be provided by several packages, 
50
#       thus the interface must not change - this include the name of 
51
#       this file. See the AndroidBuilder package too.
52
#
53
#
54
#......................................................................#
55
 
56
require 5.008_002;
57
use strict;
58
use warnings;
59
 
60
use Getopt::Long qw(:config pass_through);
61
use File::Path;
62
 
63
use JatsError;
64
use JatsSystem;
65
use JatsEnv;
66
use FileUtils;
67
use JatsProperties;
68
use JatsVersionUtils;
69
use ReadBuildConfig;
70
use JatsCopy;
71
use ArrayHashUtils;
72
 
73
#
74
#   Globals
75
#   Command line arguments
76
#
77
my $opt_verbose = $ENV{GBE_VERBOSE};
78
my $opt_buildFile;
79
my $opt_interface;
80
my $opt_gbetype = 'P';
6351 dpurdie 81
my @opt_gbetypes;
4937 dpurdie 82
my $opt_clean;
83
my $opt_pkgname;
84
my $opt_pkgversion;
85
my $opt_platform;
86
my $opt_populate;
6351 dpurdie 87
my $opt_populateDebug;
88
my $opt_populateProd;
5412 dpurdie 89
my $opt_autotest;
5444 dpurdie 90
my $opt_hastests;
4937 dpurdie 91
my @opt_jlibs;                  # List of Jats Libraries
92
my @opt_elibs;                  # List of 3rd party libraries
93
my @opt_jars;                   # List of JARs
94
my @opt_aars;                   # List of AARs
95
 
96
#
97
#   Configuration
98
#   Map JATS platforms to Shared library targets
6455 dpurdie 99
#       The key is a JATS name for the target
100
#       The value is the subdir that android will expect to find the files in
4937 dpurdie 101
#
102
my %SharedLibMap = (
6455 dpurdie 103
    'ANDROIDARM'    => 'armeabi',
6463 dpurdie 104
    'ANDROIDARMV7'  => 'armeabi-v7a',
4937 dpurdie 105
    'ANDROIDMIPS'   => 'mips',
106
    'ANDROIDX86'    => 'x86',
6444 dpurdie 107
 
108
    'ANDROIDARM64'  => 'arm64-v8a',
109
    'ANDROIDMIPS64' => 'mips64',
110
    'ANDROIDX86_64' => 'x86_64',
4937 dpurdie 111
    );
112
 
113
our $GBE_HOSTMACH;              # Sanity Test of machine type
114
our $GBE_MAKE_TARGET;           # Current build target
115
 
116
my $androidJars;                # Root of injected JARs and AARs
117
my $androidJniBase;             # Root of injected JNI files
118
my $androidJniProd;             # Root of injected JNI files - Prod
119
my $androidJniDebug;            # Root of injected JNI files - Debug
120
my $androidBuildSuffix = '';    # Prefix build commands
121
 
122
#-------------------------------------------------------------------------------
123
# Function        : Main Entry Point 
124
#
125
# Description     : Main entry to this program
126
#
127
# Inputs          : @ARGV           - Array of command line arguments
128
#                                     See file header
129
#
130
# Returns         : 0               - No Error
131
#                   1               - Error encountered    
132
#
133
InitFileUtils();
134
ErrorConfig( 'name'    => 'ANDROIDBUILDER',
135
             'verbose' => $opt_verbose);
136
$opt_verbose = $::ScmVerbose;               # Get the calculated verbosity level
137
 
138
#
139
#   Install local signal handlers to process GetOptions messages
140
#
141
local $SIG{__WARN__} = sub { ReportError('AndroidBuilder.' . "@_"); };
142
local $SIG{__DIE__} = sub { ReportError('AndroidBuilder.' . "@_"); };
143
my $result = GetOptions (
144
                "verbose:+"     => \$opt_verbose,       # flag
145
                "f=s"           => \$opt_buildFile,     # string
146
                "i=s"           => \$opt_interface,     # Interface directory
6351 dpurdie 147
                "t=s"           => \@opt_gbetypes,      # string
4937 dpurdie 148
                "pn=s"          => \$opt_pkgname,       # string
149
                "pv=s"          => \$opt_pkgversion,    # string
150
                "pf=s"          => \$opt_platform,      # string
151
                "clean"         => \$opt_clean,         # flag
152
                "populate"      => \$opt_populate,      # flag
5412 dpurdie 153
                "autotest"      => \$opt_autotest,      # flag
5444 dpurdie 154
                "hastests"      => \$opt_hastests,      # flag
4937 dpurdie 155
                "Jar=s"         => \@opt_jars,
156
                "Aar=s"         => \@opt_aars,
157
                );
158
 
159
#
160
#   Restore signal handlers and report parse errors
161
#
162
$SIG{__WARN__} = 'DEFAULT';
163
$SIG{__DIE__} = 'DEFAULT';
164
Error('AndroidBuilder. Invalid call options detected') if (!$result);
165
 
166
#
167
#   Process remaining arguments
168
#   Only --Lname and --lname are valid
169
#
170
foreach my $arg (@ARGV) {
171
    if ($arg =~ m~^[-]{1,2}l(.*)~) {
172
        push @opt_jlibs, $1;
173
    } elsif ($arg =~ m~^[-]{1,2}L(.*)~) {
174
        push @opt_elibs, $1;
175
    } else {
176
        ReportError("Invalid option: $arg");
177
    }
178
}
6351 dpurdie 179
 
4937 dpurdie 180
ErrorDoExit();
181
 
182
#
183
#   Sanity Test
184
#
185
ReportError ("Gradle build file not specified") unless ( defined $opt_buildFile); 
186
ReportError ("Gradle build file not found: $opt_buildFile") unless ( -f $opt_buildFile);
187
 
188
ReportError ("Interface directory not specified") unless ( defined $opt_interface);
189
ReportError ("Interface directory not found: $opt_interface") unless ( -d $opt_interface);
190
 
191
ReportError ("Package Name not specified") unless ( defined $opt_pkgname); 
192
ReportError ("Package Version not specified") unless ( defined $opt_pkgversion); 
193
 
194
EnvImport('GBE_HOSTMACH');
195
ReportError ("AndroidStudioBuilder is only supported under win32","This machine is: ".$::GBE_HOSTMACH ) unless ( $::GBE_HOSTMACH eq 'win32' );
196
ReportError ("Platform not found") unless ( defined $opt_platform );
197
 
198
ErrorDoExit();
199
 
200
#
201
#   Basic setup
202
#
6351 dpurdie 203
#   Handle multiple opt_gbetypes for populate mode
204
#   If we are ony building for Prod, then only populate production artifacts 
205
#   If we are ony building for Debug, then only populate debug artifacts 
206
foreach ( @opt_gbetypes) {
207
    $opt_gbetype = $_;
208
    $opt_populateProd = 1 if ($opt_gbetype eq 'P');
209
    $opt_populateDebug = 1 if ($opt_gbetype eq 'D');
210
}
211
 
4937 dpurdie 212
$androidBuildSuffix = ($opt_gbetype eq 'P') ? 'Release' : 'Debug';
213
 
214
#
215
#   The user provides the root build.gradle file
216
#   There MUST be a settings.gradle in the same directory
217
#       This is a JATS assumption - subject to change
218
#   There will be some other files there too
219
#   Calculate the root of the project
220
#
221
$opt_buildFile = RelPath(AbsPath($opt_buildFile));
222
my $project_root = StripFileExt($opt_buildFile);
223
   $project_root = '.' unless $project_root;
224
my $project_settingsFile = catfile($project_root, 'settings.gradle');
225
 
226
Message ("Project Base:" . Getcwd());
227
Message ("Project Root:" . $project_root);
228
Verbose ("Project Settings file:" . $project_settingsFile);
229
 
230
#
231
#   Directories to store Jar's, Aar's and JNI shared libarares
232
#   are created in the interface directory
233
#
234
$androidJars      = catdir($opt_interface, 'jatsLibs');
235
$androidJniBase   = catdir($opt_interface, 'jatsJni');
236
$androidJniProd   = catdir($androidJniBase, 'Release');
237
$androidJniDebug  = catdir($androidJniBase, 'Debug');
238
 
239
Verbose ("Interface:" . $opt_interface);
240
Verbose ("Android Jars: $androidJars");
241
Verbose ("Android JNI : $androidJniBase");
242
 
243
Error ("Gradle settings file not found: $project_settingsFile") unless ( -f $project_settingsFile);
244
 
245
#
246
#   Essential tool
247
#       gradle     - setup 
248
#                    GRADLE_USER_HOME
249
#                    JAVA_HOME
250
#                    
251
ReadBuildConfig( $opt_interface, $opt_platform, '--NoTest' );
252
 
253
my $gradleTool = getToolInfo('gradle', 'JAVA_VERSION', 'GRADLE_BIN');
254
my $gradleBinDir = catdir($gradleTool->{PKGBASE}, $gradleTool->{TOOLROOT}, $gradleTool->{GRADLE_BIN});
255
 
256
#
257
#   Setup a gradle home
258
#   Its used to cache stuff - create it within the interface directory
259
#
260
my $gradleHomeTarget = CleanPath(FullPath(catdir($opt_interface, 'gradleUserHome')));
261
mkpath($gradleHomeTarget, 0, 0775) 
262
    unless ( -d $gradleHomeTarget);
263
$ENV{GRADLE_USER_HOME} = $gradleHomeTarget;
264
Verbose("GRADLE_USER_HOME:", $ENV{GRADLE_USER_HOME});
265
 
266
#
267
#   Setup the required version of Java for the tool
5289 dpurdie 268
#   Force JAVA_OPTS to set Min/Max Heap
269
#       Use JAVA_OPTS because
270
#           _JAVA_OPTIONS causes ssytem to emit a lot of warnings that _JAVA_OPTIONS is being used
271
#           Use of org.gradle.jvmargs in gradle.properties causes warnins about forking speed
272
#           Fixing the max size will provide consistent builds
273
#           Perhaps one day it will be configured 
4937 dpurdie 274
#
275
my $javaVersion = $gradleTool->{JAVA_VERSION};
276
ReportError ("$javaVersion not defined.", "Building ANDROID requires $javaVersion be installed and correctly configured.") 
277
    unless $ENV{$javaVersion};
278
$ENV{JAVA_HOME}=$ENV{$javaVersion};
5289 dpurdie 279
$ENV{JAVA_OPTS} = '-Xms256m -Xmx1024m';
4937 dpurdie 280
 
281
#
282
#   Essential tool
283
#       androidSdk  - setup path to the android executable
284
#
285
my $androidSdkTool = getToolInfo('androidSdk');
286
my $androidSdk = catdir($androidSdkTool->{PKGBASE}, $androidSdkTool->{TOOLROOT} );
287
Verbose ("Android SDK : $androidSdk");
288
ReportError("Tool Package 'androidSdk' - Invalid SDK Basedir", "Sdk Base: $androidSdk" )
289
    unless -d ($androidSdk);
290
 
291
#   Essential tool
292
#       androidGradleRepo   - A repo of gradle plugings for android
293
#
294
my $androidGradleRepo = getToolInfo('androidGradleRepo');
295
my $gradleMavenRepo = catdir($androidGradleRepo->{PKGBASE}, $androidGradleRepo->{TOOLROOT});
296
Verbose ("Maven Repo. Gradle support for android : $gradleMavenRepo");
297
 
298
ErrorDoExit();
299
 
300
#
301
#   Create a gradle file with JATS provided version information
302
#   and paths for use within the gradle build.
303
#
304
#   Always do this as the 'clean' will need them
305
#
306
createGradleFiles($opt_clean);
307
 
308
#
309
#   Clean out any build artifacts
310
#
311
if ($opt_clean)
312
{
313
    #
314
    #   Invoke GRADLE on the build script - if present
315
    #
316
    Message ("Clean the existing build");
317
    runGradle ('clean');
318
    deleteGeneratedFiles();
319
    exit 0;
320
}
321
 
322
#
323
#   If we are only populating the build then we 
324
#   need to inject dependencies.
325
#
326
if ($opt_populate)
327
{
328
    deleteInjectedFiles();
329
    injectDependencies();
330
 
331
    Verbose ("Populate complete");
332
    exit 0;
333
}
334
 
335
#
336
#   Build the Android project through gradle
337
#
5412 dpurdie 338
my $rv = 0;
339
if ( ! $opt_autotest )
340
{
341
    #
342
    #   Build the project - does not run unit tests
343
    #       assemble all the code
344
    #       assemble code for unit tests -  does not run unit tests 
345
    #
346
    Message ("Build the android project: $androidBuildSuffix");
5444 dpurdie 347
    my @tasks;
348
    push (@tasks, 'assemble'. $androidBuildSuffix); 
349
    push (@tasks, 'assemble' . $androidBuildSuffix . 'UnitTest') if $opt_hastests;
5412 dpurdie 350
    $rv = runGradle('assemble'. $androidBuildSuffix , 'assemble' . $androidBuildSuffix . 'UnitTest');
351
    Error("Cannot build AndroidStudio project") if $rv;
352
}
353
else
354
{
355
    #
356
    #   Run unit tests - does not build the project, although it 
357
    #   will build the unit tests, but these have been buitl before
358
    #   
359
    #   If the gradle run fails, then its because one or more of the unit tests failed
360
    #   This is not a build failure as we want to process the test results and feed
361
    #   them up the chain
362
    #   
363
    #   Post processing MUST detect and report errors
364
    #
365
    Message ("Run Unit Tests within the android project: $androidBuildSuffix");
366
    $rv = runGradle('test'. $androidBuildSuffix);
367
    Message ("Unit Test reports: $rv");
368
}
4937 dpurdie 369
exit(0);
370
 
371
#-------------------------------------------------------------------------------
372
# Function        : runGradle 
373
#
374
# Description     : Run gradle to build the project
375
#                   Generate a command like:
376
#                   PathToGradle/gradle --offline -I SomePath/init.gradle <task> 
377
#
378
#                   Use an init-script to inject a sanity test into the build
379
#                   Ensure that the user is using our version numbers.
380
#
381
# Inputs          : task        - Task to run
382
#
383
# Returns         : Returns the error code of the build
384
#
385
sub runGradle
386
{
5412 dpurdie 387
    my (@tasks) = @_;
388
    Verbose ("runGradle: @tasks");
4937 dpurdie 389
 
390
    #   The Windows batch file can run in debug mode
391
    #   Make sure that it doesn't by default
392
    $ENV{DEBUG} = "" unless $opt_verbose;
393
 
394
    my $gradleProg = catdir($gradleBinDir, 'gradle');
395
    Verbose ("GradleProg: $gradleProg");
396
 
397
    #
398
    #   Locate the 'init.gradle' script
399
    #   Its co-located with this script
400
    #
401
    my $initScript = catdir(StripFileExt(__FILE__), 'init.gradle');
402
    Verbose ("Gradle Init Script: $initScript");
403
 
404
    #
405
    #   Build up the arg list
406
    #
407
    my @gradleArgs;
408
    push (@gradleArgs, '--offline');
409
#    push (@gradleArgs, '--info');
410
#    push (@gradleArgs, '--debug');
5307 dpurdie 411
#    push (@gradleArgs, '--stacktrace');
4937 dpurdie 412
    push (@gradleArgs, '--info') if $opt_verbose;
413
    push (@gradleArgs, '--debug') if ($opt_verbose > 2);
5307 dpurdie 414
    push (@gradleArgs, '--stacktrace') if ($opt_verbose > 3);
5412 dpurdie 415
    push (@gradleArgs, '-I', $initScript) unless ($tasks[0] =~ m/clean/); 
5307 dpurdie 416
    push (@gradleArgs, '-p', $project_root);
4937 dpurdie 417
 
5412 dpurdie 418
    my $rv = System('--NoShell', '--NoExit', $gradleProg, @gradleArgs, @tasks);
4937 dpurdie 419
    return $rv;
420
}
421
 
422
#-------------------------------------------------------------------------------
423
# Function        : createGradleFiles 
424
#
425
# Description     : Calculate Version information
426
#                       gradle.properies
427
#                       local.properties
428
#
429
# Inputs          : quiet           - No output 
430
#
431
# Returns         : Nothing
432
#
433
sub createGradleFiles
434
{
435
    my ($quiet) = @_;
436
 
437
    #
438
    #   Generate Package Versioning information   
439
    #       Need a text string and a number
440
    #       Generate the 'number' from the version number
441
    #
442
    my $version_text;
443
    my $version_num;
444
 
445
    $version_text = $opt_pkgversion;
446
    my ($major, $minor, $patch, $build )= SplitVersion($opt_pkgversion);
447
    foreach my $item ($major, $minor, $patch, $build)
448
    {
449
        Error("Package version has invalid form. It contains non-numeric parts", $item)
450
            unless ( $item =~ m~^\d+$~);
451
    }
452
    $version_num = ($major << 24) + ($minor << 16) + ($patch << 8) + $build;
453
 
454
    Message ("Project Version Txt:" . $version_text) unless $quiet;
455
    Message ("Project Version Num:" . $version_num) unless $quiet;
456
 
457
    #
458
    #   Create the gradle.properties file
459
    #       It needs to be in the projects root directory
460
    #
461
    my $gradleProperies = catfile($project_root,'gradle.properties');
462
    Message ("Create gradle.properties file: " . $gradleProperies) unless $quiet;
463
 
464
    my $data = JatsProperties::New();
465
 
466
    $data->setProperty('GBE_VERSION_NAME' , $version_text);
467
    $data->setProperty('GBE_VERSION_CODE' , $version_num);
468
 
469
    $data->setProperty('GBE_JARLIBS'        , NicePath($androidJars));
470
    $data->setProperty('GBE_JNI_RELEASE'    , NicePath($androidJniProd));
471
    $data->setProperty('GBE_JNI_DEBUG'      , NicePath($androidJniDebug));
472
    $data->setProperty('GBE_GRADLE_REPO'    , NicePath($gradleMavenRepo));
473
 
5412 dpurdie 474
    #
475
    #   Create properties for JAVA Stores
476
    #   Name of variable is based on the package name and prject suffix
477
    #       Forced to uppercase
478
    #       '-' replaced with '_'
479
    #
480
    foreach my $pkg (getPackageList())
481
    {
482
        my $base = $pkg->getBase(3);
483
        if ($base)
484
        {
485
            my $jarDir = catdir($base, 'jar');
486
            if (-d $jarDir )
487
            {
488
                $data->setProperty( $pkg->getUnifiedName('GBE_REPO_') , NicePath($jarDir));
489
            }
490
        }
491
    }
492
 
4937 dpurdie 493
    $data->store( $gradleProperies );
494
 
495
    #
496
    #   Create the local.properties file
497
    #       It needs to be in the projects root directory
498
    #
499
    #   May be able to do without this file - iff we set ANDROID_HOME
500
    #
501
    my $localProperies = catfile($project_root,'local.properties');
502
    Message ("Create local.properties file: " . $localProperies) unless $quiet;
503
 
504
    $data = JatsProperties::New();
505
    $data->setProperty('sdk.dir' , NicePath($androidSdk));
506
    $data->store( $localProperies );
507
}
508
 
509
#-------------------------------------------------------------------------------
510
# Function        : injectDependencies 
511
#
512
# Description     : Inject dependencies
513
#
514
#                   The android build can make use of files in a specific directory
515
#                   Place Jar and Aar files in a specific directory under the root of the project
516
#                   Use a directory called jatsLibs
517
#                   There are two types of files that can be placed in that directory
518
#                   These appear to be:
519
#                       1) .jar files
520
#                       2) .aar files
521
#
522
#                   NDK files will be process automatically by the builder, once the build is made
523
#                   aware of the location. We are not using the AndroidStudio default location
524
#                   Place them into jatsJni within the project root
525
#                       1) Shared libraries provided by NDK components
526
#
527
#                   Need to keep the production and debug JNI files seperate
528
#
529
#                   The gradle dependency processing needs both production and
530
#                   debug dependencies to be present at all times
531
#                   
532
#                   Create three areas:
533
#                       jatsLibs        - Prod and Debug Jars and Ars
534
#                       jatsJniDebug    - Debug JNI files
535
#                       jatsJniProd     - Production JNI files
536
#
537
# Inputs          : 
538
#
539
# Returns         : 
540
#
541
sub injectDependencies
542
{
543
    my @jlist;                  # List of JARs from default directories
544
    my @jpathlist;              # List of JARs from named directories
545
    my @alist;                  # List of AARs from default directories
546
    my @apathlist;              # List of AARs from named directories
547
    my @libListProd;            # List of production libraries
548
    my @libListDebug;           # List of debug libraries
549
    my @platformParts;          # Platforms Parts
550
 
4990 dpurdie 551
    my @jarSearch;              # Search paths - diagnostic display
552
    my @aarSearch;
553
    my @libSearch;
4937 dpurdie 554
 
4990 dpurdie 555
 
4937 dpurdie 556
    #
557
    #   Only if we need to do something
558
    #
559
    return unless (@opt_jars || @opt_aars || @opt_elibs || @opt_jlibs);
560
 
561
    #
562
    #   Determine the list of platformm parts
563
    #   This is where 'lib' files will be found
564
    #
565
    @platformParts = getPlatformParts();
566
    Verbose("Platform Parts", @platformParts);
567
 
568
    #
569
    #   Create search entries suitable for the CopyDir
570
    #   We will delete entries as the files are copied
571
    #   Allow for:
572
    #       jar/aar files to have a .jar /.aar suffix (optional)
573
    #       jar/aar files to have path specified with a package
574
    #
575
    #   Split into two lists ( per type ): 
576
    #       Those with a path and those without
577
    #
578
    foreach my $item ( @opt_jars) {
579
        $item =~ s~\.jar~~i;
580
        if ($item =~ m~/~) {
581
            UniquePush \@jpathlist, $item;
582
        } else {
583
            UniquePush \@jlist, $item;
584
        }
585
    }
586
 
587
    foreach my $item ( @opt_aars) {
588
        $item =~ s~\.aar~~i;
589
        if ($item =~ m~/~) {
590
            UniquePush \@apathlist, $item;
591
        } else {
592
            UniquePush \@alist, $item;
593
        }
594
    }
595
 
596
    #   Shared libraries
597
    #   Create full names
598
    foreach my $item ( @opt_elibs) {
6351 dpurdie 599
        UniquePush (\@libListDebug, 'lib' . $item . '.so');
600
        UniquePush (\@libListProd,  'lib' . $item . '.so');
4937 dpurdie 601
    }
602
 
603
    foreach my $item ( @opt_jlibs) {
6351 dpurdie 604
        UniquePush (\@libListDebug, 'lib' . $item . 'D.so') if $opt_populateDebug;
605
        UniquePush (\@libListProd , 'lib' . $item . 'P.so') if $opt_populateProd;
4937 dpurdie 606
    }
607
 
608
    #
609
    #   Where does it go
610
    #       JARs/AARs - ROOT/jatsLibs
611
    #       LIBS      - ROOT/jatsJni
612
    #
613
    #   Scan all external packages, and the interface directory
614
    #       Transfer in the required file types
615
    #
616
    my @pkg_paths = getPackagePaths("--Interface=$opt_interface");
617
    foreach my $pkg ( @pkg_paths)
618
    {
619
        #
620
        #   Copy in all JAR files found in dependent packages
621
        #   Need to allow for Jars that have a P/D suffix as well as those that don't
622
        #
623
        my $jarDir = catdir($pkg,'jar');
4990 dpurdie 624
        push @jarSearch, $jarDir;
4937 dpurdie 625
        if (-d $jarDir && @jlist)
626
        {
627
            Verbose("Jar Dir Found found", $jarDir);
628
            Message ("Copy in: $jarDir");
629
 
630
            #
631
            #   Create a matchlist from the JAR list
632
            #   Create a regular expresssion to find a suitable file
633
            #
634
            my @mlist;
635
            foreach  ( @jlist) {
636
                push @mlist, $_ . '.jar|' . $_ . 'P.jar|'. $_ . 'D.jar';
637
            }
638
            CopyDir ( $jarDir, $androidJars,
639
                        'MatchRE' => \@mlist,
640
                        'Log' => $opt_verbose + 1,
641
                        'SymlinkFiles' => 1,
642
                        'Examine' => sub 
643
                            {
644
                                my ($opt) = @_;
645
                                my $baseName = $opt->{file};
646
                                $baseName =~ s~\.jar~~;
647
                                $baseName =~ s~[PD]$~~;
648
                                ArrayDelete \@jlist, $baseName;
649
                                return 1;
650
                            },
651
                    );
652
        }
653
 
654
        #
655
        #   Copy in JARs specified by a full pathname
656
        #   Need to allow for Jars that have a P/D suffix as well as those that don't
657
        #
658
        my @jpathlistBase = @jpathlist;
659
        foreach my $file (@jpathlistBase) 
660
        {
661
            foreach my $suffix ( '', 'P' ,'D')
662
            {
663
                my $jarFile = catdir($pkg, $file . $suffix . '.jar');
4990 dpurdie 664
                push @jarSearch, $jarFile;
4937 dpurdie 665
                if (-f $jarFile)
666
                {
667
                    Verbose("Jar File Found found", $jarDir);
668
                    Message ("Copy in: $jarFile");
669
                    CopyFile ( $jarFile, $androidJars,
670
                                'Log' => $opt_verbose + 1,
671
                                'SymlinkFiles' => 1,
672
                             );
673
                    ArrayDelete \@jpathlist, $file;
674
                }
675
            }
676
        }
677
 
678
        #
679
        #   Copy in AAR files found in dependent packages
680
        #   Need to allow for both AAR files with a -debug/-release suffix 
681
        #   as well as those without
682
        #
683
        foreach my $part (@platformParts)
684
        {
685
            my $aarDir = catdir($pkg,'lib/' . $part);
4990 dpurdie 686
            push @aarSearch, $aarDir;
4937 dpurdie 687
            if (-d $aarDir && @alist)
688
            {
689
                Verbose("Library Dir Found found", $aarDir);
690
                Message ("Copy in: $aarDir");
691
 
692
                #
693
                #   Create a matchlist from the AAR list
694
                #   Create a regular expresssion to find a suitable file
695
                #
696
                my @mlist;
697
                foreach  ( @alist) {
698
                    push @mlist, $_ . '.aar|' . $_ . '-debug.aar|' . $_ . '-release.aar';
699
                }
700
 
701
                CopyDir ( $aarDir, $androidJars,
702
                            'MatchRE' => \@mlist,
703
                            'Log' => $opt_verbose + 1,
704
                            'SymlinkFiles' => 1,
705
                            'Examine' => sub 
706
                                {
707
                                    my ($opt) = @_;
708
                                    my $baseName = $opt->{file};
709
                                    $baseName =~ s~\.aar$~~;
710
                                    $baseName =~ s~-release$~~;
711
                                    $baseName =~ s~-debug$~~;
712
                                    ArrayDelete \@alist, $baseName;
713
                                    return 1;
714
                                },
715
                            );
716
            }
717
        }
718
 
719
 
720
        #
721
        #   Copy in AAR files specified by a full pathname
722
        #   Need to allow for both AAR files with a -debug/-release suffix 
723
        #   as well as those without
724
        #
725
        my @apathlistBase = @apathlist;
726
        foreach my $file (@apathlistBase) 
727
        {
728
            foreach my $suffix ( '', '-release', '-debug')
729
            {
730
                my $aarFile = catdir($pkg, $file . $suffix . '.aar');
4990 dpurdie 731
                push @aarSearch, $aarFile;
4937 dpurdie 732
                if (-f $aarFile)
733
                {
734
                    Verbose("Aar File Found found", $aarFile);
735
                    Message ("Copy in: $aarFile");
736
                    CopyFile ( $aarFile, $androidJars,
737
                                'Log' => $opt_verbose + 1,
738
                                'SymlinkFiles' => 1,
739
                             );
740
                    ArrayDelete \@apathlist, $file;
741
                }
742
            }
743
        }
744
 
745
        #
746
        #   Build up the Shared Library structure as used by JNI
747
        #   Note: Only support current JATS format
748
        #   Copy in .so files and in to process massage the pathname so that
749
        #   it confirms to that expected by the Android Project
750
        #
751
        my $libDir = catdir($pkg, 'lib');
4990 dpurdie 752
        push @libSearch, $libDir;
4937 dpurdie 753
        if (-d $libDir && @libListProd)
754
        {
755
            Verbose("Lib Dir Found found", $libDir);
756
            Message ("Copy in: $libDir");
757
            CopyDir ( $libDir, $androidJniProd,
758
                        'Match' => \@libListProd,
759
                        'Log' => $opt_verbose + 1,
760
                        'SymlinkFiles' => 1,
761
                        'Examine' => sub 
762
                            { 
763
                                my ($opt) = @_;
764
                                foreach my $platform ( keys %SharedLibMap ) {
765
                                    my $replace = $SharedLibMap{$platform};
766
                                    if ($opt->{'target'} =~ s~/$platform/~/$replace/~)
767
                                    {
768
                                        ArrayDelete \@libListProd, $opt->{file};
769
                                        return 1;
770
                                    }
771
                                }
772
                                return 0;
773
                            },
774
                    );
775
        }
776
 
777
        if (-d $libDir && @libListDebug)
778
        {
779
            Verbose("Lib Dir Found found", $libDir);
780
            Message ("Copy in: $libDir");
781
            CopyDir ( $libDir, $androidJniDebug,
782
                        'Match' => \@libListDebug,
783
                        'Log' => $opt_verbose + 1,
784
                        'SymlinkFiles' => 1,
785
                        'Examine' => sub 
786
                            { 
787
                                my ($opt) = @_;
788
                                foreach my $platform ( keys %SharedLibMap ) {
789
                                    my $replace = $SharedLibMap{$platform};
790
                                    if ($opt->{'target'} =~ s~/$platform/~/$replace/~)
791
                                    {
792
                                        ArrayDelete \@libListDebug, $opt->{file};
793
                                        return 1;
794
                                    }
795
                                }
796
                                return 0;
797
                            },
798
                    );
799
        }
800
 
801
    }
802
 
803
    #
804
    #   Report files that could not be located. They were deleted from the lists
805
    #   as they were processed
806
    #
4990 dpurdie 807
    if (@jlist || @jpathlist || @alist || @apathlist ||  @libListProd || @libListDebug )
4937 dpurdie 808
    {
4990 dpurdie 809
        ReportError("External dependencies not found:", @jlist , @jpathlist , @alist , @apathlist ,  @libListProd , @libListDebug);
810
        if (@jlist || @jpathlist)
811
        {
812
            ReportError("Jar Search Path", @jarSearch);
813
        }
814
 
815
        if (@alist || @apathlist)
816
        {
817
            ReportError("Aar Search Path", @aarSearch);
818
        }
819
 
820
        if ( @libListProd || @libListDebug)
821
        {
822
            ReportError("Lib Search Path", @libSearch);
823
        }
824
    ErrorDoExit();
4937 dpurdie 825
    }
826
}
827
 
828
#-------------------------------------------------------------------------------
829
# Function        : deleteGeneratedFiles 
830
#
831
# Description     : Delete files that we generate
832
#
833
# Inputs          : 
834
#
835
# Returns         : 
836
#
837
sub deleteGeneratedFiles
838
{
839
    #
840
    #   Delete files that we will create
841
    #
842
    my @deleteList = qw(local.properties gradle.properties);
843
    foreach my $file (@deleteList)
844
    {
845
        Verbose ("Deleting $project_root/$file");
846
        unlink catfile($project_root, $file);
847
    }
848
 
849
}
850
 
851
#-------------------------------------------------------------------------------
852
# Function        : deleteInjectedFiles 
853
#
854
# Description     : Delete files that we inject
855
#
856
# Inputs          : 
857
#
858
# Returns         : 
859
#
860
sub deleteInjectedFiles
861
{
862
    #
863
    #   Remove the jatsJars and JatsJni directories
864
    #   These are created by this tool
865
    #
866
    Verbose("RmDirTree($androidJars)");
867
    RmDirTree($androidJars);
868
 
869
    Verbose("RmDirTree($androidJniBase)");
870
    RmDirTree($androidJniBase);
871
}
872
 
873
 
874
#-------------------------------------------------------------------------------
875
# Function        : NicePath 
876
#
877
# Description     : Process a path and return one that is
878
#                       An absolute Path
879
#                       Uses '/'
880
#
881
# Inputs          : path            - Path to process
882
#
883
# Returns         : A Nice version of path
884
#
885
sub NicePath
886
{
887
    my ($path) = @_;
888
    $path = FullPath($path);
889
    $path =~ s~\\~/~g;
890
    $path = CleanPath($path);
891
    return $path;
892
}
893