PR target/27599
[official-gcc.git] / libjava / classpath / lib / mkdep.pl.in
blobb30fd7ae974508cff5e59f3421d2770d4624a57c
1 #!@PERL@
3 # Create a dependency file for use with make that will
4 # a) not have duplicate entries
5 # b) not include the source of a file as a dependency to separate file,
6 # just the class of the file
7 # c) use jikes .u files
8 # d) includes classes which need native compilation via simple parsing
9 # to find native methods requiring use of javah
11 use strict;
13 my ( $dir, $dep ) = "";
14 my @dirs = ( 'java', 'javax', 'gnu' );
15 my ( $depout ) = "makefile.dep";
16 my ( $classout ) = "classes.dep";
17 my ( $headerout ) = "headers.dep";
18 my ( $javaout ) = "java.dep";
19 my @deps = ();
20 my @natives = ();
21 use vars qw ( $classout $headerout @dirs @deps @natives $dir $dep $depout );
23 # main
25 if ($#ARGV == 0)
27 if ($ARGV[0] =~ /^-h/)
29 findNativeFiles();
30 writeNativeFile();
32 elsif ($ARGV[0] =~ /^-d/)
34 foreach $dir (@dirs)
36 # find all .u files recursively and parse'm
37 findDepFiles($dir);
39 writeDepFile();
41 elsif ($ARGV[0] =~ /^-c/)
43 findClassFiles();
44 writeClassFile();
46 elsif ($ARGV[0] =~ /^-j/)
48 findJavaFiles();
49 writeJavaFile();
52 else
54 print "Usage:\n";
55 print "mkdep.pl -h \tfor header files\n";
56 print "mkdep.pl -c \tfor a list of classes\n";
57 print "mkdep.pl -j \tfor a list of java files\n";
58 print "mkdep.pl -d \tfor dependency generation from jikes .u files\n";
62 sub writeNativeFile
64 my ($i, $j, $k, $l) = "";
65 my $top_srcdir = "../";
66 if (defined $ENV{'top_srcdir'}) {
67 $top_srcdir = $ENV{'top_srcdir'};
69 my $top_srcdir_regex = $top_srcdir;
70 if ($top_srcdir_regex !~ /.*\/$/) {
71 $top_srcdir_regex .= '/';
73 $top_srcdir_regex =~ s/\./\\\./g; # replace . with \.
74 $top_srcdir_regex =~ s/\//\\\//g; # replace / with \/
75 # print "regex is $top_srcdir_regex\n";
76 open(MAKEDEP, ">$headerout") || die "Could not open file ", $headerout;
78 # the HEADERS = ... stuff
79 if ($#natives > -1)
81 print MAKEDEP "CP_HEADERS = \\", "\n";
82 foreach $i (0 .. $#natives-1)
84 $j = $natives[$i];
85 $j =~ s/^$top_srcdir_regex//; # remove ../ or similar
86 $j =~ s/^(\.\.\/)+//g; # remove all preceding ../
87 $j =~ s/^vm\/reference\///; # remove vm/reference/
88 $j =~ s/\//_/g; # replace / with _
89 $j =~ s/\.java$/\.h/; # replace .java with .h
90 print MAKEDEP " \$(top_builddir)/include/", $j, " \\", "\n";
92 $j = $natives[$#natives];
93 $j =~ s/^$top_srcdir_regex//; # remove ../
94 $j =~ s/^(\.\.\/)+//g; # remove all preceding ../
95 $j =~ s/^vm\/reference\///; # remove vm/reference/
96 $j =~ s/\//_/g; # replace / with _
97 $j =~ s/\.java/\.h/; # replace .java with .h
98 print MAKEDEP " \$(top_builddir)/include/", $j, "\n\n";
100 # print rules to make .h files
101 # y/x.h : z/x.class
102 # y/x.h : ../z/x.java
103 # javah -jni z.x
104 # mv y_x.h $(top_srcdir)/include
106 # j = y/x.h
107 # k = z/x.class
108 # k = ../z/x.java
109 # l = z.x
110 foreach $i (0 .. $#natives-1)
112 $j = $natives[$i];
113 $j =~ s/^$top_srcdir_regex//; # remove ../
114 $j =~ s/^(\.\.\/)+//g; # remove all preceding ../
115 $j =~ s/^vm\/reference\///; # remove vm/reference/
116 # $k = $l = $j;
117 $l = $j;
118 $j =~ s/\//_/g; # replace / with _
119 $j =~ s/\.java$/\.h/; # replace .java with .h
121 $k = $natives[$i]; # the original .java file
122 # $k =~ s/\.java$/\.class/; # replace .java with .class
124 $l =~ s/\.java$//; # remove .class
125 $l =~ s/\//\./g; # replace / with .
127 print MAKEDEP "\$(top_builddir)/include/", $j, " : ", $k, "\n";
128 print MAKEDEP "\t\$(JAVAH) ", $l, "\n";
129 print MAKEDEP "\tmv ", $j, " \$(top_builddir)/include\n\n";
131 $j = $natives[$#natives];
132 $j =~ s/^$top_srcdir_regex//; # remove ../
133 $j =~ s/^(\.\.\/)+//g; # remove all preceding ../
134 $j =~ s/^vm\/reference\///; # remove vm/reference/
135 # $k = $l = $j;
136 $l = $j;
137 $j =~ s/\//_/g; # replace / with _
138 $j =~ s/\.java/\.h/; # replace .java with .h
140 $k = $natives[$#natives]; # the original .java file
141 # $k =~ s/\.java$/\.class/; # replace .java with .class
143 $l =~ s/\.java$//; # remove .class
144 $l =~ s/\//\./g; # replace / with .
146 print MAKEDEP "\$(top_builddir)/include/", $j, " : ", $k, "\n";
147 print MAKEDEP "\t\$(JAVAH) ", $l, "\n";
148 print MAKEDEP "\tmv ", $j, " \$(top_builddir)/include\n\n";
150 close(MAKEDEP);
153 sub writeJavaFile
155 my ($i, $j, $class, $depend, $source, $depend_source) = "";
157 open(MAKEDEP, ">$javaout") || die "Could not open file ", $classout;
159 # the JAVA_SRCS = ... stuff
160 if ($#natives > -1)
162 print MAKEDEP "JAVA_SRCS = \\", "\n";
163 foreach $i (0 .. $#natives-1)
165 $j = $natives[$i];
166 print MAKEDEP " ", $j, " \\", "\n";
168 $j = $natives[$#natives];
169 print MAKEDEP " ", $j, "\n\n";
171 close(MAKEDEP);
174 sub writeClassFile
176 my ($i, $j, $class, $depend, $source, $depend_source) = "";
178 open(MAKEDEP, ">$classout") || die "Could not open file ", $classout;
180 # the CLASSES = ... stuff
181 if ($#natives > -1)
183 print MAKEDEP "CLASSES = \\", "\n";
184 foreach $i (0 .. $#natives-1)
186 $j = $natives[$i];
187 $j =~ s/\.java$/\.class/;
188 print MAKEDEP " ", $j, " \\", "\n";
190 $j = $natives[$#natives];
191 $j =~ s/\.java$/\.class/;
192 print MAKEDEP " ", $j, "\n\n";
194 close(MAKEDEP);
197 sub writeDepFile
199 my ($i, $j, $class, $depend, $source, $depend_source) = "";
201 open(MAKEDEP, ">$depout") || die "Could not open file ", $depout;
203 # the class dependencies
204 foreach $i (@deps)
206 open(FILE, "<$i") || die "Could not open file ", $i, "\n";
207 while(<FILE>)
209 chop;
210 ($class, $depend) = /(.+) : (.+)$/;
211 $source = $class;
212 $source =~ s/\.class$/\.java/;
213 if (($source eq $depend) || ($depend !~ /\.java$/))
215 if ($depend =~ /^\.\.\/.+\.class$/)
217 $depend =~ s/^\.\.\///;
219 if (($depend =~ /\.java$/) && ($depend !~ /^\.\.\//))
221 $depend = "../" . $depend;
223 print MAKEDEP $class, " : ", $depend, "\n";
226 print MAKEDEP "\n";
227 close(FILE);
229 close(MAKEDEP);
232 sub findJavaFiles
234 my ($file) = "";
235 open(CLASSES, "<classes") || die "Could not open file classes\n";
236 while(<CLASSES>)
238 chop;
239 $file = $_;
240 push @natives, $file;
242 close(CLASSES);
245 sub findClassFiles
247 my ($file) = "";
248 open(CLASSES, "<classes") || die "Could not open file classes\n";
249 while(<CLASSES>)
251 chop;
252 $file = $_;
253 $file =~ s/^\.\.\///;
254 push @natives, $file;
256 close(CLASSES);
259 sub findNativeFiles
261 my ($file) = "";
262 open(CLASSES, "<classes") || die "Could not open file classes\n";
263 while(<CLASSES>)
265 chop;
266 $file = $_;
267 if (hasNativeMethod($file))
269 push @natives, $file;
273 close(CLASSES);
276 sub hasNativeMethod
278 my ($file) = @_;
279 my ($line, $one, $two) = "";
280 open(FILE, "<$file") || die "Could not open file ", $file, "\n";
281 while(<FILE>)
283 chop;
284 $one = $two;
285 $two = $_;
287 $line = $one . " " . $two;
288 if ( ($line =~ /^\s*public\s.*native\s+\S+\s+\S+\s*\(/) ||
289 ($line =~ /^\s*public\s.*native\s+\S+\s+\S+\s+\S+\s*\(/) ||
290 ($line =~ /^\s*protected\s.*native\s+\S+\s+\S+\s*\(/) ||
291 ($line =~ /^\s*protected\s.*native\s+\S+\s+\S+\s+\S+\s*\(/) ||
292 ($line =~ /^\s*private\s.*native\s+\S+\s+\S+\s*\(/) ||
293 ($line =~ /^\s*private\s.*native\s+\S+\s+\S+\s+\S+\s*\(/) ||
294 ($line =~ /^\s*abstract\s.*native\s+\S+\s+\S+\s*\(/) ||
295 ($line =~ /^\s*final\s.*native\s+\S+\s+\S+\s*\(/) ||
296 ($line =~ /^\s*synchronized\s.*native\s+\S+\s+\S+\s*\(/) ||
297 ($line =~ /^\s*native\s.*/) )
299 close(FILE);
300 return 1;
303 close(FILE);
304 return 0;
307 sub findDepFiles
310 my ($dir) = @_;
311 my (@dirs) = ();
312 my (@local_deps) = ();
313 my (@entries) = ();
314 my ($i, $local_dep) = "";
315 if (opendir(DIR, $dir))
317 @entries = grep(-d "$dir/$_" && !/^\.\.?$/, readdir(DIR));
318 foreach $i (@entries)
320 push @dirs, "$dir/$i";
322 rewinddir(DIR);
323 @entries= grep(/\.u$/, readdir(DIR));
324 closedir(DIR);
325 foreach $i (@entries)
327 push @local_deps, "$dir/$i";
329 push @deps, @local_deps;
330 foreach $i (@dirs)
332 findDepFiles($i);