Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
407 dpurdie 1
########################################################################
2
# Copyright (C) 2007 ERG Limited, All rights reserved
3
#
4
# Module name   : jats.sh
5
# Module type   : Makefile system
6
# Compiler(s)   : n/a
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
35
#                       CopyDir                 - Copy directory tree
36
#                       CopyFile                - Copy a file
37
#                       CopyBinFile             - Copy an executable file
38
#                       CopyLibFile             - Copy a library file
39
#                       CreateDir               - Create a directory
40
#                       DebianFiles             - Specify control and script files
41
#                       EchoFile                - Place text into a file
42
#                       MakeSymLink             - Create a symbolic link
43
#                       PackageDescription      - Specify the package description
44
#                       SetFilePerms            - Set file permissions
45
#                       SetVerbose              - Control progress display
46
#                       IsProduct               - Flow control
47
#                       IsPlatform              - Flow control
48
#                       IsTarget                - Flow control
427 dpurdie 49
#                       IsVariant               - Flow control
407 dpurdie 50
#
51
#                 Thoughts for expansion:
52
#                       ConvertFile             - Option to convert file(s) to Unix Text
53
#                       ReplaceTags             - Replace Tags on target file
54
#                       SrcDir                  - Extend path for resolving local files
55
#
56
#                   Less used:
57
#                        ExpandLinkFiles        - Expand .LINK files
58
#
59
#                   Internal Use:
60
#                        FindFiles              - Find a file
61
#                        ResolveFile            - Resolve a 'local' source file
62
#                        
63
#......................................................................#
64
 
411 dpurdie 65
require 5.006_001;
407 dpurdie 66
use strict;
67
use warnings;
68
 
69
use Getopt::Long;
70
use File::Path;
71
use File::Copy;
72
use File::Find;
73
use JatsSystem;
74
use FileUtils;
75
use JatsError;
76
use ReadBuildConfig;
423 dpurdie 77
use JatsCopy ();                            # Don't import anything
407 dpurdie 78
 
79
#
80
#   Globals
81
#
425 dpurdie 82
my $DebianWorkDirBase;                      # Workspace
407 dpurdie 83
my $DebianWorkDir;                          # Dir to create file system image within
84
 
85
#
86
#   Command line options
87
#
88
my $opt_debug   = $ENV{'GBE_DEBUG'};        # Allow global debug
89
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
90
my $opt_vargs;                              # Verbose arg
91
my $opt_help = 0;
92
my $opt_manual = 0;
93
my $opt_clean = 0;
94
my $opt_platform;
95
my $opt_type;
96
my $opt_buildname;
97
my $opt_buildversion;
98
my $opt_interfacedir;
99
my $opt_target;
100
my $opt_product;
101
my $opt_package_script;
102
my $opt_interfaceincdir;
103
my $opt_interfacelibdir;
104
my $opt_interfacebindir;
105
my $opt_libdir;
106
my $opt_bindir;
107
my $opt_localincdir;
108
my $opt_locallibdir;
109
my $opt_localbindir;
110
my $opt_pkgdir;
111
my $opt_pkglibdir;
112
my $opt_pkgbindir;
113
my $opt_pkgpkgdir;
114
my $opt_output;
417 dpurdie 115
my $opt_name;
427 dpurdie 116
my $opt_variant;
407 dpurdie 117
 
118
#
119
#   Options derived from script directives
120
#
121
my $opt_control = '';
122
my $opt_prerm = '';
123
my $opt_postrm = '';
124
my $opt_preinst = '';
125
my $opt_postinst = '';
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
407 dpurdie 134
 
135
#-------------------------------------------------------------------------------
136
# Function        : Main Entry point
137
#
138
# Description     : This function will be called when the package is initialised
139
#                   Extract arguments from the users environment
140
#
141
#                   Done here to greatly simplify the user script
142
#                   There should be no junk in the user script - keep it simple
143
#
144
# Inputs          :
145
#
146
# Returns         : 
147
#
148
main();
149
sub main
150
{
151
    my $result = GetOptions (
152
                "verbose:s"         => \$opt_vargs,
153
                "clean"             => \$opt_clean,
154
                "Type=s"            => \$opt_type,
155
                "BuildName=s"       => \$opt_buildname,
156
                "BuildVersion=s"    => \$opt_buildversion,
157
                "Platform=s"        => \$opt_platform,
158
                "Target=s"          => \$opt_target,
159
                "Product=s"         => \$opt_product,
160
                "DebianPackage=s"   => \$opt_package_script,
161
                "InterfaceDir=s"    => \$opt_interfacedir,
162
                "InterfaceIncDir=s" => \$opt_interfaceincdir,
163
                "InterfaceLibDir=s" => \$opt_interfacelibdir,
164
                "InterfaceBinDir=s" => \$opt_interfacebindir,
165
                "LibDir=s"          => \$opt_libdir,
166
                "BinDir=s"          => \$opt_bindir,
167
                "LocalIncDir=s"     => \$opt_localincdir,
168
                "LocalLibDir=s"     => \$opt_locallibdir,
169
                "LocalBinDir=s"     => \$opt_localbindir,
170
                "PackageDir=s"      => \$opt_pkgdir,
171
                "PackageLibDir=s"   => \$opt_pkglibdir,
172
                "PackageBinDir=s"   => \$opt_pkgbindir,
173
                "PackagePkgDir=s"   => \$opt_pkgpkgdir,
174
                "Output=s"          => \$opt_output,
427 dpurdie 175
                "Variant:s"         => \$opt_variant,
417 dpurdie 176
                "Name=s"            => \$opt_name,
407 dpurdie 177
    );
178
 
179
    $opt_verbose++ unless ( $opt_vargs eq '@' );
180
 
181
    ErrorConfig( 'name'    => 'DebianUtils',
182
                 'verbose' => $opt_verbose,
183
                 'debug'   => $opt_debug );
184
 
185
    #
186
    #   Init the FileSystem Uiltity interface
187
    #
188
    InitFileUtils();
189
 
190
    #
191
    #   Ensure that we have all required options
192
    #
193
    Error ("Platform not set")                  unless ( $opt_platform );
194
    Error ("Type not set")                      unless ( $opt_type );
195
    Error ("BuildName not set")                 unless ( $opt_buildname );
196
    Error ("BuildVersion not set")              unless ( $opt_buildversion );
197
    Error ("InterfaceDir not set")              unless ( $opt_interfacedir );
198
    Error ("Target not set")                    unless ( $opt_target );
199
    Error ("Product not set")                   unless ( $opt_product );
200
    Error ("DebianPackage not set")             unless ( $opt_package_script );
201
    Error ("Ouput File not set")                unless ( $opt_output );
417 dpurdie 202
    Error ("Build Name not set")                unless ( $opt_name );
407 dpurdie 203
 
204
    #
205
    #   Clean up the build name
206
    #   Match any work done in the MakeDebianPackage directive
207
    #
208
    $opt_buildname =~ s~_~-~g;
209
 
210
    #
211
    #   Read in relevent config information
212
    #
213
    ReadBuildConfig ($opt_interfacedir, $opt_platform, '--NoTest' );
214
 
215
    #
216
    #   Build the package image in a directory based on the target being created
217
    #
425 dpurdie 218
    $DebianWorkDirBase = "$opt_platform$opt_type.image";
219
    $DebianWorkDir = "$DebianWorkDirBase/$opt_name";
407 dpurdie 220
 
221
    #
222
    #   Configure the System command to fail on any error
223
    #
224
    SystemConfig ( ExitOnError => 1 );
225
 
226
    #
227
    #   Extract the 'name' of the package from the output path
228
    #   Display purposes only
229
    #
230
    my $DebianPkgName = StripDirExt($opt_output);
231
 
232
    #
233
    #   Display variables used
234
    #
235
    Message    "=Building Debian Package =============================================";
417 dpurdie 236
    Message    "Build $opt_name";
407 dpurdie 237
    Message    "       Package: $opt_buildname";
427 dpurdie 238
    Message    "       Variant: $opt_variant" if ($opt_variant);
407 dpurdie 239
    Message    "       Version: $opt_buildversion";
240
    Message    "  Building for: $opt_platform, $opt_target";
241
    Message    "       Product: $opt_product";
242
    Message    "          Type: $opt_type";
243
    Verbose    "       Verbose: $opt_verbose";
244
    Verbose    "  InterfaceDir: $opt_interfacedir";
245
    Message    "       Package: $DebianPkgName";
246
    Message    "======================================================================";
247
 
248
    #
249
    #   Perform Clean up
250
    #   Invoked during "make clean" or "make clobber"
251
    #
252
    if ( $opt_clean )
253
    {
254
        Message ("Remove packaging directory: $DebianWorkDir");
425 dpurdie 255
 
256
        #
257
        #   Remove the directory for this package
258
        #   Remove the general work dir - if all packages have been cleaned
259
        #
407 dpurdie 260
        rmtree( $DebianWorkDir );
425 dpurdie 261
        rmdir( $DebianWorkDirBase );
407 dpurdie 262
        rmtree ($opt_output) if ( -f $opt_output );
263
        exit;
264
    }
265
 
266
    #
267
    #   Clean  out the WORK directory
268
    #   Always start with a clean slate
269
    #
270
    #   Ensure that the base of the directory tree does not have 'setgid'
271
    #       This will upset the debian packager
272
    #       This may be an artifact from the users directory and not expected
273
    #
274
    rmtree( $DebianWorkDir );
275
    mkpath( $DebianWorkDir );
276
 
277
    my $perm = (stat $DebianWorkDir)[2] & 0777;
278
    chmod ( $perm & 0777, $DebianWorkDir );
279
 
280
    #
281
    #   Invoke the user script to do the hard work
282
    #
283
    do $opt_package_script;
284
 
285
    #
286
    #   Complete the building of the package
287
    #
288
    BuildDebianPackage ();
289
    Message ("Created Debian Package");
290
}
291
 
