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.File;
ohrstrom@1504: import java.util.HashMap;
ohrstrom@1504: import java.util.HashSet;
ohrstrom@1504: import java.util.Map;
ohrstrom@1504: import java.util.Set;
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * The build state class captures the source code and generated artifacts
ohrstrom@1504: * from a build. There are usually two build states, the previous one (prev),
ohrstrom@1504: * loaded from the javac_state file, and the current one (now).
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 BuildState {
ohrstrom@1504: private Map modules = new HashMap();
ohrstrom@1504: private Map packages = new HashMap();
ohrstrom@1504: private Map sources = new HashMap();
ohrstrom@1504: private Map artifacts = new HashMap();
ohrstrom@1504: // Map from package to a set of packages that depend on said package.
ohrstrom@1504: private Map> dependents = new HashMap>();
ohrstrom@1504:
ohrstrom@1504: public Map modules() { return modules; }
ohrstrom@1504: public Map packages() { return packages; }
ohrstrom@1504: public Map sources() { return sources; }
ohrstrom@1504: public Map artifacts() { return artifacts; }
ohrstrom@1504: public Map> dependents() { return dependents; }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Lookup a module from a name. Create the module if it does
ohrstrom@1504: * not exist yet.
ohrstrom@1504: */
ohrstrom@1504: public Module lookupModule(String mod) {
ohrstrom@1504: Module m = modules.get(mod);
ohrstrom@1504: if (m == null) {
ohrstrom@1504: m = new Module(mod, "???");
ohrstrom@1504: modules.put(mod, m);
ohrstrom@1504: }
ohrstrom@1504: return m;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Find a module from a given package name. For example:
ohrstrom@1504: * The package name "base:java.lang" will fetch the module named "base".
ohrstrom@1504: * The package name ":java.net" will fetch the default module.
ohrstrom@1504: */
ohrstrom@1504: Module findModuleFromPackageName(String pkg) {
ohrstrom@1504: int cp = pkg.indexOf(':');
ohrstrom@1504: assert(cp != -1);
ohrstrom@1504: String mod = pkg.substring(0, cp);
ohrstrom@1504: return lookupModule(mod);
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Collect all packages, sources and artifacts for all modules
ohrstrom@1504: * into the build state.
ohrstrom@1504: *
ohrstrom@1504: * @param m The set of modules.
ohrstrom@1504: */
ohrstrom@1504: public void collectPackagesSourcesAndArtifacts(Map m) {
ohrstrom@1504: modules = m;
ohrstrom@1504: // Extract all the found packages.
ohrstrom@1504: for (Module i : modules.values()) {
ohrstrom@1504: for (Map.Entry j : i.packages().entrySet()) {
ohrstrom@1504: Package p = packages.get(j.getKey());
ohrstrom@1504: // Check that no two different packages are stored under same name.
ohrstrom@1504: assert(p == null || p == j.getValue());
ohrstrom@1504: if (p == null) {
ohrstrom@1504: p = j.getValue();
ohrstrom@1504: packages.put(j.getKey(),j.getValue());
ohrstrom@1504: }
ohrstrom@1504: for (Map.Entry k : p.sources().entrySet()) {
ohrstrom@1504: Source s = sources.get(k.getKey());
ohrstrom@1504: // Check that no two different sources are stored under same name.
ohrstrom@1504: assert(s == null || s == k.getValue());
ohrstrom@1504: if (s == null) {
ohrstrom@1504: s = k.getValue();
ohrstrom@1504: sources.put(k.getKey(), k.getValue());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: for (Map.Entry g : p.artifacts().entrySet()) {
ohrstrom@1504: File f = artifacts.get(g.getKey());
ohrstrom@1504: // Check that no two artifacts are stored under the same file.
ohrstrom@1504: assert(f == null || f == g.getValue());
ohrstrom@1504: if (f == null) {
ohrstrom@1504: f = g.getValue();
ohrstrom@1504: artifacts.put(g.getKey(), g.getValue());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Collect all the artifacts of all modules and packages.
ohrstrom@1504: *
ohrstrom@1504: * @param m The set of modules.
ohrstrom@1504: */
ohrstrom@1504: public void collectArtifacts(Map m) {
ohrstrom@1504: modules = m;
ohrstrom@1504: // Extract all the found packages.
ohrstrom@1504: for (Module i : modules.values()) {
ohrstrom@1504: for (Map.Entry j : i.packages().entrySet()) {
ohrstrom@1504: Package p = packages.get(j.getKey());
ohrstrom@1504: // Check that no two different packages are stored under same name.
ohrstrom@1504: assert(p == null || p == j.getValue());
ohrstrom@1504: p = j.getValue();
ohrstrom@1504: packages.put(j.getKey(),j.getValue());
ohrstrom@1504: for (Map.Entry g : p.artifacts().entrySet()) {
ohrstrom@1504: File f = artifacts.get(g.getKey());
ohrstrom@1504: // Check that no two artifacts are stored under the same file.
ohrstrom@1504: assert(f == null || f == g.getValue());
ohrstrom@1504: artifacts.put(g.getKey(), g.getValue());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Calculate the package dependents (ie the reverse of the dependencies).
ohrstrom@1504: */
ohrstrom@1504: public void calculateDependents() {
ohrstrom@1504: dependents = new HashMap>();
ohrstrom@1504: for (String s : packages.keySet()) {
ohrstrom@1504: Package p = packages.get(s);
ohrstrom@1504: for (String d : p.dependencies()) {
ohrstrom@1504: Set ss = dependents.get(d);
ohrstrom@1504: if (ss == null) {
ohrstrom@1504: ss = new HashSet();
ohrstrom@1504: dependents.put(d, ss);
ohrstrom@1504: }
ohrstrom@1504: // Add the dependent information to the global dependent map.
ohrstrom@1504: ss.add(s);
ohrstrom@1504: Package dp = packages.get(d);
ohrstrom@1504: // Also add the dependent information to the package specific map.
ohrstrom@1504: // Normally, you do not compile java.lang et al. Therefore
ohrstrom@1504: // there are several packages that p depends upon that you
ohrstrom@1504: // do not have in your state database. This is perfectly fine.
ohrstrom@1504: if (dp != null) {
ohrstrom@1504: // But this package did exist in the state database.
ohrstrom@1504: dp.addDependent(p.name());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Verify that the setModules method above did the right thing when
ohrstrom@1504: * running through the module->package->source structure.
ohrstrom@1504: */
ohrstrom@1504: public void checkInternalState(String msg, boolean linkedOnly, Map srcs) {
ohrstrom@1504: boolean baad = false;
ohrstrom@1504: Map original = new HashMap();
ohrstrom@1504: Map calculated = new HashMap();
ohrstrom@1504:
ohrstrom@1504: for (String s : sources.keySet()) {
ohrstrom@1504: Source ss = sources.get(s);
ohrstrom@1504: if (ss.isLinkedOnly() == linkedOnly) {
ohrstrom@1504: calculated.put(s,ss);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: for (String s : srcs.keySet()) {
ohrstrom@1504: Source ss = srcs.get(s);
ohrstrom@1504: if (ss.isLinkedOnly() == linkedOnly) {
ohrstrom@1504: original.put(s,ss);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: if (original.size() != calculated.size()) {
ohrstrom@1504: Log.error("INTERNAL ERROR "+msg+" original and calculated are not the same size!");
ohrstrom@1504: baad = true;
ohrstrom@1504: }
ohrstrom@1504: if (!original.keySet().equals(calculated.keySet())) {
ohrstrom@1504: Log.error("INTERNAL ERROR "+msg+" original and calculated do not have the same domain!");
ohrstrom@1504: baad = true;
ohrstrom@1504: }
ohrstrom@1504: if (!baad) {
ohrstrom@1504: for (String s : original.keySet()) {
ohrstrom@1504: Source s1 = original.get(s);
ohrstrom@1504: Source s2 = calculated.get(s);
ohrstrom@1504: if (s1 == null || s2 == null || !s1.equals(s2)) {
ohrstrom@1504: Log.error("INTERNAL ERROR "+msg+" original and calculated have differing elements for "+s);
ohrstrom@1504: }
ohrstrom@1504: baad = true;
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: if (baad) {
ohrstrom@1504: for (String s : original.keySet()) {
ohrstrom@1504: Source ss = original.get(s);
ohrstrom@1504: Source sss = calculated.get(s);
ohrstrom@1504: if (sss == null) {
ohrstrom@1504: Log.error("The file "+s+" does not exist in calculated tree of sources.");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: for (String s : calculated.keySet()) {
ohrstrom@1504: Source ss = calculated.get(s);
ohrstrom@1504: Source sss = original.get(s);
ohrstrom@1504: if (sss == null) {
ohrstrom@1504: Log.error("The file "+s+" does not exist in original set of found sources.");
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Load a module from the javac state file.
ohrstrom@1504: */
ohrstrom@1504: public Module loadModule(String l) {
ohrstrom@1504: Module m = Module.load(l);
ohrstrom@1504: modules.put(m.name(), m);
ohrstrom@1504: return m;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Load a package from the javac state file.
ohrstrom@1504: */
ohrstrom@1504: public Package loadPackage(Module lastModule, String l) {
ohrstrom@1504: Package p = Package.load(lastModule, l);
ohrstrom@1504: lastModule.addPackage(p);
ohrstrom@1504: packages.put(p.name(), p);
ohrstrom@1504: return p;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Load a source from the javac state file.
ohrstrom@1504: */
ohrstrom@1504: public Source loadSource(Package lastPackage, String l, boolean is_generated) {
ohrstrom@1504: Source s = Source.load(lastPackage, l, is_generated);
ohrstrom@1504: lastPackage.addSource(s);
ohrstrom@1504: sources.put(s.name(), s);
ohrstrom@1504: return s;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * During an incremental compile we need to copy the old javac state
ohrstrom@1504: * information about packages that were not recompiled.
ohrstrom@1504: */
ohrstrom@1504: public void copyPackagesExcept(BuildState prev, Set recompiled, Set removed) {
ohrstrom@1504: for (String pkg : prev.packages().keySet()) {
ohrstrom@1504: // Do not copy recompiled or removed packages.
ohrstrom@1504: if (recompiled.contains(pkg) || removed.contains(pkg)) continue;
ohrstrom@1504: Module mnew = findModuleFromPackageName(pkg);
ohrstrom@1504: Package pprev = prev.packages().get(pkg);
ohrstrom@1504: mnew.addPackage(pprev);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }