Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
289 dpurdie 1
########################################################################
2
# Copyright (C) 2008 ERG Limited, All rights reserved
3
#
4
# Module name   : MakeObject.pm
5
# Module type   : Makefile system
6
# Compiler(s)   : Perl
7
# Environment(s): jats
8
#
9
# Description   : A class to describe an buildable object
10
#                 Used to contain information about
11
#                 Static libs, Merged Libs, Shared Libs
12
#
13
# Usage:
14
#               Create a new Factory for a given Type
15
#               Create new instances of that type
16
#       Per Type
17
#               NewType             - Create a New Type factory
18
#               New                 - Create new instance
19
#               NewAdd              - Create New Instance and add to lists
20
#               Get                 - Get an existing instance
21
#               AllTargets          - Return array of all items getPath's
22
#       Per Object
23
#               getBaseDir          - Base Dir of Built Object
24
#               getFullName         - Full Name of Built Object
25
#               getPath             - BaseDir and Full Name
26
#
27
#               addItem             - Push an item onto named list (ie: OBJS)
28
#               getItems            - get Ref to named list
29
#
30
#......................................................................#
31
 
32
use strict;
33
use warnings;
34
 
35
package MakeObject;
36
 
37
use JatsError;
38
 
39
#-------------------------------------------------------------------------------
40
# Function        : NewType
41
#
42
# Description     : Creates a new Type of Object
43
#                   A factory for creating classes that will contain
44
#                   a set of common types
45
#
46
# Inputs          : $name       - Text name
47
#                   \@ARRAY     - Ref to an array that will be maintained
48
#                   $base       - Base directory
49
#                   $code       - Function to create names of new objects
50
#
51
# Returns         : A Reference to a class that this can be used
52
#
53
sub NewType
54
{
55
    my ($name, $pArray, $base, $code) = @_;
56
    my $self  = {};
57
 
58
    $base .= '/';
59
    $base =~ s~//~/~;
60
 
61
    $self->{NAME} = $name;
62
    $self->{ARRAY} = $pArray;
63
    $self->{BASEDIR} = $base;
64
    $self->{NFUNCT} = $code;
65
    $self->{LIST} = {};
66
 
67
    bless ($self, __PACKAGE__ );
68
    return $self;
69
}
70
 
71
#-------------------------------------------------------------------------------
72
# Function        : New
73
#
74
# Description     : Create a new object of this type
75
#                   Do not add to internal lists
76
#                   Can be used to create a temp template
77
#
78
# Inputs          : $name   - Name of the object
79
#
80
# Returns         : 
81
#
82
sub New
83
{
84
    my ($self, $name) = @_;
85
 
86
    Error ("Bad method to create a New MakeObject" .  ref($self))
87
        unless ( $self && ref($self) eq __PACKAGE__ );
88
 
89
    return  MakeObjectBody::NewBody(@_);
90
}
91
 
92
#-------------------------------------------------------------------------------
93
# Function        : NewAdd
94
#
95
# Description     : Create a new object of this type
96
#                   Add it to the list of known objects
97
#
98
# Inputs          : $name   - Name of the object
99
#
100
# Returns         : 
101
#
102
sub NewAdd
103
{
104
    my $self = shift;
105
    my $obj = New( $self, @_ );
106
 
107
    #
108
    #   Maintain data items
109
    #       Hash of objects entries for ready access
110
    #       Array list - used by user makefiles
111
    #           ie: @LIBS
112
    #
113
    my $name = $obj->{NAME};
114
    $self->{LIST}{$name} = $obj;
115
    push( @{$self->{ARRAY}}, $name );
116
 
117
    return $obj
118
}
119
 
120
#-------------------------------------------------------------------------------
121
# Function        : Get
122
#
123
# Description     : Retrieve an item
124
#                   Also used to test existance
125
#
126
# Inputs          : $self
127
#                   $name       - Name of item
128
#
129
# Returns         : Ref or undef if not known
130
#
131
sub Get
132
{
133
    my $parent = shift;
134
    my ($name) = @_;
135
 
136
    #
137
    #   Return Item
138
    #   Will not create hash entry if it doesn't exist
139
    #
140
    return $parent->{LIST}{$name};
141
}
142
 
143
#-------------------------------------------------------------------------------
144
# Function        : AllTargets
145
#
146
# Description     : Return an array of all targets
147
#
148
# Inputs          : $self
149
#
150
# Returns         : A Ref to an array
151
#                   that contains the 'makefile' path to the generated items
152
#
153
sub AllTargets
154
{
155
    my $self = shift;
156
    my @result;
157
 
158
    #
159
    #   Use the Array element as the source of the list:
160
    #       1) Retains the order in which the user specified the artifacts
161
    #       2) Allows user to manipulate the list
162
    #
163
 
164
    foreach my $lib ( @{$self->{ARRAY}} )
165
    {
166
        Error ("Entry missing for: $lib in the collection of " . $self->{NAME} )
167
            unless ( exists ($self->{LIST}{$lib}) );
168
        push @result, $self->{LIST}{$lib}->getPath();
169
    }
170
    \@result;
171
}
172
 
173
#sub DESTROY
174
#{
175
#    DebugDumpData("DESTROY", \@_ );
176
#}
177
 
178
 
179
################################################################################
180
#   Internal Package
181
#
182
package MakeObjectBody;
183
use JatsError;
184
 
185
#-------------------------------------------------------------------------------
186
# Function        : NewBody
187
#
188
# Description     : Create a new 'empty' entry
189
#
190
# Inputs          : $parent         - Ref to Base Type
191
#                   $name           - Name
192
#
193
# Returns         : Ref to a an object that can be manipulated
194
#
195
sub NewBody
196
{
197
    my $parent = shift;
198
    my ($name) = @_;
199
    my $self  = {};
200
 
201
    Error ("No Name provided creating a new " . $parent->{NAME})
202
        unless ( $name );
203
 
204
    Error ("Creating duplicate name: $name of type: " . $parent->{NAME} )
205
        if ( exists $parent->{LIST}{$name} );
206
 
207
    Error ("Bad object name : \"$name\"",
208
            "Contains invalid characters" )
209
        if ( $name =~ m~[^-.\$a-zA-Z0-9_]~ );
210
 
211
    $self->{NAME}     = $name;
212
    $self->{BASEDIR}  = $parent->{BASEDIR};
213
    $self->{FULLNAME} = $parent->{NFUNCT}($name);
214
 
215
    bless ($self, __PACKAGE__);
216
    return $self;
217
}
218
 
219
#-------------------------------------------------------------------------------
220
# Function        : getPath
221
#                   getBaseDir
222
#                   getFullName
223
#
224
# Description     : Accessor functions
225
#
226
# Inputs          : $self
227
#
228
# Returns         : 
229
#
230
 
231
sub getPath
232
{
233
    my $self = shift;
234
    return $self->{BASEDIR} . $self->{FULLNAME};
235
}
236
 
237
sub getBaseDir
238
{
239
    my $self = shift;
240
    return $self->{BASEDIR};
241
}
242
 
243
sub getFullName
244
{
245
    my $self = shift;
246
    return $self->{FULLNAME};
247
}
248
 
249
#-------------------------------------------------------------------------------
250
# Function        : getItems
251
#                   addItem
252
#
253
# Description     : Accessor functions for lists of things
254
#                   The list will spring into existance if it doesn't exist
255
#                   Will return a ref to the list or a ref to an empty list
256
#
257
# Inputs          : $self
258
#                   $name               - Name of item list
259
#                   $value              - Value to add
260
#
261
# Returns         : 
262
#
263
 
264
sub getItems
265
{
266
    my ( $self, $name ) = @_;
267
    return $self->{$name} || [];
268
}
269
 
270
sub addItem
271
{
272
    my ($self, $name, $value) = @_;
273
    push @{$self->{$name}}, $value;
274
}
275
 
276
 
277
1;
278