Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
407 dpurdie 1
########################################################################
3921 dpurdie 2
# Copyright (C) 1998-2013 Vix Technology, All rights reserved
407 dpurdie 3
#
3921 dpurdie 4
# Module name   : DebianPackager.pl
407 dpurdie 5
# Module type   : Makefile system
3921 dpurdie 6
# Compiler(s)   : Perl
407 dpurdie 7
# Environment(s): jats
8
#
9
# Description   : This program is invoked by the MakeDebianPackage
10
#                 directive, that is a part of this package
11
#
12
#                 The program will use a user-provided script in order
13
#                 to create a Debian Package.
14
#
15
#                 The user script may call a number of directives in order to
16
#                 construct an image of the package being installed.
17
#
18
#                 The script specifies Debian configuration scaripts that
19
#                 will be embedded in the package.
20
#
21
#                 This program will:
22
#                   Construct a filesystem image under control of the directives
23
#                   within the user script
24
#
25
#                   Massage the Debian control file
26
#
27
#                   Create a Debian Package
28
#
29
#                   Transfer it to the users 'BIN' directory, where it is
30
#                   available to be packaged.
31
#
32
#                 Summary of directives available to the user-script:
33
#                       AddInitScript           - Add an init script
34
#                       CatFile                 - Append to a file
4641 dpurdie 35
#                       ConvertFile             - Convert file(s) to Unix or Dos Text
407 dpurdie 36
#                       CopyDir                 - Copy directory tree
37
#                       CopyFile                - Copy a file
38
#                       CopyBinFile             - Copy an executable file
39
#                       CopyLibFile             - Copy a library file
40
#                       CreateDir               - Create a directory
41
#                       DebianFiles             - Specify control and script files
4636 dpurdie 42
#                       DebianControlFile       - Specify control and script files
43
#                       DebianDepends           - Add Depends entry to control file
407 dpurdie 44
#                       EchoFile                - Place text into a file
45
#                       MakeSymLink             - Create a symbolic link
46
#                       PackageDescription      - Specify the package description
4641 dpurdie 47
#                       ReplaceTags             - Replace Tags on target file
407 dpurdie 48
#                       SetFilePerms            - Set file permissions
49
#                       SetVerbose              - Control progress display
50
#                       IsProduct               - Flow control
51
#                       IsPlatform              - Flow control
52
#                       IsTarget                - Flow control
427 dpurdie 53
#                       IsVariant               - Flow control
407 dpurdie 54
#
55
#                 Thoughts for expansion:
56
#                       SrcDir                  - Extend path for resolving local files
57
#
58
#                   Less used:
59
#                        ExpandLinkFiles        - Expand .LINK files
60
#
61
#                   Internal Use:
62
#                        FindFiles              - Find a file
63
#                        ResolveFile            - Resolve a 'local' source file
4641 dpurdie 64
#                        chmodItem              - Set file or directory permissions
407 dpurdie 65
#                        
66
#......................................................................#
67
 
411 dpurdie 68
require 5.006_001;
407 dpurdie 69
use strict;
70
use warnings;
71
 
72
use Getopt::Long;
73
use File::Path;
74
use File::Copy;
75
use File::Find;
76
use JatsSystem;
77
use FileUtils;
78
use JatsError;
4636 dpurdie 79
use JatsLocateFiles;
407 dpurdie 80
use ReadBuildConfig;
423 dpurdie 81
use JatsCopy ();                            # Don't import anything
407 dpurdie 82
 
83
#
84
#   Globals
85
#
425 dpurdie 86
my $DebianWorkDirBase;                      # Workspace
407 dpurdie 87
my $DebianWorkDir;                          # Dir to create file system image within
88
 
89
#
90
#   Command line options
91
#
92
my $opt_debug   = $ENV{'GBE_DEBUG'};        # Allow global debug
93
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
94
my $opt_vargs;                              # Verbose arg
95
my $opt_help = 0;
96
my $opt_manual = 0;
97
my $opt_clean = 0;
98
my $opt_platform;
99
my $opt_type;
100
my $opt_buildname;
101
my $opt_buildversion;
102
my $opt_interfacedir;
103
my $opt_target;
104
my $opt_product;
105
my $opt_package_script;
106
my $opt_interfaceincdir;
107
my $opt_interfacelibdir;
108
my $opt_interfacebindir;
109
my $opt_libdir;
110
my $opt_bindir;
111
my $opt_localincdir;
112
my $opt_locallibdir;
113
my $opt_localbindir;
114
my $opt_pkgdir;
115
my $opt_pkglibdir;
116
my $opt_pkgbindir;
117
my $opt_pkgpkgdir;
118
my $opt_output;
417 dpurdie 119
my $opt_name;
427 dpurdie 120
my $opt_variant;
3921 dpurdie 121
my $opt_pkgarch;
407 dpurdie 122
 
123
#
124
#   Options derived from script directives
125
#
126
my $opt_description;
127
 
415 dpurdie 128
#
129
#   Globals
130
#
131
my @ResolveFileList;                    # Cached Package File List
132
my @ResolveBinFileList;                 # Cached PackageBin File List
133
my @ResolveLibFileList;                 # Cached PackageLib File List
4635 dpurdie 134
my %DebianControlFiles;                 # Control Files
135
my %DebianControlFileNames;             # Control Files by name
136
my @DependencyList;                     # Package Dependencies
4666 dpurdie 137
my @ConfigList;                         # Config Files
407 dpurdie 138
 
139
#-------------------------------------------------------------------------------
140
# Function        : Main Entry point
141
#
142
# Description     : This function will be called when the package is initialised
143
#                   Extract arguments from the users environment
144
#
145
#                   Done here to greatly simplify the user script
146
#                   There should be no junk in the user script - keep it simple
147
#
148
# Inputs          :
149
#
150
# Returns         : 
151
#
152
main();
153
sub main
154
{
155
    my $result = GetOptions (
156
                "verbose:s"         => \$opt_vargs,
157
                "clean"             => \$opt_clean,
158
                "Type=s"            => \$opt_type,
4105 dpurdie 159
                "BuildName=s"       => \$opt_buildname,                     # Raw Jats Package Name (Do not use)
160
                "Name=s"            => \$opt_name,                          # Massaged Debian Package Name
407 dpurdie 161
                "BuildVersion=s"    => \$opt_buildversion,
162
                "Platform=s"        => \$opt_platform,
163
                "Target=s"          => \$opt_target,
164
                "Product=s"         => \$opt_product,
165
                "DebianPackage=s"   => \$opt_package_script,
166
                "InterfaceDir=s"    => \$opt_interfacedir,
167
                "InterfaceIncDir=s" => \$opt_interfaceincdir,
168
                "InterfaceLibDir=s" => \$opt_interfacelibdir,
169
                "InterfaceBinDir=s" => \$opt_interfacebindir,
170
                "LibDir=s"          => \$opt_libdir,
171
                "BinDir=s"          => \$opt_bindir,
172
                "LocalIncDir=s"     => \$opt_localincdir,
173
                "LocalLibDir=s"     => \$opt_locallibdir,
174
                "LocalBinDir=s"     => \$opt_localbindir,
175
                "PackageDir=s"      => \$opt_pkgdir,
176
                "PackageLibDir=s"   => \$opt_pkglibdir,
177
                "PackageBinDir=s"   => \$opt_pkgbindir,
178
                "PackagePkgDir=s"   => \$opt_pkgpkgdir,
179
                "Output=s"          => \$opt_output,
427 dpurdie 180
                "Variant:s"         => \$opt_variant,
3921 dpurdie 181
                "PkgArch:s"         => \$opt_pkgarch,
407 dpurdie 182
    );
183
    $opt_verbose++ unless ( $opt_vargs eq '@' );
184
 
185
    ErrorConfig( 'name'    => 'DebianUtils',
186
                 'verbose' => $opt_verbose,
187
                 'debug'   => $opt_debug );
188
 
189
    #
190
    #   Init the FileSystem Uiltity interface
191
    #
192
    InitFileUtils();
193
 
194
    #
195
    #   Ensure that we have all required options
196
    #
197
    Error ("Platform not set")                  unless ( $opt_platform );
198
    Error ("Type not set")                      unless ( $opt_type );
199
    Error ("BuildName not set")                 unless ( $opt_buildname );
4105 dpurdie 200
    Error ("Debian Package Name not set")       unless ( $opt_name );
407 dpurdie 201
    Error ("BuildVersion not set")              unless ( $opt_buildversion );
202
    Error ("InterfaceDir not set")              unless ( $opt_interfacedir );
203
    Error ("Target not set")                    unless ( $opt_target );
204
    Error ("Product not set")                   unless ( $opt_product );
205
    Error ("DebianPackage not set")             unless ( $opt_package_script );
206
    Error ("Ouput File not set")                unless ( $opt_output );
207
 
208
    #
209
    #   Read in relevent config information
210
    #
211
    ReadBuildConfig ($opt_interfacedir, $opt_platform, '--NoTest' );
212
 
213
    #
214
    #   Build the package image in a directory based on the target being created
215
    #
425 dpurdie 216
    $DebianWorkDirBase = "$opt_platform$opt_type.image";
217
    $DebianWorkDir = "$DebianWorkDirBase/$opt_name";
407 dpurdie 218
 
219
    #
220
    #   Configure the System command to fail on any error
221
    #
222
    SystemConfig ( ExitOnError => 1 );
223
 
224
    #
225
    #   Extract the 'name' of the package from the output path
226
    #   Display purposes only
227
    #
228
    my $DebianPkgName = StripDirExt($opt_output);
229
 
230
    #
231
    #   Display variables used
232
    #
233
    Message    "=Building Debian Package =============================================";
417 dpurdie 234
    Message    "Build $opt_name";
407 dpurdie 235
    Message    "       Package: $opt_buildname";
427 dpurdie 236
    Message    "       Variant: $opt_variant" if ($opt_variant);
407 dpurdie 237
    Message    "       Version: $opt_buildversion";
238
    Message    "  Building for: $opt_platform, $opt_target";
239
    Message    "       Product: $opt_product";
240
    Message    "          Type: $opt_type";
3921 dpurdie 241
    Message    "      Pkg Arch: $opt_pkgarch" if ($opt_pkgarch);
407 dpurdie 242
    Verbose    "       Verbose: $opt_verbose";
243
    Verbose    "  InterfaceDir: $opt_interfacedir";
244
    Message    "       Package: $DebianPkgName";
245
    Message    "======================================================================";
246
 
247
    #
3921 dpurdie 248
    #   Defaults
249
    #
250
    $opt_pkgarch = $opt_platform unless ( $opt_pkgarch );
251
 
252
    #
407 dpurdie 253
    #   Perform Clean up
254
    #   Invoked during "make clean" or "make clobber"
255
    #
256
    if ( $opt_clean )
257
    {
258
        Message ("Remove packaging directory: $DebianWorkDir");
425 dpurdie 259
 
260
        #
261
        #   Remove the directory for this package
262
        #   Remove the general work dir - if all packages have been cleaned
263
        #
407 dpurdie 264
        rmtree( $DebianWorkDir );
425 dpurdie 265
        rmdir( $DebianWorkDirBase );
407 dpurdie 266
        rmtree ($opt_output) if ( -f $opt_output );
267
        exit;
268
    }
269
 
270
    #
271
    #   Clean  out the WORK directory
272
    #   Always start with a clean slate
273
    #
274
    #   Ensure that the base of the directory tree does not have 'setgid'
275
    #       This will upset the debian packager
276
    #       This may be an artifact from the users directory and not expected
277
    #
278
    rmtree( $DebianWorkDir );
279
    mkpath( $DebianWorkDir );
280
 
281
    my $perm = (stat $DebianWorkDir)[2] & 0777;
282
    chmod ( $perm & 0777, $DebianWorkDir );
283
 
284
    #
285
    #   Invoke the user script to do the hard work
286
    #
4139 dpurdie 287
    unless (my $return = do $opt_package_script) {
288
            Error ("Couldn't parse $opt_package_script: $@") if $@;
289
            Error ("Couldn't do $opt_package_script: $!")    unless defined $return;
290
        }
407 dpurdie 291
 
292
    #
293
    #   Complete the building of the package
294
    #
295
    BuildDebianPackage ();
296
    Message ("Created Debian Package");
297
}
298
 
