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