Subversion Repositories DevTools

Rev

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