Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/GenClassesBuilder.java
66646 views
1
/*
2
* Copyright (c) 2017, 2021, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.sysdict.share;
25
26
import jdk.test.lib.JDKToolLauncher;
27
import jdk.test.lib.Utils;
28
import jdk.test.lib.process.ProcessTools;
29
30
import java.io.IOException;
31
import java.nio.file.Files;
32
import java.nio.file.Path;
33
import java.nio.file.Paths;
34
import java.util.Arrays;
35
import java.util.stream.Stream;
36
37
public class GenClassesBuilder {
38
public static void main(String[] args) {
39
if (args == null || args.length == 0) {
40
throw new Error("args can't be empty");
41
}
42
for (String arg : args) {
43
switch (arg) {
44
case "btree":
45
build("btree", "BTree",
46
() -> BTreeGen.main(new String[] {"default"}));
47
break;
48
case "leans":
49
build("leans", "Leans",
50
() -> ChainGen.main(new String[] {"leans"}));
51
break;
52
case "fats":
53
build("fats", "Fats",
54
() -> ChainGen.main(new String[] {"fats"}));
55
break;
56
default:
57
throw new Error("unkown target " + arg);
58
}
59
}
60
}
61
62
private static void build(String name, String prefix, Runnable generator) {
63
generator.run();
64
Path genSrcDir = Paths.get(name, "genSrc", "nsk", "sysdict", "share");
65
Path classesDir = Paths.get(name,"classes").toAbsolutePath();
66
try {
67
Files.createDirectories(genSrcDir);
68
} catch (IOException e) {
69
throw new Error("can't create dir " + genSrcDir, e);
70
}
71
moveJavaFiles(genSrcDir, prefix);
72
73
JDKToolLauncher javac = JDKToolLauncher.create("javac")
74
.addToolArg("-J-Xmx1G")
75
.addToolArg("-d")
76
.addToolArg(classesDir.toString())
77
.addToolArg("-cp")
78
.addToolArg(Utils.TEST_CLASS_PATH);
79
80
try (Stream<Path> stream = Files.walk(genSrcDir)) {
81
stream.map(Path::toAbsolutePath)
82
.map(Path::toString)
83
.filter(s -> s.endsWith(".java"))
84
.forEach(javac::addToolArg);
85
} catch (IOException e) {
86
throw new Error("traverse dir " + genSrcDir, e);
87
}
88
89
executeTool(javac);
90
buildJar(name + ".jar", classesDir);
91
}
92
93
private static void executeTool(JDKToolLauncher tool) {
94
String[] command = tool.getCommand();
95
try {
96
ProcessTools.executeCommand(command)
97
.shouldHaveExitValue(0);
98
} catch (Error | RuntimeException e) {
99
throw e;
100
} catch (Throwable e) {
101
throw new Error("execution of " + Arrays.toString(command) + " failed", e);
102
}
103
}
104
105
private static void buildJar(String name, Path dir) {
106
JDKToolLauncher jar = JDKToolLauncher.create("jar")
107
.addToolArg("cf")
108
.addToolArg(name)
109
.addToolArg("-C")
110
.addToolArg(dir.toString())
111
.addToolArg(".");
112
executeTool(jar);
113
}
114
115
private static void moveJavaFiles(Path dir, String prefix) {
116
try (Stream<Path> stream = Files.list(Paths.get("."))) {
117
stream.filter(Files::isRegularFile)
118
.filter(p -> {
119
String s = p.getFileName().toString();
120
return s.startsWith(prefix) && s.endsWith(".java");})
121
.forEach(p -> move(p, dir));
122
} catch (IOException e) {
123
throw new Error("can't traverse current dir", e);
124
}
125
}
126
127
private static void move(Path src, Path dstDir) {
128
if (Files.notExists(src)) {
129
throw new Error("file " + src + " doesn't exit");
130
}
131
try {
132
Files.move(src, dstDir.resolve(src.getFileName()));
133
} catch (IOException e) {
134
throw new Error("can't move " + src + " to " + dstDir, e);
135
}
136
}
137
}
138
139