Subversion Repositories DevTools

Rev

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