re PR bootstrap/35216 (Out of memory building java)
[platform/upstream/gcc.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 # * bcheaders 
26 #         as bc, but generate header files and compile with CNI.
27 # * package
28 #         objects in this package (and possibly sub-packages,
29 #         if they do not appear in the map) will be compiled en masse
30 #         from source into a single object, using the C++ ABI.
31 # * ordinary
32 #         objects in this package (and possibly sub-packages
33 #         if they do not appear in the map) will be compiled one at
34 #         a time into separate .o files.
35 # * ignore
36 #         objects in this package are not used.  Note however that
37 #         most ignored files are actually handled by listing them in
38 #         'standard.omit'
39 # * interpreter
40 #         objects in this package (and possibly sub-packages,
41 #         if they do not appear in the map) are only compiled if
42 #         the interpreter is enabled.  They are compiled as with the
43 #         'package' specifier.
44 #
45 # If a package does not appear in the map, the default is 'package'.
46 global package_map
47 set package_map(.) package
48
49 # These are ignored in Classpath.
50 set package_map(gnu/test) ignore
51 set package_map(gnu/javax/swing/plaf/gtk) ignore
52 set package_map(gnu/gcj/tools/gc_analyze) ignore
53
54 set package_map(gnu/java/awt/peer/swing) bc
55
56 set package_map(gnu/xml/aelfred2) bc
57 set package_map(gnu/xml/dom) bc
58 set package_map(gnu/xml/libxmlj) bc
59 set package_map(gnu/xml/pipeline) bc
60 set package_map(gnu/xml/stream) bc
61 set package_map(gnu/xml/transform) bc
62 set package_map(gnu/xml/util) bc
63 set package_map(gnu/xml/validation) bc
64 set package_map(gnu/xml/xpath) bc
65 set package_map(javax/imageio) bc
66 set package_map(javax/xml) bc
67 set package_map(gnu/java/beans) bc
68 set package_map(gnu/java/awt/dnd/peer/gtk) bc
69 set package_map(gnu/java/util/prefs/gconf) bc
70 set package_map(gnu/java/awt/peer/gtk) bc
71 set package_map(gnu/java/awt/dnd/peer/gtk) bc
72 set package_map(gnu/java/awt/peer/qt) bc
73 set package_map(gnu/java/awt/peer/x) bc
74 set package_map(gnu/java/util/prefs/gconf) bc
75 set package_map(gnu/javax/sound/midi) bc
76 set package_map(gnu/javax/sound/sampled/gstreamer) ignore
77 set package_map(org/xml) bc
78 set package_map(org/w3c) bc
79 set package_map(org/relaxng) bc
80 set package_map(javax/rmi) bc
81 set package_map(org/omg/IOP) bc
82 set package_map(org/omg/PortableServer) bc
83 set package_map(org/omg/CosNaming) bc
84 set package_map(org/omg/CORBA_2_3) bc
85 set package_map(org/omg/Messaging) bc
86 set package_map(org/omg/stub) bc
87 set package_map(org/omg/CORBA) bc
88 set package_map(org/omg/PortableInterceptor) bc
89 set package_map(org/omg/DynamicAny) bc
90 set package_map(org/omg/SendingContext) bc
91 set package_map(org/omg/Dynamic) bc
92 set package_map(gnu/CORBA) bc
93 set package_map(gnu/javax/rmi) bc
94 set package_map(gnu/java/lang/management) bcheaders
95 set package_map(java/lang/management) bc
96 set package_map(gnu/classpath/management) bc
97 set package_map(gnu/javax/management) bc
98
99 # parser/HTML_401F.class is really big, and there have been complaints
100 # about this package requiring too much memory to build.  So, we
101 # compile it as separate objects.  But, we're careful to compile the
102 # sub-packages as packages.
103 set package_map(gnu/javax/swing/text/html/parser) ordinary
104 set package_map(gnu/javax/swing/text/html/parser/models) package
105 set package_map(gnu/javax/swing/text/html/parser/support) package
106
107 # More special cases.  These end up in their own library.
108 # Note that if we BC-compile AWT we must update these as well.
109 set package_map(gnu/gcj/xlib) package
110 set package_map(gnu/awt/xlib) package
111
112 # These packages should only be included if the interpreter is
113 # enabled.
114 set package_map(gnu/classpath/jdwp) interpreter
115 set package_map(gnu/classpath/jdwp/event) interpreter
116 set package_map(gnu/classpath/jdwp/event/filters) interpreter
117 set package_map(gnu/classpath/jdwp/exception) interpreter
118 set package_map(gnu/classpath/jdwp/id) interpreter
119 set package_map(gnu/classpath/jdwp/processor) interpreter
120 set package_map(gnu/classpath/jdwp/transport) interpreter
121 set package_map(gnu/classpath/jdwp/util) interpreter
122 set package_map(gnu/classpath/jdwp/value) interpreter
123 set package_map(gnu/gcj/jvmti) interpreter
124
125 # Some BC ABI packages have classes which must not be compiled BC.
126 # This maps such packages to a grep expression for excluding such
127 # classes.
128 global exclusion_map
129 makearray exclusion_map
130 # set exclusion_map(java/awt) AWTPermission
131
132 # This maps a package name to a list of corresponding .java file base
133 # names.  The package name will either appear as a key in package_map,
134 # or it will be '.' for the default.
135 global name_map
136 makearray name_map
137
138 # This maps a java file base name, like 'java/lang/Object.java', to
139 # the source directory in which it resides.  We keep a layer of
140 # indirection here so that we can override sources in Classpath with
141 # our own sources.
142 global dir_map
143 makearray dir_map
144
145 # An entry in this map means that all .properties files in the
146 # corresponding directory should be ignored.
147 global properties_map
148 makearray properties_map
149
150 # logging.properties is installed and is editable.
151 set properties_map(java/util/logging) _
152
153 # We want to be able to load xerces if it is on the class path.  So,
154 # we have to avoid compiling in the XML-related service files.
155 set properties_map(META-INF/services/javax.xml.parsers.DocumentBuilderFactory) _
156 set properties_map(META-INF/services/javax.xml.parsers.SAXParserFactory) _
157 set properties_map(META-INF/services/javax.xml.parsers.TransformerFactory) _
158 set properties_map(META-INF/services/org.relaxng.datatype.DatatypeLibraryFactory) _
159 set properties_map(META-INF/services/org.w3c.dom.DOMImplementationSourceList) _
160 set properties_map(META-INF/services/org.xml.sax.driver) _
161 set properties_map(META-INF/services/javax.sound.sampled.spi.AudioFileReader.in) ignore
162 set properties_map(META-INF/services/javax.sound.sampled.spi.MixerProvider) ignore
163 set properties_map(META-INF/services/javax.sound.sampled.spi.MixerProvider.in) ignore
164
165 # List of all properties files.
166 set properties_files {}
167
168 # List of all '@' files that we are going to compile.
169 set package_files {}
170
171 # List of all '@' files that we are going to compile if the
172 # interpreter is enabled.
173 set interpreter_package_files {}
174
175 # List of all header file variables.
176 set header_vars {}
177
178 # List of all header file variables for interpreter packages.
179 set interpreter_header_vars {}
180
181 # List of all BC object files.
182 set bc_objects {}
183
184 # List of regexps for matching ignored files.
185 set ignore_rx_list {}
186
187
188 # Return true if a given file should be ignored.
189 # The argument is the path name including the package part.
190 proc ignore_file_p {file} {
191   global ignore_rx_list
192   foreach rx $ignore_rx_list {
193     if {[regexp -- $rx $file]} {
194       verbose "ignoring $file for $rx"
195       return 1
196     }
197   }
198   return 0
199 }
200
201 # Read a '.omit' file and update the internal data structures.
202 proc read_omit_file {name} {
203   global ignore_rx_list
204   set fd [open $name r]
205   while {! [eof $fd]} {
206     set line [gets $fd]
207
208     # Classpath's entries bogusly start with "../".
209     if {[string match ../* $line]} {
210       set line [string range $line 3 end]
211     }
212
213     if {$line != ""} {
214       lappend ignore_rx_list $line
215     }
216   }
217   close $fd
218 }
219
220 # Classify a single source file.
221 proc classify_source_file {basedir file} {
222   global package_map name_map dir_map
223
224   if {[ignore_file_p $file]} {
225     return
226   }
227
228   set seen [info exists dir_map($file)]
229   set dir_map($file) $basedir
230   set pkg $file
231   while {1} {
232     if {[info exists package_map($pkg)]} {
233       # If the entry is 'package', then set up a new entry for the
234       # file's package.
235       if {$package_map($pkg) == "package"} {
236         set pkg [file dirname $file]
237         set package_map($pkg) package
238       }
239       verbose "classify succeeded: $file -> $pkg"
240       if {! $seen} {
241         lappend name_map($pkg) $file
242       }
243       return
244     }
245     set pkg [file dirname $pkg]
246   }
247   error "can't happen"
248 }
249
250 # Scan a directory and its subdirectories for .java source files or
251 # .properties files.  Note that we keep basedir and subdir separate so
252 # we can properly update our global data structures.
253 proc scan_directory {basedir subdir} {
254   global dir_map properties_map properties_files
255
256   set subdirs {}
257   set files {}
258   set here [pwd]
259   cd $basedir/$subdir
260   foreach file [lsort [glob -nocomplain *]] {
261     if {[string match *.java $file]} {
262       lappend files $subdir/$file
263     } elseif {[string match *.properties $file]} {
264       if {! [info exists properties_map($subdir)]} {
265         # We assume there aren't any overrides.
266         lappend properties_files $basedir/$subdir/$file
267       }
268     } elseif {[string match *.css $file]} {
269         # Special case for default.css needed by javax.swing.text.html.
270         lappend properties_files $basedir/$subdir/$file
271     } elseif {[file isdirectory $file]} {
272       lappend subdirs $subdir/$file
273     } elseif {$subdir == "META-INF/services"} {
274       # Service files are generally included as properties.
275       if {! [info exists properties_map($subdir/$file)]} {
276         lappend properties_files $basedir/$subdir/$file
277       }
278     }
279   }
280   cd $here
281
282   # Recurse first, so that we don't create new packages too eagerly.
283   foreach dir $subdirs {
284     scan_directory $basedir $dir
285   }
286
287   foreach file $files {
288     classify_source_file $basedir $file
289   }
290 }
291
292 # Scan known packages beneath the base directory for .java source
293 # files.
294 proc scan_packages {basedir} {
295   foreach subdir {gnu java javax org sun com META-INF} {
296     if {[file exists $basedir/$subdir]} {
297       scan_directory $basedir $subdir
298     }
299   }
300 }
301
302 # Emit a rule for a 'bc' package.
303 proc emit_bc_rule {package} {
304   global package_map exclusion_map bc_objects
305
306   if {$package == "."} {
307     set pkgname ordinary
308   } else {
309     set pkgname $package
310   }
311   set varname [join [split $pkgname /] _]_source_files
312   set loname [join [split $pkgname /] -].lo
313   set tname [join [split $pkgname /] -].list
314
315   puts "$loname: \$($varname)"
316   # Create a temporary list file and then compile it.  This works
317   # around the libtool problem mentioned in PR 21058.  classpath was
318   # built first, so the class files are to be found there.
319   set omit ""
320   if {[info exists exclusion_map($package)]} {
321     set omit "| grep -v $exclusion_map($package)"
322   }
323   puts  "\t@find \$(srcdir)/classpath/lib/$package -name '*.class'${omit} > $tname"
324   puts -nonewline "\t\$(LTGCJCOMPILE) -fsource-filename=\$(here)/classpath/lib/classes "
325   if {$package_map($package) == "bc"} {
326     puts -nonewline "-fjni "
327   }
328   # Unless bc is disabled with --disable-libgcj-bc, $(LIBGCJ_BC_FLAGS) is:
329   #   -findirect-dispatch -fno-indirect-classes
330   puts "\$(LIBGCJ_BC_FLAGS) -c -o $loname @$tname"
331   puts "\t@rm -f $tname"
332   puts ""
333
334   # We skip these because they are built into their own libraries and
335   # are handled specially in Makefile.am.
336   if {$loname != "gnu-java-awt-peer-qt.lo" && $loname != "gnu-java-awt-peer-x.lo"} {
337     lappend bc_objects $loname
338   }
339 }
340
341 # Emit a rule for a 'package' package.
342 proc emit_package_rule_to_list {package package_files_list} {
343   global package_map exclusion_map $package_files_list
344
345   if {$package == "."} {
346     set pkgname ordinary
347   } else {
348     set pkgname $package
349   }
350   set varname [join [split $pkgname /] _]_source_files
351   set base $pkgname
352   set lname $base.list
353   set dname $base.deps
354
355   if {$pkgname == "java/lang"} {
356     # Object and Class are special cases due to an apparent compiler
357     # bug.  Process is a special case because we don't build all
358     # concrete implementations of Process on all platforms.
359     set omit "| tr ' ' '\\n' | fgrep -v Object.class | fgrep -v Class.class | egrep -v '\(Ecos\|Posix\|Win32\)Process' "
360   } else {
361     set omit ""
362   }
363
364   # A rule to make the phony file we are going to compile.
365   puts "$lname: \$($varname)"
366   puts "\t@\$(mkinstalldirs) \$(dir \$@)"
367   puts "\techo \$(srcdir)/classpath/lib/$package/*.class $omit> $lname"
368   puts ""
369   puts "-include $dname"
370   puts ""
371   puts ""
372
373   if {$pkgname != "gnu/gcj/xlib" && $pkgname != "gnu/awt/xlib"
374       && $pkgname != "gnu/gcj/tools/gcj_dbtool"} {
375     lappend  $package_files_list $lname
376   }
377 }
378
379 proc emit_package_rule {package} {
380   global package_files
381   emit_package_rule_to_list $package package_files
382 }
383
384 proc emit_interpreter_rule {package} {
385   global interpreter_package_files
386   emit_package_rule_to_list $package interpreter_package_files
387 }
388
389 # Emit a rule to build a package full of 'ordinary' files, that is,
390 # one .o for each .java.
391 proc emit_ordinary_rule {package} {
392   global name_map package_files
393
394   foreach file $name_map($package) {
395     # Strip off the '.java'.
396     set root [file rootname $file]
397
398     # Look for all included .class files.  Assumes that we don't have
399     # multiple top-level classes in a .java file.
400     set lname $root.list
401     set dname $root.deps
402
403     puts "$lname: classpath/$file"
404     puts "\t@\$(mkinstalldirs) \$(dir \$@)"
405     puts "\techo \$(srcdir)/classpath/lib/${root}*.class> $lname"
406     puts ""
407     puts "-include $dname"
408     puts ""
409     puts ""
410
411     lappend package_files $lname
412   }
413 }
414
415 # Emit a package-like rule for a platform-specific Process
416 # implementation.
417 proc emit_process_package_rule {platform} {
418   set base "java/process-$platform"
419   set lname $base.list
420   set dname $base.deps
421
422   puts "$lname: java/lang/${platform}Process.java"
423   puts "\t@\$(mkinstalldirs) \$(dir \$@)"
424   puts "\techo \$(srcdir)/classpath/lib/java/lang/${platform}Process*.class > $lname"
425   puts ""
426   puts "-include $dname"
427   puts ""
428   puts ""
429 }
430
431 # Emit a source file variable for a package, and corresponding header
432 # file variable, if needed.
433 proc emit_source_var {package} {
434   global package_map name_map dir_map header_vars interpreter_header_vars
435
436   if {$package == "."} {
437     set pkgname ordinary
438   } else {
439     set pkgname $package
440   }
441   set uname [join [split $pkgname /] _]
442   set varname ${uname}_source_files
443   puts -nonewline "$varname ="
444
445   makearray dirs
446   foreach base [lsort $name_map($package)] {
447     # Terminate previous line.
448     puts " \\"
449     # Having files start with './' is ugly and confuses the automake
450     # "dirstamp" code; see automake PR 461.
451     set ndir $dir_map($base)/
452     if {$ndir == "./"} {
453       set ndir ""
454     }
455     puts -nonewline "${ndir}${base}"
456     set dirs($dir_map($base)) 1
457   }
458   puts ""
459   puts ""
460
461   if {$package_map($package) != "bc"} {
462     # Ugly code to build up the appropriate patsubst.
463     set result "\$(patsubst %.java,%.h,\$($varname))"
464     # We use -decreasing so that classpath/external will be stripped
465     # before classpath.
466     foreach dir [lsort -decreasing [array names dirs]] {
467       if {$dir != "."} {
468         set result "\$(patsubst $dir/%,%,$result)"
469       }
470     }
471
472     if {$package == "." || $package == "java/lang"} {
473       # Ugly hack.
474       set result "\$(filter-out java/lang/Object.h java/lang/Class.h,$result)"
475     }
476
477     puts "${uname}_header_files = $result"
478     puts ""
479     if {$pkgname != "gnu/gcj/xlib" && $pkgname != "gnu/awt/xlib"} {
480         if {$package_map($package) == "interpreter"} {
481           lappend interpreter_header_vars "${uname}_header_files"
482         } else {
483           lappend header_vars "${uname}_header_files"
484         }
485     }
486   }
487 }
488
489 # Pretty-print a Makefile variable.
490 proc pp_var {name valueList {pre ""} {post ""}} {
491   puts ""
492   puts -nonewline "$name ="
493   foreach val $valueList {
494     puts " \\"
495     puts -nonewline "  ${pre}${val}${post}"
496   }
497   puts ""
498 }
499
500 global argv
501 if {[llength $argv] > 0 && [lindex $argv 0] == "-verbose"} {
502   set is_verbose 1
503 }
504
505 # Read the proper .omit files.
506 read_omit_file standard.omit.in
507 read_omit_file classpath/lib/standard.omit.in
508
509 # Scan classpath first.
510 scan_packages classpath
511 scan_packages classpath/external/sax
512 scan_packages classpath/external/w3c_dom
513 scan_packages classpath/external/relaxngDatatype
514 scan_packages classpath/external/jsr166
515 # Resource files.
516 scan_packages classpath/resource
517 # Now scan our own files; this will correctly override decisions made
518 # when scanning classpath.
519 scan_packages .
520 # Files created by the build.
521 classify_source_file classpath gnu/java/locale/LocaleData.java
522 classify_source_file classpath gnu/java/security/Configuration.java
523
524 puts "## This file was automatically generated by scripts/makemake.tcl"
525 puts "## Do not edit!"
526 puts ""
527
528 foreach package [lsort [array names package_map]] {
529   if {$package_map($package) == "ignore"} {
530     continue
531   }
532   if {! [info exists name_map($package)]} {
533     continue
534   }
535
536   emit_source_var $package
537
538   if {$package_map($package) == "bc"} {
539     emit_bc_rule $package
540   } elseif {$package_map($package) == "bcheaders"} {
541     emit_bc_rule $package
542   } elseif {$package_map($package) == "ordinary"} {
543     emit_ordinary_rule $package
544   } elseif {$package_map($package) == "package"} {
545     emit_package_rule $package
546   } elseif {$package_map($package) == "interpreter"} {
547     emit_interpreter_rule $package
548   } else {
549     error "unrecognized type: $package_map($package)"
550   }
551 }
552
553 emit_process_package_rule Ecos
554 emit_process_package_rule Win32
555 emit_process_package_rule Posix
556
557 puts "if INTERPRETER"
558 pp_var interpreter_packages_source_files $interpreter_package_files
559 pp_var interpreter_header_files $interpreter_header_vars "\$(" ")"
560 puts ""
561 puts "else"
562 puts ""
563 puts "interpreter_packages_source_files="
564 puts ""
565 puts "interpreter_header_files="
566 puts ""
567 puts "endif"
568
569 lappend package_files {$(interpreter_packages_source_files)}
570 lappend header_vars interpreter_header_files
571
572 pp_var all_packages_source_files $package_files
573 pp_var ordinary_header_files $header_vars "\$(" ")"
574 pp_var bc_objects $bc_objects
575 pp_var property_files $properties_files