File: autodsp

package info (click to toggle)
serveez 0.1.5-2.0.etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 4,564 kB
  • ctags: 3,698
  • sloc: ansic: 34,819; sh: 8,507; lisp: 1,633; awk: 392; perl: 376; makefile: 335
file content (554 lines) | stat: -rwxr-xr-x 13,857 bytes parent folder | download | duplicates (3)
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#! /usr/local/bin/perl
#
# autodsp -- Run this script to re-generate all Windows projects.
#
# Copyright (C) 2001, 2002 Stefan Jahn <stefan@lkcc.org>
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this package; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.  
#
# $Id: autodsp,v 1.5 2002/10/01 15:07:01 ela Exp $
#

use strict;

# package constants
my $VERSION = "1.0.1";
my $PACKAGE = "autodsp";

# options
my $verbose = 0;
my $version = "5.00";
my $codepage = "0x409";
my $extradefs = " /D \"__MINGW32__\" /D \"HAVE_CONFIG_H\" /D __STDC__=0";

my @input_files = find_input_files ();

check_arguments (@ARGV);

foreach (@input_files) {
    my $file = $_;
    create_ap ($file, read_ap ($file));
}

#
# find input files
#
sub find_input_files {

    my @files = `find . -name "*.ap" -type f`;
    foreach (@files) {
        chop $_;
        s/^\.\/(.*)$/$1/;
    }
    return @files;
}

#
# check the command line
#
sub check_arguments {

    my (@args) = @_;

    while (@args) {

	if (@args[0] eq "--verbose" || @args[0] eq "-v") {
	    $verbose = 1;
	} elsif (@args[0] eq "--vc5" || @args[0] eq "-5") {
	    $version = "5.00";
	} elsif (@args[0] eq "--vc6" || @args[0] eq "-6") {
	    $version = "6.00";
	} elsif (@args[0] eq "--version" || @args[0] eq "-V") {
	    print "$PACKAGE $VERSION\n";
	    exit 0;
	} elsif (@args[0] eq "--help" || @args[0] eq "-h") {
	    print "Usage: autodsp [options]
Options:
  -h, --help           display this help message
  -v, --verbose        list processed file(s)
  -5, --vc5            generate version 5.00 files (default)
  -6, --vc6            generate version 6.00 files
  -V, --version        print version number, then exit\n";
	    exit 0;
	}
	shift @args;
    }
}

#
# convert -l linker flags
#
sub replace_libs {

    my $libs = "";
    foreach (split (/ /, shift @_)) {
	s/^-l(\S+)$/lib$1\.lib/;
	$libs .= " " . $_;
    }
    return $libs;
}

#
# convert -L linker flags
#
sub replace_ldflags {

    my ($ldflags, $prefix) = @_;
    my $flags = "";
    foreach (split (/ /, $ldflags)) {
	s/^-L(\S+)$/\/libpath:\"$1\/$prefix\"/;
	$flags .= " " . $_;
    }
    return $flags;
}

#
# convert -D cpp flags
#
sub replace_defs {

    my $defs = "";
    foreach (split (/ /, shift @_)) {
	if (/^-D(\S+)=(.*)$/) {
	    $_ = "/D $1=$2";
	} else {
	    s/^-D(\S+)$/\/D \"$1\"/;
	}
	$defs .= " " . $_;
    }
    return $defs;
}

#
# convert -I cpp flags
#
sub replace_includes {

    my $includes = "";
    foreach (split (/ /, shift @_)) {
	s/^-I(\S+)$/\/I \"$1\"/;
	$includes .= " " . $_;
    }
    return $includes;
}

#
# creates DSP (Developer Studio Project) file or DSW (Developer Studio 
# Workspace) file depending on the target type
#
sub create_ap {

    my ($file, %variables) = @_;

    die "autodsp: No TARGET_TYPE specified\n" unless 
	defined $variables{'TARGET_TYPE'};

    if ($variables{'TARGET_TYPE'} =~ /project/i) {
	create_dsw ($file, %variables);
    } else {
	create_dsp ($file, %variables);
    }
}

