summaryrefslogtreecommitdiff
path: root/samwise/PerlSam/Generator.pm
blob: 3a6e5cade74496ea44e33f07f64510462ae5917d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# $Id$

package PerlSam::Generator;

###############################################################################
# Forward Declarations

sub ConvertPathToRelative ($);
sub ExpandIDLFiles (\%);
sub ExpandBaseNames (\@);
sub ExpandLibraries (\@);
sub ExpandIncludeDirs (\@);
sub ProjectOrder (\%);

# some private declarations
sub GetLibInfo ();
sub SetLibInfo (\%);
sub GenerateDependencies (\%);

###############################################################################
# Instantiations

use PerlSam::Generator::Borland;
use PerlSam::Generator::GNUMake;
use PerlSam::Generator::Automake;
use PerlSam::Generator::MSVC6;
#use PerlSam::Generator::MSVC7;
use PerlSam::Generator::View;
use PerlSam::Generator::VisualAge;

###############################################################################
# Global

use Cwd;
use Data::Dumper;
use strict;

###############################################################################
# Global

# Return the default generators
sub GetDefaults ()
{
    return ('msvc6', 'gnumake', 'borland');
}

###############################################################################
# Constructor

sub new (@)
{
    my $proto = shift;
    my $class = ref ($proto) || $proto;
    my $self = {};

    @{$self->{GENERATORS}} = ();

    foreach my $name (@_) {
        if ($name eq 'view') {
            push @{$self->{GENERATORS}}, new PerlSam::Generator::View;
        }
        elsif ($name eq 'msvc6') {
            push @{$self->{GENERATORS}}, new PerlSam::Generator::MSVC6;
        }
        elsif ($name eq 'gnumake') {
            push @{$self->{GENERATORS}}, new PerlSam::Generator::GNUMake;
        }
        elsif ($name eq 'borland') {
            push @{$self->{GENERATORS}}, new PerlSam::Generator::Borland;
        }
        elsif ($name eq 'automake') {
            push @{$self->{GENERATORS}}, new PerlSam::Generator::Automake;
        }
        elsif ($name eq 'visualage') {
            push @{$self->{GENERATORS}}, new PerlSam::Generator::VisualAge;
        }
        else {
            print STDERR "Error: Unrecognized Generator <$name>\n";
            exit 1;
        }
    }

    bless ($self, $class);
    return $self;
}

###############################################################################
# Public Methods

sub GenerateWorkspace (\%)
{
    my $self = shift;

    foreach my $generator (@{$self->{GENERATORS}}) {
        $generator->GenerateWorkspace (@_);
    }
}

sub GenerateProjects (\%)
{
    my $self = shift;

    foreach my $generator (@{$self->{GENERATORS}}) {
        $generator->GenerateProjects (@_);
    }
}

sub SetLibraryInfo (\%)
{
    my $self = shift;
    my $data = shift;

    SetLibInfo (%{$data});
}

###############################################################################
# Private Methods

# global data
my $generator_lib_info;

sub GetLibInfo ()
{
    return $generator_lib_info;
}

sub SetLibInfo (\%)
{
    $generator_lib_info = shift;
}

# Takes in a string of paths based on ACE_ROOT and converts them to relative
# paths based on the current directory (example: /bin -> ../../bin)
sub ConvertPathToRelative ($)
{
    my $args = shift;
    $args =~ s/\s\s*/ /g;
    $args =~ s/\s$//g;
    my @list = split / /, $args;
    my $result = "";
    my $root;

    my $current = getcwd ();
    my $traverse = $current;
    my $external = 0;
    my @current_list;

    # This little bit of code walks up the path looking ACE_ROOT.  If
    # not found, just default to using "$ACE_ROOT/"

    while (1) {
        # Are we in the "root" yet?
        if (-r "$traverse/samwise/PerlSam/Generator.pm") {
            last;
        }

        # Move up in the directory tree by lopping off the last part of
        # the path
        if ($traverse =~ m/(.*)\/[^\/]*/) {
            $traverse = $1;
            $root .= "../";
        }
        else {
            # Ran out of path, default to environment variable
            $root = "\$ACE_ROOT/";
            $external = 1;
            last;
        }
    }

    # Remove the trailing slash
    $root =~ s/\/$//;

    if (!$external) {
        # Figure out what our relative current directory is
        $current =~ s/^\Q$traverse\E\///;
        @current_list = split /\//, $current;
    }

    # Rebuild the stringified list
    foreach my $entry (@list) {
        my $this_root = $root;
        my @this_current = @current_list;

        # Loop off any common parts.  So if current directory is
        # "\TAO\tests" and the entry is "\TAO\" then reduce the root
        if (!$external) {
            if ($entry =~ m/^\//) {
                while ($#this_current >= 0) {
                    my $top_dir = shift @this_current;
                    if ($entry && $entry =~ s/^\/$top_dir//) {
                        if ($this_root eq '..') {
                            $this_root = '.';
                        } else {
                            $this_root =~ s/^\.\.\///;
                        }
                    } else {
                        last;
                    }
                }
		$result .= $this_root . $entry . " ";
            } else {
	        $result .= $entry . " ";
	    }
        }

    }

    # Remove the trailing space from the stringified list.
    $result =~ s/ $//;

    return $result;
}

