ohrstrom@1504: /*
ohrstrom@1504: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504: *
ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504: *
ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504: * accompanied this code).
ohrstrom@1504: *
ohrstrom@1504: * You should have received a copy of the GNU General Public License version
ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504: *
ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504: * questions.
ohrstrom@1504: */
ohrstrom@1504:
ohrstrom@1504: package com.sun.tools.sjavac;
ohrstrom@1504:
ohrstrom@1504: import java.io.*;
ohrstrom@1504: import java.util.Collections;
ohrstrom@1504: import java.util.Date;
ohrstrom@1504: import java.util.Set;
ohrstrom@1504: import java.util.HashSet;
ohrstrom@1504: import java.util.List;
ohrstrom@1504: import java.util.Map;
ohrstrom@1504: import java.util.HashMap;
ohrstrom@1504: import java.text.SimpleDateFormat;
ohrstrom@1504: import java.net.URI;
ohrstrom@1504: import java.util.*;
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * The javac state class maintains the previous (prev) and the current (now)
ohrstrom@1504: * build states and everything else that goes into the javac_state file.
ohrstrom@1504: *
ohrstrom@1504: *
This is NOT part of any supported API.
ohrstrom@1504: * If you write code that depends on this, you do so at your own
ohrstrom@1504: * risk. This code and its internal interfaces are subject to change
ohrstrom@1504: * or deletion without notice.
ohrstrom@1504: */
ohrstrom@1504: public class JavacState
ohrstrom@1504: {
ohrstrom@1504: // The arguments to the compile. If not identical, then it cannot
ohrstrom@1504: // be an incremental build!
ohrstrom@1504: String theArgs;
ohrstrom@1504: // The number of cores limits how many threads are used for heavy concurrent work.
ohrstrom@1504: int numCores;
ohrstrom@1504:
ohrstrom@1504: // The bin_dir/javac_state
ohrstrom@1504: private String javacStateFilename;
ohrstrom@1504: private File javacState;
ohrstrom@1504:
ohrstrom@1504: // The previous build state is loaded from javac_state
ohrstrom@1504: private BuildState prev;
ohrstrom@1504: // The current build state is constructed during the build,
ohrstrom@1504: // then saved as the new javac_state.
ohrstrom@1504: private BuildState now;
ohrstrom@1504:
ohrstrom@1504: // Something has changed in the javac_state. It needs to be saved!
ohrstrom@1504: private boolean needsSaving;
ohrstrom@1504: // If this is a new javac_state file, then do not print unnecessary messages.
ohrstrom@1504: private boolean newJavacState;
ohrstrom@1504:
ohrstrom@1504: // These are packages where something has changed and the package
ohrstrom@1504: // needs to be recompiled. Actions that trigger recompilation:
ohrstrom@1504: // * source belonging to the package has changed
ohrstrom@1504: // * artifact belonging to the package is lost, or its timestamp has been changed.
ohrstrom@1504: // * an unknown artifact has appeared, we simply delete it, but we also trigger a recompilation.
ohrstrom@1504: // * a package that is tainted, taints all packages that depend on it.
ohrstrom@1504: private Set taintedPackages;
ohrstrom@1504: // After a compile, the pubapis are compared with the pubapis stored in the javac state file.
ohrstrom@1504: // Any packages where the pubapi differ are added to this set.
ohrstrom@1504: // Later we use this set and the dependency information to taint dependent packages.
ohrstrom@1504: private Set packagesWithChangedPublicApis;
ohrstrom@1504: // When a module-info.java file is changed, taint the module,
ohrstrom@1504: // then taint all modules that depend on that that module.
ohrstrom@1504: // A module dependency can occur directly through a require, or
ohrstrom@1504: // indirectly through a module that does a public export for the first tainted module.
ohrstrom@1504: // When all modules are tainted, then taint all packages belonging to these modules.
ohrstrom@1504: // Then rebuild. It is perhaps possible (and valuable?) to do a more finegrained examination of the
ohrstrom@1504: // change in module-info.java, but that will have to wait.
ohrstrom@1504: private Set taintedModules;
ohrstrom@1504: // The set of all packages that has been recompiled.
ohrstrom@1504: // Copy over the javac_state for the packages that did not need recompilation,
ohrstrom@1504: // verbatim from the previous (prev) to the new (now) build state.
ohrstrom@1504: private Set recompiledPackages;
ohrstrom@1504:
ohrstrom@1504: // The output directories filled with tasty artifacts.
ohrstrom@1504: private File binDir, gensrcDir, headerDir;
ohrstrom@1504:
ohrstrom@1504: // The current status of the file system.
ohrstrom@1504: private Set binArtifacts;
ohrstrom@1504: private Set gensrcArtifacts;
ohrstrom@1504: private Set headerArtifacts;
ohrstrom@1504:
ohrstrom@1504: // The status of the sources.
ohrstrom@1504: Set removedSources = null;
ohrstrom@1504: Set addedSources = null;
ohrstrom@1504: Set modifiedSources = null;
ohrstrom@1504:
ohrstrom@1504: // Visible sources for linking. These are the only
ohrstrom@1504: // ones that -sourcepath is allowed to see.
ohrstrom@1504: Set visibleSrcs;
ohrstrom@1504:
ohrstrom@1504: // Visible classes for linking. These are the only
ohrstrom@1504: // ones that -classpath is allowed to see.
ohrstrom@1504: // It maps from a classpath root to the set of visible classes for that root.
ohrstrom@1504: // If the set is empty, then all classes are visible for that root.
ohrstrom@1504: // It can also map from a jar file to the set of visible classes for that jar file.
ohrstrom@1504: Map> visibleClasses;
ohrstrom@1504:
ohrstrom@1504: // Setup two transforms that always exist.
ohrstrom@1504: private CopyFile copyFiles = new CopyFile();
ohrstrom@1504: private CompileJavaPackages compileJavaPackages = new CompileJavaPackages();
ohrstrom@1504:
ohrstrom@1504: // Where to send stdout and stderr.
ohrstrom@1504: private PrintStream out, err;
ohrstrom@1504:
ohrstrom@1504: JavacState(String[] args, File bd, File gd, File hd, boolean permitUnidentifiedArtifacts, boolean removeJavacState,
ohrstrom@1504: PrintStream o, PrintStream e) {
ohrstrom@1504: out = o;
ohrstrom@1504: err = e;
ohrstrom@1504: numCores = Main.findNumberOption(args, "-j");
ohrstrom@1504: theArgs = "";
ohrstrom@1504: for (String a : removeArgsNotAffectingState(args)) {
ohrstrom@1504: theArgs = theArgs+a+" ";
ohrstrom@1504: }
ohrstrom@1504: binDir = bd;
ohrstrom@1504: gensrcDir = gd;
ohrstrom@1504: headerDir = hd;
ohrstrom@1504: javacStateFilename = binDir.getPath()+File.separator+"javac_state";
ohrstrom@1504: javacState = new File(javacStateFilename);
ohrstrom@1504: if (removeJavacState && javacState.exists()) {
ohrstrom@1504: javacState.delete();
ohrstrom@1504: }
ohrstrom@1504: newJavacState = false;
ohrstrom@1504: if (!javacState.exists()) {
ohrstrom@1504: newJavacState = true;
ohrstrom@1504: // If there is no javac_state then delete the contents of all the artifact dirs!
ohrstrom@1504: // We do not want to risk building a broken incremental build.
ohrstrom@1504: // BUT since the makefiles still copy things straight into the bin_dir et al,
ohrstrom@1504: // we avoid deleting files here, if the option --permit-unidentified-classes was supplied.
ohrstrom@1504: if (!permitUnidentifiedArtifacts) {
ohrstrom@1504: deleteContents(binDir);
ohrstrom@1504: deleteContents(gensrcDir);
ohrstrom@1504: deleteContents(headerDir);
ohrstrom@1504: }
ohrstrom@1504: needsSaving = true;
ohrstrom@1504: }
ohrstrom@1504: prev = new BuildState();
ohrstrom@1504: now = new BuildState();
ohrstrom@1504: taintedPackages = new HashSet();
ohrstrom@1504: recompiledPackages = new HashSet();
ohrstrom@1504: packagesWithChangedPublicApis = new HashSet();
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: public BuildState prev() { return prev; }
ohrstrom@1504: public BuildState now() { return now; }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Remove args not affecting the state.
ohrstrom@1504: */
ohrstrom@1504: static String[] removeArgsNotAffectingState(String[] args) {
ohrstrom@1504: String[] out = new String[args.length];
ohrstrom@1504: int j = 0;
ohrstrom@1504: for (int i = 0; i vs) {
ohrstrom@1504: visibleSrcs = new HashSet();
ohrstrom@1504: for (String s : vs.keySet()) {
ohrstrom@1504: Source src = vs.get(s);
ohrstrom@1504: visibleSrcs.add(src.file().toURI());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Specify which classes are visible to the compiler through -classpath.
ohrstrom@1504: */
ohrstrom@1504: public void setVisibleClasses(Map vs) {
ohrstrom@1504: visibleSrcs = new HashSet();
ohrstrom@1504: for (String s : vs.keySet()) {
ohrstrom@1504: Source src = vs.get(s);
ohrstrom@1504: visibleSrcs.add(src.file().toURI());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: /**
ohrstrom@1504: * Returns true if this is an incremental build.
ohrstrom@1504: */
ohrstrom@1504: public boolean isIncremental() {
ohrstrom@1504: return !prev.sources().isEmpty();
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Find all artifacts that exists on disk.
ohrstrom@1504: */
ohrstrom@1504: public void findAllArtifacts() {
ohrstrom@1504: binArtifacts = findAllFiles(binDir);
ohrstrom@1504: gensrcArtifacts = findAllFiles(gensrcDir);
ohrstrom@1504: headerArtifacts = findAllFiles(headerDir);
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Lookup the artifacts generated for this package in the previous build.
ohrstrom@1504: */
ohrstrom@1504: private Map fetchPrevArtifacts(String pkg) {
ohrstrom@1504: Package p = prev.packages().get(pkg);
ohrstrom@1504: if (p != null) {
ohrstrom@1504: return p.artifacts();
ohrstrom@1504: }
ohrstrom@1504: return new HashMap();
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Delete all prev artifacts in the currently tainted packages.
ohrstrom@1504: */
ohrstrom@1504: public void deleteClassArtifactsInTaintedPackages() {
ohrstrom@1504: for (String pkg : taintedPackages) {
ohrstrom@1504: Map arts = fetchPrevArtifacts(pkg);
ohrstrom@1504: for (File f : arts.values()) {
ohrstrom@1504: if (f.exists() && f.getName().endsWith(".class")) {
ohrstrom@1504: f.delete();
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Mark the javac_state file to be in need of saving and as a side effect,
ohrstrom@1504: * it gets a new timestamp.
ohrstrom@1504: */
ohrstrom@1504: private void needsSaving() {
ohrstrom@1504: needsSaving = true;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Save the javac_state file.
ohrstrom@1504: */
ohrstrom@1504: public void save() throws IOException {
ohrstrom@1504: if (!needsSaving) return;
ohrstrom@1504: try (FileWriter out = new FileWriter(javacStateFilename)) {
ohrstrom@1504: StringBuilder b = new StringBuilder();
ohrstrom@1504: long millisNow = System.currentTimeMillis();
ohrstrom@1504: Date d = new Date(millisNow);
ohrstrom@1504: SimpleDateFormat df =
ohrstrom@1504: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
ohrstrom@1504: b.append("# javac_state ver 0.3 generated "+millisNow+" "+df.format(d)+"\n");
ohrstrom@1504: b.append("# This format might change at any time. Please do not depend on it.\n");
ohrstrom@1504: b.append("# M module\n");
ohrstrom@1504: b.append("# P package\n");
ohrstrom@1504: b.append("# S C source_tobe_compiled timestamp\n");
ohrstrom@1504: b.append("# S L link_only_source timestamp\n");
ohrstrom@1504: b.append("# G C generated_source timestamp\n");
ohrstrom@1504: b.append("# A artifact timestamp\n");
ohrstrom@1504: b.append("# D dependency\n");
ohrstrom@1504: b.append("# I pubapi\n");
ohrstrom@1504: b.append("# R arguments\n");
ohrstrom@1504: b.append("R ").append(theArgs).append("\n");
ohrstrom@1504:
ohrstrom@1504: // Copy over the javac_state for the packages that did not need recompilation.
ohrstrom@1504: now.copyPackagesExcept(prev, recompiledPackages, new HashSet());
ohrstrom@1504: // Save the packages, ie package names, dependencies, pubapis and artifacts!
ohrstrom@1504: // I.e. the lot.
ohrstrom@1504: Module.saveModules(now.modules(), b);
ohrstrom@1504:
ohrstrom@1504: String s = b.toString();
ohrstrom@1504: out.write(s, 0, s.length());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Load a javac_state file.
ohrstrom@1504: */
ohrstrom@1504: public static JavacState load(String[] args, File binDir, File gensrcDir, File headerDir,
ohrstrom@1504: boolean permitUnidentifiedArtifacts, PrintStream out, PrintStream err) {
ohrstrom@1504: JavacState db = new JavacState(args, binDir, gensrcDir, headerDir, permitUnidentifiedArtifacts, false, out, err);
ohrstrom@1504: Module lastModule = null;
ohrstrom@1504: Package lastPackage = null;
ohrstrom@1504: Source lastSource = null;
ohrstrom@1504: boolean noFileFound = false;
ohrstrom@1504: boolean foundCorrectVerNr = false;
ohrstrom@1504: boolean newCommandLine = false;
ohrstrom@1504: boolean syntaxError = false;
ohrstrom@1504:
ohrstrom@1504: try (BufferedReader in = new BufferedReader(new FileReader(db.javacStateFilename))) {
ohrstrom@1504: for (;;) {
ohrstrom@1504: String l = in.readLine();
ohrstrom@1504: if (l==null) break;
ohrstrom@1504: if (l.length()>=3 && l.charAt(1) == ' ') {
ohrstrom@1504: char c = l.charAt(0);
ohrstrom@1504: if (c == 'M') {
ohrstrom@1504: lastModule = db.prev.loadModule(l);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'P') {
ohrstrom@1504: if (lastModule == null) { syntaxError = true; break; }
ohrstrom@1504: lastPackage = db.prev.loadPackage(lastModule, l);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'D') {
ohrstrom@1504: if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
ohrstrom@1504: lastPackage.loadDependency(l);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'I') {
ohrstrom@1504: if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
ohrstrom@1504: lastPackage.loadPubapi(l);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'A') {
ohrstrom@1504: if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
ohrstrom@1504: lastPackage.loadArtifact(l);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'S') {
ohrstrom@1504: if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
ohrstrom@1504: lastSource = db.prev.loadSource(lastPackage, l, false);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'G') {
ohrstrom@1504: if (lastModule == null || lastPackage == null) { syntaxError = true; break; }
ohrstrom@1504: lastSource = db.prev.loadSource(lastPackage, l, true);
ohrstrom@1504: } else
ohrstrom@1504: if (c == 'R') {
ohrstrom@1504: String ncmdl = "R "+db.theArgs;
ohrstrom@1504: if (!l.equals(ncmdl)) {
ohrstrom@1504: newCommandLine = true;
ohrstrom@1504: }
ohrstrom@1504: } else
ohrstrom@1504: if (c == '#') {
ohrstrom@1504: if (l.startsWith("# javac_state ver ")) {
ohrstrom@1504: int sp = l.indexOf(" ", 18);
ohrstrom@1504: if (sp != -1) {
ohrstrom@1504: String ver = l.substring(18,sp);
ohrstrom@1504: if (!ver.equals("0.3")) {
ohrstrom@1504: break;
ohrstrom@1504: }
ohrstrom@1504: foundCorrectVerNr = true;
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: } catch (FileNotFoundException e) {
ohrstrom@1504: // Silently create a new javac_state file.
ohrstrom@1504: noFileFound = true;
ohrstrom@1504: } catch (IOException e) {
ohrstrom@1504: Log.info("Dropping old javac_state because of errors when reading it.");
ohrstrom@1504: db = new JavacState(args, binDir, gensrcDir, headerDir, permitUnidentifiedArtifacts, true, out, err);
ohrstrom@1504: foundCorrectVerNr = true;
ohrstrom@1504: newCommandLine = false;
ohrstrom@1504: syntaxError = false;
ohrstrom@1504: }
ohrstrom@1504: if (foundCorrectVerNr == false && !noFileFound) {
ohrstrom@1504: Log.info("Dropping old javac_state since it is of an old version.");
ohrstrom@1504: db = new JavacState(args, binDir, gensrcDir, headerDir, permitUnidentifiedArtifacts, true, out, err);
ohrstrom@1504: } else
ohrstrom@1504: if (newCommandLine == true && !noFileFound) {
ohrstrom@1504: Log.info("Dropping old javac_state since a new command line is used!");
ohrstrom@1504: db = new JavacState(args, binDir, gensrcDir, headerDir, permitUnidentifiedArtifacts, true, out, err);
ohrstrom@1504: } else
ohrstrom@1504: if (syntaxError == true) {
ohrstrom@1504: Log.info("Dropping old javac_state since it contains syntax errors.");
ohrstrom@1504: db = new JavacState(args, binDir, gensrcDir, headerDir, permitUnidentifiedArtifacts, true, out, err);
ohrstrom@1504: }
ohrstrom@1504: db.prev.calculateDependents();
ohrstrom@1504: return db;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Mark a java package as tainted, ie it needs recompilation.
ohrstrom@1504: */
ohrstrom@1504: public void taintPackage(String name, String because) {
ohrstrom@1504: if (!taintedPackages.contains(name)) {
ohrstrom@1504: if (because != null) Log.debug("Tainting "+Util.justPackageName(name)+" because "+because);
ohrstrom@1504: // It has not been tainted before.
ohrstrom@1504: taintedPackages.add(name);
ohrstrom@1504: needsSaving();
ohrstrom@1504: Package nowp = now.packages().get(name);
ohrstrom@1504: if (nowp != null) {
ohrstrom@1504: for (String d : nowp.dependents()) {
ohrstrom@1504: taintPackage(d, because);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * This packages need recompilation.
ohrstrom@1504: */
ohrstrom@1504: public Set taintedPackages() {
ohrstrom@1504: return taintedPackages;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Clean out the tainted package set, used after the first round of compiles,
ohrstrom@1504: * prior to propagating dependencies.
ohrstrom@1504: */
ohrstrom@1504: public void clearTaintedPackages() {
ohrstrom@1504: taintedPackages = new HashSet();
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Go through all sources and check which have been removed, added or modified
ohrstrom@1504: * and taint the corresponding packages.
ohrstrom@1504: */
ohrstrom@1504: public void checkSourceStatus(boolean check_gensrc) {
ohrstrom@1504: removedSources = calculateRemovedSources();
ohrstrom@1504: for (Source s : removedSources) {
ohrstrom@1504: if (!s.isGenerated() || check_gensrc) {
ohrstrom@1504: taintPackage(s.pkg().name(), "source "+s.name()+" was removed");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: addedSources = calculateAddedSources();
ohrstrom@1504: for (Source s : addedSources) {
ohrstrom@1504: String msg = null;
ohrstrom@1504: if (isIncremental()) {
ohrstrom@1504: // When building from scratch, there is no point
ohrstrom@1504: // printing "was added" for every file since all files are added.
ohrstrom@1504: // However for an incremental build it makes sense.
ohrstrom@1504: msg = "source "+s.name()+" was added";
ohrstrom@1504: }
ohrstrom@1504: if (!s.isGenerated() || check_gensrc) {
ohrstrom@1504: taintPackage(s.pkg().name(), msg);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: modifiedSources = calculateModifiedSources();
ohrstrom@1504: for (Source s : modifiedSources) {
ohrstrom@1504: if (!s.isGenerated() || check_gensrc) {
ohrstrom@1504: taintPackage(s.pkg().name(), "source "+s.name()+" was modified");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Acquire the compile_java_packages suffix rule for .java files.
ohrstrom@1504: */
ohrstrom@1504: public Map getJavaSuffixRule() {
ohrstrom@1504: Map sr = new HashMap();
ohrstrom@1504: sr.put(".java", compileJavaPackages);
ohrstrom@1504: return sr;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Acquire the copying transform.
ohrstrom@1504: */
ohrstrom@1504: public Transformer getCopier() {
ohrstrom@1504: return copyFiles;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * If artifacts have gone missing, force a recompile of the packages
ohrstrom@1504: * they belong to.
ohrstrom@1504: */
ohrstrom@1504: public void taintPackagesThatMissArtifacts() {
ohrstrom@1504: for (Package pkg : prev.packages().values()) {
ohrstrom@1504: for (File f : pkg.artifacts().values()) {
ohrstrom@1504: if (!f.exists()) {
ohrstrom@1504: // Hmm, the artifact on disk does not exist! Someone has removed it....
ohrstrom@1504: // Lets rebuild the package.
ohrstrom@1504: taintPackage(pkg.name(), ""+f+" is missing.");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Propagate recompilation through the dependency chains.
ohrstrom@1504: * Avoid re-tainting packages that have already been compiled.
ohrstrom@1504: */
ohrstrom@1504: public void taintPackagesDependingOnChangedPackages(Set pkgs, Set recentlyCompiled) {
ohrstrom@1504: for (Package pkg : prev.packages().values()) {
ohrstrom@1504: for (String dep : pkg.dependencies()) {
ohrstrom@1504: if (pkgs.contains(dep) && !recentlyCompiled.contains(pkg.name())) {
ohrstrom@1504: taintPackage(pkg.name(), " its depending on "+dep);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Scan all output dirs for artifacts and remove those files (artifacts?)
ohrstrom@1504: * that are not recognized as such, in the javac_state file.
ohrstrom@1504: */
ohrstrom@1504: public void removeUnidentifiedArtifacts() {
ohrstrom@1504: Set allKnownArtifacts = new HashSet();
ohrstrom@1504: for (Package pkg : prev.packages().values()) {
ohrstrom@1504: for (File f : pkg.artifacts().values()) {
ohrstrom@1504: allKnownArtifacts.add(f);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: // Do not forget about javac_state....
ohrstrom@1504: allKnownArtifacts.add(javacState);
ohrstrom@1504:
ohrstrom@1504: for (File f : binArtifacts) {
ohrstrom@1504: if (!allKnownArtifacts.contains(f)) {
ohrstrom@1504: Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
ohrstrom@1504: f.delete();
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: for (File f : headerArtifacts) {
ohrstrom@1504: if (!allKnownArtifacts.contains(f)) {
ohrstrom@1504: Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
ohrstrom@1504: f.delete();
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: for (File f : gensrcArtifacts) {
ohrstrom@1504: if (!allKnownArtifacts.contains(f)) {
ohrstrom@1504: Log.debug("Removing "+f.getPath()+" since it is unknown to the javac_state.");
ohrstrom@1504: f.delete();
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Remove artifacts that are no longer produced when compiling!
ohrstrom@1504: */
ohrstrom@1504: public void removeSuperfluousArtifacts(Set recentlyCompiled) {
ohrstrom@1504: // Nothing to do, if nothing was recompiled.
ohrstrom@1504: if (recentlyCompiled.size() == 0) return;
ohrstrom@1504:
ohrstrom@1504: for (String pkg : now.packages().keySet()) {
ohrstrom@1504: // If this package has not been recompiled, skip the check.
ohrstrom@1504: if (!recentlyCompiled.contains(pkg)) continue;
ohrstrom@1504: Collection arts = now.artifacts().values();
ohrstrom@1504: for (File f : fetchPrevArtifacts(pkg).values()) {
ohrstrom@1504: if (!arts.contains(f)) {
ohrstrom@1504: Log.debug("Removing "+f.getPath()+" since it is now superfluous!");
ohrstrom@1504: if (f.exists()) f.delete();
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Return those files belonging to prev, but not now.
ohrstrom@1504: */
ohrstrom@1504: private Set calculateRemovedSources() {
ohrstrom@1504: Set removed = new HashSet();
ohrstrom@1504: for (String src : prev.sources().keySet()) {
ohrstrom@1504: if (now.sources().get(src) == null) {
ohrstrom@1504: removed.add(prev.sources().get(src));
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: return removed;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Return those files belonging to now, but not prev.
ohrstrom@1504: */
ohrstrom@1504: private Set calculateAddedSources() {
ohrstrom@1504: Set added = new HashSet();
ohrstrom@1504: for (String src : now.sources().keySet()) {
ohrstrom@1504: if (prev.sources().get(src) == null) {
ohrstrom@1504: added.add(now.sources().get(src));
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: return added;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Return those files where the timestamp is newer.
ohrstrom@1504: * If a source file timestamp suddenly is older than what is known
ohrstrom@1504: * about it in javac_state, then consider it modified, but print
ohrstrom@1504: * a warning!
ohrstrom@1504: */
ohrstrom@1504: private Set calculateModifiedSources() {
ohrstrom@1504: Set modified = new HashSet();
ohrstrom@1504: for (String src : now.sources().keySet()) {
ohrstrom@1504: Source n = now.sources().get(src);
ohrstrom@1504: Source t = prev.sources().get(src);
ohrstrom@1504: if (prev.sources().get(src) != null) {
ohrstrom@1504: if (t != null) {
ohrstrom@1504: if (n.lastModified() > t.lastModified()) {
ohrstrom@1504: modified.add(n);
ohrstrom@1504: } else if (n.lastModified() < t.lastModified()) {
ohrstrom@1504: modified.add(n);
ohrstrom@1504: Log.warn("The source file "+n.name()+" timestamp has moved backwards in time.");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: return modified;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Recursively delete a directory and all its contents.
ohrstrom@1504: */
ohrstrom@1504: private static void deleteContents(File dir) {
ohrstrom@1504: if (dir != null && dir.exists()) {
ohrstrom@1504: for (File f : dir.listFiles()) {
ohrstrom@1504: if (f.isDirectory()) {
ohrstrom@1504: deleteContents(f);
ohrstrom@1504: }
ohrstrom@1504: f.delete();
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Run the copy translator only.
ohrstrom@1504: */
ohrstrom@1504: public void performCopying(File binDir, Map suffixRules) {
ohrstrom@1504: Map sr = new HashMap();
ohrstrom@1504: for (Map.Entry e : suffixRules.entrySet()) {
ohrstrom@1504: if (e.getValue() == copyFiles) {
ohrstrom@1504: sr.put(e.getKey(), e.getValue());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: perform(binDir, sr);
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Run all the translators that translate into java source code.
ohrstrom@1504: * I.e. all translators that are not copy nor compile_java_source.
ohrstrom@1504: */
ohrstrom@1504: public void performTranslation(File gensrcDir, Map suffixRules) {
ohrstrom@1504: Map sr = new HashMap();
ohrstrom@1504: for (Map.Entry e : suffixRules.entrySet()) {
ohrstrom@1504: if (e.getValue() != copyFiles &&
ohrstrom@1504: e.getValue() != compileJavaPackages) {
ohrstrom@1504: sr.put(e.getKey(), e.getValue());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: perform(gensrcDir, sr);
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Compile all the java sources. Return true, if it needs to be called again!
ohrstrom@1504: */
ohrstrom@1504: public boolean performJavaCompilations(File binDir,
ohrstrom@1504: String serverSettings,
ohrstrom@1504: String[] args,
ohrstrom@1504: Set recentlyCompiled,
ohrstrom@1504: boolean[] rcValue) {
ohrstrom@1504: Map suffixRules = new HashMap();
ohrstrom@1504: suffixRules.put(".java", compileJavaPackages);
ohrstrom@1504: compileJavaPackages.setExtra(serverSettings);
ohrstrom@1504: compileJavaPackages.setExtra(args);
ohrstrom@1504:
ohrstrom@1504: rcValue[0] = perform(binDir, suffixRules);
ohrstrom@1504: recentlyCompiled.addAll(taintedPackages());
ohrstrom@1504: clearTaintedPackages();
ohrstrom@1504: boolean again = !packagesWithChangedPublicApis.isEmpty();
ohrstrom@1504: taintPackagesDependingOnChangedPackages(packagesWithChangedPublicApis, recentlyCompiled);
ohrstrom@1504: packagesWithChangedPublicApis = new HashSet();
ohrstrom@1504: return again && rcValue[0];
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Store the source into the set of sources belonging to the given transform.
ohrstrom@1504: */
ohrstrom@1504: private void addFileToTransform(Map>> gs, Transformer t, Source s) {
ohrstrom@1504: Map> fs = gs.get(t);
ohrstrom@1504: if (fs == null) {
ohrstrom@1504: fs = new HashMap>();
ohrstrom@1504: gs.put(t, fs);
ohrstrom@1504: }
ohrstrom@1504: Set ss = fs.get(s.pkg().name());
ohrstrom@1504: if (ss == null) {
ohrstrom@1504: ss = new HashSet();
ohrstrom@1504: fs.put(s.pkg().name(), ss);
ohrstrom@1504: }
ohrstrom@1504: ss.add(s.file().toURI());
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * For all packages, find all sources belonging to the package, group the sources
ohrstrom@1504: * based on their transformers and apply the transformers on each source code group.
ohrstrom@1504: */
ohrstrom@1504: private boolean perform(File outputDir, Map suffixRules)
ohrstrom@1504: {
ohrstrom@1504: boolean rc = true;
ohrstrom@1504: // Group sources based on transforms. A source file can only belong to a single transform.
ohrstrom@1504: Map>> groupedSources = new HashMap>>();
ohrstrom@1504: for (Source src : now.sources().values()) {
ohrstrom@1504: Transformer t = suffixRules.get(src.suffix());
ohrstrom@1504: if (t != null) {
ohrstrom@1504: if (taintedPackages.contains(src.pkg().name()) && !src.isLinkedOnly()) {
ohrstrom@1504: addFileToTransform(groupedSources, t, src);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: // Go through the transforms and transform them.
ohrstrom@1504: for (Map.Entry>> e : groupedSources.entrySet()) {
ohrstrom@1504: Transformer t = e.getKey();
ohrstrom@1504: Map> srcs = e.getValue();
ohrstrom@1504: // These maps need to be synchronized since multiple threads will be writing results into them.
ohrstrom@1504: Map> packageArtifacts = Collections.synchronizedMap(new HashMap>());
ohrstrom@1504: Map> packageDependencies = Collections.synchronizedMap(new HashMap>());
ohrstrom@1504: Map packagePublicApis = Collections.synchronizedMap(new HashMap());
ohrstrom@1504:
ohrstrom@1504: boolean r = t.transform(srcs,
ohrstrom@1504: visibleSrcs,
ohrstrom@1504: visibleClasses,
ohrstrom@1504: prev.dependents(),
ohrstrom@1504: outputDir.toURI(),
ohrstrom@1504: packageArtifacts,
ohrstrom@1504: packageDependencies,
ohrstrom@1504: packagePublicApis,
ohrstrom@1504: 0,
ohrstrom@1504: isIncremental(),
ohrstrom@1504: numCores,
ohrstrom@1504: out,
ohrstrom@1504: err);
ohrstrom@1504: if (!r) rc = false;
ohrstrom@1504:
ohrstrom@1504: for (String p : srcs.keySet()) {
ohrstrom@1504: recompiledPackages.add(p);
ohrstrom@1504: }
ohrstrom@1504: // The transform is done! Extract all the artifacts and store the info into the Package objects.
ohrstrom@1504: for (Map.Entry> a : packageArtifacts.entrySet()) {
ohrstrom@1504: Module mnow = now.findModuleFromPackageName(a.getKey());
ohrstrom@1504: mnow.addArtifacts(a.getKey(), a.getValue());
ohrstrom@1504: }
ohrstrom@1504: // Extract all the dependencies and store the info into the Package objects.
ohrstrom@1504: for (Map.Entry> a : packageDependencies.entrySet()) {
ohrstrom@1504: Set deps = a.getValue();
ohrstrom@1504: Module mnow = now.findModuleFromPackageName(a.getKey());
ohrstrom@1504: mnow.setDependencies(a.getKey(), deps);
ohrstrom@1504: }
ohrstrom@1504: // Extract all the pubapis and store the info into the Package objects.
ohrstrom@1504: for (Map.Entry a : packagePublicApis.entrySet()) {
ohrstrom@1504: Module mprev = prev.findModuleFromPackageName(a.getKey());
ohrstrom@1504: List pubapi = Package.pubapiToList(a.getValue());
ohrstrom@1504: Module mnow = now.findModuleFromPackageName(a.getKey());
ohrstrom@1504: mnow.setPubapi(a.getKey(), pubapi);
ohrstrom@1504: if (mprev.hasPubapiChanged(a.getKey(), pubapi)) {
ohrstrom@1504: // Aha! The pubapi of this package has changed!
ohrstrom@1504: // It can also be a new compile from scratch.
ohrstrom@1504: if (mprev.lookupPackage(a.getKey()).existsInJavacState()) {
ohrstrom@1504: // This is an incremental compile! The pubapi
ohrstrom@1504: // did change. Trigger recompilation of dependents.
ohrstrom@1504: packagesWithChangedPublicApis.add(a.getKey());
ohrstrom@1504: Log.info("The pubapi of "+Util.justPackageName(a.getKey())+" has changed!");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: return rc;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Utility method to recursively find all files below a directory.
ohrstrom@1504: */
ohrstrom@1504: private static Set findAllFiles(File dir) {
ohrstrom@1504: Set foundFiles = new HashSet();
ohrstrom@1504: if (dir == null) {
ohrstrom@1504: return foundFiles;
ohrstrom@1504: }
ohrstrom@1504: recurse(dir, foundFiles);
ohrstrom@1504: return foundFiles;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: private static void recurse(File dir, Set foundFiles) {
ohrstrom@1504: for (File f : dir.listFiles()) {
ohrstrom@1504: if (f.isFile()) {
ohrstrom@1504: foundFiles.add(f);
ohrstrom@1504: } else if (f.isDirectory()) {
ohrstrom@1504: recurse(f, foundFiles);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Compare the calculate source list, with an explicit list, usually supplied from the makefile.
ohrstrom@1504: * Used to detect bugs where the makefile and sjavac have different opinions on which files
ohrstrom@1504: * should be compiled.
ohrstrom@1504: */
ohrstrom@1504: public void compareWithMakefileList(File makefileSourceList)
ohrstrom@1504: throws ProblemException
ohrstrom@1504: {
ohrstrom@1504: // If we are building on win32 using for example cygwin the paths in the makefile source list
ohrstrom@1504: // might be /cygdrive/c/.... which does not match c:\....
ohrstrom@1504: // We need to adjust our calculated sources to be identical, if necessary.
ohrstrom@1504: boolean mightNeedRewriting = File.pathSeparatorChar == ';';
ohrstrom@1504:
ohrstrom@1504: if (makefileSourceList == null) return;
ohrstrom@1504:
ohrstrom@1504: Set calculatedSources = new HashSet();
ohrstrom@1504: Set listedSources = new HashSet();
ohrstrom@1504:
ohrstrom@1504: // Create a set of filenames with full paths.
ohrstrom@1504: for (Source s : now.sources().values()) {
ohrstrom@1504: calculatedSources.add(s.file().getPath());
ohrstrom@1504: }
ohrstrom@1504: // Read in the file and create another set of filenames with full paths.
ohrstrom@1504: try {
ohrstrom@1504: BufferedReader in = new BufferedReader(new FileReader(makefileSourceList));
ohrstrom@1504: for (;;) {
ohrstrom@1504: String l = in.readLine();
ohrstrom@1504: if (l==null) break;
ohrstrom@1504: l = l.trim();
ohrstrom@1504: if (mightNeedRewriting) {
ohrstrom@1504: if (l.indexOf(":") == 1 && l.indexOf("\\") == 2) {
ohrstrom@1504: // Everything a-ok, the format is already C:\foo\bar
ohrstrom@1504: } else if (l.indexOf(":") == 1 && l.indexOf("/") == 2) {
ohrstrom@1504: // The format is C:/foo/bar, rewrite into the above format.
ohrstrom@1504: l = l.replaceAll("/","\\\\");
ohrstrom@1504: } else if (l.charAt(0) == '/' && l.indexOf("/",1) != -1) {
ohrstrom@1504: // The format might be: /cygdrive/c/foo/bar, rewrite into the above format.
ohrstrom@1504: // Do not hardcode the name cygdrive here.
ohrstrom@1504: int slash = l.indexOf("/",1);
ohrstrom@1504: l = l.replaceAll("/","\\\\");
ohrstrom@1504: l = ""+l.charAt(slash+1)+":"+l.substring(slash+2);
ohrstrom@1504: }
ohrstrom@1504: if (Character.isLowerCase(l.charAt(0))) {
ohrstrom@1504: l = Character.toUpperCase(l.charAt(0))+l.substring(1);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: listedSources.add(l);
ohrstrom@1504: }
ohrstrom@1504: } catch (FileNotFoundException e) {
ohrstrom@1504: throw new ProblemException("Could not open "+makefileSourceList.getPath()+" since it does not exist!");
ohrstrom@1504: } catch (IOException e) {
ohrstrom@1504: throw new ProblemException("Could not read "+makefileSourceList.getPath());
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: for (String s : listedSources) {
ohrstrom@1504: if (!calculatedSources.contains(s)) {
ohrstrom@1504: throw new ProblemException("The makefile listed source "+s+" was not calculated by the smart javac wrapper!");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: for (String s : calculatedSources) {
ohrstrom@1504: if (!listedSources.contains(s)) {
ohrstrom@1504: throw new ProblemException("The smart javac wrapper calculated source "+s+" was not listed by the makefiles!");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }