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