Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/GenClassesBuilder.java
66646 views
/*1* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.sysdict.share;2425import jdk.test.lib.JDKToolLauncher;26import jdk.test.lib.Utils;27import jdk.test.lib.process.ProcessTools;2829import java.io.IOException;30import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.util.Arrays;34import java.util.stream.Stream;3536public class GenClassesBuilder {37public static void main(String[] args) {38if (args == null || args.length == 0) {39throw new Error("args can't be empty");40}41for (String arg : args) {42switch (arg) {43case "btree":44build("btree", "BTree",45() -> BTreeGen.main(new String[] {"default"}));46break;47case "leans":48build("leans", "Leans",49() -> ChainGen.main(new String[] {"leans"}));50break;51case "fats":52build("fats", "Fats",53() -> ChainGen.main(new String[] {"fats"}));54break;55default:56throw new Error("unkown target " + arg);57}58}59}6061private static void build(String name, String prefix, Runnable generator) {62generator.run();63Path genSrcDir = Paths.get(name, "genSrc", "nsk", "sysdict", "share");64Path classesDir = Paths.get(name,"classes").toAbsolutePath();65try {66Files.createDirectories(genSrcDir);67} catch (IOException e) {68throw new Error("can't create dir " + genSrcDir, e);69}70moveJavaFiles(genSrcDir, prefix);7172JDKToolLauncher javac = JDKToolLauncher.create("javac")73.addToolArg("-J-Xmx1G")74.addToolArg("-d")75.addToolArg(classesDir.toString())76.addToolArg("-cp")77.addToolArg(Utils.TEST_CLASS_PATH);7879try (Stream<Path> stream = Files.walk(genSrcDir)) {80stream.map(Path::toAbsolutePath)81.map(Path::toString)82.filter(s -> s.endsWith(".java"))83.forEach(javac::addToolArg);84} catch (IOException e) {85throw new Error("traverse dir " + genSrcDir, e);86}8788executeTool(javac);89buildJar(name + ".jar", classesDir);90}9192private static void executeTool(JDKToolLauncher tool) {93String[] command = tool.getCommand();94try {95ProcessTools.executeCommand(command)96.shouldHaveExitValue(0);97} catch (Error | RuntimeException e) {98throw e;99} catch (Throwable e) {100throw new Error("execution of " + Arrays.toString(command) + " failed", e);101}102}103104private static void buildJar(String name, Path dir) {105JDKToolLauncher jar = JDKToolLauncher.create("jar")106.addToolArg("cf")107.addToolArg(name)108.addToolArg("-C")109.addToolArg(dir.toString())110.addToolArg(".");111executeTool(jar);112}113114private static void moveJavaFiles(Path dir, String prefix) {115try (Stream<Path> stream = Files.list(Paths.get("."))) {116stream.filter(Files::isRegularFile)117.filter(p -> {118String s = p.getFileName().toString();119return s.startsWith(prefix) && s.endsWith(".java");})120.forEach(p -> move(p, dir));121} catch (IOException e) {122throw new Error("can't traverse current dir", e);123}124}125126private static void move(Path src, Path dstDir) {127if (Files.notExists(src)) {128throw new Error("file " + src + " doesn't exit");129}130try {131Files.move(src, dstDir.resolve(src.getFileName()));132} catch (IOException e) {133throw new Error("can't move " + src + " to " + dstDir, e);134}135}136}137138139