Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
1119 dpurdie 1
########################################################################
6302 dpurdie 2
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
1119 dpurdie 3
#
6302 dpurdie 4
# Module name   : ManifestFiles.pm
1119 dpurdie 5
# Module type   : Makefile system
6302 dpurdie 6
# Environment(s): JATS Build System
5654 dpurdie 7
# Documents     : MASS-00232 Format of the Linux App Upgrade Manifest File
1119 dpurdie 8
#
9
# Description   : This package extends the JATS toolset at build time
10
#                 It provides additional directives to the JATS makefiles
11
#                 to simplify the directives.
12
#
13
#                 This directive does all its work at 'build' time
14
#                 It uses makefile.pl directives
15
#
16
# Operation     : This package adds the JATS directive ManifestFiles
17
#                 This is used to create linux manifest files
18
#
19
# Syntax        : ManifestFiles (<platforms>, Options+);
20
#                 See the function header for details
21
#
22
#......................................................................#
23
 
24
require 5.6.1;
25
use strict;
26
use warnings;
6306 dpurdie 27
use ArrayHashUtils;
1119 dpurdie 28
 
29
#
30
#   Globals
6932 dpurdie 31
#   These are exposed to all of JATS, so:
32
#       Limit the number (ideally none)
33
#       Be away of the potential name conflicts
1119 dpurdie 34
#
35
 
36
#-------------------------------------------------------------------------------
37
# Function        : BEGIN
38
#
39
# Description     : Setup directive hooks
40
#                   Register a function to be called just before we start to
41
#                   generate makefiles. This will be used to process the data
42
#                   collected in all the ManifestFiles directives
43
#
44
# Inputs          : None
45
#
46
# Returns         : None
47
#
48
BEGIN
49
{
6932 dpurdie 50
    RegisterMakefileGenerate (\&ManifestFiles::Generate);
1119 dpurdie 51
}
52
 
53
#-------------------------------------------------------------------------------
54
# Function        : ManifestFiles
55
#
56
# Description     : Create a ManifestFiles entry
57
#
58
# Inputs          : Platform             - Active Platform Selector
59
#                   Options              - One or more options
60
#                       --Name=Name             - Mandatory
61
#                       --Tier=Name             - Mandatory
62
#                       --Architecture=xxxx     - Default actitecture is the current target
63
#                       --Product=yyyy          - Product Family
64
#                       --Debian=BaseName[,--Prod,--Debug,--Arch=xxxx,--Product=yyyy]
6302 dpurdie 65
#                       --Apk=BaseName[,--Prod,--Debug,--Platform=pppp]
66
#                                                   Default platform = ANDROID
67
#                                                   Default type = Production
1119 dpurdie 68
#                       --MugPackage=Package[,--Subdir=subdir[,subdir]+]
69
#                       --SrcFile=xxx
1125 alewis 70
#                       --SrcFileNoCopy=xxx
1123 dpurdie 71
#                       --Comment=xxx           - Add comment to Manifst
1125 alewis 72
#                       --NoManifestVersion     - Create unversioned Manifest File
73
#                       --PkgSubdir=xxx         - Specifies packaging subdir
5105 dpurdie 74
#                       --ImportManifest=Package[,--Subdir=subdir,--ReWrite] - Import Manifest from package
5649 dpurdie 75
#                       --Md5                   - Add MD5 checksums to the Manifest File
5767 alewis 76
#                       --Dmf                   - Generate the Device Management Framework
77
#                                                 combined archive ZIP file.
78
#                       --DmfVersion=xxxx       - Generate the Device Management Framework
79
#                                                 combined archive ZIP using a modified
80
#                                                 version number; only for testing!
6364 dpurdie 81
#                       --LineLength=nnn        - Limit line length. Default is 79
82
#                       --SubManifest           - Create a sub-manifest (not installable). Manifest will have no version.
83
#                                                 Gives a default name, tier and subdir if not explicitly provided.
6932 dpurdie 84
#                       --NoSignatures          - Prevent the generation of .sig files
85
#                                                 Default is to generate .sigFiles
86
#                       --NoWarn                - Supress warnings about unused packages
87
#                                                 Will supress sanity test. Useful when generating multiple
88
#                                                 manifests.                          
89
#                                                 
90
# Notes: On sig file generation
91
#   This tool will use an internal key file to generate signatures
92
#   UNLESS the tool is being run on a special build machine with a controlled signature file
93
#   the generated signatures will not work on-target.
94
#   
1119 dpurdie 95
#
96
# Returns         : Nothing
97
#
98
sub ManifestFiles
99
{
100
    my( $platforms, @elements ) = @_;
101
    Debug2( "ManifestFiles($platforms, @elements)" );
102
    return if ( ! ActivePlatform($platforms) );
103
 
104
    my $name;
105
    my $tier;
106
    my @files;
6306 dpurdie 107
    my %fileVersions;
1119 dpurdie 108
    my $mug_dir;
109
    my $default_arch = $::ScmPlatform;
110
    my $default_prod = '';
1129 dpurdie 111
    my $imported_manifest = 0;
5649 dpurdie 112
    my $include_md5 = 0;
5767 alewis 113
    my $generate_dmf = 0;
5771 alewis 114
    my $dmf_version = $::ScmBuildVersionFull;
6314 dpurdie 115
    my $useDefaultLineWidth = 1;
6364 dpurdie 116
    my $is_sub_manifest = 0;
1119 dpurdie 117
 
118
    #
119
    #   Collect user options
120
    #
121
    foreach ( @elements )
122
    {
123
        if ( m~^--Name=(.+)~ ) {
6302 dpurdie 124
            if ( $name ) {
1119 dpurdie 125
                ReportError ("ManifestFiles:--Name option is only allowed once");
126
                next;
127
            }
128
            $name = $1;
129
 
130
        } elsif ( m~^--Tier=(.+)~ ) {
6302 dpurdie 131
            if ( $tier ) {
1119 dpurdie 132
                ReportError ("ManifestFiles:--Tier option is only allowed once");
133
                next;
134
            }
135
            $tier = $1;
136
 
1123 dpurdie 137
        } elsif ( m~^--Comment=~ ) {
138
            my $cmt = $_;
139
            $cmt =~ s~.+=~~;
140
            $cmt =~ s~\s*\n\s*~\n~g;
141
            push @files, {'cmt' => $cmt };
142
 
1119 dpurdie 143
        } elsif ( m~^--Debian=(.+)~ ) {
6932 dpurdie 144
            push @files, {'file' => ManifestFiles::_LocateDebianFile($1, $default_arch, $default_prod)};
1119 dpurdie 145
 
6302 dpurdie 146
        } elsif ( m~^--Apk=(.+)~ ) {
6932 dpurdie 147
            my $apkData = ManifestFiles::_LocateApkFile($1, $default_arch);
6306 dpurdie 148
            my ($fname, $fversion) = split($;, $apkData);
149
            $fileVersions{$fname} = $fversion;
150
            push @files, {'file' => $fname };
6314 dpurdie 151
            $useDefaultLineWidth = 0;
6302 dpurdie 152
 
1119 dpurdie 153
        } elsif ( m~^--SrcFile=(.+)~ ) {
1123 dpurdie 154
            push @files, {'file' => LocatePreReq($1)};
5649 dpurdie 155
 
1125 alewis 156
        } elsif ( m~^--SrcFileNoCopy=(.+)~ ) {
157
            push @files, {'filenocopy' => $1};
5649 dpurdie 158
 
1119 dpurdie 159
        } elsif ( m~^--MugPackage=(.+)~ ) {
6302 dpurdie 160
            if ( $mug_dir ) {
1119 dpurdie 161
                ReportError ("ManifestFiles:--MugPackage option is only allowed once");
162
                next;
5649 dpurdie 163
            }
6932 dpurdie 164
            $mug_dir = ManifestFiles::_LocateMugDir($1);
1119 dpurdie 165
 
166
        } elsif ( m/^--Arch(.*)=(.+)/ ) {
167
            $default_arch = $2;
168
 
169
        } elsif ( m/^--Product=(.+)/ ) {
170
            $default_prod = $1;
171
 
1125 alewis 172
        } elsif ( m/^--NoManifestVersion/i ) {
6932 dpurdie 173
            $ManifestFiles::Manifest_has_version = 0;
1125 alewis 174
 
175
        } elsif ( m/^--PkgSubdir=(.+)/i ) {
6932 dpurdie 176
            if ( $ManifestFiles::pkg_subdir ) {
1129 dpurdie 177
                ReportError ("ManifestFiles:--PkgSubdir option is only allowed once");
1125 alewis 178
                next;
179
            }
6932 dpurdie 180
            $ManifestFiles::pkg_subdir = $1;
1125 alewis 181
 
1129 dpurdie 182
        } elsif ( m/^--ImportManifest=(.+)/i ) {
6932 dpurdie 183
            my $import_info = ManifestFiles::_ImportManifest($1, $tier, $name);
1129 dpurdie 184
#DebugDumpData("ImportInfo", $import_info );
185
            push @files, {'manifest' => $import_info };
186
 
187
            #
188
            #   Fill in details unless already provided
189
            #
190
            $tier = $import_info->{'tier'} unless ( defined $tier );
191
            $name = $import_info->{'name'} unless ( defined $name );
192
            $imported_manifest = 1;
193
 
5649 dpurdie 194
        } elsif (m/^--Md5/i) {
6314 dpurdie 195
            $include_md5 = 1;
196
            $useDefaultLineWidth = 0;
5649 dpurdie 197
 
5767 alewis 198
        } elsif (m/^--Dmf/i) {
199
            $generate_dmf = 1
200
 
201
        } elsif ( m/^--DmfVersion=(.+)/ ) {
202
            $generate_dmf = 1;
203
            $dmf_version = $1;
204
 
6314 dpurdie 205
        } elsif ( m/^--LineLength=(\d+)$/i ) {
6932 dpurdie 206
            $ManifestFiles::ManifestLineWidth = $1;
6314 dpurdie 207
            $useDefaultLineWidth = 0;
208
 
6364 dpurdie 209
        } elsif (m/^--SubManifest/i) {
210
            $is_sub_manifest = 1;
6932 dpurdie 211
            $ManifestFiles::Manifest_has_version = 0;
212
            $name = $::ScmPlatform unless $name;
213
            $ManifestFiles::pkg_subdir = $name unless $ManifestFiles::pkg_subdir;
214
            $tier = 0 unless defined($tier);
6364 dpurdie 215
 
6932 dpurdie 216
        } elsif (m/^--(No)?Signatures/i) {
217
           $ManifestFiles::noSigs = !! $1;
218
 
219
        } elsif (m/^--(No)?Warn/i) {
220
           $ManifestFiles::noWarn = !! $1;
221
 
1119 dpurdie 222
        } else {
223
            ReportError ("ManifestFiles: Unknown option or argument: $_");
224
 
225
        }
226
    }
227
 
228
    #
229
    #   Sanity test the user options
230
    #
231
    ReportError ("ManifestFiles: No name specified")
232
        unless $name;
233
    ReportError ("ManifestFiles: No tier specified")
1129 dpurdie 234
        unless defined ($tier);
6302 dpurdie 235
    ReportError ("ManifestFiles: Cannot mix --Debian/-Apk/--SrcFile with --MugPackage in one directive")
1129 dpurdie 236
        if ( $mug_dir && (@files || $imported_manifest) );
1119 dpurdie 237
    ReportError ("ManifestFiles: Must specify files to add to Manifest")
1129 dpurdie 238
        unless ( $mug_dir ||  @files || $imported_manifest);
1119 dpurdie 239
    ErrorDoExit();
240
 
241
    #
6314 dpurdie 242
    #   Set ManifestLineWidth
243
    #   The default is largely historical - for MOS
244
    #   
6932 dpurdie 245
    unless (defined $ManifestFiles::ManifestLineWidth) {
246
        $ManifestFiles::ManifestLineWidth = $useDefaultLineWidth ? 79 : 0;
6314 dpurdie 247
    }
6932 dpurdie 248
    Verbose("ManifestLineWidth:$ManifestFiles::ManifestLineWidth");
6314 dpurdie 249
 
250
    #
1119 dpurdie 251
    #   Save information for processing at the end of the parsing phase
252
    #   Data collected from ALL the ManifestFiles directives will be collected
253
    #   and processed into one Manifest file
254
    #
255
    my %data;
256
    $data{tier} = $tier;
257
    $data{name} = $name;
258
    $data{files} = \@files;
6306 dpurdie 259
    $data{fileVersions} = \%fileVersions;
1119 dpurdie 260
    $data{mugdir} = $mug_dir;
6932 dpurdie 261
    $data{pkgsubdir} = $ManifestFiles::pkg_subdir;
5649 dpurdie 262
    $data{md5} = $include_md5;
5767 alewis 263
    $data{dmf} = $generate_dmf;
264
    $data{arch} = $default_arch;
265
    $data{dmf_version} = $dmf_version;
6364 dpurdie 266
    $data{is_sub_manifest} = $is_sub_manifest;
5767 alewis 267
 
1129 dpurdie 268
#DebugDumpData("DirectiveData", \%data );
1119 dpurdie 269
 
6932 dpurdie 270
    push @ManifestFiles::Manifests, \%data;
6302 dpurdie 271
}
1119 dpurdie 272
 
