summaryrefslogtreecommitdiff
path: root/samwise/PerlSam/Generator/Borland.pm
blob: a26486c27206040a5c297f5ef71419f9266c8046 (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
# $Id$

package PerlSam::Generator::Borland;

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

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

sub GetIncDir ($);

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

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

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

###############################################################################
# Methods

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

    $output .= "#\n";
    $output .= "# \$Id\$\n";
    $output .= "#\n";
    $output .= "# Borland Workspace Makefile generated by the Samwise Compiler\n";
    $output .= "#\n";
    $output .= "\n";

    #
    # Output the list of subdirectories, if there are any
    #

    if (scalar @{$data->{WORKSPACE}->{SUBDIRS}} > 0) {
        $output .= "# Subdirectories\n";
        $output .= "\n";
        $output .= "DIRS =";
    
        foreach my $dir (@{$data->{WORKSPACE}->{SUBDIRS}}) {
            $output .= " \\\n       $dir";
        }
        $output .= "\n";
        $output .= "\n";
    }
    
    #
    # Output the list of projects, if there are any
    #
    
    if (scalar @{$data->{WORKSPACE}->{PROJECTLINKS}->{LIST}} > 0) {
        $output .= "# Subprojects\n";
        $output .= "\n";
        $output .= "MAKEFILES =";

        foreach my $project (@{$data->{WORKSPACE}->{PROJECTLINKS}->{LIST}}) {
            $output .= " \\\n            $project.bor";
        }
        $output .= "\n";
        $output .= "\n";
    }
    
    $output .= "!include <\$(ACE_ROOT)\\include\\makeinclude\\recurse.bor>\n";

    my $file_name = "Makefile.bor";

    print "Creating Workspace: $file_name\n";

    my $file_handle = new FileHandle ($file_name, "w");
    print $file_handle $output;    
}

