Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
6133 dpurdie 1
########################################################################
2
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
3
#
4
# Module name   : ToolsetFiles.pm
5
# Module type   : JATS Utility
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : Provide access to the file GbeFiles.cfg
10
#                 Provides methods to create, maintain and read the file
11
#                 
12
#                 ToolsetFiles::AddFile
13
#                 ToolsetFiles::AddDir
14
#                 ToolsetFiles::GetFiles
15
#                 ToolsetFiles::GetSubTrees
16
#                 ToolsetFiles::GetDataFile
17
#                 
18
#           Internal Use Only
19
#                 readData
20
#                 writeData
21
#                 rebuildSubdirList
22
#
23
#......................................................................#
24
 
25
require 5.008_002;
26
use strict;
27
use warnings;
28
 
29
#===============================================================================
30
package ToolsetFiles;
31
use JatsError;
32
use FileUtils;
33
use ConfigurationFile;
34
 
35
# automatically export what we need into namespace of caller.
36
use Exporter();
37
our (@ISA, @EXPORT, %EXPORT_TAGS, @EXPORT_OK);
38
@ISA         = qw(Exporter);
39
@EXPORT      = qw();
40
@EXPORT_OK   = qw();
41
 
42
%EXPORT_TAGS = (All => [@EXPORT, @EXPORT_OK]);
43
 
44
#
45
#   Global variables
46
#
47
our %GBE_TOOLSETFiles;          # Needs to be 'our'. Data store
48
my $dataDir;                    # Path to the interface directory
49
my $dataFile;                   # Path to GbeFiles.cfg 
50
 
51
#-------------------------------------------------------------------------------
52
# Function        : ToolsetFiles::AddFile
53
#
54
# Description     : Maintain a data structure of files that are created
55
#                   by the makefile creation process.
56
#
57
#                   Used to simplify the clobber process
58
#                   All files 'added' will be deleted as a part of a clobber
59
#
60
#                   Maintains an on-disk data structure
61
#
62
# Inputs          : fileList        - Files to add to the list
63
#
64
# Returns         : Nothing
65
#
66
sub AddFile
67
{
68
    my (@fileList) = @_;
69
    Verbose2 ("ToolsetFile:", @fileList);
70
 
71
    #
72
    #   Read in the existing data
73
    #
74
    readData();
75
 
76
    # Capture the package root directory
77
    $GBE_TOOLSETFiles{Root} = FullPath($::ScmRoot)
78
        unless defined $GBE_TOOLSETFiles{Root};
79
 
80
    #
81
    # Save to disk if
82
    #   Target directory exists - creation may be delayed
83
    #   We have added entries
84
    #
85
    if ( @fileList )
86
    {
87
        #
88
        #   Add files
89
        #       Need to be full paths
90
        #       Paths are store relative to the Root
91
        #
92
        foreach ( @fileList )
93
        {
94
            $GBE_TOOLSETFiles{Files}{RelPath(FullPath($_), $GBE_TOOLSETFiles{Root} )} = 1;
95
        }
96
 
97
        #   Save file
98
        writeData();
99
    }
100
}
101
 
102
#-------------------------------------------------------------------------------
103
# Function        : GetFiles 
104
#
105
# Description     : Return an array of files from the stored data structure
106
#
107
# Inputs          : None
108
#
109
# Returns         : An array of files
110
#
111
sub GetFiles
112
{
113
    readData();
114
    return keys %{$GBE_TOOLSETFiles{Files}}
115
}
116
 