292
#-------------------------------------------------------------------------------
293
# Function        : BuildDebianPackage
294
#
295
# Description     : This function will create the Debian Package
296
#                   and transfer it to the target directory
297
#
298
# Inputs          : None
299
#
300
# Returns         : Nothing
301
#
302
sub BuildDebianPackage
303
{
304
    Error ("BuildDebianPackage: No Control File or Package Description")
305
        unless ( $opt_control || $opt_description );
306
 
307
    #
308
    #   Convert the FileSystem Image into a Debian Package
309
    #       Insert Debian control files
310
    #
311
    Verbose ("Copy in the Debian Control Files");
312
    mkdir ( "$DebianWorkDir/DEBIAN" );
313
 
314
    CopyFile ( $opt_prerm,    "/DEBIAN", "prerm" )    if $opt_prerm;
315
    CopyFile ( $opt_postrm,   "/DEBIAN", "postrm" )   if $opt_postrm;
316
    CopyFile ( $opt_preinst,  "/DEBIAN", "preinst" )  if $opt_preinst;
317
    CopyFile ( $opt_postinst, "/DEBIAN", "postinst" ) if $opt_postinst;
318
 
319
    UpdateControlFile ($opt_control );
320
    System ( 'chmod', '-R', 'a+rx', "$DebianWorkDir/DEBIAN" );
321
    System ( 'build_dpkg.sh', '-b', $DebianWorkDir);
322
    System ( 'mv', '-f', "$DebianWorkDir.deb", $opt_output );
323
 
324
    System ("build_dpkg.sh", '-I', $opt_output) if (IsVerbose(1));
325
 
326
}
327
 
328
#-------------------------------------------------------------------------------
329
# Function        : UpdateControlFile
330
#
331
# Description     : Update the Debian 'control' file to fix up varoius fields
332
#                   within the file.
333
#
334
#                   If the files has not been specified, then a basic control
335
#                   file will be provided.
336
#
337
#                   This routine knows where the control file will be placed
338
#                   within the output work space.
339
#
340
# Inputs          : $src            - Path to source file
341
#                   Uses global variables
342
#
343
# Returns         : Nothing
344
#
345
sub UpdateControlFile
346
{
347
    my($src) = @_;
348
    my $dst = "$DebianWorkDir/DEBIAN/control";
349
 
350
    unless ( $src )
351
    {
352
        CreateControlFile();
353
        return;
354
    }
355
 
356
    Verbose ("UpdateControlFile: $dst" );
357
    $src = ResolveFile( 0, $src );
358
 
359
    open (SF, '<', $src) || Error ("UpdateControlFile: Cannot open $src");
360
    open (DF, '>', $dst) || Error ("UpdateControlFile: Cannot create:$dst");
361
    while ( <SF> )
362
    {
363
        s~\s*$~~;
364
        if ( m~^Package:~ ) {
365
            $_ = "Package: $opt_buildname";
366
 
367
        } elsif ( m~^Version:~ ) {
368
            $_ = "Version: $opt_buildversion";
369
 
370
        } elsif ( m~^Architecture:~ ) {
371
            $_ = "Architecture: $opt_platform";
372
 
373
        } elsif ( $opt_description && m~^Description:~ ) {
374
            $_ = "Description: $opt_description";
375
        }
376
        print DF $_ , "\n";
377
    }
378
    close (SF);
379
    close (DF);
380
}
381
 
382
#-------------------------------------------------------------------------------
383
# Function        : CreateControlFile
384
#
385
# Description     : Craete a basic debian control file
386
#
387
# Inputs          : Uses global variables
388
#
389
# Returns         : 
390
#
391
sub CreateControlFile
392
{
393
    my $dst = "$DebianWorkDir/DEBIAN/control";
394
 
395
    Verbose ("CreateControlFile: $dst" );
396
 
397
    open (DF, '>', $dst) || Error ("CreateControlFile: Cannot create:$dst");
398
    print DF "Package: $opt_buildname\n";
399
    print DF "Version: $opt_buildversion\n";
400
    print DF "Section: main\n";
401
    print DF "Priority: standard\n";
402
    print DF "Architecture: $opt_platform\n";
403
    print DF "Essential: yes\n";
404
    print DF "Maintainer: ERG\n";
405
    print DF "Description: $opt_description\n";
406
    close (DF);
407
}
408
 
409
#-------------------------------------------------------------------------------
410
# Function        : SetVerbose
411
#
412
# Description     : Set the level of verbosity
413
#                   Display activity
414
#
415
# Inputs          : Verbosity level
416
#                       0 - Use makefile verbosity (Default)
417
#                       1..2
418
#
419
# Returns         : 
420
#
421
sub SetVerbose
422
{
423
    my ($level) = @_;
424
 
425
    $level = $opt_verbose unless ( $level );
426
    $opt_verbose = $level;
427
    ErrorConfig( 'verbose' => $level);
428
}
429
 
430
 
431
#-------------------------------------------------------------------------------
432
# Function        : DebianFiles
433
#
434
# Description     : Name Debian builder control files
435
#                   May be called multiple times
436
#
437
# Inputs          : Options
438
#                       --Control=file
439
#                       --PreRm=file
440
#                       --PostRm=file
441
#                       --PreInst=file
442
#                       --PostInst=file
443
#
444
# Returns         : Nothing
445
#
446
sub DebianFiles
447
{
448
    #
449
    #   Exctact names
450
    #
451
    Verbose ("Specify Debian Control Files and Scripts");
452
    foreach  ( @_ )
453
    {
454
        if ( m/^--Control=(.+)/ ) {
455
            $opt_control = $1;
456
 
457
        } elsif ( m/^--PreRm=(.+)/ ) {
458
            $opt_prerm = $1;
459
 
460
        } elsif ( m/^--PostRm=(.+)/ ) {
461
            $opt_postrm = $1;
462
 
463
        } elsif ( m/^--PreInst=(.+)/ ) {
464
            $opt_preinst  = $1;
465
 
466
        } elsif ( m/^--PostInst=(.+)/ ) {
467
            $opt_postinst = $1;
468
 
469
        } else {
470
            Error ("DebianFiles: Unknown option: $_");
471
        }
472
    }
473
}
474
 
475
#-------------------------------------------------------------------------------
476
# Function        : PackageDescription
477
#
478
# Description     : Specify the Package Description
479
#                   Keep it short
480
#
481
# Inputs          : $description
482
#
483
# Returns         : 
484
#
485
sub PackageDescription
486
{
487
    ($opt_description) = @_;
488
}
489
 
490
#-------------------------------------------------------------------------------
491
# Function        : MakeSymLink
492
#
493
# Description     : Create a symlink - with error detection
494
#
495
# Inputs          : old_file    - Link Target
496
#                                 Path to the link target
497
#                                 If an ABS path is provided, the routine will
498
#                                 attempt to create a relative link.
499
#                   new_file    - Relative to the output work space
500
#                                 Path to where the 'link' file will be created
501
#                   Options     - Must be last
502
#                                 --NoClean         - Don't play with links
503
#                                 --NoDotDot        - Don't create symlinks with ..
504
#
505
# Returns         : Nothing
506
#
507
sub MakeSymLink
508
{
509
    my $no_clean;
510
    my $no_dot;
511
    my @args;
512
 
513
    #
514
    #   Extract options
515
    #
516
    foreach ( @_ )
517
    {
518
        if ( m/^--NoClean/i ) {
519
            $no_clean = 1;
520
 
521
        } elsif ( m/^--NoDotDot/i ) {
522
            $no_dot = 1;
523
 
524
        } elsif ( m/^--/ ) {
525
            Error ("MakeSymLink: Unknown option: $_");
526
 
527
        } else {
528
            push @args, $_;
529
        }
530
    }
531
 
532
    my ($old_file, $new_file) = @args;
533
 
534
    my $tfile = $DebianWorkDir . '/' . $new_file;
535
    $tfile =~ s~//~/~;
536
    Verbose ("Symlink $old_file -> $new_file" );
537
 
538
    #
539
    #   Create the directory in which the link will be placed
540
    #   Remove any existing file of the same name
541
    #
542
    my $dir = StripFileExt( $tfile );
543
    mkpath( $dir) unless -d $dir;
544
    unlink $tfile;
545
 
546
    #
547
    #   Determine a good name of the link
548
    #   Convert to a relative link in an attempt to prune them
549
    #
550
    my $sfile = $old_file;
551
    unless ( $no_clean )
552
    {
553
        $sfile = CalcRelPath( StripFileExt( $new_file ), $old_file );
554
        $sfile = $old_file if ( $no_dot && $sfile =~ m~^../~ );
555
    }
556
 
557
    my $result = symlink $sfile, $tfile;
558
    Error ("Cannot create symlink. $old_file -> $new_file") unless ( $result );
559
}
560
 
561
#-------------------------------------------------------------------------------
562
# Function        : CopyFile
563
#
564
# Description     : Copy a file to a target dir
565
#                   Used for text files, or files with fixed names
566
#
567
# Inputs          : $src
568
#                   $dst_dir    - Within the output workspace
569
#                   $dst_name   - Output Name [Optional]
570
#                   Options     - Common Copy Options
571
#
572
# Returns         : Full path to destination file
573
#
574
sub CopyFile
575
{
576
    CopyFileCommon( \&ResolveFile, @_ );
577
}
578
 
579
#-------------------------------------------------------------------------------
580
# Function        : CopyBinFile
581
#
582
# Description     : Copy a file to a target dir
583
#                   Used for executable programs. Will look in places where
584
#                   programs are stored.
585
#
586
# Inputs          : $src
587
#                   $dst_dir    - Within the output workspace
588
#                   $dst_name   - Output Name [Optional]
589
#
590
#                   Options:
591
#                       --FromPackage
592
#                       --SoftLink=xxxx
593
#                       --LinkFile=xxxx
594
#
595
#
596
# Returns         : Full path to destination file
597
#
598
sub CopyBinFile
599
{
600
    CopyFileCommon( \&ResolveBinFile, @_ );
601
}
602
 
603
#-------------------------------------------------------------------------------
604
# Function        : CopyLibFile
605
#
606
# Description     : Copy a file to a target dir
607
#                   Used for shared programs. Will look in places where
608
#                   shared libraries are stored.
609
#
610
# Inputs          : $src        - Base for 'realname' (no lib, no extension)
611
#                   $dst_dir    - Within the output workspace
612
#                   $dst_name   - Output Name [Optional, but not suggested]
613
#
614
# Returns         : Full path to destination file
615
#
616
# Notes           : Copying 'lib' files
617
#                   These are 'shared libaries. There is no provision for copying
618
#                   static libraries.
619
#
620
#                   The tool will attempt to copy a well-formed 'realname' library
621
#                   The soname of the library should be constructed on the target
622
#                   platform using ldconfig.
623
#                   There is no provision to copy the 'linker' name
624
#
625
#                   Given a request to copy a library called 'fred', then the
626
#                   well formed 'realname' will be:
627
#                           libfred[P|D|]].so.nnnnn
628
#                   where:
629
#                           nnnn is the library version
630
#                           [P|D|] indicates Production, Debug or None
631
#
632
#                   The 'soname' is held within the realname form of the library
633
#                   and will be created by lsconfig.
634
#
635
#                   The 'linkername' would be libfred[P|D|].so. This is only
636
#                   needed when linking against the library.
637
#
638
#
639
#                   The routine will also recognize Windows DLLs
640
#                   These are of the form fred[P|D|].nnnnn.dll
641
#
642
sub CopyLibFile
643
{
644
    CopyFileCommon( \&ResolveLibFile, @_ );
645
}
646
 
647
#-------------------------------------------------------------------------------
648
# Function        : CopyFileCommon
649
#
650
# Description     : Common ( internal File Copy )
651
#
652
# Inputs          : $resolver           - Ref to function to resolve source file
653
#                   $src                - Source File Name
654
#                   $dst_dir            - Target Dir
655
#                   $dst_name           - Target Name (optional)
656
#                   Options
657
#                   Options:
658
#                       --FromPackage
659
#                       --SoftLink=xxxx
660
#                       --LinkFile=xxxx
661
#
662
# Returns         : 
663
#
664
sub CopyFileCommon
665
{
666
    my $from_package = 0;
667
    my $isa_linkfile = 0;
668
    my @llist;
669
    my @args;
670
 
671
    #
672
    #   Parse options
673
    #
674
    foreach ( @_ )
675
    {
676
        if ( m/^--FromPackage/ ) {
677
            $from_package = 1;
678
 
679
        } elsif ( m/^--LinkFile/ ) {
680
            $isa_linkfile = 1;
681
 
682
        } elsif ( m/^--SoftLink=(.+)/ ) {
683
            push @llist, $1;
684
 
685
        } elsif ( m/^--/ ) {
686
            Error ("FileCopy: Unknown option: $_");
687
 
688
        } else {
689
            push @args, $_;
690
        }
691
    }
692
 
693
    #
694
    #   Extract non-options.
695
    #   These are the bits that are left over
696
    #
697
    my ($resolver, $src, $dst_dir, $dst_name ) = @args;
698
 
699
    #
700
    #   Clean up dest_dir. Must start with a / and not end with one
701
    #
702
    $dst_dir = "/$dst_dir/";
703
    $dst_dir =~ s~/+~/~g;
704
    $dst_dir =~ s~/$~~;
705
 
706
    Verbose ("CopyFile: $src, $dst_dir, " . ($dst_name || ''));
707
    foreach $src ( &$resolver( $from_package, $src ) )
708
    {
709
        my $dst_fname = $dst_name ? $dst_name : StripDir($src);
710
        my $dst_file = "$dst_dir/$dst_fname";
711
        Verbose ("CopyFile: Copy $src, $dst_file" );
712
 
713
 
714
        #
715
        #   LinkFiles are special
716
        #   They get concatenated to any existing LINKS File
717
        #
718
        if ( $isa_linkfile )
719
        {
720
            CatFile ( $src, "$dst_dir/.LINKS" );
721
        }
722
        else
723
        {
724
            mkpath( "$DebianWorkDir$dst_dir", 0, 0775);
725
            unlink ("$DebianWorkDir$dst_file");
726
            System ('cp','-f', $src, "$DebianWorkDir$dst_file" );
727
 
728
            foreach my $lname ( @llist )
729
            {
730
                $lname = $dst_dir . '/' . $lname unless ( $lname =~ m ~^/~ );
731
                MakeSymLink( $dst_file ,$lname);
732
            }
733
        }
734
    }
735
}
736
 
