comparison src/share/tools/MakeDeps/Database.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 8b48a7bd2bf7
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 import java.io.*;
26 import java.util.*;
27
28 public class Database {
29 private MacroDefinitions macros;
30 // allFiles is kept in lexicographically sorted order. See get().
31 private FileList allFiles;
32 // files that have implicit dependency on platform files
33 // e.g. os.hpp: os_<os_family>.hpp os_<os_arch>.hpp but only
34 // recorded if the platform file was seen.
35 private FileList platformFiles;
36 private FileList outerFiles;
37 private FileList indivIncludes;
38 private FileList grandInclude; // the results for the grand include file
39 private long threshold;
40 private int nOuterFiles;
41 private int nPrecompiledFiles;
42 private boolean missingOk;
43 private Platform plat;
44 /** These allow you to specify files not in the include database
45 which are prepended and appended to the file list, allowing
46 you to have well-known functions at the start and end of the
47 text segment (allows us to find out in a portable fashion
48 whether the current PC is in VM code or not upon a crash) */
49 private String firstFile;
50 private String lastFile;
51
52 public Database(Platform plat, long t) {
53 this.plat = plat;
54 macros = new MacroDefinitions();
55 allFiles = new FileList("allFiles", plat);
56 platformFiles = new FileList("platformFiles", plat);
57 outerFiles = new FileList("outerFiles", plat);
58 indivIncludes = new FileList("IndivIncludes", plat);
59 grandInclude = new FileList(plat.getGIFileTemplate().nameOfList(), plat);
60
61 threshold = t;
62 nOuterFiles = 0;
63 nPrecompiledFiles = 0;
64 missingOk = false;
65 firstFile = null;
66 lastFile = null;
67 };
68
69 public FileList getAllFiles() {
70 return allFiles;
71 }
72
73 public Iterator getMacros() {
74 return macros.getMacros();
75 }
76
77 public void canBeMissing() {
78 missingOk = true;
79 }
80
81 public boolean hfileIsInGrandInclude(FileList hfile, FileList cfile) {
82 return ((hfile.getCount() >= threshold) && (cfile.getUseGrandInclude()));
83 }
84
85 /** These allow you to specify files not in the include database
86 which are prepended and appended to the file list, allowing
87 you to have well-known functions at the start and end of the
88 text segment (allows us to find out in a portable fashion
89 whether the current PC is in VM code or not upon a crash) */
90 public void setFirstFile(String fileName) {
91 firstFile = fileName;
92 }
93
94 public void setLastFile(String fileName) {
95 lastFile = fileName;
96 }
97
98 public void get(String platFileName, String dbFileName)
99 throws FileFormatException, IOException, FileNotFoundException {
100 macros.readFrom(platFileName, missingOk);
101
102 BufferedReader reader = null;
103 try {
104 reader = new BufferedReader(new FileReader(dbFileName));
105 } catch (FileNotFoundException e) {
106 if (missingOk) {
107 return;
108 } else {
109 throw(e);
110 }
111 }
112 System.out.println("\treading database: " + dbFileName);
113 String line;
114 int lineNo = 0;
115 do {
116 line = reader.readLine();
117 lineNo++;
118 if (line != null) {
119 StreamTokenizer tokenizer =
120 new StreamTokenizer(new StringReader(line));
121 tokenizer.slashSlashComments(true);
122 tokenizer.wordChars('_', '_');
123 tokenizer.wordChars('<', '>');
124 // NOTE: if we didn't have to do this line by line,
125 // we could trivially recognize C-style comments as
126 // well.
127 // tokenizer.slashStarComments(true);
128 int numTok = 0;
129 int res;
130 String unexpandedIncluder = null;
131 String unexpandedIncludee = null;
132 do {
133 res = tokenizer.nextToken();
134 if (res != StreamTokenizer.TT_EOF) {
135 if (numTok == 0) {
136 unexpandedIncluder = tokenizer.sval;
137 } else if (numTok == 1) {
138 unexpandedIncludee = tokenizer.sval;
139 } else {
140 throw new FileFormatException(
141 "invalid line: \"" + line +
142 "\". Error position: line " + lineNo
143 );
144 }
145 numTok++;
146 }
147 } while (res != StreamTokenizer.TT_EOF);
148
149 if ((numTok != 0) && (numTok != 2)) {
150 throw new FileFormatException(
151 "invalid line: \"" + line +
152 "\". Error position: line " + lineNo
153 );
154 }
155
156 if (numTok == 2) {
157 // Non-empty line
158 String includer = macros.expand(unexpandedIncluder);
159 String includee = macros.expand(unexpandedIncludee);
160
161 if (includee.equals(plat.generatePlatformDependentInclude())) {
162 MacroDefinitions localExpander = macros.copy();
163 MacroDefinitions localExpander2 = macros.copy();
164 localExpander.setAllMacroBodiesTo("pd");
165 localExpander2.setAllMacroBodiesTo("");
166
167 // unexpanded_includer e.g. thread_<os_arch>.hpp
168 // thread_solaris_i486.hpp -> _thread_pd.hpp.incl
169
170 FileName pdName =
171 plat.getInclFileTemplate().copyStem(
172 localExpander.expand(unexpandedIncluder)
173 );
174
175 // derive generic name from platform specific name
176 // e.g. os_<arch_os>.hpp => os.hpp. We enforce the
177 // restriction (imperfectly) noted in includeDB_core
178 // that platform specific files will have an underscore
179 // preceding the macro invocation.
180
181 // First expand macro as null string.
182
183 String newIncluder_temp =
184 localExpander2.expand(unexpandedIncluder);
185
186 // Now find "_." and remove the underscore.
187
188 String newIncluder = "";
189
190 int len = newIncluder_temp.length();
191 int count = 0;
192
193 for ( int i = 0; i < len - 1 ; i++ ) {
194 if (newIncluder_temp.charAt(i) == '_' && newIncluder_temp.charAt(i+1) == '.') {
195 count++;
196 } else {
197 newIncluder += newIncluder_temp.charAt(i);
198 }
199 }
200 newIncluder += newIncluder_temp.charAt(len-1);
201
202 if (count != 1) {
203 throw new FileFormatException(
204 "Unexpected filename format for platform dependent file.\nline: \"" + line +
205 "\".\nError position: line " + lineNo
206 );
207 }
208
209 FileList p = allFiles.listForFile(includer);
210 p.setPlatformDependentInclude(pdName.dirPreStemSuff());
211
212 // Add an implicit dependency on platform
213 // specific file for the generic file
214
215 p = platformFiles.listForFile(newIncluder);
216
217 // if this list is empty then this is 1st
218 // occurance of a platform dependent file and
219 // we need a new version of the include file.
220 // Otherwise we just append to the current
221 // file.
222
223 PrintWriter pdFile =
224 new PrintWriter(
225 new FileWriter(pdName.dirPreStemSuff(),
226 !p.isEmpty())
227 );
228 pdFile.println("# include \"" + includer + "\"");
229 pdFile.close();
230
231 // Add the platform specific file to the list
232 // for this generic file.
233
234 FileList q = allFiles.listForFile(includer);
235 p.addIfAbsent(q);
236 } else {
237 FileList p = allFiles.listForFile(includer);
238 if (isOuterFile(includer))
239 outerFiles.addIfAbsent(p);
240
241 if (includee.equals(plat.noGrandInclude())) {
242 p.setUseGrandInclude(false);
243 } else {
244 FileList q = allFiles.listForFile(includee);
245 p.addIfAbsent(q);
246 }
247 }
248 }
249 }
250 } while (line != null);
251 reader.close();
252
253 // Keep allFiles in well-known order so we can easily determine
254 // whether the known files are the same
255 allFiles.sortByName();
256
257 // Add first and last files differently to prevent a mistake
258 // in ordering in the include databases from breaking the
259 // error reporting in the VM.
260 if (firstFile != null) {
261 FileList p = allFiles.listForFile(firstFile);
262 allFiles.setFirstFile(p);
263 outerFiles.setFirstFile(p);
264 }
265
266 if (lastFile != null) {
267 FileList p = allFiles.listForFile(lastFile);
268 allFiles.setLastFile(p);
269 outerFiles.setLastFile(p);
270 }
271 }
272
273 public void compute() {
274 System.out.println("\tcomputing closures\n");
275 // build both indiv and grand results
276 for (Iterator iter = outerFiles.iterator(); iter.hasNext(); ) {
277 indivIncludes.add(((FileList) iter.next()).doCFile());
278 ++nOuterFiles;
279 }
280
281 if (!plat.haveGrandInclude())
282 return; // nothing in grand include
283
284 // count how many times each include is included & add em to grand
285 for (Iterator iter = indivIncludes.iterator(); iter.hasNext(); ) {
286 FileList indivInclude = (FileList) iter.next();
287 if (!indivInclude.getUseGrandInclude()) {
288 continue; // do not bump count if my files cannot be
289 // in grand include
290 }
291 indivInclude.doFiles(grandInclude); // put em on
292 // grand_include list
293 for (Iterator incListIter = indivInclude.iterator();
294 incListIter.hasNext(); ) {
295 ((FileList) incListIter.next()).incrementCount();
296 }
297 }
298 }
299
300 // Not sure this is necessary in Java
301 public void verify() {
302 for (Iterator iter = indivIncludes.iterator(); iter.hasNext(); ) {
303 if (iter.next() == null) {
304 plat.abort();
305 }
306 }
307 }
308
309 public void put() throws IOException {
310 writeIndividualIncludes();
311
312 if (plat.haveGrandInclude())
313 writeGrandInclude();
314
315 writeGrandUnixMakefile();
316 }
317
318 private void writeIndividualIncludes() throws IOException {
319 System.out.println("\twriting individual include files\n");
320
321 for (Iterator iter = indivIncludes.iterator(); iter.hasNext(); ) {
322 FileList list = (FileList) iter.next();
323 System.out.println("\tcreating " + list.getName());
324 list.putInclFile(this);
325 }
326 }
327
328 private void writeGrandInclude() throws IOException {
329 System.out.println("\twriting grand include file\n");
330 PrintWriter inclFile =
331 new PrintWriter(new FileWriter(plat.getGIFileTemplate().dirPreStemSuff()));
332 plat.writeGIPragma(inclFile);
333 for (Iterator iter = grandInclude.iterator(); iter.hasNext(); ) {
334 FileList list = (FileList) iter.next();
335 if (list.getCount() >= threshold) {
336 inclFile.println("# include \"" +
337 plat.getGIFileTemplate().getInvDir() +
338 list.getName() +
339 "\"");
340 nPrecompiledFiles += 1;
341 }
342 }
343 inclFile.println();
344 inclFile.close();
345 }
346
347 private void writeGrandUnixMakefile() throws IOException {
348 if (!plat.writeDeps())
349 return;
350
351 System.out.println("\twriting dependencies file\n");
352 PrintWriter gd =
353 new PrintWriter(new FileWriter(
354 plat.getGDFileTemplate().dirPreStemSuff())
355 );
356 gd.println("# generated by makeDeps");
357 gd.println();
358
359
360 // HACK ALERT. The compilation of ad_<arch> files is very slow.
361 // We want to start compiling them as early as possible. The compilation
362 // order on unix is dependant on the order we emit files here.
363 // By sorting the output before emitting it, we expect
364 // that ad_<arch> will be compiled early.
365 boolean shouldSortObjFiles = true;
366
367 if (shouldSortObjFiles) {
368 ArrayList sortList = new ArrayList();
369
370 // We need to preserve the ordering of the first and last items
371 // in outerFiles.
372 int size = outerFiles.size() - 1;
373 String firstName = removeSuffixFrom(((FileList)outerFiles.get(0)).getName());
374 String lastName = removeSuffixFrom(((FileList)outerFiles.get(size)).getName());
375
376 for (int i=1; i<size; i++) {
377 FileList anOuterFile = (FileList)outerFiles.get(i);
378 String stemName = removeSuffixFrom(anOuterFile.getName());
379 sortList.add(stemName);
380 }
381 Collections.sort(sortList);
382
383 // write Obj_Files = ...
384 gd.println("Obj_Files = \\");
385 gd.println(firstName + plat.objFileSuffix() + " \\");
386 for (Iterator iter = sortList.iterator(); iter.hasNext(); ) {
387 gd.println(iter.next() + plat.objFileSuffix() + " \\");
388 }
389 gd.println(lastName + plat.objFileSuffix() + " \\");
390 gd.println();
391 gd.println();
392 } else {
393 // write Obj_Files = ...
394 gd.println("Obj_Files = \\");
395 for (Iterator iter = outerFiles.iterator(); iter.hasNext(); ) {
396 FileList anOuterFile = (FileList) iter.next();
397
398 String stemName = removeSuffixFrom(anOuterFile.getName());
399 gd.println(stemName + plat.objFileSuffix() + " \\");
400 }
401 gd.println();
402 gd.println();
403 }
404
405 if (nPrecompiledFiles > 0) {
406 // write Precompiled_Files = ...
407 gd.println("Precompiled_Files = \\");
408 for (Iterator iter = grandInclude.iterator(); iter.hasNext(); ) {
409 FileList list = (FileList) iter.next();
410 gd.println(list.getName() + " \\");
411 }
412 gd.println();
413 gd.println();
414 }
415
416 gd.println("DTraced_Files = \\");
417 for (Iterator iter = outerFiles.iterator(); iter.hasNext(); ) {
418 FileList anOuterFile = (FileList) iter.next();
419
420 if (anOuterFile.hasListForFile("dtrace.hpp")) {
421 String stemName = removeSuffixFrom(anOuterFile.getName());
422 gd.println(stemName + plat.objFileSuffix() + " \\");
423 }
424 }
425 gd.println();
426 gd.println();
427
428 {
429 // write each dependency
430
431 for (Iterator iter = indivIncludes.iterator(); iter.hasNext(); ) {
432
433 FileList anII = (FileList) iter.next();
434
435 String stemName = removeSuffixFrom(anII.getName());
436 String inclFileName =
437 plat.getInclFileTemplate().copyStem(anII.getName()).
438 preStemSuff();
439
440 gd.println(stemName + plat.objFileSuffix() + " " +
441 stemName + plat.asmFileSuffix() + ": \\");
442
443 printDependentOn(gd, anII.getName());
444 // this gets the include file that includes all that
445 // this file needs (first level) since nested includes
446 // are skipped to avoid cycles.
447 printDependentOn(gd, inclFileName);
448
449 if ( plat.haveGrandInclude() ) {
450 printDependentOn(gd,
451 plat.getGIFileTemplate().preStemSuff());
452 }
453
454 for (Iterator iiIter = anII.iterator(); iiIter.hasNext(); ) {
455 FileList hfile = (FileList) iiIter.next();
456 if (!hfileIsInGrandInclude(hfile, anII) ||
457 plat.writeDependenciesOnHFilesFromGI()) {
458 printDependentOn(gd, hfile.getName());
459 }
460 if (platformFiles.hasListForFile(hfile.getName())) {
461 FileList p =
462 platformFiles.listForFile(hfile.getName());;
463 for (Iterator hiIter = p.iterator();
464 hiIter.hasNext(); ) {
465 FileList hi2 = (FileList) hiIter.next();
466 if (!hfileIsInGrandInclude(hi2, p)) {
467 printDependentOn(gd, hi2.getName());
468 }
469 }
470 }
471 }
472
473 if (plat.includeGIDependencies()
474 && nPrecompiledFiles > 0
475 && anII.getUseGrandInclude()) {
476 gd.println(" $(Precompiled_Files) \\");
477 }
478 gd.println();
479 gd.println();
480 }
481 }
482
483 gd.close();
484 }
485
486 public void putDiffs(Database previous) throws IOException {
487 System.out.println("\tupdating output files\n");
488
489 if (!indivIncludes.compareLists(previous.indivIncludes)
490 || !grandInclude.compareLists(previous.grandInclude)) {
491 System.out.println("The order of .c or .s has changed, or " +
492 "the grand include file has changed.");
493 put();
494 return;
495 }
496
497 Iterator curIter = indivIncludes.iterator();
498 Iterator prevIter = previous.indivIncludes.iterator();
499
500 try {
501 while (curIter.hasNext()) {
502 FileList newCFileList = (FileList) curIter.next();
503 FileList prevCFileList = (FileList) prevIter.next();
504 if (!newCFileList.compareLists(prevCFileList)) {
505 System.out.println("\tupdating " + newCFileList.getName());
506 newCFileList.putInclFile(this);
507 }
508 }
509 }
510 catch (Exception e) {
511 throw new InternalError("assertion failure: cur and prev " +
512 "database lists changed unexpectedly.");
513 }
514
515 writeGrandUnixMakefile();
516 }
517
518 private void printDependentOn(PrintWriter gd, String name) {
519 gd.print(" ");
520 gd.print(plat.dependentPrefix() + name);
521 }
522
523 private boolean isOuterFile(String s) {
524 int len = s.length();
525 String[] suffixes = plat.outerSuffixes();
526 for (int i = 0; i < suffixes.length; i++) {
527 String suffix = suffixes[i];
528 int suffLen = suffix.length();
529 if ((len >= suffLen) &&
530 (plat.fileNameStringEquality(s.substring(len - suffLen),
531 suffix))) {
532 return true;
533 }
534 }
535 return false;
536 }
537
538 private String removeSuffixFrom(String s) {
539 int idx = s.lastIndexOf('.');
540 if (idx <= 0)
541 plat.abort();
542 return s.substring(0, idx);
543 }
544 }