299
#-------------------------------------------------------------------------------
300
# Function        : BuildDebianPackage
301
#
302
# Description     : This function will create the Debian Package
303
#                   and transfer it to the target directory
304
#
305
# Inputs          : None
306
#
307
# Returns         : Nothing
308
#
309
sub BuildDebianPackage
310
{
311
    Error ("BuildDebianPackage: No Control File or Package Description")
4635 dpurdie 312
        unless ( exists($DebianControlFiles{'control'}) || $opt_description );
407 dpurdie 313
 
314
    #
315
    #   Convert the FileSystem Image into a Debian Package
316
    #       Insert Debian control files
317
    #
318
    Verbose ("Copy in the Debian Control Files");
319
    mkdir ( "$DebianWorkDir/DEBIAN" );
320
 
4635 dpurdie 321
    #
322
    #   Copy in all the named Debian Control files
323
    #       Ignore any control file. It will be done next
324
    #
325
    foreach my $key ( keys %DebianControlFiles )
326
    {
327
        next if ($key eq 'control');
4676 dpurdie 328
        CopyFile ( $DebianControlFiles{$key}, '/DEBIAN', $key  );
4635 dpurdie 329
    }
4666 dpurdie 330
 
331
    #
332
    #   Create 'conffiles'
333
    #       Append to any user provided file
334
    if ( @ConfigList )
335
    {
336
        my $conffiles = "$DebianWorkDir/DEBIAN/conffiles";
337
        Warning("Appending user specified entries to conffiles") if ( -f $conffiles);
338
        FileAppend( $conffiles, @ConfigList );
339
    }
4635 dpurdie 340
 
341
    #
342
    #   Massage the 'control' file
343
    #
344
    UpdateControlFile ($DebianControlFiles{'control'} );
407 dpurdie 345
 
4635 dpurdie 346
    #
347
    #   Mark all files in the debian folder as read-execute
348
    #
407 dpurdie 349
    System ( 'chmod', '-R', 'a+rx', "$DebianWorkDir/DEBIAN" );
350
    System ( 'build_dpkg.sh', '-b', $DebianWorkDir);
351
    System ( 'mv', '-f', "$DebianWorkDir.deb", $opt_output );
352
 
353
    System ("build_dpkg.sh", '-I', $opt_output) if (IsVerbose(1));
354
 
355
}
356
 
357
#-------------------------------------------------------------------------------
358
# Function        : UpdateControlFile
359
#
4188 dpurdie 360
# Description     : Update the Debian 'control' file to fix up various fields
407 dpurdie 361
#                   within the file.
362
#
363
#                   If the files has not been specified, then a basic control
364
#                   file will be provided.
365
#
366
#                   This routine knows where the control file will be placed
367
#                   within the output work space.
368
#
369
# Inputs          : $src            - Path to source file
370
#                   Uses global variables
371
#
372
# Returns         : Nothing
373
#
374
sub UpdateControlFile
375
{
376
    my($src) = @_;
377
    my $dst = "$DebianWorkDir/DEBIAN/control";
378
 
379
    unless ( $src )
380
    {
381
        CreateControlFile();
382
        return;
383
    }
384
 
4635 dpurdie 385
    #
386
    #   User has provided a control file
387
    #       Tweak the internals
388
    #
407 dpurdie 389
    Verbose ("UpdateControlFile: $dst" );
390
    $src = ResolveFile( 0, $src );
391
 
4635 dpurdie 392
    #   Calc depends line
4636 dpurdie 393
    my $depData = join (', ', @DependencyList );
4635 dpurdie 394
 
395
    open (SF, '<', $src) || Error ("UpdateControlFile: Cannot open:$src, $!");
396
    open (DF, '>', $dst) || Error ("UpdateControlFile: Cannot create:$dst, $!");
407 dpurdie 397
    while ( <SF> )
398
    {
399
        s~\s*$~~;
400
        if ( m~^Package:~ ) {
4105 dpurdie 401
            $_ = "Package: $opt_name";
407 dpurdie 402
 
403
        } elsif ( m~^Version:~ ) {
404
            $_ = "Version: $opt_buildversion";
405
 
406
        } elsif ( m~^Architecture:~ ) {
3921 dpurdie 407
            $_ = "Architecture: $opt_pkgarch";
407 dpurdie 408
 
409
        } elsif ( $opt_description && m~^Description:~ ) {
410
            $_ = "Description: $opt_description";
4635 dpurdie 411
 
412
        } elsif ( m~^Depends:~ ) {
413
            $_ = "Depends: $depData";
414
            $depData = '';
407 dpurdie 415
        }
416
        print DF $_ , "\n";
417
    }
4635 dpurdie 418
 
407 dpurdie 419
    close (SF);
420
    close (DF);
4635 dpurdie 421
 
422
    #
423
    #   Warn if Depends section is needed
424
    #
425
    Error ("No Depends section seen in user control file") 
426
        if ($depData);
407 dpurdie 427
}
428
 
429
#-------------------------------------------------------------------------------
430
# Function        : CreateControlFile
431
#
432
# Description     : Craete a basic debian control file
433
#
434
# Inputs          : Uses global variables
435
#
436
# Returns         : 
437
#
438
sub CreateControlFile
439
{
440
    my $dst = "$DebianWorkDir/DEBIAN/control";
441
 
442
    Verbose ("CreateControlFile: $dst" );
443
 
4636 dpurdie 444
    my $depData = join (', ', @DependencyList );
4635 dpurdie 445
 
407 dpurdie 446
    open (DF, '>', $dst) || Error ("CreateControlFile: Cannot create:$dst");
4105 dpurdie 447
    print DF "Package: $opt_name\n";
407 dpurdie 448
    print DF "Version: $opt_buildversion\n";
449
    print DF "Section: main\n";
450
    print DF "Priority: standard\n";
3921 dpurdie 451
    print DF "Architecture: $opt_pkgarch\n";
452
    print DF "Essential: No\n";
453
    print DF "Maintainer: Vix Technology\n";
407 dpurdie 454
    print DF "Description: $opt_description\n";
4635 dpurdie 455
    print DF "Depends: $depData\n" if ($depData);
456
 
407 dpurdie 457
    close (DF);
458
}
459
 
460
#-------------------------------------------------------------------------------
461
# Function        : SetVerbose
462
#
463
# Description     : Set the level of verbosity
464
#                   Display activity
465
#
466
# Inputs          : Verbosity level
467
#                       0 - Use makefile verbosity (Default)
468
#                       1..2
469
#
470
# Returns         : 
471
#
472
sub SetVerbose
473
{
474
    my ($level) = @_;
475
 
476
    $level = $opt_verbose unless ( $level );
477
    $opt_verbose = $level;
478
    ErrorConfig( 'verbose' => $level);
479
}
480
 
481
 
482
#-------------------------------------------------------------------------------
483
# Function        : DebianFiles
484
#
485
# Description     : Name Debian builder control files
486
#                   May be called multiple times
487
#
488
# Inputs          : Options
489
#                       --Control=file
490
#                       --PreRm=file
491
#                       --PostRm=file
492
#                       --PreInst=file
493
#                       --PostInst=file
4635 dpurdie 494
#                         
407 dpurdie 495
#
496
# Returns         : Nothing
497
#
498
sub DebianFiles
499
{
500
    #
4635 dpurdie 501
    #   Extract names
407 dpurdie 502
    #
503
    Verbose ("Specify Debian Control Files and Scripts");
504
    foreach  ( @_ )
505
    {
4635 dpurdie 506
        if ( m/^--Control=(.+)/i ) {
507
            DebianControlFile('control',$1)
407 dpurdie 508
 
4635 dpurdie 509
        } elsif ( m/^--PreRm=(.+)/i ) {
510
            DebianControlFile('prerm',$1)
407 dpurdie 511
 
4635 dpurdie 512
        } elsif ( m/^--PostRm=(.+)/i ) {
513
            DebianControlFile('postrm',$1)
407 dpurdie 514
 
4635 dpurdie 515
        } elsif ( m/^--PreInst=(.+)/i ) {
516
            DebianControlFile('preinst',$1)
407 dpurdie 517
 
4635 dpurdie 518
        } elsif ( m/^--PostInst=(.+)/i ) {
519
            DebianControlFile('postinst',$1)
407 dpurdie 520
 
521
        } else {
522
            Error ("DebianFiles: Unknown option: $_");
523
        }
524
    }
525
}
526
 
