Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/GenClassesBuilder.java
41161 views
/*1* Copyright (c) 2017, 2018, 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("-d")74.addToolArg(classesDir.toString())75.addToolArg("-cp")76.addToolArg(Utils.TEST_CLASS_PATH);7778try (Stream<Path> stream = Files.walk(genSrcDir)) {79stream.map(Path::toAbsolutePath)80.map(Path::toString)81.filter(s -> s.endsWith(".java"))82.forEach(javac::addToolArg);83} catch (IOException e) {84throw new Error("traverse dir " + genSrcDir, e);85}8687executeTool(javac);88buildJar(name + ".jar", classesDir);89}9091private static void executeTool(JDKToolLauncher tool) {92String[] command = tool.getCommand();93try {94ProcessTools.executeCommand(command)95.shouldHaveExitValue(0);96} catch (Error | RuntimeException e) {97throw e;98} catch (Throwable e) {99throw new Error("execution of " + Arrays.toString(command) + " failed", e);100}101}102103private static void buildJar(String name, Path dir) {104JDKToolLauncher jar = JDKToolLauncher.create("jar")105.addToolArg("cf")106.addToolArg(name)107.addToolArg("-C")108.addToolArg(dir.toString())109.addToolArg(".");110executeTool(jar);111}112113private static void moveJavaFiles(Path dir, String prefix) {114try (Stream<Path> stream = Files.list(Paths.get("."))) {115stream.filter(Files::isRegularFile)116.filter(p -> {117String s = p.getFileName().toString();118return s.startsWith(prefix) && s.endsWith(".java");})119.forEach(p -> move(p, dir));120} catch (IOException e) {121throw new Error("can't traverse current dir", e);122}123}124125private static void move(Path src, Path dstDir) {126if (Files.notExists(src)) {127throw new Error("file " + src + " doesn't exit");128}129try {130Files.move(src, dstDir.resolve(src.getFileName()));131} catch (IOException e) {132throw new Error("can't move " + src + " to " + dstDir, e);133}134}135}136137138