117
#-------------------------------------------------------------------------------
118
# Function        : ToolsetFiles::AddDir
119
#
120
# Description     : Maintain a data structure of directories that are used
121
#                   by the makefile creation process.
122
#
123
#                   Used to track directories used by the build. These are used
124
#                   to calculate package signatures and fingerprints
125
#
126
#                   Maintains an on-disk data structure
127
#
128
# Inputs          : $dir        - Files to add to the list
129
#                   $mode       - 'Internal', Include SubDir
130
#                   
131
#                   $mode=Internal 
132
#                       directories are ignored
133
#                           
134
#                   $mode=Include and SubDir
135
#                       Are processed to remove subdirectories
136
#                       Needing a list of distinct directory trees that are a 
137
#                       part of the build. Used to calculate signatures.
138
#                            
139
#
140
# Returns         : Nothing
141
#
142
sub AddDir
143
{
144
    my ($dir, $mode) = @_;
145
    Verbose2 ("ToolsetDir:", $dir, $mode);
146
    #
147
    #   Only track directories that exist
148
    return unless -d $dir;
149
 
150
    #
151
    #   Read in the existing data
152
    #
153
    readData();
154
 
155
    #
156
    #   Need to know the current directory in order to calculate the
157
    #   FullPath and others
158
    #
159
    Error ("Internal: ToolsetFiles. Cwd not defined")
160
        unless ( defined $::Cwd );
161
 
162
    # Capture the package root directory
163
    $GBE_TOOLSETFiles{Root} = FullPath($::ScmRoot)
164
        unless defined $GBE_TOOLSETFiles{Root};
165
 
166
    #
167
    # Save to disk if
168
    #   Target directory exists - creation may be delayed
169
    #   We have added entries
170
    #
171
    my $dirList = ($mode =~ m/Internal/i) ? 'Internal' : 'Src';
172
    if ( $dir )
173
    {
174
        #
175
        #   Add files - Need to be full paths
176
        #
177
        my $relDir = RelPath(FullPath($dir), $GBE_TOOLSETFiles{Root} );
178
 
179
        #
180
        #   Ignore Src directories that are a subdirectory of the current root dir
181
        #
182
        if (($relDir =~ m~^\.\.(/|$)~) || ($dirList eq 'Internal'))
183
        {
184
 
185
            # Add the new item and rebuild the subdir list
186
            @{$GBE_TOOLSETFiles{Dirs}{$dirList}} = rebuildSubdirList($relDir, @{$GBE_TOOLSETFiles{Dirs}{$dirList}});
187
 
188
            #   Save file
189
            writeData();
190
        }
191
    }
192
}
193
 
194
#-------------------------------------------------------------------------------
195
# Function        : ToolsetFiles::GetSubTrees 
196
#
197
# Description     : Return an ordered list of directory subtrees used by the build
198
#                   These will be absolute paths
199
#                   
200
#                   This contains a list of all directories used by the build/make 
201
#                   as discovered when creating files. 
202
#                       IFF all source was below the build.pl dir, then we wouldn't need
203
#                       to do this and life would be much simpler (and faster)
204
#                       
205
#                   Used by the 'sandbox':
206
#                       To create a fingerprint over all files in a package.
207
#                   Used by 'buildlib':
208
#                       To create a signature of the package
209
#
210
# Inputs          : $interface  - (Optional) Path to the interface directory
211
#
212
# Returns         : Ordered list of absolute paths of all subdirectory trees discovered
213
#                   during the build phase.
214
#                   
215
#                   Needs to be the same order on all machines
216
#
217
sub GetSubTrees
218
{
219
    my ($interface) = @_;
220
    my @dirList;
221
 
222
    #
223
    #   Read in GbeFiles.cfg
224
    #   It must exist
225
    #
226
    readData($interface) || Error ("Internal: ToolsetFiles::GetSubTrees - GbeFiles not found");
227
 
228
    #
229
    #   Generate a list of directories in the package
230
    #   This is the root directory and all other Src directories discovered
231
    #
232
    push @dirList, $GBE_TOOLSETFiles{Root};
233
    if (exists $GBE_TOOLSETFiles{Dirs}{Src})
234
    {
235
        foreach my $dir ( sort {uc($a) cmp uc($b) } @{$GBE_TOOLSETFiles{Dirs}{Src}})
236
        {
237
            push @dirList,CleanPath(catdir($GBE_TOOLSETFiles{Root}, $dir)); 
238
        }
239
    }
240
 
241
    # Process the complete list to remove subdirectories
242
    @dirList = rebuildSubdirList(@dirList);
243
#DebugDumpData("GetSubTrees", \@dirList);
244
    return @dirList;
245
}
246
#-------------------------------------------------------------------------------
247
# Function        : rebuildSubdirList 
248
#
249
# Description     : Internal function - not intended to be used externally
250
# 
251
#                   Rebuild the subdirectory list
252
#                   Remove items that are subdirectories of other items
253
#                   We only want the parents, not children
254
#
255
# Inputs          : @dirList        - List of items to process 
256
#
257
# Returns         : Rebuild list
258
#
259
sub rebuildSubdirList
260
{
261
    #   Process the complete list to remove subdirectories
262
    #   Process is:
263
    #       Sort list. Will end up with shortest directories first, thus subdirs will follow parents
264
    #       Insert each item into a new list iff it is not a subdir of something already in the list
265
    #
266
    my @newList;
267
    my @dirList = sort {uc($a) cmp uc($b)} @_;
268
 
269
    foreach my $newItem ( @dirList )
270
    {
271
        my $match = 0;
272
        foreach my $item ( @newList )
273
        {
274
            if (index ($newItem, $item) == 0)
275
            {
276
                $match = 1;
277
                last;
278
            }
279
        }
280
        if (! $match)
281
        {
282
            push @newList, $newItem;
283
        }
284
   }
285
 
286
   return @newList;
287
}
288
 