#
# creates a Workspace file
#
sub create_dsw {

    my ($infile, %variables) = @_;
    local (*OUT_FILE);
    my ($f, $s, $orgfile, $p, $file, $suffix);
    
    # create output file
    $suffix = ".dsw";
    $file = $infile;
    $orgfile = $file;
    $file =~ s/^(.*)\.ap$/$1$suffix/;
    open (OUT_FILE, ">".$file) || 
	die "autodsp: couldn't create \`$file': $!\n";
    print "autodsp: creating $file\n" if $verbose;

    # check OWNER; then print file header
    die "autodsp: $orgfile: No OWNER specified\n"
	unless defined $variables{'OWNER'};
    print OUT_FILE 
	"Microsoft Developer Studio Workspace File, " .
	"Format Version ". $version ."\r\n";

    # print autodsp header
    $orgfile = `basename $orgfile`; chop $orgfile;
    $file = `basename $file`; chop $file;
    print OUT_FILE
	"#\r\n" .
	"# $file generated by $PACKAGE $VERSION from $orgfile\r\n" .
	"#\r\n\r\n";

    # check PROJECTS; then print all
    die "autodsp: $orgfile: No PROJECTS specified\n" 
	unless defined $variables{'PROJECTS'};
    foreach $s (split (/ /, $variables{'PROJECTS'})) {

	$p = `basename $s`;
	chop $p;
	$f = $s;
	$f =~ s/\\/\//g;
	$f .= ".dsp";

	print OUT_FILE
	    "Project: \"" . $p . "\"=\"" . $f . "\"" .
	    " - Package Owner=" . $variables{'OWNER'} . "\r\n\r\n";

	# check dependencies
	if (defined ($variables{$p."_DEPENDENCIES"})) {

	    print OUT_FILE
		"Package=" . $variables{'OWNER'} . "\r\n" .
		"{{{\r\n";
	    # output each dependency
	    foreach (split (/ /, $variables{$p."_DEPENDENCIES"})) {
		print OUT_FILE 
		    "    Begin Project Dependency\r\n" .
		    "    Project_Dep_Name " . $_ . "\r\n" .
		    "    End Project Dependency\r\n";
	    }
	    print OUT_FILE "}}}\r\n";
	}
    }

    close (OUT_FILE);
    return;
}

#
# creates a Project file
#
sub create_dsp {

    my ($infile, %variables) = @_;
    local (*OUT_FILE);
    my ($s, $type, %types, $orgfile, $file, $suffix);

    # types of targets
    %types = (
	      "0x0103" => "\"Win32 (x86) Console Application\"",
	      "0x0102" => "\"Win32 (x86) Dynamic-Link Library\"",
	      );

    # create output file
    $suffix = ".dsp";
    $file = $infile;
    $orgfile = $file;
    $file =~ s/^(.*)\.ap$/$1$suffix/;
    open (OUT_FILE, ">".$file) || 
	die "autodsp: couldn't create \`$file': $!\n";
    print "autodsp: creating $file\n" if $verbose;

    # check OWNER and NAME; then print file header
    die "autodsp: $orgfile: No OWNER specified\n"
	unless defined $variables{'OWNER'};
    die "autodsp: $orgfile: No NAME specified\n"
	unless defined $variables{'NAME'};
    die "autodsp: $orgfile: Invalid OWNER specified\n" 
	unless $variables{'OWNER'} =~ m/^<[0-9]>$/;
    die "autodsp: $orgfile: Invalid NAME specified\n" 
	unless $variables{'NAME'} =~ m/^[a-zA-Z0-9]*$/;
    print OUT_FILE 
	"# Microsoft Developer Studio Project File - Name=\"" . 
	$variables{'NAME'} . 
	"\" - Package Owner=" . $variables{'OWNER'} . "\r\n";
    print OUT_FILE
	"# Microsoft Developer Studio Generated Build File, " .
	"Format Version ". $version ."\r\n";

    # print autodsp header
    $orgfile = `basename $orgfile`; chop $orgfile;
    $file = `basename $file`; chop $file;
    print OUT_FILE
	"#\r\n" .
	"# $file generated by $PACKAGE $VERSION from $orgfile\r\n" .
	"#\r\n\r\n";

    # check TARGET_TYPE; then print it
    $s = $variables{'TARGET_TYPE'};
    if ($s =~ /console.*app/i) {
	$type = "0x0103";
    } elsif ($s =~ /dll/i) {
	$type = "0x0102";
    } else {
	die "autodsp: $orgfile: Invalid TARGET_TYPE specified\n";
    }
    print OUT_FILE "# TARGTYPE " . $types{$type} . " " . $type . "\r\n\r\n";

    # these !MESSAGE's are necessary
    print OUT_FILE "!MESSAGE There are 2 configurations.\r\n";
    print OUT_FILE
	"!MESSAGE \"" . $variables{'NAME'} . " - Win32 Release\" " .
	"(based on " . $types{$type} . ")\r\n";
    print OUT_FILE
	"!MESSAGE \"" . $variables{'NAME'} . " - Win32 Debug\" " .
	"(based on " . $types{$type} . ")\r\n";

    # output project header
    print OUT_FILE "\r\n# Begin Project\r\n";
    print OUT_FILE 
	"CPP=cl.exe\r\n" . 
	"RSC=rc.exe\r\n" . 
	"BSC32=bscmake.exe\r\n" . 
	"LINK32=link.exe\r\n" . 
	"MTL=midl.exe\r\n";

    # generate release target
    print OUT_FILE 
	"\r\n!IF \"\$(CFG)\" == \"" . $variables{'NAME'} . 
	" - Win32 Release\"\r\n\r\n";
    print OUT_FILE create_opt ($type, %variables);

    # generate debug target
    print OUT_FILE 
	"\r\n!ELSEIF \"\$(CFG)\" == \"" . $variables{'NAME'} . 
	" - Win32 Debug\"\r\n\r\n";
    print OUT_FILE create_dbg ($type, %variables);

    print OUT_FILE "\r\n!ENDIF\r\n\r\n";

    # print target header
    print OUT_FILE 
	"# Begin Target\r\n" .
	"# Name \"" . $variables{'NAME'} . " - Win32 Release\"\r\n" .
	"# Name \"" . $variables{'NAME'} . " - Win32 Debug\"\r\n\r\n";

    # check SOURCES; then print all
    die "autodsp: No SOURCES specified\n" 
	unless defined $variables{'SOURCES'};
    foreach $s (split (/ /, $variables{'SOURCES'})) {
	$s =~ s/\\/\//g;
	print OUT_FILE
	    "# Begin Source File\r\n" .
	    "SOURCE=\"" . $s . "\"\r\n" .
	    "# End Source File\r\n\r\n";
    }

    # print target and project footer
    print OUT_FILE
	"# End Target\r\n" .
	"# End Project\r\n";

    close (OUT_FILE);
    return;
}

