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