Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.sjavac; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.File; |
aoqi@0 | 29 | import java.util.HashMap; |
aoqi@0 | 30 | import java.util.HashSet; |
aoqi@0 | 31 | import java.util.Map; |
aoqi@0 | 32 | import java.util.Set; |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * The build state class captures the source code and generated artifacts |
aoqi@0 | 36 | * from a build. There are usually two build states, the previous one (prev), |
aoqi@0 | 37 | * loaded from the javac_state file, and the current one (now). |
aoqi@0 | 38 | * |
aoqi@0 | 39 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 40 | * If you write code that depends on this, you do so at your own |
aoqi@0 | 41 | * risk. This code and its internal interfaces are subject to change |
aoqi@0 | 42 | * or deletion without notice.</b></p> |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | public class BuildState { |
aoqi@0 | 45 | private Map<String,Module> modules = new HashMap<String,Module>(); |
aoqi@0 | 46 | private Map<String,Package> packages = new HashMap<String,Package>(); |
aoqi@0 | 47 | private Map<String,Source> sources = new HashMap<String,Source>(); |
aoqi@0 | 48 | private Map<String,File> artifacts = new HashMap<String,File>(); |
aoqi@0 | 49 | // Map from package to a set of packages that depend on said package. |
aoqi@0 | 50 | private Map<String,Set<String>> dependents = new HashMap<String,Set<String>>(); |
aoqi@0 | 51 | |
aoqi@0 | 52 | public Map<String,Module> modules() { return modules; } |
aoqi@0 | 53 | public Map<String,Package> packages() { return packages; } |
aoqi@0 | 54 | public Map<String,Source> sources() { return sources; } |
aoqi@0 | 55 | public Map<String,File> artifacts() { return artifacts; } |
aoqi@0 | 56 | public Map<String,Set<String>> dependents() { return dependents; } |
aoqi@0 | 57 | |
aoqi@0 | 58 | /** |
aoqi@0 | 59 | * Lookup a module from a name. Create the module if it does |
aoqi@0 | 60 | * not exist yet. |
aoqi@0 | 61 | */ |
aoqi@0 | 62 | public Module lookupModule(String mod) { |
aoqi@0 | 63 | Module m = modules.get(mod); |
aoqi@0 | 64 | if (m == null) { |
aoqi@0 | 65 | m = new Module(mod, "???"); |
aoqi@0 | 66 | modules.put(mod, m); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | return m; |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * Find a module from a given package name. For example: |
aoqi@0 | 73 | * The package name "base:java.lang" will fetch the module named "base". |
aoqi@0 | 74 | * The package name ":java.net" will fetch the default module. |
aoqi@0 | 75 | */ |
aoqi@0 | 76 | Module findModuleFromPackageName(String pkg) { |
aoqi@0 | 77 | int cp = pkg.indexOf(':'); |
aoqi@0 | 78 | assert(cp != -1); |
aoqi@0 | 79 | String mod = pkg.substring(0, cp); |
aoqi@0 | 80 | return lookupModule(mod); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | /** |
aoqi@0 | 84 | * Store references to all packages, sources and artifacts for all modules |
aoqi@0 | 85 | * into the build state. I.e. flatten the module tree structure |
aoqi@0 | 86 | * into global maps stored in the BuildState for easy access. |
aoqi@0 | 87 | * |
aoqi@0 | 88 | * @param m The set of modules. |
aoqi@0 | 89 | */ |
aoqi@0 | 90 | public void flattenPackagesSourcesAndArtifacts(Map<String,Module> m) { |
aoqi@0 | 91 | modules = m; |
aoqi@0 | 92 | // Extract all the found packages. |
aoqi@0 | 93 | for (Module i : modules.values()) { |
aoqi@0 | 94 | for (Map.Entry<String,Package> j : i.packages().entrySet()) { |
aoqi@0 | 95 | Package p = packages.get(j.getKey()); |
aoqi@0 | 96 | // Check that no two different packages are stored under same name. |
aoqi@0 | 97 | assert(p == null || p == j.getValue()); |
aoqi@0 | 98 | if (p == null) { |
aoqi@0 | 99 | p = j.getValue(); |
aoqi@0 | 100 | packages.put(j.getKey(),j.getValue()); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | for (Map.Entry<String,Source> k : p.sources().entrySet()) { |
aoqi@0 | 103 | Source s = sources.get(k.getKey()); |
aoqi@0 | 104 | // Check that no two different sources are stored under same name. |
aoqi@0 | 105 | assert(s == null || s == k.getValue()); |
aoqi@0 | 106 | if (s == null) { |
aoqi@0 | 107 | s = k.getValue(); |
aoqi@0 | 108 | sources.put(k.getKey(), k.getValue()); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | } |
aoqi@0 | 111 | for (Map.Entry<String,File> g : p.artifacts().entrySet()) { |
aoqi@0 | 112 | File f = artifacts.get(g.getKey()); |
aoqi@0 | 113 | // Check that no two artifacts are stored under the same file. |
aoqi@0 | 114 | assert(f == null || f == g.getValue()); |
aoqi@0 | 115 | if (f == null) { |
aoqi@0 | 116 | f = g.getValue(); |
aoqi@0 | 117 | artifacts.put(g.getKey(), g.getValue()); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | } |
aoqi@0 | 120 | } |
aoqi@0 | 121 | } |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | /** |
aoqi@0 | 125 | * Store references to all artifacts found in the module tree into the maps |
aoqi@0 | 126 | * stored in the build state. |
aoqi@0 | 127 | * |
aoqi@0 | 128 | * @param m The set of modules. |
aoqi@0 | 129 | */ |
aoqi@0 | 130 | public void flattenArtifacts(Map<String,Module> m) { |
aoqi@0 | 131 | modules = m; |
aoqi@0 | 132 | // Extract all the found packages. |
aoqi@0 | 133 | for (Module i : modules.values()) { |
aoqi@0 | 134 | for (Map.Entry<String,Package> j : i.packages().entrySet()) { |
aoqi@0 | 135 | Package p = packages.get(j.getKey()); |
aoqi@0 | 136 | // Check that no two different packages are stored under same name. |
aoqi@0 | 137 | assert(p == null || p == j.getValue()); |
aoqi@0 | 138 | p = j.getValue(); |
aoqi@0 | 139 | packages.put(j.getKey(),j.getValue()); |
aoqi@0 | 140 | for (Map.Entry<String,File> g : p.artifacts().entrySet()) { |
aoqi@0 | 141 | File f = artifacts.get(g.getKey()); |
aoqi@0 | 142 | // Check that no two artifacts are stored under the same file. |
aoqi@0 | 143 | assert(f == null || f == g.getValue()); |
aoqi@0 | 144 | artifacts.put(g.getKey(), g.getValue()); |
aoqi@0 | 145 | } |
aoqi@0 | 146 | } |
aoqi@0 | 147 | } |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | /** |
aoqi@0 | 151 | * Calculate the package dependents (ie the reverse of the dependencies). |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | public void calculateDependents() { |
aoqi@0 | 154 | dependents = new HashMap<String,Set<String>>(); |
aoqi@0 | 155 | for (String s : packages.keySet()) { |
aoqi@0 | 156 | Package p = packages.get(s); |
aoqi@0 | 157 | for (String d : p.dependencies()) { |
aoqi@0 | 158 | Set<String> ss = dependents.get(d); |
aoqi@0 | 159 | if (ss == null) { |
aoqi@0 | 160 | ss = new HashSet<String>(); |
aoqi@0 | 161 | dependents.put(d, ss); |
aoqi@0 | 162 | } |
aoqi@0 | 163 | // Add the dependent information to the global dependent map. |
aoqi@0 | 164 | ss.add(s); |
aoqi@0 | 165 | Package dp = packages.get(d); |
aoqi@0 | 166 | // Also add the dependent information to the package specific map. |
aoqi@0 | 167 | // Normally, you do not compile java.lang et al. Therefore |
aoqi@0 | 168 | // there are several packages that p depends upon that you |
aoqi@0 | 169 | // do not have in your state database. This is perfectly fine. |
aoqi@0 | 170 | if (dp != null) { |
aoqi@0 | 171 | // But this package did exist in the state database. |
aoqi@0 | 172 | dp.addDependent(p.name()); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | } |
aoqi@0 | 175 | } |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | /** |
aoqi@0 | 179 | * Verify that the setModules method above did the right thing when |
aoqi@0 | 180 | * running through the module->package->source structure. |
aoqi@0 | 181 | */ |
aoqi@0 | 182 | public void checkInternalState(String msg, boolean linkedOnly, Map<String,Source> srcs) { |
aoqi@0 | 183 | boolean baad = false; |
aoqi@0 | 184 | Map<String,Source> original = new HashMap<String,Source>(); |
aoqi@0 | 185 | Map<String,Source> calculated = new HashMap<String,Source>(); |
aoqi@0 | 186 | |
aoqi@0 | 187 | for (String s : sources.keySet()) { |
aoqi@0 | 188 | Source ss = sources.get(s); |
aoqi@0 | 189 | if (ss.isLinkedOnly() == linkedOnly) { |
aoqi@0 | 190 | calculated.put(s,ss); |
aoqi@0 | 191 | } |
aoqi@0 | 192 | } |
aoqi@0 | 193 | for (String s : srcs.keySet()) { |
aoqi@0 | 194 | Source ss = srcs.get(s); |
aoqi@0 | 195 | if (ss.isLinkedOnly() == linkedOnly) { |
aoqi@0 | 196 | original.put(s,ss); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | } |
aoqi@0 | 199 | if (original.size() != calculated.size()) { |
aoqi@0 | 200 | Log.error("INTERNAL ERROR "+msg+" original and calculated are not the same size!"); |
aoqi@0 | 201 | baad = true; |
aoqi@0 | 202 | } |
aoqi@0 | 203 | if (!original.keySet().equals(calculated.keySet())) { |
aoqi@0 | 204 | Log.error("INTERNAL ERROR "+msg+" original and calculated do not have the same domain!"); |
aoqi@0 | 205 | baad = true; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | if (!baad) { |
aoqi@0 | 208 | for (String s : original.keySet()) { |
aoqi@0 | 209 | Source s1 = original.get(s); |
aoqi@0 | 210 | Source s2 = calculated.get(s); |
aoqi@0 | 211 | if (s1 == null || s2 == null || !s1.equals(s2)) { |
aoqi@0 | 212 | Log.error("INTERNAL ERROR "+msg+" original and calculated have differing elements for "+s); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | baad = true; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | } |
aoqi@0 | 217 | if (baad) { |
aoqi@0 | 218 | for (String s : original.keySet()) { |
aoqi@0 | 219 | Source ss = original.get(s); |
aoqi@0 | 220 | Source sss = calculated.get(s); |
aoqi@0 | 221 | if (sss == null) { |
aoqi@0 | 222 | Log.error("The file "+s+" does not exist in calculated tree of sources."); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | } |
aoqi@0 | 225 | for (String s : calculated.keySet()) { |
aoqi@0 | 226 | Source ss = calculated.get(s); |
aoqi@0 | 227 | Source sss = original.get(s); |
aoqi@0 | 228 | if (sss == null) { |
aoqi@0 | 229 | Log.error("The file "+s+" does not exist in original set of found sources."); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | /** |
aoqi@0 | 236 | * Load a module from the javac state file. |
aoqi@0 | 237 | */ |
aoqi@0 | 238 | public Module loadModule(String l) { |
aoqi@0 | 239 | Module m = Module.load(l); |
aoqi@0 | 240 | modules.put(m.name(), m); |
aoqi@0 | 241 | return m; |
aoqi@0 | 242 | } |
aoqi@0 | 243 | |
aoqi@0 | 244 | /** |
aoqi@0 | 245 | * Load a package from the javac state file. |
aoqi@0 | 246 | */ |
aoqi@0 | 247 | public Package loadPackage(Module lastModule, String l) { |
aoqi@0 | 248 | Package p = Package.load(lastModule, l); |
aoqi@0 | 249 | lastModule.addPackage(p); |
aoqi@0 | 250 | packages.put(p.name(), p); |
aoqi@0 | 251 | return p; |
aoqi@0 | 252 | } |
aoqi@0 | 253 | |
aoqi@0 | 254 | /** |
aoqi@0 | 255 | * Load a source from the javac state file. |
aoqi@0 | 256 | */ |
aoqi@0 | 257 | public Source loadSource(Package lastPackage, String l, boolean is_generated) { |
aoqi@0 | 258 | Source s = Source.load(lastPackage, l, is_generated); |
aoqi@0 | 259 | lastPackage.addSource(s); |
aoqi@0 | 260 | sources.put(s.name(), s); |
aoqi@0 | 261 | return s; |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | /** |
aoqi@0 | 265 | * During an incremental compile we need to copy the old javac state |
aoqi@0 | 266 | * information about packages that were not recompiled. |
aoqi@0 | 267 | */ |
aoqi@0 | 268 | public void copyPackagesExcept(BuildState prev, Set<String> recompiled, Set<String> removed) { |
aoqi@0 | 269 | for (String pkg : prev.packages().keySet()) { |
aoqi@0 | 270 | // Do not copy recompiled or removed packages. |
aoqi@0 | 271 | if (recompiled.contains(pkg) || removed.contains(pkg)) continue; |
aoqi@0 | 272 | Module mnew = findModuleFromPackageName(pkg); |
aoqi@0 | 273 | Package pprev = prev.packages().get(pkg); |
aoqi@0 | 274 | mnew.addPackage(pprev); |
aoqi@0 | 275 | // Do not forget to update the flattened data. |
aoqi@0 | 276 | packages.put(pkg, pprev); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | } |
aoqi@0 | 279 | } |