737
#-------------------------------------------------------------------------------
738
# Function        : CopyDir
739
#
740
# Description     : Copy a directory to a target dir
741
#
742
# Inputs          : $src_dir    - Local to the user
743
#                                 Symbolic Name
744
#                   $dst_dir    - Within the output workspace
745
#                   Options
423 dpurdie 746
#                       --Merge             - Don't delete first
747
#                       --Source=Name       - Source via Symbolic Name
425 dpurdie 748
#                       --FromPackage       - Souve via package roots
407 dpurdie 749
#
750
# Returns         :
751
#
752
sub CopyDir
753
{
754
    my ($src_dir, $dst_dir, @opts) = @_;
755
    my $opt_merge;
756
    my $opt_base;
411 dpurdie 757
    my $from_interface = 0;
407 dpurdie 758
 
759
    $dst_dir = $DebianWorkDir . '/' . $dst_dir;
760
    $dst_dir =~ s~//~/~;
761
 
762
    #
763
    #   Detect and expand Symbolic names in the Source Directory
764
    #
765
    foreach  ( @opts )
766
    {
767
        if ( m/^--Merge/ ) {
768
            $opt_merge = 1;
769
        } elsif ( m/^--Source=(.+)/ ) {
425 dpurdie 770
            my $name = $1;
771
            Verbose2 ("CopyDir: Source: $name");
772
            Error ("Source directory can only be specified once")
773
                if ( defined $opt_base );
774
 
775
            $name = lc($name);
407 dpurdie 776
            my %CopyDirSymbolic = (
777
                'interfaceincdir'   => $opt_interfaceincdir,
778
                'interfacelibdir'   => $opt_interfacelibdir,
779
                'interfacebindir'   => $opt_interfacebindir,
780
                'libdir'            => $opt_libdir,
781
                'bindir'            => $opt_bindir,
782
                'localincdir'       => $opt_localincdir,
783
                'locallibdir'       => $opt_locallibdir,
784
                'localbindir'       => $opt_localbindir,
785
                'packagebindir'     => $opt_pkgbindir,
786
                'packagelibdir'     => $opt_pkglibdir,
787
                'packagepkgdir'     => $opt_pkgpkgdir,
788
                'packagedir'        => $opt_pkgdir,
789
            );
790
 
791
            if ( exists $CopyDirSymbolic{$name} )
792
            {
793
                $opt_base = $CopyDirSymbolic{$name};
411 dpurdie 794
 
795
                #
796
                #   If sourceing from interface, then follow
797
                #   symlinks in the copy. All files will be links anyway
798
                #
799
                $from_interface = 1
800
                    if ( $name =~ m~^interface~ );
407 dpurdie 801
            }
802
            else
803
            {
804
                DebugDumpData ("CopyDirSymbolic", \%CopyDirSymbolic);
805
                Error ("CopyDir: Unknown Source Name: $name" );
806
            }
425 dpurdie 807
 
808
        } elsif ( m/^--FromPackage/ ) {
809
            Verbose2 ("CopyDir: FromPackage: $src_dir");
810
            Error ("Source directory can only be specified once")
811
                if ( defined $opt_base );
812
 
813
            my @path;
814
            foreach my $entry ( getPackageList() )
815
            {
816
                my $base = $entry->getBase(3);
817
                next unless ( defined $base );
818
                if ( -d $base . '/' . $src_dir )
819
                {
820
                    push @path, $base;
821
                }
822
            }
823
 
824
            Error ("CopyDir: Cannot find source dir in any package: $src_dir")
825
                if ( $#path < 0 );
826
            Error ("CopyDir: Requested path found in mutiple packages: $src_dir",
827
                    @path ) if ( $#path > 0 );
828
            $opt_base = pop @path;
829
 
830
            #
831
            #   If sourceing from interface, then follow symlinks in the copy.
832
            #   All files will be links anyway
833
            #
834
            #   This is a very ugly test for 'interface'
835
            #
836
            $from_interface = 1
837
                if ( $opt_base =~ m~/interface/~ );
838
 
407 dpurdie 839
        } else {
840
            Error ("CopyDir: Unknown option: $_" );
841
        }
842
    }
843
 
844
    $src_dir = $opt_base . '/' . $src_dir if ( $opt_base );
845
    $src_dir =~ s~//~/~g;
846
    $src_dir =~ s~/$~~;
847
 
848
    Verbose ("CopyDir: $src_dir, $dst_dir");
849
    Error ("CopyDir: Directory not found: $src_dir") unless ( -d $src_dir );
850
 
851
    #
423 dpurdie 852
    #   Setup the copy options
407 dpurdie 853
    #
423 dpurdie 854
    my %copyOpts;
855
    $copyOpts{'IgnoreDirs'} = ['.svn'];
856
    $copyOpts{'EmptyDirs'} = 1;
857
    $copyOpts{'DeleteFirst'} = 1 unless $opt_merge;
858
    $copyOpts{'Log'} = 1 if ( $opt_verbose > 1 );
859
    $copyOpts{'DuplicateLinks'} = 1 unless ( $from_interface );
407 dpurdie 860
 
861
    #
423 dpurdie 862
    #   Transfer the directory
407 dpurdie 863
    #
423 dpurdie 864
    JatsCopy::CopyDir ( $src_dir, $dst_dir, \%copyOpts );
407 dpurdie 865
 
866
    #
867
    #   Expand link files that may have been copied in
868
    #
869
    Verbose ("Locate LINKFILES in $DebianWorkDir");
870
    ExpandLinkFiles();
871
}
872
 
873
#-------------------------------------------------------------------------------
874
# Function        : AddInitScript
875
#
876
# Description     : Add an Init Script to the target
877
#                   Optionally create start and stop links
878
#
879
# Inputs          : $script     - Name of the init script
880
#                   $start      - Start Number
881
#                   $stop       - Stop Number
882
#                   Options:
883
#                       --NoCopy        - Don't copy the script, just add links
884
#                       --Afc           - Place in AFC init area
885
#                       --FromPackage   - Source is in a package
886
#
887
# Returns         : 
888
#
889
sub AddInitScript
890
{
891
    my $no_copy;
892
    my $basedir = "";
893
    my @args;
894
    my $from_package = 0;
895
 
896
    #
897
    #   Process and Remove options
898
    #
899
    foreach  ( @_ )
900
    {
901
        if ( m/^--NoCopy/ ) {
902
            $no_copy = 1;
903
 
904
        } elsif ( m/^--Afc/ ) {
905
            $basedir = "/afc";
906
 
907
        } elsif ( m/^--FromPackage/ ) {
908
            $from_package = 1;
909
 
910
        } elsif ( m/^--/ ) {
911
            Error ("AddInitScript: Unknown option: $_");
912
 
913
        } else {
914
            push @args, $_;
915
 
916
        }
917
    }
918
 
919
    my( $script, $start, $stop ) = @args;
920
    Error ("No script file specified") unless ( $script );
921
    Warning("AddInitScript: No start or stop index specified") unless ( $start || $stop );
922
    Verbose ("AddInitScript: $script, " . ($start || 'No Start') . ", " . ($stop || 'No Stop'));
923
    $script = ResolveFile($from_package, $script );
924
 
925
    my $tdir = $basedir . "/etc/init.d/init.d";
926
    my $base = StripDir($script);
927
 
928
    CopyFile( $script, $tdir ) unless $no_copy;
929
 
930
    my $link;
931
    if ( $start )
932
    {
933
        $link = sprintf ("${basedir}/etc/init.d/S%2.2d%s", $start, $base );
934
        MakeSymLink( "$tdir/$base", $link);
935
    }
936
 
937
    if ( $stop )
938
    {
939
        $link = sprintf ("${basedir}/etc/init.d/K%2.2d%s", $stop, $base );
940
        MakeSymLink( "$tdir/$base", $link);
941
    }
942
}
943
 
944
#-------------------------------------------------------------------------------
945
# Function        : CatFile
946
#
947
# Description     : Copy a file to the end of a file
948
#
949
# Inputs          : $src
950
#                   $dst    - Within the output workspace
951
#
952
# Returns         :
953
#
954
sub CatFile
955
{
956
    my ($src, $dst) = @_;
957
 
958
    $dst = $DebianWorkDir . '/' . $dst;
959
    $dst =~ s~//~/~;
960
    Verbose ("CatFile: $src, $dst");
961
    $src = ResolveFile(0, $src );
962
 
963
    open (SF, '<', $src)  || Error ("CatFile: Cannot open $src");
964
    open (DF, '>>', $dst) || Error ("CatFile: Cannot create:$dst");
965
    while ( <SF> )
966
    {
967
        print DF $_;
968
    }
969
    close (SF);
970
    close (DF);
971
}
972
 
973
#-------------------------------------------------------------------------------
974
# Function        : EchoFile
975
#
976
# Description     : Echo simple text to a file
977
#
978
# Inputs          : $file   - Within the output workspace
979
#                   $text
980
#
981
# Returns         : 
982
#
983
sub EchoFile
984
{
985
    my ($file, $text) = @_;
986
    Verbose ("EchoFile: $file");
987
 
988
    $file = $DebianWorkDir . '/' . $file;
989
    $file =~ s~//~/~;
990
 
991
    unlink $file;
992
    open (DT, ">", $file ) || Error ("Cannot create $file");
993
    print DT  $text || Error ("Cannot print to $file");
994
    close DT;
995
}
996
 
997
#-------------------------------------------------------------------------------
998
# Function        : SetFilePerms
999
#
1000
# Description     : Set file permissions on one or more files or directories
1001
#
1002
# Inputs          : $perm           - Perm Mask
1003
#                   @paths          - List of paths/files to process
1004
#                   Options
1005
#                       --Recurse   - Recurse subdirs
1006
#
1007
# Returns         : 
1008
#
1009
sub SetFilePerms
1010
{
1011
 
1012
    my @args;
1013
    my $perms;
1014
    my $recurse = 0;
1015
 
1016
    #
1017
    #   Process and Remove options
1018
    #
1019
    foreach  ( @_ )
1020
    {
1021
        if ( m/^--Recurse/ ) {
1022
            $recurse = 1;
1023
 
1024
        } elsif ( m/^--/ ) {
1025
            Error ("SetFilePerms: Unknown option: $_");
1026
 
1027
        } else {
1028
            push @args, $_;
1029
 
1030
        }
1031
    }
1032
 
1033
    $perms = shift @args;
1034
    Error ("SetFilePerms: No Permissions" ) unless ( $perms );
1035
 
1036
    foreach my $path ( @args )
1037
    {
1038
        Verbose ("Set permissions; $perms, $path");
1039
        my $full_path = $DebianWorkDir . '/' . $path;
1040
        if ( -f $full_path )
1041
        {
1042
            System ('chmod', $perms, $full_path );
1043
        }
1044
        elsif ( -d $full_path )
1045
        {
1046
            System ('chmod', '-R', $perms, $full_path ) if ($recurse);
1047
            System ('chmod', $perms, $full_path ) unless ($recurse);
1048
        }
1049
        else
1050
        {
1051
            Warning("SetFilePerms: Path not found: $path");
1052
        }
1053
    }
1054
}
1055
 
1056
#-------------------------------------------------------------------------------
1057
# Function        : CreateDir
1058
#
1059
# Description     : Create a directory within the target workspace
1060
#
1061
# Inputs          : $path           - Name of the target directory
1062
#
1063
# Returns         : Nothing
1064
#
1065
sub CreateDir
1066
{
1067
    my ($path) = @_;
1068
 
1069
    Verbose ("Create Dir: $path");
1070
    mkpath( $DebianWorkDir . '/' . $path );
1071
}
1072
 
1073
#-------------------------------------------------------------------------------
1074
# Function        : IsProduct
1075
#                   IsPlatform
1076
#                   IsTarget
427 dpurdie 1077
#                   IsVariant
407 dpurdie 1078
#
1079
# Description     : This function allows some level of control in the
1080
#                   packaging scripts. It will return true if the current
1081
#                   product is listed.
1082
#
1083
#                   Ugly after thought
1084
#
1085
#                   Intended use:
1086
#                       Xxxxxx(...) if (IsProduct( 'aaa',bbb' );
1087
#
1088
# Inputs          : products    - a list of products to compare against
1089
#
1090
# Returns         : True if the current build is for one of the listed products
1091
#
1092
sub IsProduct
1093
{
1094
    foreach ( @_ )
1095
    {
1096
        return 1 if ( $opt_product eq $_ );
1097
    }
1098
    return 0;
1099
}
1100
 
1101
sub IsPlatform
1102
{
1103
    foreach ( @_ )
1104
    {
1105
        return 1 if ( $opt_platform eq $_ );
1106
    }
1107
    return 0;
1108
}
1109
 
1110
sub IsTarget
1111
{
1112
    foreach ( @_ )
1113
    {
1114
        return 1 if ( $opt_target eq $_ );
1115
    }
1116
    return 0;
1117
}
1118
 
427 dpurdie 1119
sub IsVariant
1120
{
1121
    foreach ( @_ )
1122
    {
1123
        return 1 if ( $opt_variant eq $_ );
1124
    }
1125
    return 0;
1126
}
407 dpurdie 1127
 
1128
#-------------------------------------------------------------------------------
1129
# Function        : FindFiles
1130
#
1131
# Description     : Locate files within a given dir tree
1132
#
1133
# Inputs          : $root           - Base of the search
1134
#                   $match          - Re to match
1135
#
1136
# Returns         : A list of files that match
1137
#
1138
my @FIND_LIST;
1139
my $FIND_NAME;
1140
 
1141
sub FindFiles
1142
{
1143
    my ($root, $match ) = @_;
1144
    Verbose2("FindFiles: Root: $root, Match: $match");
1145
 
1146
    #
1147
    #   Becareful of closure, Must use globals
1148
    #
1149
    @FIND_LIST = ();
1150
    $FIND_NAME = $match;
1151
    File::Find::find( \&find_files, $root);
1152
 
1153
    #
1154
    #   Find callback program
1155
    #
1156
    sub find_files
1157
    {
1158
        my $item =  $File::Find::name;
1159
 
1160
        return if ( -d $File::Find::name );
1161
        return unless ( $_ =~ m~$FIND_NAME~ );
1162
        push @FIND_LIST, $item;
1163
    }
1164
    return @FIND_LIST;
1165
}
1166
 
1167
#-------------------------------------------------------------------------------
1168
# Function        : CalcRelPath
1169
#
1170
# Description     : Return the relative path to the current working directory
1171
#                   as provided in $Cwd
1172
#
1173
# Inputs          : $Cwd - Base dir
1174
#                   $base - Path to convert
1175
#
1176
# Returns         : Relative path from the $Cwd
1177
#
1178
sub CalcRelPath
1179
{
1180
    my ($Cwd, $base) = @_;
1181
 
1182
    my @base = split ('/', $base );
1183
    my @here = split ('/', $Cwd );
1184
    my $result;
1185
 
1186
    Debug("RelPath: Source: $base");
1187
 
1188
    return $base unless ( $base =~ m~^/~ );
1189
 
1190
    #
1191
    #   Remove common bits from the head of both lists
1192
    #
1193
    while ( $#base >= 0 && $#here >= 0 && $base[0] eq $here[0] )
1194
    {
1195
        shift @base;
1196
        shift @here;
1197
    }
1198
 
1199
    #
1200
    #   Need to go up some directories from here and then down into base
1201
    #
1202
    $result = '../' x ($#here + 1);
1203
    $result .= join ( '/', @base);
1204
    $result = '.' unless ( $result );
1205
    $result =~ s~//~/~g;
1206
    $result =~ s~/$~~;
1207
 
1208
    Debug("RelPath: Result: $result");
1209
    return $result;
1210
}
1211
 
1212
#-------------------------------------------------------------------------------
1213
# Function        : ExpandLinkFiles
1214
#
1215
# Description     : Look for .LINK files in the output image and expand
1216
#                   the links into softlinks
1217
#
1218
# Inputs          : None
1219
#                   The rouine works on the $DebianWorkDir directory tree
1220
#
1221
# Returns         : Nothing
1222
#                   Will remove .LINKS files that are processed
1223
#
1224
sub ExpandLinkFiles
1225
{
1226
    foreach my $linkfile ( FindFiles( $DebianWorkDir, ".LINKS" ))
1227
    {
1228
        next if ( $linkfile =~ m~/\.svn/~ );
1229
        my $BASEDIR = StripFileExt( $linkfile );
1230
        $BASEDIR =~ s~^$DebianWorkDir/~~;
1231
        Verbose "Expand links: $BASEDIR";
1232
 
1233
        open (LF, "<", $linkfile ) || Error ("Cannot open link file: $linkfile" );
1234
        while ( <LF> )
1235
        {
1236
            chomp;
1237
            next if ( m~^#~ );
1238
            next unless ( $_ );
1239
            my ($link, $file) = split;
1240
 
1241
            MakeSymLink($file ,"$BASEDIR/$link", '--NoDotDot' );
1242
        }
1243
        close (LF);
1244
        unlink $linkfile;
1245
    }
1246
}
1247
 
1248
#-------------------------------------------------------------------------------
1249
# Function        : ResolveFile
1250
#
1251
# Description     : Determine where the source for a file is
415 dpurdie 1252
#                   Will look in (default):
407 dpurdie 1253
#                       Local directory
1254
#                       Local Include
415 dpurdie 1255
#                   Or  (FromPackage)
1256
#                       Our Package directory
1257
#                       Interface directory (BuildPkgArchives)
1258
#                       Packages (LinkPkgArchive)
1259
#
407 dpurdie 1260
#                   Will scan 'parts' subdirs
1261
#
1262
# Inputs          : $from_package       - 0 - Local File
1263
#                   $file
1264
#
1265
# Returns         : Path
1266
#
1267
sub ResolveFile
1268
{
1269
    my ($from_package, $file) = @_;
1270
    my $wildcard = ($file =~ /[*?]/);
415 dpurdie 1271
    my @path;
407 dpurdie 1272
 
1273
    #
415 dpurdie 1274
    #   Determine the paths to search
1275
    #
1276
    if ( $from_package )
1277
    {
1278
        unless ( @ResolveFileList )
1279
        {
1280
            push @ResolveFileList, $opt_pkgdir;
1281
            foreach my $entry ( getPackageList() )
1282
            {
1283
                push @ResolveFileList, $entry->getBase(3);
1284
            }
1285
        }
1286
        @path = @ResolveFileList;
1287
    }
1288
    else
1289
    {
1290
        @path = ('.', $opt_localincdir);
1291
    }
1292
 
1293
    #
407 dpurdie 1294
    #   Determine a full list of 'parts' to search
1295
    #   This is provided within the build information
1296
    #
1297
    my @parts = getPlatformParts ();
1298
    push @parts, '';
1299
 
1300
    my @done;
1301
    foreach my $root (  @path )
1302
    {
1303
        foreach my $subdir ( @parts )
1304
        {
1305
            my $sfile;
415 dpurdie 1306
            $sfile = "$root/$subdir/$file";
1307
            $sfile =~ s~//~/~g;
1308
            $sfile =~ s~^./~~g;
1309
            Verbose2("LocateFile: $sfile, $root, $subdir");
1310
            if ( $wildcard )
1311
            {
1312
                push @done, glob ( $sfile );
1313
            }
1314
            else
1315
            {
1316
                push @done, $sfile if ( -f $sfile || -l $sfile )
1317
            }
407 dpurdie 1318
        }
1319
    }
1320
 
415 dpurdie 1321
    Error ("ResolveFile: File not found: $file", "Search Path:", @path)
407 dpurdie 1322
        unless ( @done );
1323
 
1324
    Warning ("ResolveFile: Multiple instances of file found. Only first is used", @done)
1325
        if ( $#done > 0 && ! $wildcard && !wantarray );
1326
 
1327
    return wantarray ? @done : $done[0];
1328
}
1329
 
1330
#-------------------------------------------------------------------------------
1331
# Function        : ResolveBinFile
1332
#
415 dpurdie 1333
# Description     : Determine where the source for a BIN file is
1334
#                   Will look in (default):
1335
#                       Local directory
1336
#                       Local Include
1337
#                   Or  (FromPackage)
1338
#                       Our Package directory
1339
#                       Interface directory (BuildPkgArchives)
1340
#                       Packages (LinkPkgArchive)
407 dpurdie 1341
#                   Will scan 'parts' subdirs
1342
#
1343
# Inputs          : $from_package       - 0 - Local File
415 dpurdie 1344
#                   $file
407 dpurdie 1345
#
1346
# Returns         : Path
1347
#
1348
sub ResolveBinFile
1349
{
1350
    my ($from_package, $file) = @_;
1351
    my @path;
1352
    my @types;
1353
    my $wildcard = ($file =~ /[*?]/);
1354
 
415 dpurdie 1355
    #
1356
    #   Determine the paths to search
1357
    #
407 dpurdie 1358
    if ( $from_package )
1359
    {
415 dpurdie 1360
        unless ( @ResolveBinFileList )
1361
        {
1362
            push @ResolveBinFileList, $opt_pkgdir . '/bin';
1363
            foreach my $entry ( getPackageList() )
1364
            {
1365
                if ( my $path = $entry->getBase(3) )
1366
                {
1367
                    $path .= '/bin';
1368
                    push @ResolveBinFileList, $path if ( -d $path );
1369
                }
1370
            }
1371
        }
1372
        @path = @ResolveBinFileList;
407 dpurdie 1373
        @types = ($opt_type, '');
1374
    }
1375
    else
1376
    {
1377
        @path = ($opt_bindir, $opt_localbindir);
1378
        @types = '';
1379
    }
1380
 
1381
    #
1382
    #   Determine a full list of 'parts' to search
1383
    #   This is provided within the build information
1384
    #
1385
    my @parts = getPlatformParts ();
1386
    push @parts, '';
1387
 
1388
    my @done;
1389
    foreach my $root (  @path )
1390
    {
1391
        foreach my $subdir ( @parts )
1392
        {
1393
            foreach my $type ( @types )
1394
            {
1395
                my $sfile;
1396
                $sfile = "$root/$subdir$type/$file";
1397
                $sfile =~ s~//~/~g;
1398
                Verbose2("LocateBinFile: $sfile");
1399
                if ( $wildcard )
1400
                {
429 dpurdie 1401
                    foreach  ( glob ( $sfile ) )
1402
                    {
1403
                        next if ( m~\.dbg$~ );
1404
                        push @done, $_;
1405
                    }
407 dpurdie 1406
                }
1407
                else
1408
                {
415 dpurdie 1409
                    push @done, $sfile if ( -f $sfile || -l $sfile )
407 dpurdie 1410
                }
1411
            }
1412
        }
1413
    }
1414
 
415 dpurdie 1415
    Error ("ResolveBinFile: File not found: $file", "Search Path:", @path)
407 dpurdie 1416
        unless ( @done );
1417
 
1418
    Warning ("ResolveBinFile: Multiple instances of file found. Only first is used", @done)
1419
        if ( $#done > 0 && ! $wildcard && !wantarray );
1420
    return wantarray ? @done : $done[0];
1421
}
1422
 
1423
#-------------------------------------------------------------------------------
1424
# Function        : ResolveLibFile
1425
#
415 dpurdie 1426
# Description     : Determine where the source for a LIB file is
1427
#                   Will look in (default):
1428
#                       Local directory
1429
#                       Local Include
1430
#                   Or  (FromPackage)
1431
#                       Our Package directory
1432
#                       Interface directory (BuildPkgArchives)
1433
#                       Packages (LinkPkgArchive)
407 dpurdie 1434
#                   Will scan 'parts' subdirs
1435
#
1436
# Inputs          : $from_package       - 0 - Local File
415 dpurdie 1437
#                   $file       - Basename for a 'realname'
407 dpurdie 1438
#                                 Do not provide 'lib' or '.so' or version info
1439
#                                 May contain embedded options
1440
#                                   --Dll - use Windows style versioned DLL
1441
#                                   --VersionDll - USe the versioned DLL
1442
#
1443
# Returns         : Path
1444
#
1445
sub ResolveLibFile
1446
{
1447
    my ($from_package, $file) = @_;
1448
    my $wildcard = ($file =~ /[*?]/);
1449
    my @options;
1450
    my $num_dll;
415 dpurdie 1451
    my @path;
407 dpurdie 1452
    #
1453
    #   Extract options from file
1454
    #
409 alewis 1455
    $num_dll = 0;
407 dpurdie 1456
    ($file, @options) = split ( ',', $file);
1457
    foreach ( @options )
1458
    {
1459
        if ( m/^--Dll/ ) {
1460
            $num_dll = 1;
1461
        } elsif ( m/^--VersionDll/ ) {
1462
            $num_dll = 2;
1463
        } else {
1464
            Error ("Unknown suboption to ResolveLibFile: $_" );
1465
        }
1466
    }
1467
 
1468
    #
415 dpurdie 1469
    #   Determine the paths to search
1470
    #
1471
    if ( $from_package )
1472
    {
1473
        unless ( @ResolveLibFileList )
1474
        {
1475
            push @ResolveLibFileList, $opt_pkgdir . '/lib';
1476
            foreach my $entry ( getPackageList() )
1477
            {
1478
                push @ResolveLibFileList, $entry->getLibDirs(3);
1479
            }
1480
        }
1481
        @path = @ResolveLibFileList;
1482
    }
1483
    else
1484
    {
1485
        @path = ($opt_libdir, $opt_locallibdir);
1486
    }
1487
 
1488
    #
407 dpurdie 1489
    #   Determine a full list of 'parts' to search
1490
    #   This is provided within the build information
1491
    #
1492
    my @parts = getPlatformParts ();
1493
    push @parts, '';
1494
 
1495
    my @done;
1496
    foreach my $root (  @path )
1497
    {
1498
        foreach my $type ( $opt_type, '' )
1499
        {
1500
            foreach my $subdir ( @parts )
1501
            {
1502
                my $sfile;
1503
                my $exact;
1504
                if ( $num_dll == 2 ) {
1505
                    $sfile = $file . $type . '.*.dll' ;
1506
                } elsif ( $num_dll == 1 ) {
1507
                    $sfile = $file . $type . '.dll' ;
1508
                    $exact = 1;
1509
                } else {
1510
                    $sfile = "lib" . $file . $type . '.so.*';
1511
                }
1512
 
1513
                $sfile = "$root/$subdir/$sfile";
1514
                $sfile =~ s~//~/~g;
1515
                Verbose2("LocateLibFile: $sfile");
1516
                if ( $exact )
1517
                {
415 dpurdie 1518
                    push @done, $sfile if ( -f $sfile || -l $sfile );
407 dpurdie 1519
                }
419 dpurdie 1520
                elsif ($num_dll)
407 dpurdie 1521
                {
1522
                    push @done, glob ( $sfile );
1523
                }
419 dpurdie 1524
                else
1525
                {
1526
                    #
1527
                    #   Looking for .so files
1528
                    #   Filter out the soname so files
1529
                    #   Assume that the soname is shorter than the realname
429 dpurdie 1530
                    #       Ignore .dbg files.
419 dpurdie 1531
                    #
1532
                    my %sieve;
1533
                    foreach ( glob ( $sfile )  )
1534
                    {
429 dpurdie 1535
                        next if ( m~\.dbg$~ );
421 alewis 1536
                        m~(.*\.so\.)([\d\.]*\d)$~;
1537
                        if ( $1 )
1538
                        {
1539
                            my $file = $1;
1540
                            my $len = exists $sieve{$file} ? length($sieve{$file}) : 0;
1541
                            $sieve{$file} = $_
1542
                                if ( $len == 0 || length($_) > $len );
1543
                        }                                
419 dpurdie 1544
                    }
1545
 
1546
                    push @done, values %sieve;
1547
                }
407 dpurdie 1548
            }
1549
        }
1550
    }
1551
 
415 dpurdie 1552
    Error ("ResolveLibFile: File not found: $file", "Search Path:", @path)
407 dpurdie 1553
        unless ( @done );
1554
 
1555
    Warning ("ResolveLibFile: Multiple instances of file found. Only first is used", @done)
1556
        if ( $#done > 0 && ! $wildcard && !wantarray );
1557
 
1558
    return wantarray ? @done : $done[0];
1559
}
1560
 
1561
 
1562
#-------------------------------------------------------------------------------
1563
# Function        : AUTOLOAD
1564
#
1565
# Description     : Intercept bad user directives and issue a nice error message
1566
#                   This is a simple routine to report unknown user directives
1567
#                   It does not attempt to distinguish between user errors and
1568
#                   programming errors. It assumes that the program has been
1569
#                   tested. The function simply report filename and line number
1570
#                   of the bad directive.
1571
#
1572
# Inputs          : Original function arguments ( not used )
1573
#
1574
# Returns         : This function does not return
1575
#
1576
our $AUTOLOAD;
1577
sub AUTOLOAD
1578
{
1579
    my $fname = $AUTOLOAD;
1580
    $fname =~ s~^main::~~;
1581
    my ($package, $filename, $line) = caller;
1582
 
1583
    Error ("Directive not known or not allowed in this context: $fname",
1584
           "Directive: $fname( @_ );",
1585
           "File: $filename, Line: $line" );
1586
}
1587
 
1588
 
1589
1;
1590