OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / scripts / makemake.tcl
1 #!/usr/bin/tclsh
2
3 # Helper to enforce array-ness.
4 proc makearray {name} {
5   upvar $name ary
6   set ary(_) 1
7   unset ary(_)
8 }
9
10 # Verbose printer.
11 proc verbose {text} {
12 # puts stderr $text
13 }
14
15 # This maps a name to its style:
16 # * bc    objects in this package and all its sub-packages
17 #         are to be compiled with the BC ABI.  It is an error
18 #         for sub-packages to also appear in the map.
19 # * package
20 #         objects in this package (and possibly sub-packages,
21 #         if they do not appear in the map) will be compiled en masse
22 #         from source into a single object, using the C++ ABI.
23 # * ordinary
24 #         objects in this package (and possibly sub-packages
25 #         if they do not appear in the map) will be compiled one at
26 #         a time into separate .o files.
27 # * ignore
28 #         objects in this package are not used.  Note however that
29 #         most ignored files are actually handled by listing them in
30 #         'standard.omit'
31 #
32 # If a package does not appear in the map, the default is 'package'.
33 global package_map
34 set package_map(.) package
35
36 # These are ignored in Classpath.
37 set package_map(gnu/test) ignore
38 set package_map(gnu/javax/swing/plaf/gtk) ignore
39
40 set package_map(gnu/xml) bc
41 set package_map(javax/imageio) bc
42 set package_map(javax/xml) bc
43 set package_map(gnu/java/beans) bc
44 set package_map(gnu/java/awt/peer/gtk) bc
45 set package_map(gnu/java/awt/peer/qt) bc
46 set package_map(org/xml) bc
47 set package_map(org/w3c) bc
48
49 # This is handled specially by the Makefile.
50 # We still want it byte-compiled so it isn't in the .omit file.
51 set package_map(gnu/gcj/tools/gcj_dbtool/Main.java) ignore
52
53 # These are handled specially.  If we list Class.java with other files
54 # in java.lang, we hit a compiler bug.
55 set package_map(java/lang/Class.java) ignore
56 set package_map(java/lang/Object.java) ignore
57
58 # More special cases.  These end up in their own library.
59 # Note that if we BC-compile AWT we must update these as well.
60 set package_map(gnu/gcj/xlib) package
61 set package_map(gnu/awt/xlib) package
62
63 # Some BC ABI packages have classes which must not be compiled BC.
64 # This maps such packages to a grep expression for excluding such
65 # classes.
66 global exclusion_map
67 makearray exclusion_map
68 # set exclusion_map(java/awt) AWTPermission
69
70 # This maps a package name to a list of corresponding .java file base
71 # names.  The package name will either appear as a key in package_map,
72 # or it will be '.' for the default.
73 global name_map
74 makearray name_map
75
76 # This maps a java file base name, like 'java/lang/Object.java', to
77 # the source directory in which it resides.  We keep a layer of
78 # indirection here so that we can override sources in Classpath with
79 # our own sources.
80 global dir_map
81 makearray dir_map
82
83 # List of all '@' files that we are going to compile.
84 set package_files {}
85
86 # List of all header file variables.
87 set header_vars {}
88
89 # List of all BC object files.
90 set bc_objects {}
91
92 # List of regexps for matching ignored files.
93 set ignore_rx_list {}
94
95
96 # Return true if a given file should be ignored.
97 # The argument is the path name including the package part.
98 proc ignore_file_p {file} {
99   global ignore_rx_list
100   foreach rx $ignore_rx_list {
101     if {[regexp -- $rx $file]} {
102       verbose "ignoring $file for $rx"
103       return 1
104     }
105   }
106   return 0
107 }
108
109 # Read a '.omit' file and update the internal data structures.
110 proc read_omit_file {name} {
111   global ignore_rx_list
112   set fd [open $name r]
113   while {! [eof $fd]} {
114     set line [gets $fd]
115
116     # Classpath's entries bogusly start with "../".
117     if {[string match ../* $line]} {
118       set line [string range $line 3 end]
119     }
120
121     if {$line != ""} {
122       lappend ignore_rx_list $line
123     }
124   }
125   close $fd
126 }
127
128 # Classify a single source file.
129 proc classify_source_file {basedir file} {
130   global package_map name_map dir_map
131
132   if {[ignore_file_p $file]} {
133     return
134   }
135
136   set seen [info exists dir_map($file)]
137   set dir_map($file) $basedir
138   set pkg $file
139   while {1} {
140     if {[info exists package_map($pkg)]} {
141       # If the entry for '.' is 'package', then set up a new entry for
142       # the file's package.
143       if {$pkg == "." && $package_map($pkg) == "package"} {
144         set pkg [file dirname $file]
145         set package_map($pkg) package
146       }
147       verbose "classify succeeded: $file -> $pkg"
148       if {! $seen} {
149         lappend name_map($pkg) $file
150       }
151       return
152     }
153     set pkg [file dirname $pkg]
154   }
155   error "can't happen"
156 }
157
158 # Scan a directory and its subdirectories for .java source files.
159 # Note that we keep basedir and subdir separate so we can properly
160 # update our global data structures.
161 proc scan_directory {basedir subdir} {
162   global dir_map
163
164   set subdirs {}
165   set files {}
166   set here [pwd]
167   cd $basedir/$subdir
168   foreach file [lsort [glob *]] {
169     if {[string match *.java $file]} {
170       lappend files $subdir/$file
171     } elseif {[file isdirectory $file]} {
172       lappend subdirs $subdir/$file
173     }
174   }
175   cd $here
176
177   # Recurse first, so that we don't create new packages too eagerly.
178   foreach dir $subdirs {
179     scan_directory $basedir $dir
180   }
181
182   foreach file $files {
183     classify_source_file $basedir $file
184   }
185 }
186
187 # Scan known packages beneath the base directory for .java source
188 # files.
189 proc scan_packages {basedir} {
190   foreach subdir {gnu java javax org} {
191     if {[file exists $basedir/$subdir]} {
192       scan_directory $basedir $subdir
193     }
194   }
195 }
196
197 # Emit a rule for a 'bc' package.
198 proc emit_bc_rule {package} {
199   global package_map exclusion_map bc_objects
200
201   if {$package == "."} {
202     set pkgname ordinary
203   } else {
204     set pkgname $package
205   }
206   set varname [join [split $pkgname /] _]_source_files
207   set loname [join [split $pkgname /] -].lo
208   set tname [join [split $pkgname /] -].list
209
210   puts "$loname: \$($varname)"
211   # Create a temporary list file and then compile it.  This works
212   # around the libtool problem mentioned in PR 21058.  classpath was
213   # built first, so the class files are to be found there.
214   set omit ""
215   if {[info exists exclusion_map($package)]} {
216     set omit "| grep -v $exclusion_map($package)"
217   }
218   puts  "\t@find classpath/lib/$package -name '*.class'${omit} > $tname"
219   puts "\t\$(LTGCJCOMPILE) -fjni -findirect-dispatch -c -o $loname @$tname"
220   puts "\t@rm -f $tname"
221   puts ""
222
223   # We skip this one because it is built into its own library and is
224   # handled specially in Makefile.am.
225   if {$loname != "gnu-java-awt-peer-gtk.lo"} {
226     lappend bc_objects $loname
227   }
228 }
229
230 # Emit a rule for a 'package' package.
231 proc emit_package_rule {package} {
232   global package_map exclusion_map package_files
233
234   if {$package == "."} {
235     set pkgname ordinary
236   } else {
237     set pkgname $package
238   }
239   set varname [join [split $pkgname /] _]_source_files
240   set base $pkgname
241   set lname $base.list
242   set dname $base.deps
243
244   # A rule to make the phony file we are going to compile.
245   puts "$lname: \$($varname)"
246   puts "\t@\$(mkinstalldirs) \$(dir \$@)"
247   puts "\t@for file in \$($varname); do \\"
248   puts "\t  if test -f \$(srcdir)/\$\$file; then \\"
249   puts "\t    echo \$(srcdir)/\$\$file; \\"
250   puts "\t  else echo \$\$file; fi; \\"
251   puts "\tdone > $lname"
252   puts ""
253   puts "-include $dname"
254   puts ""
255   puts ""
256
257   if {$pkgname != "gnu/gcj/xlib" && $pkgname != "gnu/awt/xlib"} {
258     lappend package_files $lname
259   }
260 }
261
262 # Emit a source file variable for a package, and corresponding header
263 # file variable, if needed.
264 proc emit_source_var {package} {
265   global package_map name_map dir_map header_vars
266
267   if {$package == "."} {
268     set pkgname ordinary
269   } else {
270     set pkgname $package
271   }
272   set uname [join [split $pkgname /] _]
273   set varname ${uname}_source_files
274   puts -nonewline "$varname ="
275
276   makearray dirs
277   foreach base [lsort $name_map($package)] {
278     # Terminate previous line.
279     puts " \\"
280     # Having files start with './' is ugly and confuses the automake
281     # "dirstamp" code; see automake PR 461.
282     set ndir $dir_map($base)/
283     if {$ndir == "./"} {
284       set ndir ""
285     }
286     puts -nonewline "${ndir}${base}"
287     set dirs($dir_map($base)) 1
288   }
289   puts ""
290   puts ""
291
292   if {$package_map($package) != "bc"} {
293     # Ugly code to build up the appropriate patsubst.
294     set result "\$(patsubst %.java,%.h,\$($varname))"
295     foreach dir [lsort [array names dirs]] {
296       if {$dir != "."} {
297         set result "\$(patsubst $dir/%,%,$result)"
298       }
299     }
300
301     if {$package == "." || $package == "java/lang"} {
302       # Ugly hack.
303       set result "\$(filter-out java/lang/Object.h java/lang/Class.h,$result)"
304     }
305
306     puts "${uname}_header_files = $result"
307     puts ""
308     if {$pkgname != "gnu/gcj/xlib" && $pkgname != "gnu/awt/xlib"} {
309       lappend header_vars "${uname}_header_files"
310     }
311   }
312 }
313
314 # Pretty-print a Makefile variable.
315 proc pp_var {name valueList {pre ""} {post ""}} {
316   puts ""
317   puts -nonewline "$name ="
318   foreach val $valueList {
319     puts " \\"
320     puts -nonewline "  ${pre}${val}${post}"
321   }
322   puts ""
323 }
324
325 # Read the proper .omit files.
326 read_omit_file standard.omit.in
327 read_omit_file classpath/lib/standard.omit
328
329 # Scan classpath first.
330 scan_packages classpath
331 scan_packages classpath/external/sax
332 scan_packages classpath/external/w3c_dom
333 # Now scan our own files; this will correctly override decisions made
334 # when scanning classpath.
335 scan_packages .
336 # Files created by the build.
337 classify_source_file . java/lang/ConcreteProcess.java
338 classify_source_file classpath java/util/LocaleData.java
339 classify_source_file classpath gnu/classpath/Configuration.java
340
341 puts "## This file was automatically generated by scripts/makemake.tcl"
342 puts "## Do not edit!"
343 puts ""
344
345 foreach package [lsort [array names package_map]] {
346   if {$package_map($package) == "ignore"} {
347     continue
348   }
349   if {! [info exists name_map($package)]} {
350     continue
351   }
352
353   emit_source_var $package
354
355   if {$package_map($package) == "bc"} {
356     emit_bc_rule $package
357   } elseif {$package_map($package) == "ordinary"} {
358     # Nothing in particular to do here.
359   } elseif {$package_map($package) == "package"} {
360     emit_package_rule $package
361   } else {
362     error "unrecognized type: $package_map($package)"
363   }
364 }
365
366 pp_var all_packages_source_files $package_files
367 pp_var ordinary_header_files $header_vars "\$(" ")"
368 pp_var bc_objects $bc_objects