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