sub ExpandIDLFiles (\%)
{
    my $project = shift;

    foreach my $file (keys %{$project->{SOURCES}}) {
        my $base = $file;
        $base =~ s/\.idl$//i;

        next if (!defined $project->{SOURCES}->{$file}->{TYPE});

        if ($project->{SOURCES}->{$file}->{TYPE} eq 'clientidl') {
            $project->{SOURCES}->{$base ."C.h"} = ();
            $project->{SOURCES}->{$base ."C.i"} = ();
            $project->{SOURCES}->{$base ."C.cpp"} = ();
        }
        elsif ($project->{SOURCES}->{$file}->{TYPE} eq 'idl') {
            $project->{SOURCES}->{$base ."C.h"} = ();
            $project->{SOURCES}->{$base ."C.i"} = ();
            $project->{SOURCES}->{$base ."C.cpp"} = ();
            $project->{SOURCES}->{$base ."S.h"} = ();
            $project->{SOURCES}->{$base ."S.i"} = ();
            $project->{SOURCES}->{$base ."S.cpp"} = ();
            $project->{SOURCES}->{$base ."S_T.h"} = ();
            $project->{SOURCES}->{$base ."S_T.i"} = ();
            $project->{SOURCES}->{$base ."S_T.cpp"} = ();
            $project->{SOURCES}->{$base ."S_T.cpp"}->{TYPE} = 'template';
        }
    }
}

sub ExpandBaseNames (\@)
{
    my $libs = shift;
    my $libinfo = GetLibInfo ();

    my @results;

    foreach my $namespace (@{$libinfo->{ORDER}}) {
        foreach my $maplib (@{$libinfo->{$namespace}->{LIST}}) {
            foreach my $lib (@{$libs}) {
                my $lib_namespace;
                my $lib_name;
                if ($lib =~ m/(.*)::(.*)/) {
                    $lib_namespace = $1;
                    $lib_name = $2;
                }

                next if ($lib_namespace ne $namespace);

                if ($lib_name eq $maplib) {
                    push @results, $libinfo->{$namespace}->{DETAILS}->{$maplib}->{BASE};
                }
            }
        }
    }

    return @results;
}

sub ExpandLibraries (\@)
{
    my $libs = shift;
    my $libinfo = GetLibInfo ();

    my $results = "";

    foreach my $namespace (@{$libinfo->{ORDER}}) {
        foreach my $maplib (@{$libinfo->{$namespace}->{LIST}}) {
            foreach my $lib (@{$libs}) {
                my $lib_namespace;
                my $lib_name;
                if ($lib =~ m/(.*)::(.*)/) {
                    $lib_namespace = $1;
                    $lib_name = $2;
                }

                next if ($lib_namespace ne $namespace);

                if ($lib_name eq $maplib) {
                    my $dir = $libinfo->{$namespace}->{DETAILS}->{$maplib}->{LINK};
                    my $data = $libinfo->{$namespace}->{DETAILS}->{$maplib}->{BASE};

                    $dir =~ s/\/$//;

                    if (defined $data) {
                        $results .= ConvertPathToRelative ($dir) . "/$data ";
                    }
                    else {
                        print STDERR "Error: No base name defined for lib <$maplib>\n";
                        exit 1;
                    }
                }
            }
        }
    }

    # Add in any local libs
    foreach my $lib (@{$libs}) {
        if ($lib =~ m/^::(.*)$/) {
            $results .= "$1 ";
        }
    }

    # Get rid of the last space
    $results =~ s/ $//;

    return $results;
}

