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