289
#-------------------------------------------------------------------------------
290
# Function        : GetDataFile 
291
#
292
# Description     : Return the full path to the data file
293
#                   May be used to test existence
294
#
295
# Inputs          : $interface - Path to the interface directory (Optional) 
296
#
297
# Returns         : Path to file, or undefined
298
#
299
sub GetDataFile
300
{
301
    my ($interface) = @_;
302
    #
303
    #   Use the global path to the interface directory
304
    #   unless specifically provided by the user
305
    #
306
    if ($interface) {
307
        $dataDir = $interface;
308
    } else {
309
        Error ("Internal: ToolsetFiles. ScmRoot or ScmInterface not defined")
310
            unless ( defined $::ScmRoot && defined $::ScmInterface );
311
        $dataDir = "$::ScmRoot/$::ScmInterface";
312
    }
313
    $dataFile = "$dataDir/GbeFiles.cfg";
314
 
315
    return $dataFile if (-f $dataFile );
316
    return undef;
317
}
318
 
319
 
320
#-------------------------------------------------------------------------------
321
# Function        : readData 
322
#
323
# Description     : Read the data file into memory
324
#                   Data may not be present
325
#
326
# Inputs          : $interface - Path to the interface directory (Optional) 
327
#
328
# Returns         : True - file found and read 
329
#
330
sub readData
331
{
332
    my ($interface) = @_;
333
 
334
    #
335
    #   Use the global path to the interface directory
336
    #   unless specifically provided by the user
337
    #
338
    if ($interface) {
339
        $dataDir = $interface;
340
    } else {
341
        Error ("Internal: ToolsetFiles. ScmRoot or ScmInterface not defined")
342
            unless ( defined $::ScmRoot && defined $::ScmInterface );
343
        $dataDir = "$::ScmRoot/$::ScmInterface";
344
    }
345
    $dataFile = "$dataDir/GbeFiles.cfg";
346
 
347
    #
348
    #   Read the file on every usage
349
    #   Its used in a nested program call structure so the data may be stale
350
    #
351
    if ( -f  $dataFile )
352
    {
353
        do $dataFile;
354
        return 1 if %GBE_TOOLSETFiles;
355
    }
356
 
357
    return 0;
358
}
359
 
360
 
361
#-------------------------------------------------------------------------------
362
# Function        : writeData 
363
#
364
# Description     : Write the data out to the physical file
365
#                   Simply rewrite the file - if the target directory exists
366
#                   Its creation may be after we have started accumulating files
367
#  
368
#
369
# Inputs          : 
370
#
371
# Returns         : 
372
#
373
sub writeData
374
{
375
    if ( -d $dataDir  ) {
376
        my $fh = ConfigurationFile::New( $dataFile );
377
        $fh->Header( "ToolsetFile", "Toolset Files" );
378
        $fh->Dump( [\%GBE_TOOLSETFiles], [qw(*GBE_TOOLSETFiles)] );
379
        $fh->Close();
380
    }
381
}
382
 
383
 
384
#------------------------------------------------------------------------------
385
1;