sub ExpandIncludeDirs (\@)
{
    my $libs = shift;
    my $libinfo = GetLibInfo ();

    my $results = "";

    foreach my $namespace (@{$libinfo->{ORDER}}) {
        foreach my $maplib (@{$libinfo->{$namespace}->{LIST}}) {
            foreach my $lib (@{$libs}) {
                my $lib_namespace;
                my $lib_name;
                if ($lib =~ m/(.*)::(.*)/) {
                    $lib_namespace = $1;
                    $lib_name = $2;
                }

                next if ($lib_namespace ne $namespace);

                if ($lib_name eq $maplib) {
                    my $dir = $libinfo->{$namespace}->{DETAILS}->{$maplib}->{INCLUDE};
                    if ($results !~ m/^$dir / &&
                        $results !~ m/ $dir /)
                    {
                        $results .= $dir . " ";
                    }
                }
            }
        }
    }

    # Add in any local libs
    foreach my $lib (@{$libs}) {
        if ($lib =~ m/^::(.*)\/[^\/]+$/) {
            $results .= "$1 ";
        }
    }

    # Get rid of the last space
    $results =~ s/ $//;

    return $results;
}

sub GenerateDependencies (\%)
{
    my $self = shift;
    my $data = shift;

    print "====================\n" if ($main::verbose >= 2);
    print "Entering Generator::GenerateDependencies\n" if ($main::verbose >= 1);

    #
    # Create a mapping between output names to project names
    #

    my %output; # Mapping between output and project

    print "Calculating output/project mapping\n" if ($main::verbose >= 2);

    foreach my $project (values %{$data->{PROJECTS}}) {
        %output->{$project->{TARGET}} = $project->{NAME}
    }

    print Dumper (\%output) if ($main::verbose >= 2);

    #
    # Go through all the libraries needed by a project and create a DEPENDS
    # list for all dependencies on other projects in this workspace
    #

    foreach my $project (values %{$data->{PROJECTS}}) {
        print "Looking at project $project->{NAME}\n" if ($main::verbose >= 2);

        @{$data->{PROJECTS}->{$project->{NAME}}->{DEPENDS}} = ();

        foreach my $library (ExpandBaseNames (@{$project->{LIBS}})) {
            print "    Looking for library '$library' ..." if ($main::verbose >= 2);

            if (defined %output->{$library}) {
                print "found" if ($main::verbose >= 2);
                push @{$data->{PROJECTS}->{$project->{NAME}}->{DEPENDS}}, %output->{$library};
            }

            print "\n" if ($main::verbose >= 2);
        }
    }

    print "====================\n" if ($main::verbose>= 2);
}

sub ProjectOrder (\%)
{
    my $data = shift;

    my %done;   # Hash for indicating whether a project has been ordered for compilation
    my @order;  # Resulting order of the projects
    my $count;  # Counter to keep track of how many projects made "done" in a pass

    my $cycle = 0;  # Keep track of the cycle

    print "====================\n" if ($main::verbose >= 2);
    print "Entering Generator::ProjectOrder\n" if ($main::verbose >= 1);

    do {
        $count = 0;
        ++$cycle;

        print "Starting Cycle $cycle\n" if ($main::verbose >= 2);

        #
        # Search for projects that have all dependencies ready and add them to
        # the ordered list if so
        #

        foreach my $project (values %{$data->{PROJECTS}}) {
            print "  Looking at project $project->{NAME}..." if ($main::verbose >= 2);

            if (defined %done->{$project->{NAME}}) {
                print "done\n" if ($main::verbose >= 2);
            }
            else {
                my $notready = 0;
                foreach my $depend (@{$project->{DEPENDS}}) {
                    if (!defined %done->{$depend}) {
                        print "waiting on $depend\n" if ($main::verbose >= 2);
                        $notready = 1;
                        last;
                    }
                }

                if ($notready == 0) {
                    ++$count;
                    push @order, $project->{NAME};
                    %done->{$project->{NAME}} = 1;
                    print "ready\n" if ($main::verbose >= 2);
                }
            }
        }
    } while ($count > 0);

    #
    # Perform a verification that there are no leftovers.  If there are, then
    # it means there was a circular dependency
    #

    foreach my $project (values %{$data->{PROJECTS}}) {
        if (!defined %done->{$project->{NAME}}) {
            print STDERR "Error: Circular dependency detected for project: $project->{NAME}\n";
        }
    }

    print "Final Order: ", join (" ", @order), "\n" if ($main::verbose >= 2);
    print "====================\n" if ($main::verbose >= 2);

    return @order;
}

1;