OSDN Git Service

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