527
#-------------------------------------------------------------------------------
4635 dpurdie 528
# Function        : DebianControlFile 
529
#
530
# Description     : Add special control files to the Debian Installer 
531
#                   Not useful for embedded installers
532
#
533
#                   More general than DebianFiles()
534
#
535
# Inputs          : name            - Target Name
536
#                                     If the name starts with 'package.' then it will be replaced
537
#                                     with the name of the current package
538
#                   file            - Source File Name
4676 dpurdie 539
#                   options         - Options include
540
#                                       --FromPackage
4635 dpurdie 541
#
542
# Returns         : 
543
#
544
sub DebianControlFile
545
{
4676 dpurdie 546
    my ($name, $file, @options) = @_;
547
    my $fromPackage = 0;
4635 dpurdie 548
 
549
    #
4676 dpurdie 550
    #   Process options
551
    foreach ( @options)
552
    {
553
        if (m~^--FromPackage~) {
554
            $fromPackage = 1;
555
        }
556
        else  {
557
            ReportError(("DebianControlFile: Unknown argument: $_"));
558
        }
559
    }
560
    ErrorDoExit();
561
 
562
    #
4635 dpurdie 563
    #   Some control files need to have the package name prepended
564
    #
565
    $name =~ s~^package\.~$opt_name.~;
566
 
567
    #
568
    #   Only allow one file of each type
569
    #       Try to protect the user by testing for names by lowercase
570
    #
571
    my $simpleName = lc($name);
572
    Error("DebianControlFile: Multiple definitions for '$name' not allowed")
573
        if (exists $DebianControlFileNames{$simpleName});
574
 
4676 dpurdie 575
    my $filePath = ResolveFile($fromPackage, $file);
4635 dpurdie 576
 
577
    #
578
    #   Add info to data structures
579
    #
4676 dpurdie 580
    $DebianControlFiles{$name} = $filePath;
4635 dpurdie 581
    $DebianControlFileNames{$simpleName} = $name;
582
}
583
 
584
#-------------------------------------------------------------------------------
585
# Function        : DebianDepends 
586
#
587
# Description     : This directive allows simple dependency information to be  
588
#                   inserted into the control file
589
#
4636 dpurdie 590
#                   Not useful in embedded system
4635 dpurdie 591
#
592
# Inputs          : Entry             - A dependency entry
593
#                   ...               - More entries
594
#                   
595
#
596
# Returns         : Nothing
597
#
598
sub DebianDepends
599
{
600
    push @DependencyList, @_;
601
}
602
 
603
 
604
#-------------------------------------------------------------------------------
407 dpurdie 605
# Function        : PackageDescription
606
#
607
# Description     : Specify the Package Description
608
#                   Keep it short
609
#
610
# Inputs          : $description
611
#
612
# Returns         : 
613
#
614
sub PackageDescription
615
{
616
    ($opt_description) = @_;
617
}
618
 
619
#-------------------------------------------------------------------------------
620
# Function        : MakeSymLink
621
#
622
# Description     : Create a symlink - with error detection
623
#
624
# Inputs          : old_file    - Link Target
625
#                                 Path to the link target
626
#                                 If an ABS path is provided, the routine will
627
#                                 attempt to create a relative link.
628
#                   new_file    - Relative to the output work space
629
#                                 Path to where the 'link' file will be created
630
#                   Options     - Must be last
631
#                                 --NoClean         - Don't play with links
632
#                                 --NoDotDot        - Don't create symlinks with ..
633
#
634
# Returns         : Nothing
635
#
636
sub MakeSymLink
637
{
638
    my $no_clean;
639
    my $no_dot;
640
    my @args;
641
 
642
    #
643
    #   Extract options
644
    #
645
    foreach ( @_ )
646
    {
647
        if ( m/^--NoClean/i ) {
648
            $no_clean = 1;
649
 
650
        } elsif ( m/^--NoDotDot/i ) {
651
            $no_dot = 1;
652
 
653
        } elsif ( m/^--/ ) {
654
            Error ("MakeSymLink: Unknown option: $_");
655
 
656
        } else {
657
            push @args, $_;
658
        }
659
    }
660
 
661
    my ($old_file, $new_file) = @args;
662
 
663
    my $tfile = $DebianWorkDir . '/' . $new_file;
664
    $tfile =~ s~//~/~;
665
    Verbose ("Symlink $old_file -> $new_file" );
666
 
667
    #
668
    #   Create the directory in which the link will be placed
669
    #   Remove any existing file of the same name
670
    #
671
    my $dir = StripFileExt( $tfile );
672
    mkpath( $dir) unless -d $dir;
673
    unlink $tfile;
674
 
675
    #
676
    #   Determine a good name of the link
677
    #   Convert to a relative link in an attempt to prune them
678
    #
679
    my $sfile = $old_file;
680
    unless ( $no_clean )
681
    {
682
        $sfile = CalcRelPath( StripFileExt( $new_file ), $old_file );
683
        $sfile = $old_file if ( $no_dot && $sfile =~ m~^../~ );
684
    }
685
 
686
    my $result = symlink $sfile, $tfile;
687
    Error ("Cannot create symlink. $old_file -> $new_file") unless ( $result );
688
}
689
 
690
#-------------------------------------------------------------------------------
691
# Function        : CopyFile
692
#
693
# Description     : Copy a file to a target dir
694
#                   Used for text files, or files with fixed names
695
#
696
# Inputs          : $src
697
#                   $dst_dir    - Within the output workspace
698
#                   $dst_name   - Output Name [Optional]
699
#                   Options     - Common Copy Options
700
#
701
# Returns         : Full path to destination file
702
#
703
sub CopyFile
704
{
705
    CopyFileCommon( \&ResolveFile, @_ );
706
}
707
 
708
#-------------------------------------------------------------------------------
709
# Function        : CopyBinFile
710
#
711
# Description     : Copy a file to a target dir
712
#                   Used for executable programs. Will look in places where
713
#                   programs are stored.
714
#
715
# Inputs          : $src
716
#                   $dst_dir    - Within the output workspace
717
#                   $dst_name   - Output Name [Optional]
718
#
719
#                   Options:
720
#                       --FromPackage
721
#                       --SoftLink=xxxx
722
#                       --LinkFile=xxxx
723
#
724
#
725
# Returns         : Full path to destination file
726
#
727
sub CopyBinFile
728
{
729
    CopyFileCommon( \&ResolveBinFile, @_ );
730
}
731
 
732
#-------------------------------------------------------------------------------
733
# Function        : CopyLibFile
734
#
735
# Description     : Copy a file to a target dir
736
#                   Used for shared programs. Will look in places where
737
#                   shared libraries are stored.
738
#
739
# Inputs          : $src        - Base for 'realname' (no lib, no extension)
740
#                   $dst_dir    - Within the output workspace
741
#                   $dst_name   - Output Name [Optional, but not suggested]
742
#
743
# Returns         : Full path to destination file
744
#
745
# Notes           : Copying 'lib' files
746
#                   These are 'shared libaries. There is no provision for copying
747
#                   static libraries.
748
#
749
#                   The tool will attempt to copy a well-formed 'realname' library
750
#                   The soname of the library should be constructed on the target
751
#                   platform using ldconfig.
752
#                   There is no provision to copy the 'linker' name
753
#
754
#                   Given a request to copy a library called 'fred', then the
755
#                   well formed 'realname' will be:
756
#                           libfred[P|D|]].so.nnnnn
757
#                   where:
758
#                           nnnn is the library version
759
#                           [P|D|] indicates Production, Debug or None
760
#
761
#                   The 'soname' is held within the realname form of the library
762
#                   and will be created by lsconfig.
763
#
764
#                   The 'linkername' would be libfred[P|D|].so. This is only
765
#                   needed when linking against the library.
766
#
767
#
768
#                   The routine will also recognize Windows DLLs
769
#                   These are of the form fred[P|D|].nnnnn.dll
770
#
771
sub CopyLibFile
772
{
773
    CopyFileCommon( \&ResolveLibFile, @_ );
774
}
775
 
776
#-------------------------------------------------------------------------------
777
# Function        : CopyFileCommon
778
#
779
# Description     : Common ( internal File Copy )
780
#
781
# Inputs          : $resolver           - Ref to function to resolve source file
782
#                   $src                - Source File Name
783
#                   $dst_dir            - Target Dir
784
#                   $dst_name           - Target Name (optional)
785
#                   Options
786
#                   Options:
787
#                       --FromPackage
788
#                       --SoftLink=xxxx
789
#                       --LinkFile=xxxx
4666 dpurdie 790
#                       --ConfigFile
407 dpurdie 791
#
792
# Returns         : 
793
#
794
sub CopyFileCommon
795
{
796
    my $from_package = 0;
797
    my $isa_linkfile = 0;
4666 dpurdie 798
    my $isa_configFile = 0;
407 dpurdie 799
    my @llist;
800
    my @args;
801
 
802
    #
803
    #   Parse options
804
    #
805
    foreach ( @_ )
806
    {
807
        if ( m/^--FromPackage/ ) {
808
            $from_package = 1;
809
 
810
        } elsif ( m/^--LinkFile/ ) {
811
            $isa_linkfile = 1;
812
 
4666 dpurdie 813
        } elsif ( m/^--ConfFile/i ) {
814
            $isa_configFile = 1;
815
 
407 dpurdie 816
        } elsif ( m/^--SoftLink=(.+)/ ) {
817
            push @llist, $1;
818
 
819
        } elsif ( m/^--/ ) {
820
            Error ("FileCopy: Unknown option: $_");
821
 
822
        } else {
823
            push @args, $_;
824
        }
825
    }
826
 
827
    #
828
    #   Extract non-options.
829
    #   These are the bits that are left over
830
    #
831
    my ($resolver, $src, $dst_dir, $dst_name ) = @args;
832
 
833
    #
834
    #   Clean up dest_dir. Must start with a / and not end with one
835
    #
836
    $dst_dir = "/$dst_dir/";
837
    $dst_dir =~ s~/+~/~g;
838
    $dst_dir =~ s~/$~~;
839
 
840
    Verbose ("CopyFile: $src, $dst_dir, " . ($dst_name || ''));
841
    foreach $src ( &$resolver( $from_package, $src ) )
842
    {
843
        my $dst_fname = $dst_name ? $dst_name : StripDir($src);
844
        my $dst_file = "$dst_dir/$dst_fname";
845
        Verbose ("CopyFile: Copy $src, $dst_file" );
846
 
847
 
848
        #
849
        #   LinkFiles are special
850
        #   They get concatenated to any existing LINKS File
851
        #
852
        if ( $isa_linkfile )
853
        {
854
            CatFile ( $src, "$dst_dir/.LINKS" );
855
        }
856
        else
857
        {
858
            mkpath( "$DebianWorkDir$dst_dir", 0, 0775);
859
            unlink ("$DebianWorkDir$dst_file");
860
            System ('cp','-f', $src, "$DebianWorkDir$dst_file" );
861
 
862
            foreach my $lname ( @llist )
863
            {
864
                $lname = $dst_dir . '/' . $lname unless ( $lname =~ m ~^/~ );
865
                MakeSymLink( $dst_file ,$lname);
866
            }
867
        }
4666 dpurdie 868
 
869
        #
870
        #   ConfigFiles are marked so that they can be handled by the debain installer
871
        #
872
        if ($isa_configFile)
873
        {
874
            push @ConfigList, $dst_file;
875
        }
407 dpurdie 876
    }
877
}
878
 