6932 dpurdie 273
#==================================================================================================
274
#   End of externally exposed methods
6933 dpurdie 275
#       The remainder is package-internal
6932 dpurdie 276
#
277
package ManifestFiles;
278
 
279
use strict;
280
use warnings;
281
use Digest::file qw(digest_file_hex);
282
use Digest::file qw(digest_file_base64);
283
use File::Spec::Functions qw(rel2abs);
284
use File::Basename qw(dirname);
285
 
286
use JatsError;
287
use FileUtils;
288
use ReadBuildConfig;
289
use ArrayHashUtils;
290
use JatsSystem;
291
 
292
#
293
#   Variables
294
#   Config - must be 'our'
295
#
296
our @Manifests;                     # Manifest entries
297
our $Manifest_has_version = 1;      # Create Manifest_xxxx by default
298
our $pkg_subdir;                    # Alternate packaging
299
our $ManifestLineWidth;             # Max length of lines
300
 
301
our %package_dirs;                  # Package dirs discovered and used
302
our $ManifestFiles;                 # Files in the Manifest
303
our $noWarn = 0;                    # Control unused package warnings
304
our $pkgBase;                       # Where the package is being sourced from
6933 dpurdie 305
our $noSigs = 1;                    # Control signature generation (Force noSigs. could use use undef)
6932 dpurdie 306
 
307
#
308
#   Internal
309
#
310
my $certName;                       # Signing Cert Name
311
my $certFile;                       # Path to the certificate
312
my $keyFile;                        # Path to the keyfile
313
my $opensslProg;                    # Path to openssl program
314
my $opensslConf;                    # Path to openssl config
315
 
316
BEGIN {
317
    my $path = rel2abs( __FILE__ );
318
    $pkgBase = dirname( $path );
319
}
320
 
6302 dpurdie 321
#-------------------------------------------------------------------------------
6932 dpurdie 322
# Bring in the DMF build requirements.
323
use lib $pkgBase;
324
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
325
use JSON;
326
 
