OSDN Git Service

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