1 /* |
|
2 * Copyright (c) 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 |
|
26 package build.tools.module; |
|
27 |
|
28 import java.io.PrintWriter; |
|
29 import java.nio.file.Files; |
|
30 import java.nio.file.Path; |
|
31 import java.nio.file.Paths; |
|
32 import java.util.*; |
|
33 import java.util.stream.Collectors; |
|
34 |
|
35 /** |
|
36 * $ java build.tools.module.GenModulesList \ |
|
37 * -o modules.list \ |
|
38 * top/modules.xml ... |
|
39 */ |
|
40 public final class GenModulesList { |
|
41 private final static String USAGE = |
|
42 "Usage: GenModulesList -o <output file> path-to-modules-xml"; |
|
43 |
|
44 private Set<Module> modules = new HashSet<>(); |
|
45 private HashMap<String,Module> nameToModule = new HashMap<>(); |
|
46 |
|
47 public static void main(String[] args) throws Exception { |
|
48 GenModulesList gen = new GenModulesList(); |
|
49 gen.run(args); |
|
50 } |
|
51 |
|
52 void run(String[] args) throws Exception { |
|
53 Path outfile = null; |
|
54 int i = 0; |
|
55 while (i < args.length) { |
|
56 String arg = args[i]; |
|
57 if (arg.equals("-o")) { |
|
58 outfile = Paths.get(args[i+1]); |
|
59 i = i+2; |
|
60 } else { |
|
61 break; |
|
62 } |
|
63 } |
|
64 if (outfile == null || i >= args.length) { |
|
65 System.err.println(USAGE); |
|
66 System.exit(-1); |
|
67 } |
|
68 |
|
69 for (; i < args.length; i++) { |
|
70 Path p = Paths.get(args[i]); |
|
71 modules.addAll(ModulesXmlReader.readModules(p)); |
|
72 } |
|
73 |
|
74 modules.stream() |
|
75 .forEach(m -> nameToModule.put(m.name(), m)); |
|
76 |
|
77 Path parent = outfile.getParent(); |
|
78 if (parent != null) |
|
79 Files.createDirectories(parent); |
|
80 |
|
81 Iterable<Module> sortedModules = (new TopoSorter(modules)).result(); |
|
82 try (PrintWriter writer = new PrintWriter(outfile.toFile())) { |
|
83 for (Module m : sortedModules) { |
|
84 if (isNotAggregator(m)) { |
|
85 String deps = getModuleDependences(m).stream() |
|
86 .filter(GenModulesList::isNotAggregator) |
|
87 .map(Module::name) |
|
88 .collect(Collectors.joining(" ")); |
|
89 writer.format("%s: %s%n", m.name(), deps); |
|
90 } |
|
91 } |
|
92 } |
|
93 } |
|
94 |
|
95 private Module nameToModule(String name) { |
|
96 return nameToModule.get(name); |
|
97 } |
|
98 |
|
99 private Set<Module> getModuleDependences(Module m) { |
|
100 return m.requires().stream() |
|
101 .map(d -> d.name()) |
|
102 .map(this::nameToModule) |
|
103 .collect(Collectors.toSet()); |
|
104 } |
|
105 |
|
106 static boolean isNotAggregator(Module m) { |
|
107 return isNotAggregator(m.name()); |
|
108 } |
|
109 |
|
110 static boolean isNotAggregator(String name) { |
|
111 return AGGREGATORS.contains(name) ? false : true; |
|
112 } |
|
113 |
|
114 static final List<String> AGGREGATORS = Arrays.asList(new String[] { |
|
115 "java.se", "java.compact1", "java.compact2", "java.compact3"}); |
|
116 |
|
117 class TopoSorter { |
|
118 final Deque<Module> result = new LinkedList<>(); |
|
119 final Deque<Module> nodes = new LinkedList<>(); |
|
120 |
|
121 TopoSorter(Collection<Module> nodes) { |
|
122 nodes.stream() |
|
123 .forEach(m -> this.nodes.add(m)); |
|
124 |
|
125 sort(); |
|
126 } |
|
127 |
|
128 public Iterable<Module> result() { |
|
129 return result; |
|
130 } |
|
131 |
|
132 private void sort() { |
|
133 Deque<Module> visited = new LinkedList<>(); |
|
134 Deque<Module> done = new LinkedList<>(); |
|
135 Module node; |
|
136 while ((node = nodes.poll()) != null) { |
|
137 if (!visited.contains(node)) { |
|
138 visit(node, visited, done); |
|
139 } |
|
140 } |
|
141 } |
|
142 |
|
143 private void visit(Module m, Deque<Module> visited, Deque<Module> done) { |
|
144 if (visited.contains(m)) { |
|
145 if (!done.contains(m)) { |
|
146 throw new IllegalArgumentException("Cyclic detected: " + |
|
147 m + " " + getModuleDependences(m)); |
|
148 } |
|
149 return; |
|
150 } |
|
151 visited.add(m); |
|
152 getModuleDependences(m).stream() |
|
153 .forEach(x -> visit(x, visited, done)); |
|
154 done.add(m); |
|
155 result.addLast(m); |
|
156 } |
|
157 } |
|
158 } |
|