#
# returns linker and cflags depending on the $debugdef variable and the 
# target type (DLL or Application)
#
sub check_target_type {

    my ($type, $debugdef) = @_;
    my ($submode, $mktyplib, $subsys, $suffix);

    if ($type eq "0x0102") {
	$submode = "/D " . $debugdef . " /D \"_WINDOWS\"";
	$mktyplib = "# ADD MTL /nologo /D " . $debugdef . 
	    " /mktyplib203 /o NUL /win32\r\n";
	$subsys = "/subsystem:windows /dll";
	$suffix = ".dll";
    } elsif ($type eq "0x0103") {
	$submode = "/D " . $debugdef . " /D \"_CONSOLE\" /D \"_MBCS\"";
	$mktyplib = "";
	$subsys = "/subsystem:console";
	$suffix = ".exe";
    }
    return ($submode, $mktyplib, $subsys, $suffix);
}

#
# generates `General' dialog
#
sub create_general {

    my ($builddir, $debuglib) = @_;
    return
	"# PROP Use_MFC 0\r\n" .
	"# PROP Use_Debug_Libraries " . $debuglib . "\r\n" .
	"# PROP Output_Dir \"" . $builddir . "\"\r\n" .
	"# PROP Intermediate_Dir \"" . $builddir . "\"\r\n" .
	"# PROP Ignore_Export_Lib 0\r\n" .
	"# PROP Target_Dir \"\"\r\n";
}

#
# generates `Other' dialogs
#
sub create_others {

    my ($mktyplib, $debugdef) = @_;
    return
	$mktyplib .
	"# ADD RSC /l " . $codepage . " /d " . $debugdef . "\r\n" .
	"# ADD BSC32 /nologo\r\n";
}

