Subversion Repositories DevTools

Rev

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