JavacState.java revision 2675:4be0e35f385a
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.util.Collections;
30import java.util.Date;
31import java.util.Set;
32import java.util.HashSet;
33import java.util.List;
34import java.util.Map;
35import java.util.HashMap;
36import java.text.SimpleDateFormat;
37import java.net.URI;
38import java.util.*;
39
40import com.sun.tools.sjavac.options.Options;
41import com.sun.tools.sjavac.server.Sjavac;
42
43/**
44 * The javac state class maintains the previous (prev) and the current (now)
45 * build states and everything else that goes into the javac_state file.
46 *
47 *  <p><b>This is NOT part of any supported API.
48 *  If you write code that depends on this, you do so at your own risk.
49 *  This code and its internal interfaces are subject to change or
50 *  deletion without notice.</b>
51 */
52public class JavacState {
53    // The arguments to the compile. If not identical, then it cannot
54    // be an incremental build!
55    String theArgs;
56    // The number of cores limits how many threads are used for heavy concurrent work.
57    int numCores;
58
59    // The bin_dir/javac_state
60    private File javacState;
61
62    // The previous build state is loaded from javac_state
63    private BuildState prev;
64    // The current build state is constructed during the build,
65    // then saved as the new javac_state.
66    private BuildState now;
67
68    // Something has changed in the javac_state. It needs to be saved!
69    private boolean needsSaving;
70    // If this is a new javac_state file, then do not print unnecessary messages.
71    private boolean newJavacState;
72
73    // These are packages where something has changed and the package
74    // needs to be recompiled. Actions that trigger recompilation:
75    // * source belonging to the package has changed
76    // * artifact belonging to the package is lost, or its timestamp has been changed.
77    // * an unknown artifact has appeared, we simply delete it, but we also trigger a recompilation.
78    // * a package that is tainted, taints all packages that depend on it.
79    private Set<String> taintedPackages;
80    // After a compile, the pubapis are compared with the pubapis stored in the javac state file.
81    // Any packages where the pubapi differ are added to this set.
82    // Later we use this set and the dependency information to taint dependent packages.
83    private Set<String> packagesWithChangedPublicApis;
84    // When a module-info.java file is changed, taint the module,
85    // then taint all modules that depend on that that module.
86    // A module dependency can occur directly through a require, or
87    // indirectly through a module that does a public export for the first tainted module.
88    // When all modules are tainted, then taint all packages belonging to these modules.
89    // Then rebuild. It is perhaps possible (and valuable?) to do a more finegrained examination of the
90    // change in module-info.java, but that will have to wait.
91    private Set<String> taintedModules;
92    // The set of all packages that has been recompiled.
93    // Copy over the javac_state for the packages that did not need recompilation,
94    // verbatim from the previous (prev) to the new (now) build state.
95    private Set<String> recompiledPackages;
96
97    // The output directories filled with tasty artifacts.
98    private File binDir, gensrcDir, headerDir, stateDir;
99
100    // The current status of the file system.
101    private Set<File> binArtifacts;
102    private Set<File> gensrcArtifacts;
103    private Set<File> headerArtifacts;
104
105    // The status of the sources.
106    Set<Source> removedSources = null;
107    Set<Source> addedSources = null;
108    Set<Source> modifiedSources = null;
109
110    // Visible sources for linking. These are the only
111    // ones that -sourcepath is allowed to see.
112    Set<URI> visibleSrcs;
113
114    // Visible classes for linking. These are the only
115    // ones that -classpath is allowed to see.
116    // It maps from a classpath root to the set of visible classes for that root.
117    // If the set is empty, then all classes are visible for that root.
118    // It can also map from a jar file to the set of visible classes for that jar file.
119    Map<URI,Set<String>> visibleClasses;
120
121    // Setup transform that always exist.
122    private CompileJavaPackages compileJavaPackages = new CompileJavaPackages();
123
124    // Where to send stdout and stderr.
125    private PrintStream out, err;
126
127    // Command line options.
128    private Options options;
129
130    JavacState(Options op, boolean removeJavacState, PrintStream o, PrintStream e) {
131        options = op;
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        stateDir = Util.pathToFile(options.getStateDir());
140        javacState = new File(stateDir, "javac_state");
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.areUnidentifiedArtifactsPermitted()) {
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(javacState)) {
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.javacState))) {
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                !options.isUnidentifiedArtifactPermitted(f.getAbsolutePath())) {
517                Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
518                f.delete();
519            }
520        }
521        for (File f : headerArtifacts) {
522            if (!allKnownArtifacts.contains(f)) {
523                Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
524                f.delete();
525            }
526        }
527        for (File f : gensrcArtifacts) {
528            if (!allKnownArtifacts.contains(f)) {
529                Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
530                f.delete();
531            }
532        }
533    }
534
535    /**
536     * Remove artifacts that are no longer produced when compiling!
537     */
538    public void removeSuperfluousArtifacts(Set<String> recentlyCompiled) {
539        // Nothing to do, if nothing was recompiled.
540        if (recentlyCompiled.size() == 0) return;
541
542        for (String pkg : now.packages().keySet()) {
543            // If this package has not been recompiled, skip the check.
544            if (!recentlyCompiled.contains(pkg)) continue;
545            Collection<File> arts = now.artifacts().values();
546            for (File f : fetchPrevArtifacts(pkg).values()) {
547                if (!arts.contains(f)) {
548                    Log.debug("Removing "+f.getPath()+" since it is now superfluous!");
549                    if (f.exists()) f.delete();
550                }
551            }
552        }
553    }
554
555    /**
556     * Return those files belonging to prev, but not now.
557     */
558    private Set<Source> calculateRemovedSources() {
559        Set<Source> removed = new HashSet<>();
560        for (String src : prev.sources().keySet()) {
561            if (now.sources().get(src) == null) {
562                removed.add(prev.sources().get(src));
563            }
564        }
565        return removed;
566    }
567
568    /**
569     * Return those files belonging to now, but not prev.
570     */
571    private Set<Source> calculateAddedSources() {
572        Set<Source> added = new HashSet<>();
573        for (String src : now.sources().keySet()) {
574            if (prev.sources().get(src) == null) {
575                added.add(now.sources().get(src));
576            }
577        }
578        return added;
579    }
580
581    /**
582     * Return those files where the timestamp is newer.
583     * If a source file timestamp suddenly is older than what is known
584     * about it in javac_state, then consider it modified, but print
585     * a warning!
586     */
587    private Set<Source> calculateModifiedSources() {
588        Set<Source> modified = new HashSet<>();
589        for (String src : now.sources().keySet()) {
590            Source n = now.sources().get(src);
591            Source t = prev.sources().get(src);
592            if (prev.sources().get(src) != null) {
593                if (t != null) {
594                    if (n.lastModified() > t.lastModified()) {
595                        modified.add(n);
596                    } else if (n.lastModified() < t.lastModified()) {
597                        modified.add(n);
598                        Log.warn("The source file "+n.name()+" timestamp has moved backwards in time.");
599                    }
600                }
601            }
602        }
603        return modified;
604    }
605
606    /**
607     * Recursively delete a directory and all its contents.
608     */
609    private void deleteContents(File dir) {
610        if (dir != null && dir.exists()) {
611            for (File f : dir.listFiles()) {
612                if (f.isDirectory()) {
613                    deleteContents(f);
614                }
615                if (!options.isUnidentifiedArtifactPermitted(f.getAbsolutePath())) {
616                    Log.debug("Removing "+f.getAbsolutePath());
617                    f.delete();
618                }
619            }
620        }
621    }
622
623    /**
624     * Run the copy translator only.
625     */
626    public void performCopying(File binDir, Map<String,Transformer> suffixRules) {
627        Map<String,Transformer> sr = new HashMap<>();
628        for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
629            if (e.getValue().getClass().equals(CopyFile.class)) {
630                sr.put(e.getKey(), e.getValue());
631            }
632        }
633        perform(null, binDir, sr);
634    }
635
636    /**
637     * Run all the translators that translate into java source code.
638     * I.e. all translators that are not copy nor compile_java_source.
639     */
640    public void performTranslation(File gensrcDir, Map<String,Transformer> suffixRules) {
641        Map<String,Transformer> sr = new HashMap<>();
642        for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
643            Class<?> trClass = e.getValue().getClass();
644            if (trClass == CompileJavaPackages.class || trClass == CopyFile.class)
645                continue;
646
647            sr.put(e.getKey(), e.getValue());
648        }
649        perform(null, gensrcDir, sr);
650    }
651
652    /**
653     * Compile all the java sources. Return true, if it needs to be called again!
654     */
655    public boolean performJavaCompilations(Sjavac sjavac,
656                                           Options args,
657                                           Set<String> recentlyCompiled,
658                                           boolean[] rcValue) {
659        Map<String,Transformer> suffixRules = new HashMap<>();
660        suffixRules.put(".java", compileJavaPackages);
661        compileJavaPackages.setExtra(args);
662
663        rcValue[0] = perform(sjavac, binDir, suffixRules);
664        recentlyCompiled.addAll(taintedPackages());
665        clearTaintedPackages();
666        boolean again = !packagesWithChangedPublicApis.isEmpty();
667        taintPackagesDependingOnChangedPackages(packagesWithChangedPublicApis, recentlyCompiled);
668        packagesWithChangedPublicApis = new HashSet<>();
669        return again && rcValue[0];
670    }
671
672    /**
673     * Store the source into the set of sources belonging to the given transform.
674     */
675    private void addFileToTransform(Map<Transformer,Map<String,Set<URI>>> gs, Transformer t, Source s) {
676        Map<String,Set<URI>> fs = gs.get(t);
677        if (fs == null) {
678            fs = new HashMap<>();
679            gs.put(t, fs);
680        }
681        Set<URI> ss = fs.get(s.pkg().name());
682        if (ss == null) {
683            ss = new HashSet<>();
684            fs.put(s.pkg().name(), ss);
685        }
686        ss.add(s.file().toURI());
687    }
688
689    /**
690     * For all packages, find all sources belonging to the package, group the sources
691     * based on their transformers and apply the transformers on each source code group.
692     */
693    private boolean perform(Sjavac sjavac,
694                            File outputDir,
695                            Map<String,Transformer> suffixRules) {
696        boolean rc = true;
697        // Group sources based on transforms. A source file can only belong to a single transform.
698        Map<Transformer,Map<String,Set<URI>>> groupedSources = new HashMap<>();
699        for (Source src : now.sources().values()) {
700            Transformer t = suffixRules.get(src.suffix());
701               if (t != null) {
702                if (taintedPackages.contains(src.pkg().name()) && !src.isLinkedOnly()) {
703                    addFileToTransform(groupedSources, t, src);
704                }
705            }
706        }
707        // Go through the transforms and transform them.
708        for (Map.Entry<Transformer,Map<String,Set<URI>>> e : groupedSources.entrySet()) {
709            Transformer t = e.getKey();
710            Map<String,Set<URI>> srcs = e.getValue();
711            // These maps need to be synchronized since multiple threads will be writing results into them.
712            Map<String,Set<URI>> packageArtifacts =
713                    Collections.synchronizedMap(new HashMap<String,Set<URI>>());
714            Map<String,Set<String>> packageDependencies =
715                    Collections.synchronizedMap(new HashMap<String,Set<String>>());
716            Map<String,String> packagePublicApis =
717                    Collections.synchronizedMap(new HashMap<String, String>());
718
719            boolean  r = t.transform(sjavac,
720                                     srcs,
721                                     visibleSrcs,
722                                     visibleClasses,
723                                     prev.dependents(),
724                                     outputDir.toURI(),
725                                     packageArtifacts,
726                                     packageDependencies,
727                                     packagePublicApis,
728                                     0,
729                                     isIncremental(),
730                                     numCores,
731                                     out,
732                                     err);
733            if (!r) rc = false;
734
735            for (String p : srcs.keySet()) {
736                recompiledPackages.add(p);
737            }
738            // The transform is done! Extract all the artifacts and store the info into the Package objects.
739            for (Map.Entry<String,Set<URI>> a : packageArtifacts.entrySet()) {
740                Module mnow = now.findModuleFromPackageName(a.getKey());
741                mnow.addArtifacts(a.getKey(), a.getValue());
742            }
743            // Extract all the dependencies and store the info into the Package objects.
744            for (Map.Entry<String,Set<String>> a : packageDependencies.entrySet()) {
745                Set<String> deps = a.getValue();
746                Module mnow = now.findModuleFromPackageName(a.getKey());
747                mnow.setDependencies(a.getKey(), deps);
748            }
749            // Extract all the pubapis and store the info into the Package objects.
750            for (Map.Entry<String,String> a : packagePublicApis.entrySet()) {
751                Module mprev = prev.findModuleFromPackageName(a.getKey());
752                List<String> pubapi = Package.pubapiToList(a.getValue());
753                Module mnow = now.findModuleFromPackageName(a.getKey());
754                mnow.setPubapi(a.getKey(), pubapi);
755                if (mprev.hasPubapiChanged(a.getKey(), pubapi)) {
756                    // Aha! The pubapi of this package has changed!
757                    // It can also be a new compile from scratch.
758                    if (mprev.lookupPackage(a.getKey()).existsInJavacState()) {
759                        // This is an incremental compile! The pubapi
760                        // did change. Trigger recompilation of dependents.
761                        packagesWithChangedPublicApis.add(a.getKey());
762                        Log.info("The pubapi of "+Util.justPackageName(a.getKey())+" has changed!");
763                    }
764                }
765            }
766        }
767        return rc;
768    }
769
770    /**
771     * Utility method to recursively find all files below a directory.
772     */
773    private static Set<File> findAllFiles(File dir) {
774        Set<File> foundFiles = new HashSet<>();
775        if (dir == null) {
776            return foundFiles;
777        }
778        recurse(dir, foundFiles);
779        return foundFiles;
780    }
781
782    private static void recurse(File dir, Set<File> foundFiles) {
783        for (File f : dir.listFiles()) {
784            if (f.isFile()) {
785                foundFiles.add(f);
786            } else if (f.isDirectory()) {
787                recurse(f, foundFiles);
788            }
789        }
790    }
791
792    /**
793     * Compare the calculate source list, with an explicit list, usually supplied from the makefile.
794     * Used to detect bugs where the makefile and sjavac have different opinions on which files
795     * should be compiled.
796     */
797    public void compareWithMakefileList(File makefileSourceList) throws ProblemException {
798        // If we are building on win32 using for example cygwin the paths in the makefile source list
799        // might be /cygdrive/c/.... which does not match c:\....
800        // We need to adjust our calculated sources to be identical, if necessary.
801        boolean mightNeedRewriting = File.pathSeparatorChar == ';';
802
803        if (makefileSourceList == null) return;
804
805        Set<String> calculatedSources = new HashSet<>();
806        Set<String> listedSources = new HashSet<>();
807
808        // Create a set of filenames with full paths.
809        for (Source s : now.sources().values()) {
810            // Don't include link only sources when comparing sources to compile
811            if (!s.isLinkedOnly()) {
812                String path = s.file().getPath();
813                if (mightNeedRewriting)
814                    path = Util.normalizeDriveLetter(path);
815                calculatedSources.add(path);
816            }
817        }
818        // Read in the file and create another set of filenames with full paths.
819        try {
820            BufferedReader in = new BufferedReader(new FileReader(makefileSourceList));
821            for (;;) {
822                String l = in.readLine();
823                if (l==null) break;
824                l = l.trim();
825                if (mightNeedRewriting) {
826                    if (l.indexOf(":") == 1 && l.indexOf("\\") == 2) {
827                        // Everything a-ok, the format is already C:\foo\bar
828                    } else if (l.indexOf(":") == 1 && l.indexOf("/") == 2) {
829                        // The format is C:/foo/bar, rewrite into the above format.
830                        l = l.replaceAll("/","\\\\");
831                    } else if (l.charAt(0) == '/' && l.indexOf("/",1) != -1) {
832                        // The format might be: /cygdrive/c/foo/bar, rewrite into the above format.
833                        // Do not hardcode the name cygdrive here.
834                        int slash = l.indexOf("/",1);
835                        l = l.replaceAll("/","\\\\");
836                        l = ""+l.charAt(slash+1)+":"+l.substring(slash+2);
837                    }
838                    if (Character.isLowerCase(l.charAt(0))) {
839                        l = Character.toUpperCase(l.charAt(0))+l.substring(1);
840                    }
841                }
842                listedSources.add(l);
843            }
844        } catch (FileNotFoundException e) {
845            throw new ProblemException("Could not open "+makefileSourceList.getPath()+" since it does not exist!");
846        } catch (IOException e) {
847            throw new ProblemException("Could not read "+makefileSourceList.getPath());
848        }
849
850        for (String s : listedSources) {
851            if (!calculatedSources.contains(s)) {
852                 throw new ProblemException("The makefile listed source "+s+" was not calculated by the smart javac wrapper!");
853            }
854        }
855
856        for (String s : calculatedSources) {
857            if (!listedSources.contains(s)) {
858                throw new ProblemException("The smart javac wrapper calculated source "+s+" was not listed by the makefiles!");
859            }
860        }
861    }
862}
863