File: prep.pl

package info (click to toggle)
coin2 2.5.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 29,156 kB
  • ctags: 29,453
  • sloc: cpp: 175,383; ansic: 42,198; sh: 9,377; makefile: 7,260; perl: 990; yacc: 181; lex: 114
file content (187 lines) | stat: -rwxr-xr-x 5,665 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w

#- This file is part of the "JNI for Coin" project.
#- Copyright (C) 2002-2005 Systems in Motion. All rights reserved
#-
#- This program 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 program 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 program; if not, write to the Free Software
#- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

##################
#
# prep.pl
# Preprocess C++ header files
#
# 2002-05-21 Morten Vold
#
##################

my $strip = 0;
my $striptarget = "";
my $destdir = "prep";
my $self = __FILE__;
my @names;

my $preprocessor = "cpp"; # use gcc

# Check arguments
for (@ARGV) {
    /^--strip=(.*)$/ && do { $strip=1; $striptarget=$1; next };
    /^--cpp=(.*)$/ && do { $preprocessor=$1; next };
    /^--destdir=(.*)$/ && do { $destdir=$1; next };
    /^--help/ && do { usage(); exit 0 };
    # Other arguments are considered names of files to preprocess
    push @names, $_;
}

$compiler = $preprocessor;
$compiler =~ s/ -E//;
$compiler =~ s/ \/E//;
$compiler =~ s/ -C//;
$compiler =~ s/ \/C//;

if ( $preprocessor =~ /^[^ ]*wrapmsvc/ ) {
  $preprocessor .= " /C";
  $chop = 1;
} else {
  $preprocessor .= " -C";
  $chop = 0;
}

sub preprocess
{
    my $infile = shift;

    if (! -f $infile) {
	print STDERR "prep.pl: No such file '$infile'\n";
	return;
    }
    my $outfile = "$destdir/$infile";
    my $dir = "";
    my @elements = split(/\//, $outfile);
    $#elements--;
    foreach (@elements) {
	$dir .= "$_/";
	mkdir($dir);
    }
    `$preprocessor -I. -DCOIN_DLL -D__cplusplus $infile | $self --strip=$infile --cpp="$preprocessor" > $outfile`;
}

sub usage
{
    print STDERR "prep.pl
Copyright (C) 2002-2005 Systems in Motion AS.
This is free software and may be redistributed under the terms of the GNU GPL.

Preprocess header files as part of the \"JNI for Coin\" project.

usage:
  prep.pl [OPTION]... [FILE]...

  Where [FILE]... are the files to preprocess.

options:
  --help                - print this help message.
  --cpp=<preprocessor>  - set preprocessor command.
  --destdir=<dir>       - specifies where to put the output files.
                          The default is \"prep\" in the current
                          working directory.
  --strip=<file>        - expect cpp output on stdin, and strip 
                          everything but the parts belonging to 
                          <file>.

If no options are given, prep.pl expects the list of files to 
preprocess on the standard input (each filename separated by 
a newline).
";

}

# If we are not stripping, preprocess
if (!$strip) {
    print STDERR "prep.pl: Outputting files to $destdir/\n";
    if ($#names == -1) {
	# Take names of files to preprocess from stdin
	while (<STDIN>) {
	    chomp;
	    preprocess($_);
	}
    } else {
	# Take names of files to preprocess from command line
	for (@names) {
	    preprocess($_);
	}
    }
} 
else { 
    my $match = 0;
    # Strip everything but the target file from the preprocessor output
    while (<STDIN>) {
	if (/^\#(line)? [0-9]+ "(.*)"/ ) {
	    my $sourcefile = $2;
	    $sourcefile =~ s%\\\\%/%g;
	    $sourcefile =~ s%^\./%%g;
	    # print STDERR "file: $sourcefile && $striptarget\n";
            my $oldmatch = $match;
            # $sourcefile =~ s,^\.\\\\,,;
	    $match = ($sourcefile eq $striptarget);
            if ( $oldmatch && !$match ) {
              print "#include <$sourcefile>\n";
            }
	} elsif ($match) {
            if ( $chop ) {
              chop;
            }
            chop;
            $_ =~ s/;(.)/;\n$1/g;
            $_ =~ s/^[ ]*(public|protected|private):(.)/$1:\n$2/g;
            # cxxwrap doesn't grok declspec
            $_ =~ s/\s__declspec\(.*\)\s/ /g;
            my $line = $_;

            if ( $_ =~ m/=.* ([A-Za-z0-9_]+::[A-Z][A-Z0-9_]*)/ ) {
                my $token = $1;
                # must compile test executable to find real value instead
                # of symbolic reference because the reference doesn't
                # translate into Java... (hard to do it any other way)
                my $value = 1;
                open( FILE, ">enumdump.cpp" )
                    || die "error creating dummy file\n";
		print FILE "#include <stdio.h>\n";
		print FILE "#include <" . $striptarget . ">\n";
		print FILE "\n";
		print FILE "int main(int argc, char ** argv) {\n";
		print FILE "  printf(\"%i\", " . $token . ");\n";
		print FILE "  return 0;\n";
		print FILE "}\n";
                close( FILE );
                # FIXME: make this portable
                my $command =
                    "$compiler -I. -DCOIN_DLL -o enumdump.exe " .
                    "enumdump.cpp >enumdump.txt 2>&1";
                # print STDERR $command . "\n";
                system($command);
                open(EXE, "./enumdump.exe |")
                    || die "error executing dummy program";
                $value = <EXE>;
                close(EXE);
                system("rm enumdump.*");

                print STDERR "> symbolic enum value conversion: " .
                             $token . " => " . $value . "\n";
                $line =~ s/$token/$value/g;
            }
	    print $line . "\n";
	}
    }
}