#
# generates the Release target
#
sub create_opt {

    my ($type, %variables) = @_;
    my ($debugdef, $debugldflag, $ret, $s, $submode, $mktyplib, $subsys,
	$suffix, $builddir, $debuglib, $cflags);

    $debugdef = "\"NDEBUG\"";
    $debugldflag = " ";
    $builddir = "Opt";
    $debuglib = "0";
    $cflags = "/MD /W3 /GX /O2 /Ob2";

    ($submode, $mktyplib, $subsys, $suffix) = 
	check_target_type ($type, $debugdef);

    $ret = create_general ($builddir, $debuglib);

    $ret .= "# ADD CPP /nologo " . $cflags;
    if (defined $variables{'INCLUDES'}) {
	$ret .= replace_includes ($variables{'INCLUDES'});
    }
    $ret .= " /D \"WIN32\" " . $submode;
    $ret .= $extradefs;
    if (defined $variables{'DEFS'}) {
	$ret .= replace_defs ($variables{'DEFS'});
    }
    if (defined $variables{'opt_DEFS'}) {
	$ret .= replace_defs ($variables{'opt_DEFS'});
    }
    $ret .= " /FD /c\r\n";

    $ret .= create_others ($mktyplib, $debugdef);
    
    $ret .= "# ADD LINK32 kernel32.lib";
    if (defined $variables{'LIBS'}) {
	$ret .= replace_libs ($variables{'LIBS'});
    }
    $ret .= " /nologo " . $subsys . " /pdb:none" . 
	$debugldflag . "/machine:I386";
    if (defined $variables{'TARGET'}) {
	$ret .= " /out:\"". $variables{'TARGET'} . $suffix . "\"";
    }
    if (defined $variables{'LDFLAGS'}) {
	$ret .= replace_ldflags ($variables{'LDFLAGS'}, $builddir);
    }
    $ret .= "\r\n";
    
    return $ret;
}

#
# generates the Debug target
#
sub create_dbg {

    my ($type, %variables) = @_;
    my ($debugdef, $ret, $s, $submode, $mktyplib, $subsys, $suffix, 
	$debugldflag, $builddir, $debuglib, $cflags);

    $debugdef = "\"_DEBUG\"";
    $debugldflag = " /debug ";
    $builddir = "Dbg";
    $debuglib = "1";
    $cflags = "/MDd /W3 /Gm /GX /Zi /Od";

    ($submode, $mktyplib, $subsys, $suffix) = 
	check_target_type ($type, $debugdef);
    
    $ret = create_general ($builddir, $debuglib);

    $ret .= "# ADD CPP /nologo " . $cflags;
    if (defined $variables{'INCLUDES'}) {
	$ret .= replace_includes ($variables{'INCLUDES'});
    }
    $ret .= " /D \"WIN32\" " . $submode;
    $ret .= $extradefs;
    if (defined $variables{'DEFS'}) {
	$ret .= replace_defs ($variables{'DEFS'});
    }
    if (defined $variables{'dbg_DEFS'}) {
	$ret .= replace_defs ($variables{'dbg_DEFS'});
    }
    $ret .= " /FD /c\r\n";

    $ret .= create_others ($mktyplib, $debugdef);
    
    $ret .= "# ADD LINK32 kernel32.lib";
    if (defined $variables{'LIBS'}) {
	$ret .= replace_libs ($variables{'LIBS'});
    }
    $ret .= " /nologo " . $subsys . " /pdb:none" . 
	$debugldflag . "/machine:I386";
    if (defined $variables{'TARGET'}) {
	$ret .= " /out:\"". $variables{'TARGET'} . $suffix . "\"";
    }
    if (defined $variables{'LDFLAGS'}) {
	$ret .= replace_ldflags ($variables{'LDFLAGS'}, $builddir);
    }
    $ret .= "\r\n";
    
    return $ret;
}

#
# read a single .ap file and return a var->value hash
#
sub read_ap {

    my ($file) = @_;
    local (*IN_FILE);
    my ($line, $var, $value, %ret);

    open (IN_FILE, $file) || die "autodsp: couldn't open \`$file': $!\n";
    print "autodsp: reading $file\n" if $verbose;

    while (<IN_FILE>) {

	# clear end of lines; skips comments
	$line = $_;
	$line =~ s/[\r\n]//g;
	$line =~ s/^(.*)\#.*/$0/;

	# continues reading after trailing '\'
	while ($line =~ m/^.*\\$/) {
	    chop $line;
	    $line .= " " . <IN_FILE>;
	    $line =~ s/[\r\n]//g;
	    $line =~ s/^([^\#]*)\#.*/$1/;
	}

	# drop tabs and replace double spaces
	$line =~ s/\t/ /g;
	$line =~ s/[ ]+/ /g;

	# parse VAR=VALUE assignments
	if ($line =~ m/^([^=]*)=(.*)$/) {
	    $var = $1;
	    $value = $2;
	    $var =~ s/^\s*(\S*)/$1/;
	    $var =~ s/(\S*)\s*$/$1/;
	    $value =~ s/^\s*(\S*)/$1/;
	    $value =~ s/(\S*)\s*$/$1/;
	    $ret{$var} = $value;
	}
    }
    close (IN_FILE);
    return %ret;
}