Subversion Repositories DevTools

Rev

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