1/* 2 * Copyright (c) 2016, 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 24package compiler.aot; 25 26import jdk.test.lib.process.OutputAnalyzer; 27import java.io.File; 28import java.io.IOException; 29import java.nio.file.Files; 30import java.nio.file.Path; 31import java.nio.file.Paths; 32import java.nio.file.StandardOpenOption; 33import java.util.ArrayList; 34import java.util.Arrays; 35import java.util.List; 36import jdk.test.lib.JDKToolLauncher; 37import jdk.test.lib.Utils; 38 39/** 40 * A simple class calling AOT compiler over requested items 41 */ 42public class AotCompiler { 43 44 private final static String METHODS_LIST_FILENAME = "methodsList.txt"; 45 46 public static void main(String args[]) { 47 String className = null; 48 List<String> compileList = new ArrayList<>(); 49 String libName = null; 50 List<String> extraopts = new ArrayList<>(); 51 for (int i = 0; i < args.length; i++) { 52 switch (args[i]) { 53 case "-class": 54 className = args[++i]; 55 break; 56 case "-compile": 57 compileList.add("compileOnly " + args[++i]); 58 break; 59 case "-libname": 60 libName = args[++i]; 61 break; 62 case "-extraopt": 63 extraopts.add(args[++i]); 64 break; 65 default: 66 throw new Error("Unknown option: " + args[i]); 67 } 68 } 69 extraopts.add("-classpath"); 70 extraopts.add(Utils.TEST_CLASS_PATH + File.pathSeparator + Utils.TEST_SRC); 71 if (className != null && libName != null) { 72 OutputAnalyzer oa = launchCompiler(libName, className, extraopts, compileList); 73 oa.shouldHaveExitValue(0); 74 } else { 75 printUsage(); 76 throw new Error("Mandatory arguments aren't passed"); 77 } 78 } 79 80 public static OutputAnalyzer launchCompilerSimple(String... args) { 81 return launchJaotc(Arrays.asList(args), null); 82 } 83 84 public static OutputAnalyzer launchCompiler(String libName, String item, List<String> extraopts, 85 List<String> compList) { 86 Path file = null; 87 if (compList != null && !compList.isEmpty()) { 88 file = Paths.get(METHODS_LIST_FILENAME); 89 try { 90 Files.write(file, compList, StandardOpenOption.CREATE); 91 } catch (IOException e) { 92 throw new Error("Couldn't write " + METHODS_LIST_FILENAME + " " + e, e); 93 } 94 } 95 List<String> args = new ArrayList<>(); 96 args.add("--compile-with-assertions"); 97 args.add("--output"); 98 args.add(libName); 99 if (file != null) { 100 args.add("--compile-commands"); 101 args.add(file.toString()); 102 } 103 args.add("--class-name"); 104 args.add(item); 105 return launchJaotc(args, extraopts); 106 } 107 108 private static OutputAnalyzer launchJaotc(List<String> args, List<String> extraVmOpts) { 109 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jaotc"); 110 for (String vmOpt : Utils.getTestJavaOpts()) { 111 launcher.addVMArg(vmOpt); 112 } 113 if (extraVmOpts != null) { 114 for (String vmOpt : extraVmOpts) { 115 launcher.addVMArg(vmOpt); 116 } 117 } 118 for (String arg : args) { 119 launcher.addToolArg(arg); 120 } 121 try { 122 return new OutputAnalyzer(new ProcessBuilder(launcher.getCommand()).inheritIO().start()); 123 } catch (IOException e) { 124 throw new Error("Can't start test process: " + e, e); 125 } 126 } 127 128 public static void printUsage() { 129 System.err.println("Usage: " + AotCompiler.class.getName() 130 + " -class <class> -libname <.so name>" 131 + " [-compile <compileItems>]* [-extraopt <java option>]*"); 132 } 133} 134