sub GenerateProjects (\%)
{
    my $self = shift;
    my $projectdata = shift;
    
    foreach my $project (sort keys %{$projectdata->{PROJECTS}}) {
        my $output;
        my $data = $projectdata->{PROJECTS}->{$project};
        
        my @basenames = PerlSam::Generator::ExpandBaseNames (@{$data->{LIBS}});

        #
        # Add generated files to list of sources
        #
        
        PerlSam::Generator::ExpandIDLFiles(%{$data});
        
        #
        # Output the Makefile header
        #
                                                                                                              
        $output .= "#\n";
        $output .= "# \$Id\$\n";
        $output .= "#\n";
        $output .= "# Borland Project Makefile generated by the Samwise Compiler\n";
        $output .= "# Project Description: $data->{DESCRIPTION}\n";
        $output .= "#\n\n";
        
        #
        # Output the target
        #
        
        $output .= "NAME = $data->{TARGET}\n\n";

        #
        # Look through all source files and store them for later use
        #

        my @idlfiles;   # List of IDL files 
        my @objfiles;   # List of object files
        my @resfiles;   # List of resource files
        my %filedirs;   # Hash of all the subdirs used for source files
        
        foreach my $source (sort keys %{$data->{SOURCES}}) {
            if (defined $data->{SOURCES}->{$source}->{TYPE}
                && ($data->{SOURCES}->{$source}->{TYPE} eq 'idl'
                    || $data->{SOURCES}->{$source}->{TYPE} eq 'clientidl'))
            {
                push @idlfiles, $source;   
            }
            elsif ($source =~ m/(.*)\.cpp$/
                   && (!defined $data->{SOURCES}->{$source}->{TYPE}
                       || $data->{SOURCES}->{$source}->{TYPE} ne 'template'))
            {
                $source = $1;
                
                # Check for source file in a subdirectory
                
                if ($source =~ m/^(.*)\/([^\/]+)$/) {
                    %filedirs->{$1} = 1;
                    $source = $2;
                }
                
                push @objfiles, $source . '.obj';
            }
            elsif ($source =~ m/^(.*)\.rc$/) {
                $source = $1;

                # Check for source file in a subdirectory
                
                if ($source =~ m/^(.*)\/([^\/]+)$/) {
                    %filedirs->{$1} = 1;
                    $source = $2;
                }
                
                push @resfiles, $source . '.res';
            }
        }

        #
        # Output the list of IDL files
        #
        
        if (scalar (@idlfiles) > 0) {
            $output .= "IDLFILES =";
            foreach my $idlfile (@idlfiles) {
                $output .= " \\\n\t\$(IDLDIR)\\$idlfile";
            }
            $output .= "\n\n";
        }
        
        #
        # Output the list of object files
        #
        
        if (scalar (@objfiles) > 0) {
            $output .= "OBJFILES =";
            foreach my $objfile (sort @objfiles) {
                $output .= " \\\n\t\$(OBJDIR)\\$objfile";
            }
            $output .= "\n\n";
        }
        
        #
        # Output the list of resource files
        #

        if (scalar (@resfiles) > 0) {
            $output .= "RESOURCE =";
            foreach my $resfile (@resfiles) {
                $output .= " \$(OBJDIR)\\$resfile";
            }
            $output .= "\n\n";
        }
        
        #
        # Output the compiler flags
        #
        
        $output .= "CFLAGS =";
        
        foreach my $basename (@basenames) {
            $output .= " \\\n\t\$(" . uc $basename . "_CFLAGS)";
        }

        # If we are a library, also output our CFLAGS
        
        if (defined $data->{LIBINFO}->{BASE}) {
            $output .= " \\\n\t\$(" . uc  $data->{LIBINFO}->{BASE} . "_CFLAGS)";
        }
        
        # And if we are a library, also define our export macro(s)
        
        if (defined $data->{LIBINFO}->{EXPORT}) {
            foreach my $export (split / /, $data->{LIBINFO}->{EXPORT}) {
                $output .= " \\\n\t-D" . uc  $export . "_BUILD_DLL";
            }
        }
        
        $output .= "\n\n";
        
        #
        # Output the list of libraries
        #
        
        if ($#basenames >= 0) {
            $output .= "LIBFILES =";
            foreach my $basename (@basenames) {
                $output .= " \\\n\t\$(" . uc $basename . "_LIB)";
            }
            $output .= "\n\n";
        }

        #
        # Output the CPPDIR and IDLDIR
        #        
        
        $output .= "CPPDIR = .";
        foreach my $filedir (sort keys %filedirs) {
            $output .= ";$filedir";
        }
        $output .= "\n\n";
        
        if (scalar (@idlfiles) > 0) {
            # $TODO Can we have multiple subdirs here, like with CPPDIR?
            
            $output .= "IDLDIR = .\n\n";
        }

        #
        # Output install information for libraries
        #
        
        if ($data->{TYPE} eq 'library') {
            
            # INCDIR_NAME is based of the libinfo include data
            
            if (defined $data->{LIBINFO}->{INCLUDE}) {
                $output .= "INCDIR_NAME = " .  GetIncDir ($data->{LIBINFO}->{INCLUDE}) . "\n";
            }
            
            # INCLUDES lists all the template, header, and inline files.
            
            $output .= "INCLUDES =";

            foreach my $source (sort keys %{$data->{SOURCES}}) {
                my $filetype = $data->{SOURCES}->{$source}->{TYPE};
                if (defined $filetype && $filetype eq 'template') {
                    $output .= " \\\n\t$source";
                }
                
                $source =~ s/\//\\/g;
                
                if ($source =~ m/\.(h|i|inl|pidl)$/) {
                    $output .= " \\\n\t$source";
                }
            }
            
            $output .= "\n\n";
        }        

        #
        # Output this command if we have IDL files
        #
        
        if (scalar (@idlfiles) > 0) {
            $output .= "all: idl_src_files\n\n";
        }
        
        #
        # Output the inclusion of a system *.bor file
        #
        
        if ($data->{TYPE} eq 'executable') {
            if (defined $data->{INSTALL} && $data->{INSTALL} eq 'yes') {
                $output .= "!include <\$(ACE_ROOT)\\include\\makeinclude\\build_core_exe.bor>";
            }
            else {
                $output .= "!include <\$(ACE_ROOT)\\include\\makeinclude\\build_exe.bor>";
            }
        }
        elsif ($data->{TYPE} eq 'library') {
            $output .= "!include <\$(ACE_ROOT)\\include\\makeinclude\\build_core_library.bor>\n";
        }

        # 
        # IDL files need some explicit dependencies defined
        #

        if (scalar (@idlfiles) > 0) {
            $output .= "\n#\n# IDL Build rules\n#\n\n";
            
            $output .= "idl_src_files: \$(IDLFILES:.idl=C.cpp) \$(IDLFILES:.idl=S.cpp)\n\n";
            
            foreach my $idlfile (@idlfiles) {
                my $idlroot = "";
                
                if ($idlfile =~ m/^(.*)\.idl$/) {
                    $idlroot = $1;
                }
                
                $output .= "\$(IDLDIR)\\" . $idlroot . "C.cpp \$(IDLDIR)\\";
                $output .= $idlroot . "S.cpp: ";
                $output .= "\$(IDLDIR)\\$idlfile\n";
                $output .= "\t\$(CORE_BINDIR)\\tao_idl -g \$(CORE_BINDIR)\\gperf.exe \\\n";
                $output .= "\t\t$data->{SOURCES}->{$idlfile}->{OPTS} \\\n";
                $output .= "\t\t\$**\n\n";
            }
        }

        #
        # Save the output to the file
        #
        
        my $filename = $project . ".bor";

        print "Creating Project: $filename\n";

        my $filehandle = new FileHandle ($filename, "w");
        
        if (!defined $filehandle) {
            print STDERR "Error: Could not open $filename for writing: $!\n";
            return;
        }
        
        print $filehandle $output;    
    }
}

###############################################################################
# Internal Methods

sub GetIncDir ($)
{
    my $incdir = shift;
    my $curdir = getcwd ();
    my $result = "";
    
    #
    # Replace each ../ with the corresponding value from $curdir
    #
    
    while ($incdir =~ m/..\/(.*)/) {
        $incdir = $1;
        if ($curdir =~ m/(.*)\/(.*)/) {
            $curdir = $1;
            $result = "$2\\" . $result;
        }
    }
    
    # We don't want to end with a \, so remove it.
   
    $result =~ s/\\$//;
    
    return $result;
}
1;