327
#-------------------------------------------------------------------------------
6302 dpurdie 328
# Function        : _LocateDebianFile
329
#
330
# Description     : Locate a debian file
331
#                   Internal Function
332
#
333
#                   Scan packages for the Debian package specified
334
#                   The user provides the base name of the package
335
#                   A Debian Package name has several fields
336
#                   These are:
337
#                       1) Base Name - Provided by the user
6730 dpurdie 338
#                       2) Version - Default Version will be wildcarded if it hadn't been provided by user
6302 dpurdie 339
#                       3) Architecture - Wildcarded. Uses bin/arch directory
340
#
341
#                   Expect to find Debian Packages in the bin/PLATFORM subdir
342
#
343
# Inputs          : Debian base name, complete with suboptions
344
#
345
# Returns         : Full path of the file
346
#
347
sub _LocateDebianFile
348
{
349
    my ($arg, $arch, $product) = @_;
350
    Verbose("LocateDebianFile: Processing: $arg");
351
 
352
    my @type = qw( P D );
353
    my @debian_file_path;
6306 dpurdie 354
    my @searchPath;
6730 dpurdie 355
    my $version='*';
6302 dpurdie 356
 
1119 dpurdie 357
    #
6302 dpurdie 358
    #   Extract sub-options
359
    #       --Prod[uction]
360
    #       --Debug
361
    #       --Arch[itecture]=yyy
362
    #       --Product=yyy
6730 dpurdie 363
    #       --Version=yy.yy.yyyyy  default value is wildcard 
1119 dpurdie 364
    #
6302 dpurdie 365
    my ($base_name, @opts) = split( ',', $arg );
366
    foreach ( @opts )
367
    {
368
        if ( m/^--Arch(.*)=(.+)/ ) {
369
            $arch=$2;
370
        } elsif ( m/^--Product=(.+)/ ) {
371
            $product=$1;
372
        } elsif ( m/^--Prod/ ) {
373
            @type = 'P';
374
        } elsif ( m/^--Debug/ ) {
375
            @type = 'D';
6730 dpurdie 376
        } elsif ( m/^--Version=(.+)/ ) {
377
            $version=$1;
6302 dpurdie 378
        } else {
379
            Warning ('--Debian: Unknown Option: ' . $_);
380
        }
381
    }
382
 
5649 dpurdie 383
    #
6302 dpurdie 384
    #   Create a list of products
385
    #   ie: PRODUCT_ARCH
1119 dpurdie 386
    #
6302 dpurdie 387
    my @products;
388
    push @products, $product . '_' . $arch if ( $product );
389
    push @products, $arch;
390
 
1119 dpurdie 391
    #
6302 dpurdie 392
    #   Scan all packages for the specified debian package
1119 dpurdie 393
    #
6302 dpurdie 394
    foreach my $package_dir ( getPackagePaths ('--All') )
1119 dpurdie 395
    {
6302 dpurdie 396
        foreach my $type ( @type )
1119 dpurdie 397
        {
6302 dpurdie 398
            foreach my $prd ( @products )
1119 dpurdie 399
            {
6302 dpurdie 400
                foreach my $joiner ( qw(/ .) )
1119 dpurdie 401
                {
6302 dpurdie 402
                    my $dir = "$package_dir/bin$joiner$prd$type";
6306 dpurdie 403
                    UniquePush(\@searchPath, $dir);
5873 acammell 404
                    next unless ( -d $dir );
6734 dpurdie 405
                    my @files = glob ( "$dir/${base_name}_${version}*.deb" );
5873 acammell 406
                    next unless ( @files );
407
                    push @debian_file_path, @files;
408
                    $package_dirs{$package_dir}{used} = 1;
409
                }
410
            }
1119 dpurdie 411
        }
6302 dpurdie 412
        foreach my $type ( @type )
413
        {
414
            foreach my $prd ( @products )
415
            {
416
                my $dir = "$package_dir";
6306 dpurdie 417
                UniquePush(\@searchPath, $dir);
6730 dpurdie 418
                next unless ( -d $dir ); 
419
                if ( $version eq "*" ){
420
                    my @files = glob ( "$dir/${base_name}_${version}_${prd}_${type}.deb" );
421
                    if ( @files ){
422
                        push @debian_file_path, @files;
423
                        $package_dirs{$package_dir}{used} = 1;
424
                    }
425
                }
426
                else{
427
                    if( -e "$dir/${base_name}_${version}_${prd}_${type}.deb" ) {
428
                        my $file = "$dir/${base_name}_${version}_${prd}_${type}.deb";
429
                        push(@debian_file_path, $file);
430
                        $package_dirs{$package_dir}{used} = 1;
431
                    } elsif ( -e "$dir/${base_name}_${version}_${prd}.deb") {
432
                        my $file = "$dir/${base_name}_${version}_${prd}.deb";
433
                        push(@debian_file_path, $file);
434
                        $package_dirs{$package_dir}{used} = 1;
6734 dpurdie 435
                    }
6730 dpurdie 436
                }
6302 dpurdie 437
            }
438
        }
1119 dpurdie 439
    }
440
 
6306 dpurdie 441
    #
442
    #   Keep user informed
443
    #   Report errors and provide useful information
444
    #
445
    if (IsVerbose(1) || IsDebug(1) || $#debian_file_path != 0)
446
    {
447
        Message ("Search for ($base_name). In search Path", @searchPath);
448
    }
6730 dpurdie 449
 
6302 dpurdie 450
    ReportError ("Required Debian package not found: $base_name") unless @debian_file_path;
451
    ReportError ("Multiple matching Debian Packages located: $base_name", @debian_file_path ) if ( $#debian_file_path > 0 );
452
    return $debian_file_path[0];
453
}
454
 
455
#-------------------------------------------------------------------------------
456
# Function        : _LocateApkFile
457
#
458
# Description     : Locate a APK file
459
#                   Internal Function
460
#
461
#                   Scan packages for the APK package specified
462
#                   The user provides the base name of the package
463
#                   APK ( Android Packages )
464
#                   Expect to have a '-release' or '-debug' suffix, except those provided via a
465
#                   3rd party SDK.
466
#                   Expect to find APK Packages in the bin/PLATFORM(P/D) subdir
467
#                   Expect to find -debug in the <PLATFORM>D directory
468
#                   Expect to find -release' in the <PLATFORM>P directory
469
#                   
470
#                   Allow for:
471
#                       Full path to .apk file
472
#                       .apk in package root directory
473
#
474
# Inputs          : Apk base name, complete with suboptions
475
#
6306 dpurdie 476
# Returns         : Full path of the file $; PackageVersion
477
#                   apk packages do not appear to have version numbers in the file name
478
#                   Retain package version number for later processing
6302 dpurdie 479
#
480
sub _LocateApkFile
481
{
482
    my ($arg, $arch) = @_;
483
    Verbose("LocateApkFile: Processing: $arg");
484
 
485
    my @type = qw( P );
486
    my @apk_file_path;
487
    my %type = ('P' => '-release', 'D' => '-debug' );
6306 dpurdie 488
    my @searchPath;
6302 dpurdie 489
 
1119 dpurdie 490
    #
6302 dpurdie 491
    #   Extract sub-options
492
    #       --Prod[uction]
493
    #       --Debug
494
    #       --Architecture=yyy
1119 dpurdie 495
    #
6302 dpurdie 496
    my ($base_name, @opts) = split( ',', $arg );
497
    foreach ( @opts )
1119 dpurdie 498
    {
6302 dpurdie 499
        if ( m/^--Arch(.*)=(.+)/ ) {
500
            $arch=$2;
501
        } elsif ( m/^--Prod/ ) {
502
            @type = 'P';
503
        } elsif ( m/^--Debug/ ) {
504
            @type = 'D';
505
        } else {
506
            Warning ('--Apk: Unknown Option: ' . $_);
1119 dpurdie 507
        }
6302 dpurdie 508
    }
1119 dpurdie 509
 
6302 dpurdie 510
    #
511
    #   Scan all packages for the specified APK package
512
    #   Try:
513
    #       Raw name - for apks from the SDK or 3rd parties
514
    #       PLATFORM(P|D)/baseName-(release|debug) - Expected
515
    #       baseName-(release|debug) - Repackaged badly
6306 dpurdie 516
    # 
517
    foreach my $pkgEntry ( getPackageList() )
6302 dpurdie 518
    {
6306 dpurdie 519
        next if ($pkgEntry->getType() eq 'interface');
520
        my $pkgVersion = $pkgEntry->getVersion();
521
 
522
        my $pkgLocal = $pkgEntry->getBase(2);
523
        my $pkgRoot = $pkgEntry->getDir();
1119 dpurdie 524
 
6306 dpurdie 525
        #
526
        #   Helper function
527
        #   Uses closure
528
        #   Notes: Test the package in dpkg_archive so that we can retain the package-version
529
        #          Use the version in the interface directory if BuildPkgArchive
530
        #   $pkgLocal - Local base of the package. May in the interface directory
531
        #   $pkgRoot  - Directory in dpkg_achive. Will have version info
532
        #   $subdir   - subdir within the package
533
        #   $fname    - File to look for
534
        #   
535
        #   Returns: Nothing
536
        #   Maintains: apk_file_path. Tupple: filename, PackageVersion
537
        #
538
        my $testOneFile = sub {
539
            my ( $subdir, $fname) = @_;
540
            my $testFile = "$pkgRoot/$subdir";
541
            $testFile =~ s~//~/~g;
542
            $testFile =~ s~/$~~;
543
            UniquePush(\@searchPath, $testFile);
544
            return unless (-d $testFile);
6302 dpurdie 545
 
6306 dpurdie 546
            $testFile .= '/' . $fname;
547
            if (-f $testFile ) {
548
                if ($pkgLocal ne $pkgRoot) {
549
                    my $testFile2 = "$pkgLocal/$subdir/$fname";
550
                    $testFile2 =~ s~//~/~g;
551
                    if ( -f $testFile2 ) {
552
                        $testFile = $testFile2;
553
                    }
554
                }
555
 
556
             $testFile = join($;, $testFile, $pkgVersion);
557
             push @apk_file_path, $testFile;
558
            }
559
        };
560
 
561
        #
562
        #   Test for the specified file in the package root
563
        #
564
        $testOneFile->("", "${base_name}.apk");
565
 
566
        #
567
        #   Test for BIN/PLATFORM
568
        #   
6302 dpurdie 569
        foreach my $type ( @type )
1119 dpurdie 570
        {
6302 dpurdie 571
            my $typeSuffix = $type{$type};
6306 dpurdie 572
            foreach my $joiner ( qw(/ .) ) {
573
                $testOneFile->("bin$joiner$arch$type","${base_name}${typeSuffix}.apk");
1119 dpurdie 574
            }
575
        }
6306 dpurdie 576
 
6302 dpurdie 577
        foreach my $type ( @type )
578
        {
579
            my $typeSuffix = $type{$type};
6306 dpurdie 580
            $testOneFile->("","${base_name}${typeSuffix}.apk");
6302 dpurdie 581
        }
6306 dpurdie 582
        $package_dirs{$pkgRoot}{used} = 1 if (@apk_file_path) ;
1119 dpurdie 583
    }
1129 dpurdie 584
 
6306 dpurdie 585
    #
586
    #   Keep user informed
587
    #   Report errors and provide useful information
588
    #
589
    if (IsVerbose(1) || IsDebug(1) || $#apk_file_path != 0)
590
    {
591
        Message ("Search for ($base_name). In search Path", @searchPath);
592
    }
593
 
6302 dpurdie 594
    ReportError ("Required APK package not found: $base_name") unless @apk_file_path;
595
    ReportError ("Multiple matching APK Packages located: $base_name", @apk_file_path ) if ( $#apk_file_path > 0 );
6306 dpurdie 596
 
597
#DebugDumpData("apk_file_path", \@apk_file_path);
6302 dpurdie 598
    return $apk_file_path[0];
599
}
600
 
601
#-------------------------------------------------------------------------------
602
# Function        : _LocateMugDir
603
#
604
# Description     : Locate the directory containing the mugfiles
605
#                   Internal Function
606
#
607
# Inputs          : Mufile package, with embedded options
608
#
609
# Returns         : Full path
610
#
611
sub _LocateMugDir
612
{
613
    my ($mug_package) = @_;
614
 
1129 dpurdie 615
    #
6302 dpurdie 616
    #   Locate the mugfile subdir
1129 dpurdie 617
    #
6302 dpurdie 618
    my $package_name = $mug_package;
619
    my @dirs = 'mug';
620
    my $mug_dir;
621
 
1129 dpurdie 622
    #
6302 dpurdie 623
    #   Extract sub options
624
    #       --Subdir=xxxx,yyyy,zzzz
1129 dpurdie 625
    #
6302 dpurdie 626
    if ( $package_name =~ m/(.*?),--Subdir=(.*)/ )
1129 dpurdie 627
    {
6302 dpurdie 628
        $package_name = $1;
629
        @dirs = split( ',', $2 );
630
    }
1129 dpurdie 631
 
6302 dpurdie 632
    my $package = GetPackageEntry( $package_name );
633
    unless ( $package )
634
    {
635
        ReportError ("ManifestFiles: Package not known to build: $package_name");
636
        return undef;
637
    }
1129 dpurdie 638
 
6302 dpurdie 639
    foreach my $subdir ( @dirs )
640
    {
641
        my $dir = "$package->{'ROOT'}/$subdir";
642
        if ( -d $dir )
1129 dpurdie 643
        {
6302 dpurdie 644
            Warning ("Multiple Mugfile directories located. Only the first will be used",
645
                     "Ignoring: $subdir" )if ( $mug_dir );
646
            $mug_dir = $dir;
647
        }
648
    }
649
    ReportError ("Mugfile directory not found in package: $package_name")
650
        unless $mug_dir;
5105 dpurdie 651
 
6302 dpurdie 652
    return $mug_dir;
653
}
5105 dpurdie 654
 
1129 dpurdie 655
 
6302 dpurdie 656
#-------------------------------------------------------------------------------
657
# Function        : _ImportManifest
658
#
659
# Description     : Import an existing manifest
660
#
661
# Inputs          : Args    - PackageName[,Subdir=name,--ReWrite]
662
#                   tier    - May be null
663
#                   name    - May be null
664
#
665
# Returns         : A hash of data to be used later
666
#
667
sub _ImportManifest
668
{
669
    my ($args, $tier, $name) = @_;
670
    my @file_contents;
6364 dpurdie 671
    my @item_list;
1129 dpurdie 672
 
6302 dpurdie 673
    #
674
    #   Locate the mugfile subdir
675
    #
676
    my $package_name = $args;
677
    my @dirs = 'mug';
678
    my $pkg_dir;
679
    my $pkg_root;
680
    my $manifest;
681
    my $first_tier;
682
    my $first_name;
683
    my $rewrite;
684
 
685
    #
686
    #   Extract sub options
687
    #       --Subdir=xxxx,yyyy,zzzz
688
    #       --ReWrite
689
    #
690
    if ( $package_name =~ m/(.*?)(,.*)/ )
691
    {
692
        $package_name = $1;
693
        my @subargs = split(',--', $2);
694
        foreach ( @subargs)
5105 dpurdie 695
        {
6302 dpurdie 696
            next unless (length($_) > 0);
697
            if (m~^Subdir=(.*)~i){
698
                @dirs = split( ',', $1 );
5105 dpurdie 699
 
6302 dpurdie 700
            } elsif (m~^ReWrite~i) {
701
                $rewrite = 1;
702
 
703
            } else {
704
                ReportError("ManifestFiles: Unknown suboption to ImportManifest:" . $_);
1129 dpurdie 705
            }
706
        }
6302 dpurdie 707
    }
1129 dpurdie 708
 
6302 dpurdie 709
    my $package = GetPackageEntry( $package_name );
710
    unless ( $package )
711
    {
712
        ReportError ("ManifestFiles: Package not known to build: $package_name");
713
        return undef;
714
    }
5105 dpurdie 715
 
6302 dpurdie 716
    if (defined ($rewrite) && ( !defined($tier) || !defined($name)))
717
    {
718
        ReportError ("ManifestFiles: ImportManifest. --ReWrite cannot be used unless tier and name are specified");
719
        return undef;
720
    }
1129 dpurdie 721
 
6302 dpurdie 722
    foreach my $subdir ( @dirs )
723
    {
724
        my $dir = "$package->{'ROOT'}/$subdir";
725
        my $root = $package->{'ROOT'};
726
        if ( -d $dir )
5105 dpurdie 727
        {
6302 dpurdie 728
            Warning ("Multiple Package directories located. Only the first will be used",
729
                     "Ignoring: $subdir" )if ( $pkg_dir );
730
            $pkg_dir = $dir;
731
            $pkg_root = $root;
5105 dpurdie 732
        }
6302 dpurdie 733
    }
1129 dpurdie 734
 
6302 dpurdie 735
    unless ($pkg_dir)
736
    {
737
        ReportError ("Package directory not found in package: $package_name");
738
        return undef;
739
    }
5105 dpurdie 740
 
6302 dpurdie 741
    #
742
    #   Determine Manifest File name
743
    #
744
    foreach my $file ( glob ($pkg_dir . '/Manifest*' ) )
745
    {
746
            next unless ( -f $file );
747
            Warning ("Multiple Manifest Files find. Only the first will be used",
748
                     "Using: $manifest",
749
                     "Ignoring: $file" ) if ( $manifest );
750
            $manifest = $file;
751
    }
752
 
753
    unless ($manifest)
754
    {
755
        ReportError ("ImportManifest. No Manifest found: $package_name");
756
        return undef;
757
    }
758
 
759
 
760
    #
761
    #
762
    #
763
    open (MF, '<', $manifest ) || Error ("Cannot open the Manifest file: $manifest", $!);
764
    while ( <MF> )
765
    {
1129 dpurdie 766
        #
6302 dpurdie 767
        #   Clean trailing whitespace ( line-feed and new lines )
768
        #   Comment out [Version] data
1129 dpurdie 769
        #
6302 dpurdie 770
        s~\s+$~~;
771
        s~(\s*\[Version])~#$1~;
772
 
5649 dpurdie 773
        #
6302 dpurdie 774
        #   Part lines and determine files
775
        #
776
        next unless ( $_ );
6364 dpurdie 777
        if (( m~\s*#~ ) || ( m~\s*\[~ )) {
778
            push @item_list, { 'comment' => $_ };
779
            next;
780
        }
781
        my( $aname, $atier, $afile, @additionnal_info) = split(/\s*\,\s*/, $_);
1129 dpurdie 782
#            print "---------- $_\n";
783
#            print "T: $atier, N:$aname, F:$afile\n";
6364 dpurdie 784
        my $file =  { 'file_name' => $afile
785
                    , 'file_info' => \@additionnal_info
786
                    };
787
        push @item_list, $file;
1129 dpurdie 788
 
6302 dpurdie 789
        #
790
        #   Rewrite the name and tier
791
        #
792
        if ($rewrite)
793
        {
794
            $_ = join(',', $name, $tier, $afile);
795
            $first_tier = $tier;
796
            $first_name = $name;
797
        }
798
        else
799
        {
1129 dpurdie 800
            #
6302 dpurdie 801
            #   Capture first tier and name
1129 dpurdie 802
            #
6302 dpurdie 803
            $first_tier = $atier unless ( defined $first_tier );
804
            $first_name = $aname unless ( defined $first_name );
1129 dpurdie 805
        }
6302 dpurdie 806
    }
807
    continue
808
    {
809
        push @file_contents, $_;
810
    }
811
    close MF;
1129 dpurdie 812
 
6302 dpurdie 813
    #
814
    #   Create a hash of data that describes the manifest that has
815
    #   just been read in.
816
    #
817
    $package_dirs{$pkg_root}{used} = 1;
818
    $manifest =~ s~.*/~~;
819
    return { 'contents' => \@file_contents,
6364 dpurdie 820
              'items' => \@item_list,
6302 dpurdie 821
              'file_base' => $pkg_dir,
822
              'manifest' => $manifest,
823
              'pkg_dir' => $pkg_root,
824
              'tier' => $first_tier,
825
              'name' => $first_name,
826
              'rewrite' => $rewrite,
827
            };
1119 dpurdie 828
}
829
 
830
#-------------------------------------------------------------------------------
6932 dpurdie 831
# Function        : Generate
1119 dpurdie 832
#
833
# Description     : Internal Function
834
#                   Process all the collected data and create directives
835
#                   for the creation of the manifest
836
#
837
#                   This function will be called, just before the Makefile
838
#                   is created. The function will:
839
#                       1) Create the Manifest File
840
#                       2) Package the Manifest File
841
#                       3) Package the manifest file contents
842
#
843
#                   using (mostly) normal makefile.pl directives.
844
#
845
# Inputs          : None
846
#
847
# Returns         : Nothing
848
#
6932 dpurdie 849
sub Generate
1119 dpurdie 850
{
6932 dpurdie 851
    Debug ("Generate");
1119 dpurdie 852
    Message ("Generating Manifest File");
1123 dpurdie 853
 
854
    #
855
    #   Need at least one Manifest Entry
856
    #
6932 dpurdie 857
#DebugDumpData ( "Manifests", \@Manifests );
1123 dpurdie 858
    return unless ( @Manifests );
1119 dpurdie 859
 
860
    #
6932 dpurdie 861
    #   Init the signing subsystem
862
    # 
863
    GenSigInit();
864
 
865
    #
1125 alewis 866
    #   Determine the target packaging directory
867
    #   Default is .../mug
868
    #
869
    my $pkgdir = 'mug';
1129 dpurdie 870
    if ( exists $Manifests[0]->{pkgsubdir} && defined $Manifests[0]->{pkgsubdir} )
1125 alewis 871
    {
872
        my $subdir = $Manifests[0]->{pkgsubdir};
873
        $pkgdir .= '/' . $subdir;
874
        $pkgdir =~ s~^mug/mug~mug~;
875
    }
876
 
877
    #
1119 dpurdie 878
    #   Create the Manifest File as we process the lists
1125 alewis 879
    #   Place this in the 'lib' directory:
1119 dpurdie 880
    #       - So that it will be deleted on clobber
1123 dpurdie 881
    #       - So that it can be placed in a target-specific subdir
1125 alewis 882
    #       - So that we can have one per makefile.pl
1119 dpurdie 883
    #
884
    Error ("ManifestFiles: Needs local directory specified in build.pl") unless ( $::ScmLocal );
885
 
1125 alewis 886
    my $manifest_dir = "$::ScmPlatform.LIB";
1119 dpurdie 887
    System( "$::GBE_BIN/mkdir -p $manifest_dir" );
888
 
1125 alewis 889
    my $manifest_file = $manifest_dir . '/Manifest';
890
    $manifest_file .= '_' . $::ScmBuildVersion if ( $Manifest_has_version );
6932 dpurdie 891
    ::ToolsetGenerate( $manifest_file );
892
    Verbose ("Generate: File: $manifest_file");
5649 dpurdie 893
 
6932 dpurdie 894
    PackageManifestFile ($manifest_file, $pkgdir);
1119 dpurdie 895
    open (MF, '>', $manifest_file ) || Error ("Cannot create the Manifest file: $manifest_file");
896
 
5711 kspencer 897
    binmode (MF);
5767 alewis 898
 
6364 dpurdie 899
    if ($Manifests[0]->{is_sub_manifest} == 1) {
900
        print_mf ("# Package $::ScmBuildPackage $::ScmBuildVersion built: $::CurrentTime");
901
    } else {
902
        print_mf ("# PackageName: $::ScmBuildPackage");
903
        print_mf ("# PackageVersion: $::ScmBuildVersion");
904
        print_mf ("# BuildDate: $::CurrentTime");
905
        print_mf ("#");
906
        print_mf ("[Version],$::ScmBuildVersion");
907
        print_mf ("#");
908
    }
1119 dpurdie 909
 
910
    #
6932 dpurdie 911
    #   Insert the certificate used to verify the files
912
    #       Currently we only handle one certificate
913
    #       Certificate name must end in .crt
914
    #   In production the .crt file MUST be signed by the VixManifestRoot-CA
915
    #   The assumtion is that the public key for VixManifestRoot-CA is on the device
916
    #
917
    unless (($Manifests[0]->{is_sub_manifest} == 1) || $noSigs) {
918
        print_mf ("");
919
        print_mf ("#");
920
        print_mf ("# Signing Certificate");
921
        print_mf ("[Certificate],$certName");
922
 
923
        ::PackageFile ('*', $certFile, '--Subdir=' . $pkgdir, '--Strip' );
924
    }
925
 
926
    #
1125 alewis 927
    #   Process each tier in the order presented in the source file
1119 dpurdie 928
    #
1123 dpurdie 929
    my $last_was_comment = 0;
1119 dpurdie 930
    foreach my $entry ( @Manifests )
931
    {
932
 
933
#DebugDumpData ( "Manifest Entry", $entry );
934
 
935
        my $tier = $entry->{tier};
936
        my $name = $entry->{name};
5649 dpurdie 937
        my $include_md5 = $entry->{md5};
1119 dpurdie 938
 
5767 alewis 939
        if ( $entry->{dmf} )
940
        {
6302 dpurdie 941
            DmfGenerate($entry);
5767 alewis 942
        }
943
 
1119 dpurdie 944
        #
945
        #   Insert all the files that have been specified
946
        #   The user specified order is preserved
947
        #
1123 dpurdie 948
        #   Entries may be either a file or a comment
949
        #   Comments: Merge multiple comments and create blocks
950
        #
951
        #
1119 dpurdie 952
        my @files = @{ $entry->{files} };
1123 dpurdie 953
        foreach my $fentry ( @files )
1119 dpurdie 954
        {
1123 dpurdie 955
            if ( my $cmt = $fentry->{'cmt'} )
956
            {
957
                print_mf ('') unless ( $last_was_comment ) ;
958
                print_mf ( map (('# ' . $_) , split ("\n", $cmt) ));
959
                $last_was_comment = 1;
960
                next;
961
            }
962
 
963
            print_mf ('#') if ( $last_was_comment );
964
            if ( my $file = $fentry->{'file'} )
965
            {
966
                my $base_file = StripDir( $file );
6314 dpurdie 967
                my @items = ($name, $tier, $base_file);
5649 dpurdie 968
                if ($include_md5) {
969
                    my $md5 = digest_file_hex($file, 'MD5');
6314 dpurdie 970
                    push @items, "MD5=$md5" ;
6306 dpurdie 971
                }
972
                if (exists $entry->{fileVersions} && exists $entry->{fileVersions}{$file} ) {
6314 dpurdie 973
                    push @items, "VERSION=" . $entry->{fileVersions}{$file};
6306 dpurdie 974
                }
6314 dpurdie 975
                print_mf (join (',', @items));
6932 dpurdie 976
                PackageManifestFile ($file, $pkgdir);
1123 dpurdie 977
                $last_was_comment = 0;
978
            }
1125 alewis 979
 
980
            if ( my $file = $fentry->{'filenocopy'} )
981
            {
982
                print_mf ("$name,$tier,$file");
983
                $last_was_comment = 0;
984
            }
1129 dpurdie 985
 
986
            if ( my $emf = $fentry->{'manifest'} )
987
            {
988
                $last_was_comment = 0;
989
                #
990
                #   Insert the entire manifest
991
                #   Items are:
992
                #        contents
6364 dpurdie 993
                #        items:
994
                #               file_name + arrays of file_info
995
                #           or  comment line to copy
1129 dpurdie 996
                #        file_base
997
                #        manifest
998
                #
999
#DebugDumpData ( "Embedded Manifest Entry", $emf );
6364 dpurdie 1000
                if ($emf->{'rewrite'}) {
1001
                    foreach my $item ( @{$emf->{'items'}}) {
1002
                        if (defined($item->{'file_name'}))
1003
                        {
1004
                            my @items = ($name, $tier, $item->{'file_name'});
1005
                            my $md5_added = 0;
1006
                            foreach my $info (@{$item->{'file_info'}}) {
1007
                                push @items, $info;
1008
                                $md5_added = 1 if ($info =~ m~^MD5=~i);
1009
                            }
1010
                            if ($include_md5 && $md5_added == 0) { # add md5 if requested and not already added from submanifest
1011
                                my $md5 = digest_file_hex($emf->{'file_base'} . '/' . $item->{'file_name'}, 'MD5');
1012
                                push @items, "MD5=$md5";
1013
                            }
1014
                            print_mf (join (',', @items));
6932 dpurdie 1015
                            PackageManifestFile ( $emf->{'file_base'} . '/' . $item->{'file_name'}, $pkgdir);
6364 dpurdie 1016
                        }
1017
                        elsif (defined($item->{'comment'})) {
1018
                            print_mf($item->{'comment'});
1019
                        }
1020
                    }
1021
                    print_mf('#');
1022
                }
1023
                else {
1024
                    print_mf ($_) foreach  ( @{$emf->{'contents'}} );
1025
                    foreach my $item ( @{$emf->{'items'}}) {
6932 dpurdie 1026
                        if (defined($item->{'file_name'})) {
1027
                            PackageManifestFile ( $emf->{'file_base'} . '/' . $item->{'file_name'}, $pkgdir);
1028
                        }
6364 dpurdie 1029
                    }
1030
                    print_mf('#');
1031
                }
1129 dpurdie 1032
            }
1119 dpurdie 1033
        }
1034
 
1035
        #
1036
        #   Expand out the entire MUG directory
1037
        #   All .mug files in the MUG directory will be added to the manifest
1038
        #   The assumption is that the MUG directory has been created by
1039
        #   something that knows what its doing
1040
        #
1041
        if ( my $mugdir = $entry->{mugdir} )
1042
        {
1043
            foreach my $file ( glob ($mugdir . '/*.mug' ) )
1044
            {
1045
                next unless ( -f $file );
1046
                my $base_file = StripDir($file);
6314 dpurdie 1047
 
1048
                my @items = ($name, $tier, $base_file);
1049
 
5654 dpurdie 1050
                if ($include_md5) {
1051
                    my $md5 = digest_file_hex($file, 'MD5');
6314 dpurdie 1052
                    push @items, "MD5=$md5" ;
5654 dpurdie 1053
                }
6314 dpurdie 1054
                print_mf (join (',', @items));
6932 dpurdie 1055
                PackageManifestFile ($file, 'mug');
1119 dpurdie 1056
            }
1057
        }
1058
    }
1059
 
1060
    #
1061
    #   Complete the creation of the Manifest File
1062
    #
6364 dpurdie 1063
    print_mf ("# end of $::ScmBuildPackage");
1119 dpurdie 1064
    close MF;
1065
    ErrorDoExit();
1121 dpurdie 1066
 
1067
    #
6932 dpurdie 1068
    #   Post process files from the manifest
1069
    #   May include signature generation
1070
    GenSignatures();
1071
 
1072
    #
1121 dpurdie 1073
    #   Sanity test of packages that did not provide a debian file
1074
    #   Just a hint that something may have been missed
1075
    #
6932 dpurdie 1076
    unless ($noWarn) {
1077
        my @not_used_packages;
1078
        foreach my $package_dir ( getPackagePaths ('--All') )
1121 dpurdie 1079
        {
6932 dpurdie 1080
            next if ( $package_dir =~ m~/manifest-tool/~ );
1081
            unless ( exists $package_dirs{$package_dir}{used} )
1082
            {
1083
                push @not_used_packages, $package_dir;
1084
            }
1121 dpurdie 1085
        }
6932 dpurdie 1086
        if ( @not_used_packages )
1087
        {
1088
            Warning ("Packages that did not contribute packages to the manifest:",
1089
                      @not_used_packages );
1090
        }
1121 dpurdie 1091
    }
6932 dpurdie 1092
}
1093
 
1094
#-------------------------------------------------------------------------------
1095
# Function        : print_mf
1096
#
1097
# Description     : Internal Function
1098
#                   Print one line to the Manifest File
1099
#                   Checks the length of the line being created
1100
#
1101
# Inputs          : $line
1102
#
1103
# Returns         :
1104
#
1105
 
1106
sub print_mf
1107
{
1108
    foreach  ( @_ )
1121 dpurdie 1109
    {
6932 dpurdie 1110
        my $ll = length ($_);
1111
        ReportError ( "Manifest line too long: $ll. Max is $ManifestLineWidth.",
1112
                "Line: $_" ) if ( $ManifestLineWidth && $ll > $ManifestLineWidth);
1113
        print MF $_ . "\n";
1121 dpurdie 1114
    }
6932 dpurdie 1115
}
1121 dpurdie 1116
 
6932 dpurdie 1117
#-------------------------------------------------------------------------------
1118
# Function        : PackageManifestFile  
1119
#
1120
# Description     : Process and package a manifest file
1121
#                       Reatin a list of files in the mnifest to allow postprocessing
1122
#                       such as signature generation
1123
#                   
1124
#
1125
# Inputs          : $srcFile        - Full path to the source file
1126
#                   $pkgPath        - Target path within the package    
1127
#
1128
# Returns         : Nothing
1129
#
1130
sub PackageManifestFile {
1131
    my ($srcFile, $pkgPath) = @_;
1132
    push @{$ManifestFiles}, ({srcFile => $srcFile, pkgPath => $pkgPath});
1119 dpurdie 1133
 
6932 dpurdie 1134
    ::PackageFile ('*', $srcFile, '--Subdir=' . $pkgPath, '--Strip' );
1135
}
1136
 
1137
#-------------------------------------------------------------------------------
1138
# Function        : GenSigInit 
1139
#
1140
# Description     : Initialise the Siganture Generation process
1141
#                   Populate some globals for use in creating the manifest
1142
#                   Bail if there are errors
1143
#
1144
# Inputs          : 
1145
#
1146
# Returns         : 
1147
#
1148
sub GenSigInit
1149
{
6933 dpurdie 1150
    Debug("GenSigInit:", $noSigs);
6932 dpurdie 1151
    return if $noSigs;
1152
 
1153
    my @warnings;
1154
 
1119 dpurdie 1155
    #
6933 dpurdie 1156
    #   Locate and setup openssl
1157
    #   Should be provided by a package
1158
    #   set OPENSSL_CONF=...../openssl.cfg 
1159
    #   
1160
    $opensslProg = ::ToolExtensionProgram( 'openssl', '', '.exe');
1161
 
1162
    #   Generate  a dummy openssl.cnf - to keep the uitility happy (quiet)
1163
    $opensslConf = FullPath(CatPaths( $::ScmRoot, $::ScmInterface, 'openssl.cfg'));
1164
    TouchFile($opensslConf);
1165
    ::ToolsetGenerate($opensslConf);
1166
    $ENV{OPENSSL_CONF}= $opensslConf;
1167
 
1168
    #
1169
    #   Figure out default operation if openssl cannot be found
1170
    #       If --signature - then report error
1171
    #       If not specified, then default is -NoSig
1172
    #
1173
    unless ($opensslProg) {
1174
        if (defined $noSigs) {
1175
            ReportError ("The openssl utility is not available. Must be provided by a package") unless $opensslProg;  
1176
        } else {
1177
            Verbose("openssl not found in a package. Default to no signature");
1178
            $noSigs = 1;
1179
            return;
1180
        }
1181
    }
1182
 
1183
    #
6932 dpurdie 1184
    #   Signatures 'should' be generated for a target paltform of 'MANSIG'
1185
    #       In the automated build system this will be a controlled machine
1186
    #   Warn if attemting to create signatures for a different target    
1119 dpurdie 1187
    #
6932 dpurdie 1188
    push @warnings, "# Package build for platform other than MANSIG - will generate test signatures" if ($::ScmPlatform ne 'MANSIG');
1189
 
1190
 
1119 dpurdie 1191
    #
6932 dpurdie 1192
    #   Locate the signature file
1193
    #   Use an internal key that unless being run on a controlled build machine
1194
    #   The controlled build machine will have
1195
    #       GBE_MANIFEST_KEY set to a path that contains the keyfile
1196
    #           Yes, a user can fudge this, but not with the real file
1197
    #          
1198
    #  Internal cert created with:
1199
    #  openssl req -nodes -x509 -sha256 -newkey rsa:4096 -keyout VixPulseManifestTest.key -out VixPulseManifestTest.crt -days 10000 -subj "/C=AU/CN=Vix.Pulse.Manifest.Test"
1119 dpurdie 1200
    #
6932 dpurdie 1201
    $keyFile = CatPaths( $pkgBase, 'VixPulseManifestTest.key');
1202
    my $localKey = 1;
1203
    if (exists $ENV{GBE_MANIFEST_KEY} ) {
1204
        $keyFile = $ENV{GBE_MANIFEST_KEY};
1205
        $localKey = 0;
1206
    }
1207
    $certFile = $keyFile;
1208
    $certFile =~ s~\.key$~.crt~;
1209
    $certName = $certFile;
1210
    $certName =~ s~.*[\\/]~~;
1119 dpurdie 1211
 
6932 dpurdie 1212
    ReportError ("Manifest signing key not found", $keyFile) unless -f $keyFile;
1213
    ReportError ("Manifest signing key not found", $certFile) unless -f $certFile;
1214
 
1215
    push @warnings, "#  Using uncontrolled test key to sign the manifest" if ($localKey);
1216
    Warning("#############################################################", "#",
1217
             @warnings,
1218
            "#", "#############################################################") if (@warnings);
1219
 
1220
    ErrorDoExit();
1221
    Message("OpenSsl Prog: $opensslProg");
1222
    Message("OpenSsl Conf: $opensslConf");
1223
    Message("Signing with: $keyFile");
1224
 
1119 dpurdie 1225
}
1226
 
6932 dpurdie 1227
#-------------------------------------------------------------------------------
1228
# Function        : GenSignatures 
1229
#
1230
# Description     : Generate signatures on all packages
1231
#
1232
# Inputs          : Used the array of files in $ManifestFiles 
1233
#
1234
# Returns         : Nothing 
1235
#
1236
sub GenSignatures
1237
{
6933 dpurdie 1238
    Debug("GenSignatures:", $noSigs);
6932 dpurdie 1239
    return if $noSigs;
5767 alewis 1240
 
6932 dpurdie 1241
    foreach my $entry ( @{$ManifestFiles}) {
1242
        Verbose("PostProcess: $entry->{srcFile}, $entry->{pkgPath}");
1243
 
1244
        #
1245
        #   Generate the name of the signature file
1246
        #   Generate Packaging directives for the signature
1247
        #   
1248
        my $sigFileName = CatPaths( "$::ScmPlatform.LIB", StripDir($entry->{srcFile}) . '.sig' );
1249
        ::PackageFile ('*', $sigFileName, '--Subdir=' . $entry->{pkgPath}, '--Strip' );
1250
        ::ToolsetGenerate($sigFileName);
1251
 
1252
        #
1253
        #   Generate package signature
1254
        #   The required custom(vix) 'sig' file has the format:
1255
        #       line-1 : name of the certificate to decode
1256
        #                Assume its the keyname with .key -> .crt - with no path elements
1257
        #       line-2 : Text representation of the signature
1258
        #                The openssl dgst command generates a binary output by default
1259
        #                This is then converted into base64 with -A option
1260
        #
1261
        my $tmp1 = CatPaths( "$::ScmPlatform.LIB", 'temp1');
1262
        my $tmp2 = CatPaths( "$::ScmPlatform.LIB", 'temp2');
1263
        ::ToolsetGenerate($tmp1, $tmp2);
1264
 
1265
        Verbose("Signing: " . StripDir($entry->{srcFile}));
1266
        System( '--Exit', '--NoShell', $opensslProg, 'dgst', '-sha256', '-sign', $keyFile, '-out', $tmp1, $entry->{srcFile} );
1267
        System( '--Exit', '--NoShell', $opensslProg, 'base64', '-A', '-in', $tmp1, '-out', $tmp2 );
1268
        my $base64 = TagFileRead($tmp2);
1269
 
1270
        #
1271
        #   In a development environment we don't want to create the signature file
1272
        #   every time we run. Its noisy. Solution:
1273
        #       Only generate a signature file if:
1274
        #           It doesn't exist
1275
        #           A copy of the signed digest doesn't exist (.fpp)
1276
        #           The signed digest has changed
1277
        #   
1278
        my $fppFileName = CatPaths( "$::ScmPlatform.LIB", StripDir($entry->{srcFile}) . '.fpp' );
1279
        ::ToolsetGenerate($fppFileName);
1280
        my $genSig = 0;
1281
        $genSig = 1 unless ( -e $sigFileName &&  -e $fppFileName);
1282
        unless ($genSig) {
1283
            my $ffp = TagFileRead($fppFileName);
1284
            $genSig = ($ffp ne $base64);
1285
        }
1286
 
1287
        if ($genSig) {
1288
            FileCreate($sigFileName, $certName, $base64);
1289
            rename $tmp2, $fppFileName ;
1290
        }
1291
 
1292
        unlink $tmp1;
1293
        unlink $tmp2;
1294
    }
1295
    unlink $opensslConf;
5767 alewis 1296
}
1297
 
1298
 
1299
#-------------------------------------------------------------------------------
1300
# Function        : DmfGenerate
1301
#
1302
# Description     : Import an existing manifest
1303
#
1304
# Inputs          : entry   - The manifest that is being processed.
1305
#
1306
# Returns         : Nothing
1307
#
1308
sub DmfGenerate
1309
{
1310
    my ($entry) = @_;
1311
 
1312
    # Get the generation time.
1313
    my $gen_time = time();
1314
 
1315
    my $work_dir = "$::ScmPlatform.BIN/";
1316
    System( "$::GBE_BIN/mkdir -p $work_dir" );
1317
 
1318
    my $name = $entry->{name};
1319
    my $version = $entry->{dmf_version};
1320
 
1321
    # Configure base manifest information.
1322
    my %manifest;
5771 alewis 1323
    $manifest{'mugsetId'} = $name . '_' . $version;
5767 alewis 1324
    $manifest{'name'} = $name;
1325
    $manifest{'version'} = $version;
5771 alewis 1326
    $manifest{'packageName'} = $::ScmBuildPackage;
1327
    $manifest{'packageVersion'} = $::ScmBuildVersionFull;
5767 alewis 1328
    $manifest{'datetime'} = localtime($gen_time);
1329
    $gen_time *= 1000;  # Make to milliseconds
1330
    $manifest{'timestamp'} = $gen_time;
1331
    $manifest{'tier'} = $entry->{tier};
1332
 
1333
    # Process each file.
1334
    my @files = @{ $entry->{files} };
1335
    my $zip = Archive::Zip->new();
1336
    my $i = 0;
1337
    foreach my $fentry ( @files )
1338
    {
1339
        if ( my $file = $fentry->{'file'} )
1340
        {
6302 dpurdie 1341
            my $order = $i + 1;
5767 alewis 1342
            my $base_file = StripDir( $file );
1343
            my $publish_file = $name . '_' . $version . '_' . $order . '.aup';
1344
            my $aup_file = $work_dir . $publish_file;
1345
 
1346
            GenerateCesFile($file, $aup_file, 0x3, $gen_time, $publish_file);
1347
 
1348
            my $file_member = $zip->addFile( $aup_file, $publish_file );
1349
 
1350
            $manifest{'tasks'}[$i]{'order'} = 1 * $order;
1351
            $manifest{'tasks'}[$i]{'filename'} = $base_file;
1352
            $manifest{'tasks'}[$i]{'download'} = $publish_file;
5771 alewis 1353
            $manifest{'tasks'}[$i]{'sha256'} = digest_file_base64($file, 'SHA-256');
1354
            $manifest{'tasks'}[$i]{'size'} = -s $file;
1355
 
5767 alewis 1356
            if ($base_file =~ /\.sh$/)
1357
            {
1358
                $manifest{'tasks'}[$i]{'action'} = 'exec-shell';
1359
            }
1360
            elsif ($base_file =~ /\.deb$/)
1361
            {
1362
                $manifest{'tasks'}[$i]{'action'} = 'dpkg-install';
5771 alewis 1363
 
1364
                my ($pkg_name, $pkg_version, $pkg_arch) = ($base_file =~ /([^_]*)_([^_]*)_(.*)/);
1365
                $manifest{'tasks'}[$i]{'arch'} = $pkg_arch;
1366
                $manifest{'tasks'}[$i]{'name'} = $pkg_name;
1367
                $manifest{'tasks'}[$i]{'version'} = $pkg_version;
5767 alewis 1368
            }
1369
            else
1370
            {
1371
                ReportError ("Manifest entry $base_file does not have a supported DMF install action");
1372
            }
1373
 
1374
            $i = $i + 1;
1375
        }
1376
    }
1377
 
1378
    # Encode and commit the JSON.
1379
    my $json_encoder = JSON->new->allow_nonref;
1380
    my $json = $json_encoder->pretty->encode( \%manifest );
1381
 
1382
    my $manifest_filename = $name . '_' . $version;
1383
    my $aum_filename = $manifest_filename . '_0.aum';
1384
    my $manifest_file = $work_dir . $manifest_filename . '.json';
1385
    my $aum_file = $work_dir . $aum_filename;
1386
 
1387
    # Save our manifest.
1388
    open (J, '>', $manifest_file ) || Error ("Cannot create the DMF Manifest file");
1389
    binmode (J);
1390
    print J $json;
1391
    close J;
1392
 
1393
    GenerateCesFile($manifest_file, $aum_file, 0x2, $gen_time, $aum_filename);
1394
 
1395
    $zip->addFile($aum_file, $aum_filename);
1396
 
1397
    my $zip_filename = $work_dir . $name . '_' . $version . '.zip';
1398
    if ( $zip->writeToFileNamed($zip_filename) != AZ_OK )
1399
    {
1400
        ReportError("DMF ZIP file creation failed");
1401
    }
1402
    PackageFile('*', $zip_filename, '--Strip');
1403
    PackageFile('*', $manifest_file, '--Strip');
1404
 
1405
}
1406
 
1407
#-------------------------------------------------------------------------------
6932 dpurdie 1408
# Function        : GenerateCesFile
5767 alewis 1409
#
1410
# Description     : Import an existing manifest
1411
#
1412
# Inputs          : src_file     - The input file.
1413
#                   dst_file     - The output CES file.
1414
#                   content_type - The content type to report.
1415
#                   gen_time     - The generation time for the file.
1416
#                   filename     - The filename to embed in the CES file.
1417
#
1418
#
1419
# Returns         : Nothing
1420
#
1421
sub GenerateCesFile
1422
{
1423
    my ($src_file, $dst_file, $content_type, $gen_time, $filename) = @_;
1424
 
1425
    open (INF, '<', $src_file ) || Error ("Cannot open file $src_file for reading");
1426
    binmode (INF);
1427
 
1428
    open (OUTF, '>', $dst_file ) || Error ("Cannot open file $dst_file for writing");
1429
    binmode (OUTF);
1430
 
1431
    my $signing_key_name = "";
1432
    my $signature_size = 0;
1433
    my $format_version = 0xCE500000;
1434
    my $compression_method = 0;
1435
    my $encryption_method = 0;
1436
    my $kek_name = "";
1437
    my $encryption_key_size = 0;
1438
    my $filename_size = length($filename);
1439
 
1440
    print OUTF pack("Z32", $signing_key_name);
1441
    print OUTF pack("n", $signature_size);
1442
    print OUTF pack("N", $format_version);
1443
    print OUTF pack("N", $content_type);
1444
    print OUTF pack("Q>", $gen_time);
1445
    print OUTF pack("N", $compression_method);
1446
    print OUTF pack("N", $encryption_method);
1447
    print OUTF pack("Z32", $kek_name);
1448
    print OUTF pack("n", $encryption_key_size);
1449
    print OUTF pack("n", $filename_size);
1450
    # Encryption key HERE
1451
    print OUTF pack("A$filename_size", $filename);
1452
 
1453
    my $buf;
1454
    while (read(INF,$buf,65536))
1455
    {
1456
        print OUTF $buf;
1457
    }
1458
    print OUTF $buf;
1459
    close INF;
1460
 
1461
    # Signature HERE
1462
 
1463
    # Finish with file.
1464
    close OUTF;
1465
}
1466
 
1119 dpurdie 1467
1;