879
#-------------------------------------------------------------------------------
880
# Function        : CopyDir
881
#
882
# Description     : Copy a directory to a target dir
883
#
884
# Inputs          : $src_dir    - Local to the user
885
#                                 Symbolic Name
886
#                   $dst_dir    - Within the output workspace
887
#                   Options
423 dpurdie 888
#                       --Merge             - Don't delete first
889
#                       --Source=Name       - Source via Symbolic Name
4147 dpurdie 890
#                       --FromPackage       - Source via package roots
4152 dpurdie 891
#                       --NoIgnoreDbgFiles  - Do not ignore .dbg and .debug files in dir copy
892
#                       --IfPresent         - Not an error if the path cannot be found
407 dpurdie 893
#
894
# Returns         :
895
#
896
sub CopyDir
897
{
898
    my ($src_dir, $dst_dir, @opts) = @_;
899
    my $opt_merge;
900
    my $opt_base;
411 dpurdie 901
    my $from_interface = 0;
4152 dpurdie 902
    my $ignoreDbg = 1;
903
    my $ignoreNoDir;
904
    my $user_src_dir = $src_dir;
905
    my $opt_source;
906
    my $opt_package;
407 dpurdie 907
 
908
    $dst_dir = $DebianWorkDir . '/' . $dst_dir;
909
    $dst_dir =~ s~//~/~;
910
 
911
    #
4152 dpurdie 912
    #   Scan and collect user options
407 dpurdie 913
    #
914
    foreach  ( @opts )
915
    {
4152 dpurdie 916
        Verbose2 ("CopyDir: $_");
407 dpurdie 917
        if ( m/^--Merge/ ) {
918
            $opt_merge = 1;
4152 dpurdie 919
 
407 dpurdie 920
        } elsif ( m/^--Source=(.+)/ ) {
425 dpurdie 921
            Error ("Source directory can only be specified once")
4152 dpurdie 922
                if ( defined $opt_source );
923
            $opt_source = $1;
425 dpurdie 924
 
4152 dpurdie 925
        } elsif ( m/^--FromPackage/ ) {
926
            Error ("FromPackage can only be specified once")
927
                if ( defined $opt_package );
928
            $opt_package = 1;
929
 
930
        } elsif ( m/^--NoIgnoreDbgFiles/ ) {
931
            $ignoreDbg = 0;
932
 
933
        } elsif ( m/^--IfPresent/ ) {
934
            $ignoreNoDir = 1;
407 dpurdie 935
 
4152 dpurdie 936
        } else {
937
            Error ("CopyDir: Unknown option: $_" );
938
        }
939
    }
411 dpurdie 940
 
4152 dpurdie 941
    #
942
    #   All options have been gathered. Now process some of them
943
    #
944
    Error ("CopyDir: Cannot use both --Source and --FromPackage: $src_dir") if ($opt_source && $opt_package);
425 dpurdie 945
 
4152 dpurdie 946
    #
947
    #   Convert a symbolic path into a physical path
948
    #
949
    if ($opt_source)
950
    {
951
        Verbose2 ("CopyDir: Determine Source: $opt_source");
425 dpurdie 952
 
4152 dpurdie 953
        $opt_source = lc($opt_source);
954
        my %CopyDirSymbolic = (
955
            'interfaceincdir'   => $opt_interfaceincdir,
956
            'interfacelibdir'   => $opt_interfacelibdir,
957
            'interfacebindir'   => $opt_interfacebindir,
958
            'libdir'            => $opt_libdir,
959
            'bindir'            => $opt_bindir,
960
            'localincdir'       => $opt_localincdir,
961
            'locallibdir'       => $opt_locallibdir,
962
            'localbindir'       => $opt_localbindir,
963
            'packagebindir'     => $opt_pkgbindir,
964
            'packagelibdir'     => $opt_pkglibdir,
965
            'packagepkgdir'     => $opt_pkgpkgdir,
966
            'packagedir'        => $opt_pkgdir,
967
        );
425 dpurdie 968
 
4152 dpurdie 969
        if ( exists $CopyDirSymbolic{$opt_source} )
970
        {
971
            $opt_base = $CopyDirSymbolic{$opt_source};
425 dpurdie 972
 
973
            #
4152 dpurdie 974
            #   If sourceing from interface, then follow
975
            #   symlinks in the copy. All files will be links anyway
425 dpurdie 976
            #
977
            $from_interface = 1
4152 dpurdie 978
                if ( $opt_source =~ m~^interface~ );
979
        }
980
        else
981
        {
982
            DebugDumpData ("CopyDirSymbolic", \%CopyDirSymbolic);
983
            Error ("CopyDir: Unknown Source Name: $opt_source" );
984
        }
985
    }
425 dpurdie 986
 
4152 dpurdie 987
    #
988
    #   Locate the path within an external package
989
    #
990
    if ($opt_package)
991
    {
992
        Verbose2 ("CopyDir: FromPackage: $src_dir");
4147 dpurdie 993
 
4152 dpurdie 994
        my @path;
995
        foreach my $entry ( getPackageList() )
996
        {
997
            my $base = $entry->getBase(3);
998
            next unless ( defined $base );
999
            if ( -d $base . '/' . $src_dir )
1000
            {
1001
                push @path, $base;
1002
                $from_interface = 1
1003
                    if ( $entry->{'TYPE'} eq 'interface' );
1004
            }
407 dpurdie 1005
        }
4152 dpurdie 1006
 
1007
        if ( $#path < 0 )
1008
        {
1009
            Error ("CopyDir: Cannot find source dir in any package: $user_src_dir") unless ($ignoreNoDir);
1010
            Message ("CopyDir: Optional path not found: $user_src_dir");
1011
            return;
1012
        }
1013
 
1014
        Error ("CopyDir: Requested path found in mutiple packages: $user_src_dir",
1015
                @path ) if ( $#path > 0 );
1016
        $opt_base = pop @path;
1017
 
1018
        #
1019
        #   If sourceing from interface, then follow symlinks in the copy.
1020
        #   All files will be links anyway
1021
        #
1022
        #   This is a very ugly test for 'interface'
1023
        #
1024
        $from_interface = 1
1025
            if ( $opt_base =~ m~/interface/~ );
1026
 
407 dpurdie 1027
    }
1028
 
4152 dpurdie 1029
    #
1030
    #   Create the full source path
1031
    #   May be: from a package, from a known directoru, from a local directory
1032
    #
1033
 
407 dpurdie 1034
    $src_dir = $opt_base . '/' . $src_dir if ( $opt_base );
1035
    $src_dir =~ s~//~/~g;
1036
    $src_dir =~ s~/$~~;
1037
 
1038
    Verbose ("CopyDir: $src_dir, $dst_dir");
4152 dpurdie 1039
    unless ( -d $src_dir )
1040
    {
1041
        Error ("CopyDir: Directory not found: $user_src_dir") unless ($ignoreNoDir);
1042
        Message ("CopyDir: Optional path not found: $user_src_dir");
1043
        return;
1044
    }
407 dpurdie 1045
 
1046
    #
423 dpurdie 1047
    #   Setup the copy options
407 dpurdie 1048
    #
423 dpurdie 1049
    my %copyOpts;
4101 dpurdie 1050
    $copyOpts{'IgnoreDirs'} = ['.svn', '.git', '.cvs', '.hg'];
1051
    $copyOpts{'Ignore'} = ['.gbedir', '_gbedir'];
4147 dpurdie 1052
    push (@{$copyOpts{'Ignore'}}, '*.debug', '*.dbg') if $ignoreDbg;
423 dpurdie 1053
    $copyOpts{'EmptyDirs'} = 1;
1054
    $copyOpts{'DeleteFirst'} = 1 unless $opt_merge;
1055
    $copyOpts{'Log'} = 1 if ( $opt_verbose > 1 );
1056
    $copyOpts{'DuplicateLinks'} = 1 unless ( $from_interface );
407 dpurdie 1057
 
1058
    #
423 dpurdie 1059
    #   Transfer the directory
407 dpurdie 1060
    #
423 dpurdie 1061
    JatsCopy::CopyDir ( $src_dir, $dst_dir, \%copyOpts );
407 dpurdie 1062
 
1063
    #
1064
    #   Expand link files that may have been copied in
1065
    #
1066
    Verbose ("Locate LINKFILES in $DebianWorkDir");
1067
    ExpandLinkFiles();
1068
}
1069
 
1070
#-------------------------------------------------------------------------------
1071
# Function        : AddInitScript
1072
#
1073
# Description     : Add an Init Script to the target
1074
#                   Optionally create start and stop links
1075
#
1076
# Inputs          : $script     - Name of the init script
1077
#                   $start      - Start Number
1078
#                   $stop       - Stop Number
1079
#                   Options:
1080
#                       --NoCopy        - Don't copy the script, just add links
1081
#                       --Afc           - Place in AFC init area
1082
#                       --FromPackage   - Source is in a package
1083
#
1084
# Returns         : 
1085
#
1086
sub AddInitScript
1087
{
1088
    my $no_copy;
1089
    my $basedir = "";
1090
    my @args;
1091
    my $from_package = 0;
1092
 
4302 dpurdie 1093
    # This directive is only available on the VIX platforms
1094
    #   Kludgey test - at the moment
407 dpurdie 1095
    #
4302 dpurdie 1096
    if ($opt_pkgarch =~ m~i386~)
1097
    {
1098
        Error ("AddInitScript is not supported on this platform"); 
1099
    }
1100
 
1101
    #
407 dpurdie 1102
    #   Process and Remove options
1103
    #
1104
    foreach  ( @_ )
1105
    {
1106
        if ( m/^--NoCopy/ ) {
1107
            $no_copy = 1;
1108
 
1109
        } elsif ( m/^--Afc/ ) {
1110
            $basedir = "/afc";
1111
 
1112
        } elsif ( m/^--FromPackage/ ) {
1113
            $from_package = 1;
1114
 
1115
        } elsif ( m/^--/ ) {
1116
            Error ("AddInitScript: Unknown option: $_");
1117
 
1118
        } else {
1119
            push @args, $_;
1120
 
1121
        }
1122
    }
1123
 
1124
    my( $script, $start, $stop ) = @args;
1125
    Error ("No script file specified") unless ( $script );
1126
    Warning("AddInitScript: No start or stop index specified") unless ( $start || $stop );
1127
    Verbose ("AddInitScript: $script, " . ($start || 'No Start') . ", " . ($stop || 'No Stop'));
1128
    $script = ResolveFile($from_package, $script );
1129
 
1130
    my $tdir = $basedir . "/etc/init.d/init.d";
1131
    my $base = StripDir($script);
1132
 
1133
    CopyFile( $script, $tdir ) unless $no_copy;
1134
 
1135
    my $link;
1136
    if ( $start )
1137
    {
1138
        $link = sprintf ("${basedir}/etc/init.d/S%2.2d%s", $start, $base );
1139
        MakeSymLink( "$tdir/$base", $link);
1140
    }
1141
 
1142
    if ( $stop )
1143
    {
1144
        $link = sprintf ("${basedir}/etc/init.d/K%2.2d%s", $stop, $base );
1145
        MakeSymLink( "$tdir/$base", $link);
1146
    }
1147
}
1148
 
1149
#-------------------------------------------------------------------------------
1150
# Function        : CatFile
1151
#
1152
# Description     : Copy a file to the end of a file
1153
#
1154
# Inputs          : $src
1155
#                   $dst    - Within the output workspace
1156
#
1157
# Returns         :
1158
#
1159
sub CatFile
1160
{
1161
    my ($src, $dst) = @_;
1162
 
1163
    $dst = $DebianWorkDir . '/' . $dst;
1164
    $dst =~ s~//~/~;
1165
    Verbose ("CatFile: $src, $dst");
1166
    $src = ResolveFile(0, $src );
1167
 
1168
    open (SF, '<', $src)  || Error ("CatFile: Cannot open $src");
1169
    open (DF, '>>', $dst) || Error ("CatFile: Cannot create:$dst");
1170
    while ( <SF> )
1171
    {
1172
        print DF $_;
1173
    }
1174
    close (SF);
1175
    close (DF);
1176
}
1177
 
1178
#-------------------------------------------------------------------------------
1179
# Function        : EchoFile
1180
#
1181
# Description     : Echo simple text to a file
1182
#
1183
# Inputs          : $file   - Within the output workspace
1184
#                   $text
1185
#
1186
# Returns         : 
1187
#
1188
sub EchoFile
1189
{
1190
    my ($file, $text) = @_;
1191
    Verbose ("EchoFile: $file");
1192
 
1193
    $file = $DebianWorkDir . '/' . $file;
1194
    $file =~ s~//~/~;
1195
 
1196
    unlink $file;
1197
    open (DT, ">", $file ) || Error ("Cannot create $file");
1198
    print DT  $text || Error ("Cannot print to $file");
1199
    close DT;
1200
}
1201
 
1202
#-------------------------------------------------------------------------------
4640 dpurdie 1203
# Function        : ConvertFiles
1204
#
1205
# Description     : This sub-routine is used to remove all carrage return\line
1206
#                   feeds from a line and replace them with the platform
1207
#                   specific equivalent chars.
1208
#
1209
#                   We let PERL determine what characters are written to the
1210
#                   file base on the  platform you are running on.
1211
#
1212
#                   i.e. LF    for unix
1213
#                        CR\LF for win32
1214
#
1215
# Inputs          : outPath                 - Output directory
1216
#                   flist                   - List of files in that directory
1217
#                   or
1218
#                   SearchOptions           - Search options to find files
1219
#                                           --Recurse
1220
#                                           --NoRecurse
1221
#                                           --FilterIn=xxx
1222
#                                           --FilterInRe=xxx
1223
#                                           --FilterOut=xxx
1224
#                                           --FilterOutRe=xxx
1225
#                   Common options
1226
#                                           --Dos
1227
#                                           --Unix
1228
#
1229
#
1230
# Returns         : 1
1231
#
1232
sub ConvertFiles
1233
{
1234
    my @uargs;
1235
    my $lineEnding = "\n";
1236
    my ($dosSet, $unixSet);
1237
    my $search =  JatsLocateFiles->new( '--NoRecurse' );
1238
 
1239
    #
1240
    #   Process user arguments extracting options
1241
    #
1242
    foreach  ( @_ )
1243
    {
1244
        if ( m~^--Recurse~ ) {
1245
            $search->recurse(1);
1246
 
1247
        } elsif ( m~^--NoRecurse~) {
1248
            $search->recurse(0);
1249
 
1250
        } elsif ( /^--FilterOut=(.*)/ ) {
1251
            $search->filter_out($1);
1252
 
1253
        } elsif ( /^--FilterOutRe=(.*)/ ) {
1254
            $search->filter_out_re($1);
1255
 
1256
        } elsif ( /^--FilterIn=(.*)/ ) {
1257
            $search->filter_in($1);
1258
 
1259
        } elsif ( /^--FilterInRe=(.*)/ ) {
1260
            $search->filter_in_re($1);
1261
 
1262
        } elsif ( m~^--Dos~) {
1263
            $lineEnding = "\r\n";
1264
            $dosSet = 1;
1265
 
1266
        } elsif ( m~^--Unix~) {
1267
            $lineEnding = "\n";
1268
            $unixSet = 1;
1269
 
1270
        } elsif ( m~^--~) {
4641 dpurdie 1271
            Error ("ConvertFiles: Unknown option: $_");
4640 dpurdie 1272
 
1273
        } else {
1274
            push @uargs, $_;
1275
        }
1276
    }
1277
 
1278
    #
1279
    #   Process non-option arguments
1280
    #       - Base dir
1281
    #       - List of files
1282
    #
1283
    my ($outPath, @flist) = @uargs;
1284
    Error ("ConvertFiles: Target Dir must be specified" ) unless ( $outPath );
1285
 
1286
    #
1287
    #   Sanity Tests
1288
    #
1289
    Error ("ConvertFiles: --Dos and --Unix are mutually exclusive" ) if ( $dosSet && $unixSet );
1290
 
1291
 
1292
    #
1293
    # Convert output path to physical path
1294
    #
1295
    my $topDir = catdir($DebianWorkDir, $outPath);
1296
    Verbose("ConvertFiles: topDir: $topDir");
1297
    Error ("ConvertFiles: Path does not exist", $topDir) unless ( -e $topDir );
1298
    Error ("ConvertFiles: Path is not a directory", $topDir) unless ( -d $topDir );
1299
 
1300
    #
1301
    #   Need to determine if we are searching or simply using a file list
1302
    #   There are two forms of the functions. If any of the search options have
1303
    #   been used then we assume that we are searchine
1304
    #
1305
    if ( $search->has_filter() )
1306
    {
1307
        Error ("ConvertFiles: Cannot mix search options with named files") if ( @flist );
1308
        @flist = $search->search($topDir);
1309
    }
1310
    Error ("ConvertFiles: No files specified") unless ( @flist );
1311
 
1312
    #
1313
    #   Process all named files
1314
    #
1315
    foreach my $file ( @flist )
1316
    {
1317
 
1318
        # this is our file that we want to clean.
1319
        my ($ifileLoc) = "$topDir/$file";
1320
        my ($tfileLoc) = "$topDir/$file\.tmp";
4641 dpurdie 1321
        Verbose("ConvertFiles: $file");
4640 dpurdie 1322
 
1323
        # we will check to see if the file exists.
1324
        #
1325
        my $ifile;
1326
        my $tfile;
1327
        if ( -f "$ifileLoc" )
1328
        {
1329
            open ($ifile, "< $ifileLoc" ) or
1330
                Error("Failed to open file [$ifileLoc] : $!");
1331
 
1332
            open ($tfile, "> $tfileLoc" ) or
1333
                Error("Failed to open file [$tfileLoc] : $!");
1334
            binmode $tfile;
1335
 
1336
            while ( <$ifile> ) 
1337
            {
1338
                s~[\n\r]+$~~;               # Chomp
1339
                print $tfile "$_" . $lineEnding;
1340
            }
1341
        }
1342
        else
1343
        {
1344
            Error("ConvertFiles [$ifileLoc] does not exist.");
1345
        }
1346
 
1347
        close $ifile;
1348
        close $tfile;
1349
 
1350
 
1351
        # lets replace our original file with the new one
1352
        #
1353
        if(File::Copy::move("$tfileLoc", "$ifileLoc"))
1354
        {
1355
            Verbose2("ConvertFiles: Renamed [$tfileLoc] to [$ifileLoc] ...");
1356
        }
1357
        else
1358
        {
1359
            Error("ConvertFiles: Failed to rename file [$tfileLoc] to [$ifileLoc]: $!");
1360
        }
1361
    }
1362
 
1363
    return 1;
1364
}
1365
 
4641 dpurdie 1366
#----------------------------------------------------------------------------
1367
# Function        : ReplaceTags
1368
#
1369
# Description     : This sub-routine is used to replace Tags in one or more files
1370
#
1371
# Inputs          : outPath                 - Output directory
1372
#                   flist                   - List of files in that directory
1373
#                   or
1374
#                   SearchOptions           - Search options to find files
1375
#                                           --Recurse
1376
#                                           --NoRecurse
1377
#                                           --FilterIn=xxx
1378
#                                           --FilterInRe=xxx
1379
#                                           --FilterOut=xxx
1380
#                                           --FilterOutRe=xxx
1381
#                   Common options
1382
#                                           --Tag=Tag,Replace
1383
#                                           
1384
#
1385
# Returns         : 1
1386
#
1387
sub ReplaceTags
1388
{
1389
    my @uargs;
1390
    my $search =  JatsLocateFiles->new( '--NoRecurse' );
1391
    my @tagsList;
1392
    my $tagSep = ',';
1393
    my @tagOrder;
1394
    my %tagData;
1395
 
1396
    #
1397
    #   Process user arguments extracting options
1398
    #
1399
    foreach  ( @_ )
1400
    {
1401
        if ( m~^--Recurse~ ) {
1402
            $search->recurse(1);
1403
 
1404
        } elsif ( m~^--NoRecurse~) {
1405
            $search->recurse(0);
1406
 
1407
        } elsif ( /^--FilterOut=(.*)/ ) {
1408
            $search->filter_out($1);
1409
 
1410
        } elsif ( /^--FilterOutRe=(.*)/ ) {
1411
            $search->filter_out_re($1);
1412
 
1413
        } elsif ( /^--FilterIn=(.*)/ ) {
1414
            $search->filter_in($1);
1415
 
1416
        } elsif ( /^--FilterInRe=(.*)/ ) {
1417
            $search->filter_in_re($1);
1418
 
1419
        } elsif ( m~^--Tag=(.*)~) {
1420
            push @tagsList, $1;
1421
 
1422
        } elsif ( m~^--~) {
1423
            Error ("ReplaceTags: Unknown option: $_");
1424
 
1425
        } else {
1426
            push @uargs, $_;
1427
        }
1428
    }
1429
 
1430
    #
1431
    #   Process non-option arguments
1432
    #       - Base dir
1433
    #       - List of files
1434
    #
1435
    my ($outPath, @flist) = @uargs;
1436
    Error ("ReplaceTags: Target Dir must be specified" ) unless ( $outPath );
1437
 
1438
    #
1439
    #   Sanity Tests
1440
    #
1441
    Error ("ReplaceTags: No tags specified" ) unless ( @tagsList );
1442
 
1443
    #
1444
    # Convert output path to physical path
1445
    #
1446
    my $topDir = catdir($DebianWorkDir, $outPath);
1447
    Verbose("ReplaceTags: topDir: $topDir");
1448
    Error ("ReplaceTags: Path does not exist", $topDir) unless ( -e $topDir );
1449
    Error ("ReplaceTags: Path is not a directory", $topDir) unless ( -d $topDir );
1450
 
1451
    #
1452
    #   Convert Tags into pairs for latter use
1453
    #
1454
    my $sep = quotemeta ($tagSep );
1455
    foreach my $tag ( @tagsList )
1456
    {
1457
        my ($tname,$tvalue) = split ( $sep, $tag );
1458
        Error ("No tag value in: $tag" ) unless ( defined $tvalue );
1459
        Error ("Duplicate Tag: $tname" ) if ( exists $tagData{$tname} );
1460
        Verbose ("Tag: $tname :: $tvalue");
1461
        push @tagOrder, $tname;
1462
        $tagData{$tname} = $tvalue;
1463
    }
1464
 
1465
    #
1466
    #   Need to determine if we are searching or simply using a file list
1467
    #   There are two forms of the functions. If any of the search options have
1468
    #   been used then we assume that we are searchine
1469
    #
1470
    if ( $search->has_filter() )
1471
    {
1472
        Error ("ReplaceTags: Cannot mix search options with named files") if ( @flist );
1473
        @flist = $search->search($topDir);
1474
    }
1475
    Error ("ReplaceTags: No files specified") unless ( @flist );
1476
 
1477
    #
1478
    #   Process all named files
1479
    #
1480
    foreach my $file ( @flist )
1481
    {
1482
 
1483
        # this is our file that we want to clean.
1484
        my ($ifileLoc) = "$topDir/$file";
1485
        my ($tfileLoc) = "$topDir/$file\.tmp";
1486
        Verbose("ReplaceTags: $file");
1487
 
1488
        # we will check to see if the file exists.
1489
        #
1490
        my $ifile;
1491
        my $tfile;
1492
        if ( -f "$ifileLoc" )
1493
        {
1494
            open ($ifile, "< $ifileLoc" ) or
1495
                Error("Failed to open file [$ifileLoc] : $!");
1496
 
1497
            open ($tfile, "> $tfileLoc" ) or
1498
                Error("Failed to open file [$tfileLoc] : $!");
1499
 
1500
            while ( <$ifile> ) 
1501
            {
1502
                s~[\n\r]+$~~;               # Chomp
1503
 
1504
                #
1505
                #   Perform tag replacement
1506
                #
1507
                foreach my $tag ( @tagOrder )
1508
                {
1509
                    my $value = $tagData{$tag};
1510
                    if ( s~$tag~$value~g )
1511
                    {
1512
                        Verbose2("Replaced: $tag with $value");
1513
                    }
1514
                }
1515
 
1516
                print $tfile "$_\n";
1517
            }
1518
        }
1519
        else
1520
        {
1521
            Error("ReplaceTags [$ifileLoc] does not exist.");
1522
        }
1523
 
1524
        close $ifile;
1525
        close $tfile;
1526
 
1527
 
1528
        # lets replace our original file with the new one
1529
        #
1530
        if(File::Copy::move("$tfileLoc", "$ifileLoc"))
1531
        {
1532
            Verbose2("ReplaceTags: Renamed [$tfileLoc] to [$ifileLoc] ...");
1533
        }
1534
        else
1535
        {
1536
            Error("ReplaceTags: Failed to rename file [$tfileLoc] to [$ifileLoc]: $!");
1537
        }
1538
    }
1539
 
1540
    return 1;
1541
}
1542
 
4640 dpurdie 1543
#-------------------------------------------------------------------------------
407 dpurdie 1544
# Function        : SetFilePerms
1545
#
1546
# Description     : Set file permissions on one or more files or directories
1547
#
1548
# Inputs          : $perm           - Perm Mask
1549
#                   @paths          - List of paths/files to process
1550
#                   Options
1551
#                       --Recurse   - Recurse subdirs
1552
#
1553
# Returns         : 
1554
#
1555
sub SetFilePerms
1556
{
1557
 
1558
    my @args;
1559
    my $perms;
1560
    my $recurse = 0;
1561
 
1562
    #
1563
    #   Process and Remove options
1564
    #
1565
    foreach  ( @_ )
1566
    {
1567
        if ( m/^--Recurse/ ) {
1568
            $recurse = 1;
1569
 
1570
        } elsif ( m/^--/ ) {
1571
            Error ("SetFilePerms: Unknown option: $_");
1572
 
1573
        } else {
1574
            push @args, $_;
1575
 
1576
        }
1577
    }
1578
 
1579
    $perms = shift @args;
1580
    Error ("SetFilePerms: No Permissions" ) unless ( $perms );
1581
 
1582
    foreach my $path ( @args )
1583
    {
1584
        Verbose ("Set permissions; $perms, $path");
1585
        my $full_path = $DebianWorkDir . '/' . $path;
1586
        if ( -f $full_path )
1587
        {
1588
            System ('chmod', $perms, $full_path );
1589
        }
1590
        elsif ( -d $full_path )
1591
        {
1592
            System ('chmod', '-R', $perms, $full_path ) if ($recurse);
1593
            System ('chmod', $perms, $full_path ) unless ($recurse);
1594
        }
1595
        else
1596
        {
1597
            Warning("SetFilePerms: Path not found: $path");
1598
        }
1599
    }
1600
}
1601
 
1602
#-------------------------------------------------------------------------------
4636 dpurdie 1603
# Function        : SetPermissions 
1604
#
1605
# Description     : Called to set permissions of files/dirs in a directory structure.
1606
#                   With no options sets DirTag and all files/dirs in it to perms
1607
#   
1608
# Inputs          : path        - The directory tag to start setting permissions on
1609
#                   Options     - See below
1610
#       
1611
#   Required Options:
1612
#       One or both of
1613
#               --FilePerms=    Sets the permissions of files to this permission.
1614
#                               If not supplied then no files have their permissions changed
1615
#               --DirPerms=     Sets the permissions of directories to this permission
1616
#                               If not supplied then no directories have their permissions changed
1617
#       OR
1618
#               --Perms=        Sets the permissions of both files and directories to this permissions
1619
#                               Equivalent to supplying both --FilePerms=X && --DirPerms=X
1620
#               
1621
#   Options:                    
1622
#               --RootOnly      Only sets the permissions on the 'path' directory/file, 
1623
#                               all other options ignored
1624
#               --SkipRoot      Does not set permissions on the 'path' directory/file, 
1625
#                               obviously mutually exlusive with --RootOnly
1626
#   
1627
#       Any option supported by JatsLocateFiles. 
1628
#       Some of these include:
1629
#               
1630
#               --Recurse       Recurse the directory tree.  Does a depth first recurse so that all 
1631
#                               dir entries are processed before the dir itself (default)
1632
#               --NoRecurse     Dont recurse
1633
#               --FilterIn=     Apply permissions to files/directories that matches this value.
1634
#               --FilterInRe=   Perl RE's can be used (Not Shell wildcards) and this option
1635
#                               can be supplied mulitple times
1636
#               --FilterOut=    Dont apply permissions to any files/directories matching this value
1637
#               --FilterOutRe=  Perl RE's can be used (Not Shell wildcards) and this option
1638
#                               can be supplied mulitple times
1639
#               
1640
#                               FilterIn is applied before FilterOut.  If Recurse is specified 
1641
#                               the directory will be recursed regardless of these filters, however
1642
#                               the filter will be applied when it comes time to chmod the dir 
1643
#
1644
#------------------------------------------------------------------------------
1645
sub SetPermissions
1646
{
1647
    my ( $path, $filePerms, $dirPerms, $someDone );
1648
    my ( $rootOnly, $skipRoot ) = ( 0, 0 );
1649
 
1650
    my $search =  JatsLocateFiles->new( '--Recurse', '--DirsToo' );
1651
 
1652
    foreach ( @_ )
1653
    {
1654
        if ( m/^--Perms=(.*)/ ) {
1655
            $filePerms = $1;
1656
            $dirPerms = $1;
1657
 
1658
        } elsif (m/^--FilePerms=(.*)/ )  {
1659
            $filePerms = $1;
1660
 
1661
        } elsif ( m/^--DirPerms=(.*)/ )  {
1662
            $dirPerms = $1;
1663
 
1664
        }  elsif ( m/^--RootOnly/ ) {
1665
            $rootOnly = 1;
1666
 
1667
        } elsif ( m/^--SkipRoot/ )  {
1668
            $skipRoot = 1;
1669
 
1670
        } elsif ( m/^--Filter/ && $search->option( $_ ) ) {
1671
            Verbose2 ("Search Option: $_" );
1672
 
1673
        } elsif ( m/^--Recurse|--NoRecurse/ && $search->option( $_ ) ) {
1674
            Verbose2 ("Search Option: $_" );
1675
 
1676
        } elsif (m/^--/ ) {
1677
            Error ("SetPermissions: Unknown option: $_");
1678
 
1679
        } else  {
1680
            Error("SetPermissions 'path' already set", "Path: $_") if ( $path );
1681
            $path = $_;
1682
        }
1683
    }
1684
 
1685
    #
1686
    #   Sanity test
1687
    #
1688
    Error("SetPermissions called with out a 'path' parameter") if ( !defined($path) );
1689
    Error("SetPermissions called with out any Permissions specified") if ( !defined($filePerms) && !defined($dirPerms) );
1690
    Error("SetPermissions: Options --RootOnly & --SkipRoot are mutually exclusive" ) if ( $rootOnly && $skipRoot );
1691
 
1692
 
1693
    #   Convert the target directory name into a physical path
1694
    #   User specifies '/' as the root of the image
1695
    #   User specifies 'name' as relateve to the root of the image
1696
    #
1697
    my $topDir = $DebianWorkDir . '/' . $path;
1698
    $topDir =~ s~/+$~~;
1699
 
1700
    Verbose("SetPermissions: Called with options " . join(", ", @_));
1701
 
1702
    #
1703
    #   Only set perms on the root directory
1704
    #       This is a trivial operation
1705
    #
1706
    if ( $rootOnly )
1707
    {
1708
        $someDone += chmodItem( $topDir, $filePerms, $dirPerms );
1709
    }
1710
    else
1711
    {
1712
        #
1713
        #   Create a list of files/dirs to process
1714
        #
1715
        my @elements = $search->search( $topDir );
1716
 
1717
        foreach my $dirEntry ( @elements )
1718
        {
1719
            my $fullPath = "$topDir/$dirEntry";
1720
 
1721
            # A dir and we dont have dirperms, so skip
1722
            if ( -d $fullPath && !defined($dirPerms) )
1723
            {
1724
                Verbose2("SetPermissions: Skipping dir $fullPath as we have no dir permissions");
1725
                next;
1726
            }
1727
 
1728
            # A file and we dont have fileperms, so skip
1729
            if ( -f $fullPath && !defined($filePerms) )
1730
            {
1731
                Verbose2("SetPermissions: Skipping file $fullPath as we have no file permissions");
1732
                next;
1733
            }
1734
 
1735
            # a file or a dir and have the right permissions and we are not recursing
1736
            if ( -f $fullPath || -d $fullPath )
1737
            {
1738
                $someDone += chmodItem( $fullPath, $filePerms, $dirPerms );
1739
            }
1740
            else
1741
            {
1742
                Warning("SetPermissions: Skipping $fullPath as its not a file or directory");
1743
            }
1744
        }
1745
 
1746
        #
1747
        #   Process the topDir
1748
        #   May not be modified if --SkipRoot has been requested
1749
        #
1750
        if ( !$skipRoot && -e $topDir )
1751
        {
1752
            $someDone += chmodItem( $topDir, $filePerms, $dirPerms );
1753
        }
1754
    }
1755
 
1756
    #   Final warning
1757
    #
1758
    Warning ("SetPermissions: No files located", "Args: @_") unless ( $someDone );
1759
}
1760
 
4676 dpurdie 1761
#************ INTERNAL USE ONLY  **********************************************
4636 dpurdie 1762
# Function        : chmodItem 
1763
#
1764
# Description     : Internal
1765
#                   chmod a file or a folder
1766
#
1767
# Inputs          : item                        - Item to mod
1768
#                   filePerms                   - File perms
1769
#                   dirPerms                    - dire perms
1770
#
1771
# Returns         : 1   - Item modified
1772
#                   0   - Item not modified
1773
#
4676 dpurdie 1774
#************ INTERNAL USE ONLY  **********************************************
4636 dpurdie 1775
sub chmodItem
1776
{
1777
    my ($item, $filePerms, $dirPerms) = @_;
1778
 
1779
    if ( -d $item && defined $dirPerms)
1780
    {
1781
        Verbose("SetPermissions: $dirPerms : $item");
1782
        System ('chmod', $dirPerms, $item );
1783
        return 1;
1784
    }
1785
 
1786
    if ( -f $item  && defined $filePerms)
1787
    {
1788
        Verbose("SetPermissions: $filePerms : $item");
1789
        System ('chmod', $filePerms, $item );
1790
        return 1;
1791
    }
1792
 
1793
    return 0;
1794
}
1795
 
1796
 
1797
#-------------------------------------------------------------------------------
407 dpurdie 1798
# Function        : CreateDir
1799
#
1800
# Description     : Create a directory within the target workspace
1801
#
1802
# Inputs          : $path           - Name of the target directory
1803
#
1804
# Returns         : Nothing
1805
#
1806
sub CreateDir
1807
{
1808
    my ($path) = @_;
1809
 
1810
    Verbose ("Create Dir: $path");
1811
    mkpath( $DebianWorkDir . '/' . $path );
1812
}
1813
 
1814
#-------------------------------------------------------------------------------
1815
# Function        : IsProduct
1816
#                   IsPlatform
1817
#                   IsTarget
427 dpurdie 1818
#                   IsVariant
407 dpurdie 1819
#
1820
# Description     : This function allows some level of control in the
1821
#                   packaging scripts. It will return true if the current
1822
#                   product is listed.
1823
#
1824
#                   Ugly after thought
1825
#
1826
#                   Intended use:
1827
#                       Xxxxxx(...) if (IsProduct( 'aaa',bbb' );
1828
#
1829
# Inputs          : products    - a list of products to compare against
1830
#
1831
# Returns         : True if the current build is for one of the listed products
1832
#
1833
sub IsProduct
1834
{
1835
    foreach ( @_ )
1836
    {
1837
        return 1 if ( $opt_product eq $_ );
1838
    }
1839
    return 0;
1840
}
1841
 
1842
sub IsPlatform
1843
{
1844
    foreach ( @_ )
1845
    {
1846
        return 1 if ( $opt_platform eq $_ );
1847
    }
1848
    return 0;
1849
}
1850
 
1851
sub IsTarget
1852
{
1853
    foreach ( @_ )
1854
    {
1855
        return 1 if ( $opt_target eq $_ );
1856
    }
1857
    return 0;
1858
}
1859
 
427 dpurdie 1860
sub IsVariant
1861
{
1862
    foreach ( @_ )
1863
    {
1864
        return 1 if ( $opt_variant eq $_ );
1865
    }
1866
    return 0;
1867
}
407 dpurdie 1868
 
4676 dpurdie 1869
#************ INTERNAL USE ONLY  **********************************************
407 dpurdie 1870
# Function        : FindFiles
1871
#
1872
# Description     : Locate files within a given dir tree
1873
#
1874
# Inputs          : $root           - Base of the search
1875
#                   $match          - Re to match
1876
#
1877
# Returns         : A list of files that match
1878
#
4676 dpurdie 1879
#************ INTERNAL USE ONLY  **********************************************
407 dpurdie 1880
my @FIND_LIST;
1881
my $FIND_NAME;
1882
 
1883
sub FindFiles
1884
{
1885
    my ($root, $match ) = @_;
1886
    Verbose2("FindFiles: Root: $root, Match: $match");
1887
 
1888
    #
1889
    #   Becareful of closure, Must use globals
1890
    #
1891
    @FIND_LIST = ();
1892
    $FIND_NAME = $match;
1893
    File::Find::find( \&find_files, $root);
1894
 
1895
    #
1896
    #   Find callback program
1897
    #
1898
    sub find_files
1899
    {
1900
        my $item =  $File::Find::name;
1901
 
1902
        return if ( -d $File::Find::name );
1903
        return unless ( $_ =~ m~$FIND_NAME~ );
1904
        push @FIND_LIST, $item;
1905
    }
1906
    return @FIND_LIST;
1907
}
1908
 
1909
#-------------------------------------------------------------------------------
1910
# Function        : CalcRelPath
1911
#
1912
# Description     : Return the relative path to the current working directory
1913
#                   as provided in $Cwd
1914
#
1915
# Inputs          : $Cwd - Base dir
1916
#                   $base - Path to convert
1917
#
1918
# Returns         : Relative path from the $Cwd
1919
#
1920
sub CalcRelPath
1921
{
1922
    my ($Cwd, $base) = @_;
1923
 
1924
    my @base = split ('/', $base );
1925
    my @here = split ('/', $Cwd );
1926
    my $result;
1927
 
1928
    Debug("RelPath: Source: $base");
1929
 
1930
    return $base unless ( $base =~ m~^/~ );
1931
 
1932
    #
1933
    #   Remove common bits from the head of both lists
1934
    #
1935
    while ( $#base >= 0 && $#here >= 0 && $base[0] eq $here[0] )
1936
    {
1937
        shift @base;
1938
        shift @here;
1939
    }
1940
 
1941
    #
1942
    #   Need to go up some directories from here and then down into base
1943
    #
1944
    $result = '../' x ($#here + 1);
1945
    $result .= join ( '/', @base);
1946
    $result = '.' unless ( $result );
1947
    $result =~ s~//~/~g;
1948
    $result =~ s~/$~~;
1949
 
1950
    Debug("RelPath: Result: $result");
1951
    return $result;
1952
}
1953
 
1954
#-------------------------------------------------------------------------------
1955
# Function        : ExpandLinkFiles
1956
#
1957
# Description     : Look for .LINK files in the output image and expand
1958
#                   the links into softlinks
1959
#
1960
# Inputs          : None
1961
#                   The rouine works on the $DebianWorkDir directory tree
1962
#
1963
# Returns         : Nothing
1964
#                   Will remove .LINKS files that are processed
1965
#
1966
sub ExpandLinkFiles
1967
{
1968
    foreach my $linkfile ( FindFiles( $DebianWorkDir, ".LINKS" ))
1969
    {
1970
        next if ( $linkfile =~ m~/\.svn/~ );
1971
        my $BASEDIR = StripFileExt( $linkfile );
1972
        $BASEDIR =~ s~^$DebianWorkDir/~~;
1973
        Verbose "Expand links: $BASEDIR";
1974
 
1975
        open (LF, "<", $linkfile ) || Error ("Cannot open link file: $linkfile" );
1976
        while ( <LF> )
1977
        {
1978
            chomp;
1979
            next if ( m~^#~ );
1980
            next unless ( $_ );
1981
            my ($link, $file) = split;
1982
 
1983
            MakeSymLink($file ,"$BASEDIR/$link", '--NoDotDot' );
1984
        }
1985
        close (LF);
1986
        unlink $linkfile;
1987
    }
1988
}
1989
 
4676 dpurdie 1990
#************ INTERNAL USE ONLY  **********************************************
407 dpurdie 1991
# Function        : ResolveFile
1992
#
1993
# Description     : Determine where the source for a file is
415 dpurdie 1994
#                   Will look in (default):
407 dpurdie 1995
#                       Local directory
1996
#                       Local Include
415 dpurdie 1997
#                   Or  (FromPackage)
1998
#                       Our Package directory
1999
#                       Interface directory (BuildPkgArchives)
2000
#                       Packages (LinkPkgArchive)
2001
#
407 dpurdie 2002
#                   Will scan 'parts' subdirs
2003
#
2004
# Inputs          : $from_package       - 0 - Local File
2005
#                   $file
2006
#
2007
# Returns         : Path
2008
#
4676 dpurdie 2009
#************ INTERNAL USE ONLY  **********************************************
407 dpurdie 2010
sub ResolveFile
2011
{
2012
    my ($from_package, $file) = @_;
2013
    my $wildcard = ($file =~ /[*?]/);
415 dpurdie 2014
    my @path;
407 dpurdie 2015
 
2016
    #
415 dpurdie 2017
    #   Determine the paths to search
2018
    #
2019
    if ( $from_package )
2020
    {
2021
        unless ( @ResolveFileList )
2022
        {
2023
            push @ResolveFileList, $opt_pkgdir;
2024
            foreach my $entry ( getPackageList() )
2025
            {
2026
                push @ResolveFileList, $entry->getBase(3);
2027
            }
2028
        }
2029
        @path = @ResolveFileList;
2030
    }
2031
    else
2032
    {
2033
        @path = ('.', $opt_localincdir);
2034
    }
2035
 
2036
    #
407 dpurdie 2037
    #   Determine a full list of 'parts' to search
2038
    #   This is provided within the build information
2039
    #
2040
    my @parts = getPlatformParts ();
2041
    push @parts, '';
2042
 
2043
    my @done;
2044
    foreach my $root (  @path )
2045
    {
2046
        foreach my $subdir ( @parts )
2047
        {
2048
            my $sfile;
415 dpurdie 2049
            $sfile = "$root/$subdir/$file";
2050
            $sfile =~ s~//~/~g;
2051
            $sfile =~ s~^./~~g;
2052
            Verbose2("LocateFile: $sfile, $root, $subdir");
2053
            if ( $wildcard )
2054
            {
2055
                push @done, glob ( $sfile );
2056
            }
2057
            else
2058
            {
2059
                push @done, $sfile if ( -f $sfile || -l $sfile )
2060
            }
407 dpurdie 2061
        }
2062
    }
2063
 
415 dpurdie 2064
    Error ("ResolveFile: File not found: $file", "Search Path:", @path)
407 dpurdie 2065
        unless ( @done );
2066
 
2067
    Warning ("ResolveFile: Multiple instances of file found. Only first is used", @done)
2068
        if ( $#done > 0 && ! $wildcard && !wantarray );
2069
 
2070
    return wantarray ? @done : $done[0];
2071
}
2072
 
2073
#-------------------------------------------------------------------------------
2074
# Function        : ResolveBinFile
2075
#
415 dpurdie 2076
# Description     : Determine where the source for a BIN file is
2077
#                   Will look in (default):
2078
#                       Local directory
2079
#                       Local Include
2080
#                   Or  (FromPackage)
2081
#                       Our Package directory
2082
#                       Interface directory (BuildPkgArchives)
2083
#                       Packages (LinkPkgArchive)
407 dpurdie 2084
#                   Will scan 'parts' subdirs
2085
#
2086
# Inputs          : $from_package       - 0 - Local File
415 dpurdie 2087
#                   $file
407 dpurdie 2088
#
2089
# Returns         : Path
2090
#
2091
sub ResolveBinFile
2092
{
2093
    my ($from_package, $file) = @_;
2094
    my @path;
2095
    my @types;
2096
    my $wildcard = ($file =~ /[*?]/);
2097
 
415 dpurdie 2098
    #
2099
    #   Determine the paths to search
2100
    #
407 dpurdie 2101
    if ( $from_package )
2102
    {
415 dpurdie 2103
        unless ( @ResolveBinFileList )
2104
        {
2105
            push @ResolveBinFileList, $opt_pkgdir . '/bin';
2106
            foreach my $entry ( getPackageList() )
2107
            {
2108
                if ( my $path = $entry->getBase(3) )
2109
                {
2110
                    $path .= '/bin';
2111
                    push @ResolveBinFileList, $path if ( -d $path );
2112
                }
2113
            }
2114
        }
2115
        @path = @ResolveBinFileList;
407 dpurdie 2116
        @types = ($opt_type, '');
2117
    }
2118
    else
2119
    {
2120
        @path = ($opt_bindir, $opt_localbindir);
2121
        @types = '';
2122
    }
2123
 
2124
    #
2125
    #   Determine a full list of 'parts' to search
2126
    #   This is provided within the build information
2127
    #
2128
    my @parts = getPlatformParts ();
2129
    push @parts, '';
2130
 
2131
    my @done;
2132
    foreach my $root (  @path )
2133
    {
2134
        foreach my $subdir ( @parts )
2135
        {
2136
            foreach my $type ( @types )
2137
            {
2138
                my $sfile;
2139
                $sfile = "$root/$subdir$type/$file";
2140
                $sfile =~ s~//~/~g;
2141
                Verbose2("LocateBinFile: $sfile");
2142
                if ( $wildcard )
2143
                {
429 dpurdie 2144
                    foreach  ( glob ( $sfile ) )
2145
                    {
4143 dpurdie 2146
                        # Ignore .dbg (vix) and .debug (qt) files.
429 dpurdie 2147
                        next if ( m~\.dbg$~ );
4143 dpurdie 2148
                        next if ( m~\.debug$~ );
429 dpurdie 2149
                        push @done, $_;
2150
                    }
407 dpurdie 2151
                }
2152
                else
2153
                {
415 dpurdie 2154
                    push @done, $sfile if ( -f $sfile || -l $sfile )
407 dpurdie 2155
                }
2156
            }
2157
        }
2158
    }
2159
 
415 dpurdie 2160
    Error ("ResolveBinFile: File not found: $file", "Search Path:", @path)
407 dpurdie 2161
        unless ( @done );
2162
 
2163
    Warning ("ResolveBinFile: Multiple instances of file found. Only first is used", @done)
2164
        if ( $#done > 0 && ! $wildcard && !wantarray );
2165
    return wantarray ? @done : $done[0];
2166
}
2167
 
2168
#-------------------------------------------------------------------------------
2169
# Function        : ResolveLibFile
2170
#
415 dpurdie 2171
# Description     : Determine where the source for a LIB file is
2172
#                   Will look in (default):
2173
#                       Local directory
2174
#                       Local Include
2175
#                   Or  (FromPackage)
2176
#                       Our Package directory
2177
#                       Interface directory (BuildPkgArchives)
2178
#                       Packages (LinkPkgArchive)
407 dpurdie 2179
#                   Will scan 'parts' subdirs
2180
#
4672 dpurdie 2181
# Inputs          : $from_package   - 0:Local File
2182
#                   $file           - Basename for a 'realname'
2183
#                                     Do not provide 'lib' or '.so' or version info
2184
#                                     May contain embedded options
2185
#                                       --Dll           - Use Windows style versioned DLL
2186
#                                       --VersionDll    - Use the versioned DLL
2187
#                                       --3rdParty      - Use exact name provided
407 dpurdie 2188
#
2189
# Returns         : Path
2190
#
2191
sub ResolveLibFile
2192
{
2193
    my ($from_package, $file) = @_;
2194
    my $wildcard = ($file =~ /[*?]/);
2195
    my @options;
2196
    my $num_dll;
415 dpurdie 2197
    my @path;
407 dpurdie 2198
    #
2199
    #   Extract options from file
2200
    #
409 alewis 2201
    $num_dll = 0;
407 dpurdie 2202
    ($file, @options) = split ( ',', $file);
2203
    foreach ( @options )
2204
    {
2205
        if ( m/^--Dll/ ) {
2206
            $num_dll = 1;
2207
        } elsif ( m/^--VersionDll/ ) {
2208
            $num_dll = 2;
4672 dpurdie 2209
        } elsif ( m/^--3rdParty/ ) {
2210
            $num_dll = 3;
407 dpurdie 2211
        } else {
2212
            Error ("Unknown suboption to ResolveLibFile: $_" );
2213
        }
2214
    }
2215
 
2216
    #
415 dpurdie 2217
    #   Determine the paths to search
2218
    #
2219
    if ( $from_package )
2220
    {
2221
        unless ( @ResolveLibFileList )
2222
        {
2223
            push @ResolveLibFileList, $opt_pkgdir . '/lib';
2224
            foreach my $entry ( getPackageList() )
2225
            {
2226
                push @ResolveLibFileList, $entry->getLibDirs(3);
2227
            }
2228
        }
2229
        @path = @ResolveLibFileList;
2230
    }
2231
    else
2232
    {
2233
        @path = ($opt_libdir, $opt_locallibdir);
2234
    }
2235
 
2236
    #
407 dpurdie 2237
    #   Determine a full list of 'parts' to search
2238
    #   This is provided within the build information
2239
    #
2240
    my @parts = getPlatformParts ();
2241
    push @parts, '';
2242
 
2243
    my @done;
2244
    foreach my $root (  @path )
2245
    {
2246
        foreach my $type ( $opt_type, '' )
2247
        {
2248
            foreach my $subdir ( @parts )
2249
            {
2250
                my $sfile;
2251
                my $exact;
2252
                if ( $num_dll == 2 ) {
2253
                    $sfile = $file . $type . '.*.dll' ;
2254
                } elsif ( $num_dll == 1 ) {
2255
                    $sfile = $file . $type . '.dll' ;
2256
                    $exact = 1;
4672 dpurdie 2257
                } elsif ( $num_dll == 3 ) {
2258
                    $sfile = $file;
2259
                    $exact = 1;
407 dpurdie 2260
                } else {
2261
                    $sfile = "lib" . $file . $type . '.so.*';
2262
                }
2263
 
2264
                $sfile = "$root/$subdir/$sfile";
2265
                $sfile =~ s~//~/~g;
2266
                Verbose2("LocateLibFile: $sfile");
2267
                if ( $exact )
2268
                {
415 dpurdie 2269
                    push @done, $sfile if ( -f $sfile || -l $sfile );
407 dpurdie 2270
                }
419 dpurdie 2271
                elsif ($num_dll)
407 dpurdie 2272
                {
2273
                    push @done, glob ( $sfile );
2274
                }
419 dpurdie 2275
                else
2276
                {
2277
                    #
2278
                    #   Looking for .so files
2279
                    #   Filter out the soname so files
2280
                    #   Assume that the soname is shorter than the realname
4143 dpurdie 2281
                    #       Ignore .dbg (vix) and .debug (qt) files.
419 dpurdie 2282
                    #
2283
                    my %sieve;
2284
                    foreach ( glob ( $sfile )  )
2285
                    {
429 dpurdie 2286
                        next if ( m~\.dbg$~ );
4143 dpurdie 2287
                        next if ( m~\.debug$~ );
421 alewis 2288
                        m~(.*\.so\.)([\d\.]*\d)$~;
2289
                        if ( $1 )
2290
                        {
2291
                            my $file = $1;
2292
                            my $len = exists $sieve{$file} ? length($sieve{$file}) : 0;
2293
                            $sieve{$file} = $_
2294
                                if ( $len == 0 || length($_) > $len );
2295
                        }                                
419 dpurdie 2296
                    }
2297
 
2298
                    push @done, values %sieve;
2299
                }
407 dpurdie 2300
            }
2301
        }
2302
    }
2303
 
415 dpurdie 2304
    Error ("ResolveLibFile: File not found: $file", "Search Path:", @path)
407 dpurdie 2305
        unless ( @done );
2306
 
2307
    Warning ("ResolveLibFile: Multiple instances of file found. Only first is used", @done)
2308
        if ( $#done > 0 && ! $wildcard && !wantarray );
2309
 
2310
    return wantarray ? @done : $done[0];
2311
}
2312
 
2313
 
2314
#-------------------------------------------------------------------------------
2315
# Function        : AUTOLOAD
2316
#
2317
# Description     : Intercept bad user directives and issue a nice error message
2318
#                   This is a simple routine to report unknown user directives
2319
#                   It does not attempt to distinguish between user errors and
2320
#                   programming errors. It assumes that the program has been
2321
#                   tested. The function simply report filename and line number
2322
#                   of the bad directive.
2323
#
2324
# Inputs          : Original function arguments ( not used )
2325
#
2326
# Returns         : This function does not return
2327
#
2328
our $AUTOLOAD;
2329
sub AUTOLOAD
2330
{
2331
    my $fname = $AUTOLOAD;
2332
    $fname =~ s~^main::~~;
2333
    my ($package, $filename, $line) = caller;
2334
 
2335
    Error ("Directive not known or not allowed in this context: $fname",
2336
           "Directive: $fname( @_ );",
2337
           "File: $filename, Line: $line" );
2338
}
2339
 
2340
 
2341
1;
2342