JavacState.java revision 2571:10fc81ac75b4
1/* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.sjavac; 27 28import java.io.*; 29import java.nio.file.Path; 30import java.util.Collections; 31import java.util.Date; 32import java.util.Set; 33import java.util.HashSet; 34import java.util.List; 35import java.util.Map; 36import java.util.HashMap; 37import java.text.SimpleDateFormat; 38import java.net.URI; 39import java.util.*; 40 41import com.sun.tools.sjavac.options.Options; 42import com.sun.tools.sjavac.options.SourceLocation; 43import com.sun.tools.sjavac.server.JavacService; 44 45/** 46 * The javac state class maintains the previous (prev) and the current (now) 47 * build states and everything else that goes into the javac_state file. 48 * 49 * <p><b>This is NOT part of any supported API. 50 * If you write code that depends on this, you do so at your own 51 * risk. This code and its internal interfaces are subject to change 52 * or deletion without notice.</b></p> 53 */ 54public class JavacState 55{ 56 // The arguments to the compile. If not identical, then it cannot 57 // be an incremental build! 58 String theArgs; 59 // The number of cores limits how many threads are used for heavy concurrent work. 60 int numCores; 61 62 // The bin_dir/javac_state 63 private String javacStateFilename; 64 private File javacState; 65 66 // The previous build state is loaded from javac_state 67 private BuildState prev; 68 // The current build state is constructed during the build, 69 // then saved as the new javac_state. 70 private BuildState now; 71 72 // Something has changed in the javac_state. It needs to be saved! 73 private boolean needsSaving; 74 // If this is a new javac_state file, then do not print unnecessary messages. 75 private boolean newJavacState; 76 77 // These are packages where something has changed and the package 78 // needs to be recompiled. Actions that trigger recompilation: 79 // * source belonging to the package has changed 80 // * artifact belonging to the package is lost, or its timestamp has been changed. 81 // * an unknown artifact has appeared, we simply delete it, but we also trigger a recompilation. 82 // * a package that is tainted, taints all packages that depend on it. 83 private Set<String> taintedPackages; 84 // After a compile, the pubapis are compared with the pubapis stored in the javac state file. 85 // Any packages where the pubapi differ are added to this set. 86 // Later we use this set and the dependency information to taint dependent packages. 87 private Set<String> packagesWithChangedPublicApis; 88 // When a module-info.java file is changed, taint the module, 89 // then taint all modules that depend on that that module. 90 // A module dependency can occur directly through a require, or 91 // indirectly through a module that does a public export for the first tainted module. 92 // When all modules are tainted, then taint all packages belonging to these modules. 93 // Then rebuild. It is perhaps possible (and valuable?) to do a more finegrained examination of the 94 // change in module-info.java, but that will have to wait. 95 private Set<String> taintedModules; 96 // The set of all packages that has been recompiled. 97 // Copy over the javac_state for the packages that did not need recompilation, 98 // verbatim from the previous (prev) to the new (now) build state. 99 private Set<String> recompiledPackages; 100 101 // The output directories filled with tasty artifacts. 102 private File binDir, gensrcDir, headerDir; 103 104 // The current status of the file system. 105 private Set<File> binArtifacts; 106 private Set<File> gensrcArtifacts; 107 private Set<File> headerArtifacts; 108 109 // The status of the sources. 110 Set<Source> removedSources = null; 111 Set<Source> addedSources = null; 112 Set<Source> modifiedSources = null; 113 114 // Visible sources for linking. These are the only 115 // ones that -sourcepath is allowed to see. 116 Set<URI> visibleSrcs; 117 118 // Visible classes for linking. These are the only 119 // ones that -classpath is allowed to see. 120 // It maps from a classpath root to the set of visible classes for that root. 121 // If the set is empty, then all classes are visible for that root. 122 // It can also map from a jar file to the set of visible classes for that jar file. 123 Map<URI,Set<String>> visibleClasses; 124 125 // Setup transform that always exist. 126 private CompileJavaPackages compileJavaPackages = new CompileJavaPackages(); 127 128 // Where to send stdout and stderr. 129 private PrintStream out, err; 130 131 JavacState(Options options, boolean removeJavacState, PrintStream o, PrintStream e) { 132 out = o; 133 err = e; 134 numCores = options.getNumCores(); 135 theArgs = options.getStateArgsString(); 136 binDir = Util.pathToFile(options.getDestDir()); 137 gensrcDir = Util.pathToFile(options.getGenSrcDir()); 138 headerDir = Util.pathToFile(options.getHeaderDir()); 139 javacStateFilename = binDir.getPath()+File.separator+"javac_state"; 140 javacState = new File(javacStateFilename); 141 if (removeJavacState && javacState.exists()) { 142 javacState.delete(); 143 } 144 newJavacState = false; 145 if (!javacState.exists()) { 146 newJavacState = true; 147 // If there is no javac_state then delete the contents of all the artifact dirs! 148 // We do not want to risk building a broken incremental build. 149 // BUT since the makefiles still copy things straight into the bin_dir et al, 150 // we avoid deleting files here, if the option --permit-unidentified-classes was supplied. 151 if (!options.isUnidentifiedArtifactPermitted()) { 152 deleteContents(binDir); 153 deleteContents(gensrcDir); 154 deleteContents(headerDir); 155 } 156 needsSaving = true; 157 } 158 prev = new BuildState(); 159 now = new BuildState(); 160 taintedPackages = new HashSet<>(); 161 recompiledPackages = new HashSet<>(); 162 packagesWithChangedPublicApis = new HashSet<>(); 163 } 164 165 public BuildState prev() { return prev; } 166 public BuildState now() { return now; } 167 168 /** 169 * Remove args not affecting the state. 170 */ 171 static String[] removeArgsNotAffectingState(String[] args) { 172 String[] out = new String[args.length]; 173 int j = 0; 174 for (int i = 0; i<args.length; ++i) { 175 if (args[i].equals("-j")) { 176 // Just skip it and skip following value 177 i++; 178 } else if (args[i].startsWith("--server:")) { 179 // Just skip it. 180 } else if (args[i].startsWith("--log=")) { 181 // Just skip it. 182 } else if (args[i].equals("--compare-found-sources")) { 183 // Just skip it and skip verify file name 184 i++; 185 } else { 186 // Copy argument. 187 out[j] = args[i]; 188 j++; 189 } 190 } 191 String[] ret = new String[j]; 192 System.arraycopy(out, 0, ret, 0, j); 193 return ret; 194 } 195 196 /** 197 * Specify which sources are visible to the compiler through -sourcepath. 198 */ 199 public void setVisibleSources(Map<String,Source> vs) { 200 visibleSrcs = new HashSet<>(); 201 for (String s : vs.keySet()) { 202 Source src = vs.get(s); 203 visibleSrcs.add(src.file().toURI()); 204 } 205 } 206 207 /** 208 * Specify which classes are visible to the compiler through -classpath. 209 */ 210 public void setVisibleClasses(Map<String,Source> vs) { 211 visibleSrcs = new HashSet<>(); 212 for (String s : vs.keySet()) { 213 Source src = vs.get(s); 214 visibleSrcs.add(src.file().toURI()); 215 } 216 } 217 /** 218 * Returns true if this is an incremental build. 219 */ 220 public boolean isIncremental() { 221 return !prev.sources().isEmpty(); 222 } 223 224 /** 225 * Find all artifacts that exists on disk. 226 */ 227 public void findAllArtifacts() { 228 binArtifacts = findAllFiles(binDir); 229 gensrcArtifacts = findAllFiles(gensrcDir); 230 headerArtifacts = findAllFiles(headerDir); 231 } 232 233 /** 234 * Lookup the artifacts generated for this package in the previous build. 235 */ 236 private Map<String,File> fetchPrevArtifacts(String pkg) { 237 Package p = prev.packages().get(pkg); 238 if (p != null) { 239 return p.artifacts(); 240 } 241 return new HashMap<>(); 242 } 243 244 /** 245 * Delete all prev artifacts in the currently tainted packages. 246 */ 247 public void deleteClassArtifactsInTaintedPackages() { 248 for (String pkg : taintedPackages) { 249 Map<String,File> arts = fetchPrevArtifacts(pkg); 250 for (File f : arts.values()) { 251 if (f.exists() && f.getName().endsWith(".class")) { 252 f.delete(); 253 } 254 } 255 } 256 } 257 258 /** 259 * Mark the javac_state file to be in need of saving and as a side effect, 260 * it gets a new timestamp. 261 */ 262 private void needsSaving() { 263 needsSaving = true; 264 } 265 266 /** 267 * Save the javac_state file. 268 */ 269 public void save() throws IOException { 270 if (!needsSaving) return; 271 try (FileWriter out = new FileWriter(javacStateFilename)) { 272 StringBuilder b = new StringBuilder(); 273 long millisNow = System.currentTimeMillis(); 274 Date d = new Date(millisNow); 275 SimpleDateFormat df = 276 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); 277 b.append("# javac_state ver 0.3 generated "+millisNow+" "+df.format(d)+"\n"); 278 b.append("# This format might change at any time. Please do not depend on it.\n"); 279 b.append("# M module\n"); 280 b.append("# P package\n"); 281 b.append("# S C source_tobe_compiled timestamp\n"); 282 b.append("# S L link_only_source timestamp\n"); 283 b.append("# G C generated_source timestamp\n"); 284 b.append("# A artifact timestamp\n"); 285 b.append("# D dependency\n"); 286 b.append("# I pubapi\n"); 287 b.append("# R arguments\n"); 288 b.append("R ").append(theArgs).append("\n"); 289 290 // Copy over the javac_state for the packages that did not need recompilation. 291 now.copyPackagesExcept(prev, recompiledPackages, new HashSet<String>()); 292 // Save the packages, ie package names, dependencies, pubapis and artifacts! 293 // I.e. the lot. 294 Module.saveModules(now.modules(), b); 295 296 String s = b.toString(); 297 out.write(s, 0, s.length()); 298 } 299 } 300 301 /** 302 * Load a javac_state file. 303 */ 304 public static JavacState load(Options options, PrintStream out, PrintStream err) { 305 JavacState db = new JavacState(options, false, out, err); 306 Module lastModule = null; 307 Package lastPackage = null; 308 Source lastSource = null; 309 boolean noFileFound = false; 310 boolean foundCorrectVerNr = false; 311 boolean newCommandLine = false; 312 boolean syntaxError = false; 313 314 try (BufferedReader in = new BufferedReader(new FileReader(db.javacStateFilename))) { 315 for (;;) { 316 String l = in.readLine(); 317 if (l==null) break; 318 if (l.length()>=3 && l.charAt(1) == ' ') { 319 char c = l.charAt(0); 320 if (c == 'M') { 321 lastModule = db.prev.loadModule(l); 322 } else 323 if (c == 'P') { 324 if (lastModule == null) { syntaxError = true; break; } 325 lastPackage = db.prev.loadPackage(lastModule, l); 326 } else 327 if (c == 'D') { 328 if (lastModule == null || lastPackage == null) { syntaxError = true; break; } 329 lastPackage.loadDependency(l); 330 } else 331 if (c == 'I') { 332 if (lastModule == null || lastPackage == null) { syntaxError = true; break; } 333 lastPackage.loadPubapi(l); 334 } else 335 if (c == 'A') { 336 if (lastModule == null || lastPackage == null) { syntaxError = true; break; } 337 lastPackage.loadArtifact(l); 338 } else 339 if (c == 'S') { 340 if (lastModule == null || lastPackage == null) { syntaxError = true; break; } 341 lastSource = db.prev.loadSource(lastPackage, l, false); 342 } else 343 if (c == 'G') { 344 if (lastModule == null || lastPackage == null) { syntaxError = true; break; } 345 lastSource = db.prev.loadSource(lastPackage, l, true); 346 } else 347 if (c == 'R') { 348 String ncmdl = "R "+db.theArgs; 349 if (!l.equals(ncmdl)) { 350 newCommandLine = true; 351 } 352 } else 353 if (c == '#') { 354 if (l.startsWith("# javac_state ver ")) { 355 int sp = l.indexOf(" ", 18); 356 if (sp != -1) { 357 String ver = l.substring(18,sp); 358 if (!ver.equals("0.3")) { 359 break; 360 } 361 foundCorrectVerNr = true; 362 } 363 } 364 } 365 } 366 } 367 } catch (FileNotFoundException e) { 368 // Silently create a new javac_state file. 369 noFileFound = true; 370 } catch (IOException e) { 371 Log.info("Dropping old javac_state because of errors when reading it."); 372 db = new JavacState(options, true, out, err); 373 foundCorrectVerNr = true; 374 newCommandLine = false; 375 syntaxError = false; 376 } 377 if (foundCorrectVerNr == false && !noFileFound) { 378 Log.info("Dropping old javac_state since it is of an old version."); 379 db = new JavacState(options, true, out, err); 380 } else 381 if (newCommandLine == true && !noFileFound) { 382 Log.info("Dropping old javac_state since a new command line is used!"); 383 db = new JavacState(options, true, out, err); 384 } else 385 if (syntaxError == true) { 386 Log.info("Dropping old javac_state since it contains syntax errors."); 387 db = new JavacState(options, true, out, err); 388 } 389 db.prev.calculateDependents(); 390 return db; 391 } 392 393 /** 394 * Mark a java package as tainted, ie it needs recompilation. 395 */ 396 public void taintPackage(String name, String because) { 397 if (!taintedPackages.contains(name)) { 398 if (because != null) Log.debug("Tainting "+Util.justPackageName(name)+" because "+because); 399 // It has not been tainted before. 400 taintedPackages.add(name); 401 needsSaving(); 402 Package nowp = now.packages().get(name); 403 if (nowp != null) { 404 for (String d : nowp.dependents()) { 405 taintPackage(d, because); 406 } 407 } 408 } 409 } 410 411 /** 412 * This packages need recompilation. 413 */ 414 public Set<String> taintedPackages() { 415 return taintedPackages; 416 } 417 418 /** 419 * Clean out the tainted package set, used after the first round of compiles, 420 * prior to propagating dependencies. 421 */ 422 public void clearTaintedPackages() { 423 taintedPackages = new HashSet<>(); 424 } 425 426 /** 427 * Go through all sources and check which have been removed, added or modified 428 * and taint the corresponding packages. 429 */ 430 public void checkSourceStatus(boolean check_gensrc) { 431 removedSources = calculateRemovedSources(); 432 for (Source s : removedSources) { 433 if (!s.isGenerated() || check_gensrc) { 434 taintPackage(s.pkg().name(), "source "+s.name()+" was removed"); 435 } 436 } 437 438 addedSources = calculateAddedSources(); 439 for (Source s : addedSources) { 440 String msg = null; 441 if (isIncremental()) { 442 // When building from scratch, there is no point 443 // printing "was added" for every file since all files are added. 444 // However for an incremental build it makes sense. 445 msg = "source "+s.name()+" was added"; 446 } 447 if (!s.isGenerated() || check_gensrc) { 448 taintPackage(s.pkg().name(), msg); 449 } 450 } 451 452 modifiedSources = calculateModifiedSources(); 453 for (Source s : modifiedSources) { 454 if (!s.isGenerated() || check_gensrc) { 455 taintPackage(s.pkg().name(), "source "+s.name()+" was modified"); 456 } 457 } 458 } 459 460 /** 461 * Acquire the compile_java_packages suffix rule for .java files. 462 */ 463 public Map<String,Transformer> getJavaSuffixRule() { 464 Map<String,Transformer> sr = new HashMap<>(); 465 sr.put(".java", compileJavaPackages); 466 return sr; 467 } 468 469 470 /** 471 * If artifacts have gone missing, force a recompile of the packages 472 * they belong to. 473 */ 474 public void taintPackagesThatMissArtifacts() { 475 for (Package pkg : prev.packages().values()) { 476 for (File f : pkg.artifacts().values()) { 477 if (!f.exists()) { 478 // Hmm, the artifact on disk does not exist! Someone has removed it.... 479 // Lets rebuild the package. 480 taintPackage(pkg.name(), ""+f+" is missing."); 481 } 482 } 483 } 484 } 485 486 /** 487 * Propagate recompilation through the dependency chains. 488 * Avoid re-tainting packages that have already been compiled. 489 */ 490 public void taintPackagesDependingOnChangedPackages(Set<String> pkgs, Set<String> recentlyCompiled) { 491 for (Package pkg : prev.packages().values()) { 492 for (String dep : pkg.dependencies()) { 493 if (pkgs.contains(dep) && !recentlyCompiled.contains(pkg.name())) { 494 taintPackage(pkg.name(), " its depending on "+dep); 495 } 496 } 497 } 498 } 499 500 /** 501 * Scan all output dirs for artifacts and remove those files (artifacts?) 502 * that are not recognized as such, in the javac_state file. 503 */ 504 public void removeUnidentifiedArtifacts() { 505 Set<File> allKnownArtifacts = new HashSet<>(); 506 for (Package pkg : prev.packages().values()) { 507 for (File f : pkg.artifacts().values()) { 508 allKnownArtifacts.add(f); 509 } 510 } 511 // Do not forget about javac_state.... 512 allKnownArtifacts.add(javacState); 513 514 for (File f : binArtifacts) { 515 if (!allKnownArtifacts.contains(f)) { 516 Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state."); 517 f.delete(); 518 } 519 } 520 for (File f : headerArtifacts) { 521 if (!allKnownArtifacts.contains(f)) { 522 Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state."); 523 f.delete(); 524 } 525 } 526 for (File f : gensrcArtifacts) { 527 if (!allKnownArtifacts.contains(f)) { 528 Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state."); 529 f.delete(); 530 } 531 } 532 } 533 534 /** 535 * Remove artifacts that are no longer produced when compiling! 536 */ 537 public void removeSuperfluousArtifacts(Set<String> recentlyCompiled) { 538 // Nothing to do, if nothing was recompiled. 539 if (recentlyCompiled.size() == 0) return; 540 541 for (String pkg : now.packages().keySet()) { 542 // If this package has not been recompiled, skip the check. 543 if (!recentlyCompiled.contains(pkg)) continue; 544 Collection<File> arts = now.artifacts().values(); 545 for (File f : fetchPrevArtifacts(pkg).values()) { 546 if (!arts.contains(f)) { 547 Log.debug("Removing "+f.getPath()+" since it is now superfluous!"); 548 if (f.exists()) f.delete(); 549 } 550 } 551 } 552 } 553 554 /** 555 * Return those files belonging to prev, but not now. 556 */ 557 private Set<Source> calculateRemovedSources() { 558 Set<Source> removed = new HashSet<>(); 559 for (String src : prev.sources().keySet()) { 560 if (now.sources().get(src) == null) { 561 removed.add(prev.sources().get(src)); 562 } 563 } 564 return removed; 565 } 566 567 /** 568 * Return those files belonging to now, but not prev. 569 */ 570 private Set<Source> calculateAddedSources() { 571 Set<Source> added = new HashSet<>(); 572 for (String src : now.sources().keySet()) { 573 if (prev.sources().get(src) == null) { 574 added.add(now.sources().get(src)); 575 } 576 } 577 return added; 578 } 579 580 /** 581 * Return those files where the timestamp is newer. 582 * If a source file timestamp suddenly is older than what is known 583 * about it in javac_state, then consider it modified, but print 584 * a warning! 585 */ 586 private Set<Source> calculateModifiedSources() { 587 Set<Source> modified = new HashSet<>(); 588 for (String src : now.sources().keySet()) { 589 Source n = now.sources().get(src); 590 Source t = prev.sources().get(src); 591 if (prev.sources().get(src) != null) { 592 if (t != null) { 593 if (n.lastModified() > t.lastModified()) { 594 modified.add(n); 595 } else if (n.lastModified() < t.lastModified()) { 596 modified.add(n); 597 Log.warn("The source file "+n.name()+" timestamp has moved backwards in time."); 598 } 599 } 600 } 601 } 602 return modified; 603 } 604 605 /** 606 * Recursively delete a directory and all its contents. 607 */ 608 private static void deleteContents(File dir) { 609 if (dir != null && dir.exists()) { 610 for (File f : dir.listFiles()) { 611 if (f.isDirectory()) { 612 deleteContents(f); 613 } 614 f.delete(); 615 } 616 } 617 } 618 619 /** 620 * Run the copy translator only. 621 */ 622 public void performCopying(File binDir, Map<String,Transformer> suffixRules) { 623 Map<String,Transformer> sr = new HashMap<>(); 624 for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) { 625 if (e.getValue().getClass().equals(CopyFile.class)) { 626 sr.put(e.getKey(), e.getValue()); 627 } 628 } 629 perform(null, binDir, sr); 630 } 631 632 /** 633 * Run all the translators that translate into java source code. 634 * I.e. all translators that are not copy nor compile_java_source. 635 */ 636 public void performTranslation(File gensrcDir, Map<String,Transformer> suffixRules) { 637 Map<String,Transformer> sr = new HashMap<>(); 638 for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) { 639 Class<?> trClass = e.getValue().getClass(); 640 if (trClass == CompileJavaPackages.class || trClass == CopyFile.class) 641 continue; 642 643 sr.put(e.getKey(), e.getValue()); 644 } 645 perform(null, gensrcDir, sr); 646 } 647 648 /** 649 * Compile all the java sources. Return true, if it needs to be called again! 650 */ 651 public boolean performJavaCompilations(JavacService javacService, 652 Options args, 653 Set<String> recentlyCompiled, 654 boolean[] rcValue) { 655 Map<String,Transformer> suffixRules = new HashMap<>(); 656 suffixRules.put(".java", compileJavaPackages); 657 compileJavaPackages.setExtra(args); 658 659 rcValue[0] = perform(javacService, binDir, suffixRules); 660 recentlyCompiled.addAll(taintedPackages()); 661 clearTaintedPackages(); 662 boolean again = !packagesWithChangedPublicApis.isEmpty(); 663 taintPackagesDependingOnChangedPackages(packagesWithChangedPublicApis, recentlyCompiled); 664 packagesWithChangedPublicApis = new HashSet<>(); 665 return again && rcValue[0]; 666 } 667 668 /** 669 * Store the source into the set of sources belonging to the given transform. 670 */ 671 private void addFileToTransform(Map<Transformer,Map<String,Set<URI>>> gs, Transformer t, Source s) { 672 Map<String,Set<URI>> fs = gs.get(t); 673 if (fs == null) { 674 fs = new HashMap<>(); 675 gs.put(t, fs); 676 } 677 Set<URI> ss = fs.get(s.pkg().name()); 678 if (ss == null) { 679 ss = new HashSet<>(); 680 fs.put(s.pkg().name(), ss); 681 } 682 ss.add(s.file().toURI()); 683 } 684 685 /** 686 * For all packages, find all sources belonging to the package, group the sources 687 * based on their transformers and apply the transformers on each source code group. 688 */ 689 private boolean perform(JavacService javacService, 690 File outputDir, 691 Map<String,Transformer> suffixRules) 692 { 693 boolean rc = true; 694 // Group sources based on transforms. A source file can only belong to a single transform. 695 Map<Transformer,Map<String,Set<URI>>> groupedSources = new HashMap<>(); 696 for (Source src : now.sources().values()) { 697 Transformer t = suffixRules.get(src.suffix()); 698 if (t != null) { 699 if (taintedPackages.contains(src.pkg().name()) && !src.isLinkedOnly()) { 700 addFileToTransform(groupedSources, t, src); 701 } 702 } 703 } 704 // Go through the transforms and transform them. 705 for (Map.Entry<Transformer,Map<String,Set<URI>>> e : groupedSources.entrySet()) { 706 Transformer t = e.getKey(); 707 Map<String,Set<URI>> srcs = e.getValue(); 708 // These maps need to be synchronized since multiple threads will be writing results into them. 709 Map<String,Set<URI>> packageArtifacts = 710 Collections.synchronizedMap(new HashMap<String,Set<URI>>()); 711 Map<String,Set<String>> packageDependencies = 712 Collections.synchronizedMap(new HashMap<String,Set<String>>()); 713 Map<String,String> packagePublicApis = 714 Collections.synchronizedMap(new HashMap<String, String>()); 715 716 boolean r = t.transform(javacService, 717 srcs, 718 visibleSrcs, 719 visibleClasses, 720 prev.dependents(), 721 outputDir.toURI(), 722 packageArtifacts, 723 packageDependencies, 724 packagePublicApis, 725 0, 726 isIncremental(), 727 numCores, 728 out, 729 err); 730 if (!r) rc = false; 731 732 for (String p : srcs.keySet()) { 733 recompiledPackages.add(p); 734 } 735 // The transform is done! Extract all the artifacts and store the info into the Package objects. 736 for (Map.Entry<String,Set<URI>> a : packageArtifacts.entrySet()) { 737 Module mnow = now.findModuleFromPackageName(a.getKey()); 738 mnow.addArtifacts(a.getKey(), a.getValue()); 739 } 740 // Extract all the dependencies and store the info into the Package objects. 741 for (Map.Entry<String,Set<String>> a : packageDependencies.entrySet()) { 742 Set<String> deps = a.getValue(); 743 Module mnow = now.findModuleFromPackageName(a.getKey()); 744 mnow.setDependencies(a.getKey(), deps); 745 } 746 // Extract all the pubapis and store the info into the Package objects. 747 for (Map.Entry<String,String> a : packagePublicApis.entrySet()) { 748 Module mprev = prev.findModuleFromPackageName(a.getKey()); 749 List<String> pubapi = Package.pubapiToList(a.getValue()); 750 Module mnow = now.findModuleFromPackageName(a.getKey()); 751 mnow.setPubapi(a.getKey(), pubapi); 752 if (mprev.hasPubapiChanged(a.getKey(), pubapi)) { 753 // Aha! The pubapi of this package has changed! 754 // It can also be a new compile from scratch. 755 if (mprev.lookupPackage(a.getKey()).existsInJavacState()) { 756 // This is an incremental compile! The pubapi 757 // did change. Trigger recompilation of dependents. 758 packagesWithChangedPublicApis.add(a.getKey()); 759 Log.info("The pubapi of "+Util.justPackageName(a.getKey())+" has changed!"); 760 } 761 } 762 } 763 } 764 return rc; 765 } 766 767 /** 768 * Utility method to recursively find all files below a directory. 769 */ 770 private static Set<File> findAllFiles(File dir) { 771 Set<File> foundFiles = new HashSet<>(); 772 if (dir == null) { 773 return foundFiles; 774 } 775 recurse(dir, foundFiles); 776 return foundFiles; 777 } 778 779 private static void recurse(File dir, Set<File> foundFiles) { 780 for (File f : dir.listFiles()) { 781 if (f.isFile()) { 782 foundFiles.add(f); 783 } else if (f.isDirectory()) { 784 recurse(f, foundFiles); 785 } 786 } 787 } 788 789 /** 790 * Compare the calculate source list, with an explicit list, usually supplied from the makefile. 791 * Used to detect bugs where the makefile and sjavac have different opinions on which files 792 * should be compiled. 793 */ 794 public void compareWithMakefileList(File makefileSourceList) 795 throws ProblemException 796 { 797 // If we are building on win32 using for example cygwin the paths in the makefile source list 798 // might be /cygdrive/c/.... which does not match c:\.... 799 // We need to adjust our calculated sources to be identical, if necessary. 800 boolean mightNeedRewriting = File.pathSeparatorChar == ';'; 801 802 if (makefileSourceList == null) return; 803 804 Set<String> calculatedSources = new HashSet<>(); 805 Set<String> listedSources = new HashSet<>(); 806 807 // Create a set of filenames with full paths. 808 for (Source s : now.sources().values()) { 809 // Don't include link only sources when comparing sources to compile 810 if (!s.isLinkedOnly()) { 811 String path = s.file().getPath(); 812 if (mightNeedRewriting) 813 path = Util.normalizeDriveLetter(path); 814 calculatedSources.add(path); 815 } 816 } 817 // Read in the file and create another set of filenames with full paths. 818 try { 819 BufferedReader in = new BufferedReader(new FileReader(makefileSourceList)); 820 for (;;) { 821 String l = in.readLine(); 822 if (l==null) break; 823 l = l.trim(); 824 if (mightNeedRewriting) { 825 if (l.indexOf(":") == 1 && l.indexOf("\\") == 2) { 826 // Everything a-ok, the format is already C:\foo\bar 827 } else if (l.indexOf(":") == 1 && l.indexOf("/") == 2) { 828 // The format is C:/foo/bar, rewrite into the above format. 829 l = l.replaceAll("/","\\\\"); 830 } else if (l.charAt(0) == '/' && l.indexOf("/",1) != -1) { 831 // The format might be: /cygdrive/c/foo/bar, rewrite into the above format. 832 // Do not hardcode the name cygdrive here. 833 int slash = l.indexOf("/",1); 834 l = l.replaceAll("/","\\\\"); 835 l = ""+l.charAt(slash+1)+":"+l.substring(slash+2); 836 } 837 if (Character.isLowerCase(l.charAt(0))) { 838 l = Character.toUpperCase(l.charAt(0))+l.substring(1); 839 } 840 } 841 listedSources.add(l); 842 } 843 } catch (FileNotFoundException e) { 844 throw new ProblemException("Could not open "+makefileSourceList.getPath()+" since it does not exist!"); 845 } catch (IOException e) { 846 throw new ProblemException("Could not read "+makefileSourceList.getPath()); 847 } 848 849 for (String s : listedSources) { 850 if (!calculatedSources.contains(s)) { 851 throw new ProblemException("The makefile listed source "+s+" was not calculated by the smart javac wrapper!"); 852 } 853 } 854 855 for (String s : calculatedSources) { 856 if (!listedSources.contains(s)) { 857 throw new ProblemException("The smart javac wrapper calculated source "+s+" was not listed by the makefiles!"); 858 } 859 } 860 } 861} 862