1 /* 2 * Copyright (c) 2020, 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 import java.nio.file.Path; 25 import java.nio.file.Paths; 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.regex.Matcher; 29 import java.util.regex.Pattern; 30 import java.util.spi.ToolProvider; 31 32 public class JtregJextract { 33 private static final ToolProvider JEXTRACT = ToolProvider.findFirst("jextract") 34 .orElseThrow(() -> 35 new RuntimeException("jextract tool not found") 36 ); 37 38 private final Path inputDir; 39 private final Path outputDir; 40 41 JtregJextract() { 42 this(null, null); 43 } 44 45 JtregJextract(Path input, Path output) { 46 inputDir = (input != null) ? input : 47 Paths.get(System.getProperty("test.src", ".")); 48 outputDir = (output != null) ? output : 49 Paths.get(System.getProperty("test.classes", ".")); 50 51 } 52 53 protected String[] processArgs(String... args) { 54 Pattern sysPropPattern = Pattern.compile("'?\\$\\((.*)\\)'?"); 55 ArrayList<String> jextrOpts = new ArrayList<>(); 56 57 jextrOpts.clear(); 58 jextrOpts.add("-C-nostdinc"); 59 jextrOpts.add("-I"); 60 jextrOpts.add(inputDir.toAbsolutePath().toString()); 61 jextrOpts.add("--output"); 62 jextrOpts.add(outputDir.toAbsolutePath().toString()); 63 jextrOpts.add("--compile"); 64 65 int i = 0; 66 while (i < args.length) { 67 String opt = args[i++]; 68 switch (opt) { 69 case "--": 70 break; 71 case "--output": 72 case "--compile": 73 i++; 74 continue; 75 default: { 76 // Pattern $(system.property.name) is replaced with the 77 // value of the System property of that name. 78 Matcher m = sysPropPattern.matcher(opt); 79 if (m.matches()) { 80 jextrOpts.add(System.getProperty(m.group(1))); 81 } else { 82 jextrOpts.add(opt); 83 } 84 } 85 } 86 } 87 88 while (i < args.length) { 89 jextrOpts.add(getInputFilePath(args[i++]).toString()); 90 } 91 92 return jextrOpts.toArray(String[]::new); 93 } 94 95 protected int jextract(String... options) { 96 int result = JEXTRACT.run(System.out, System.err, processArgs(options)); 97 if (result != 0) { 98 throw new RuntimeException(JEXTRACT.name() + " returns non-zero value"); 99 } 100 return result; 101 } 102 103 private Path getInputFilePath(String filename) { 104 return inputDir.resolve(filename).toAbsolutePath(); 105 } 106 107 public static int main(String[] args) { 108 JtregJextract jj = new JtregJextract(); 109 return jj.jextract(args); 110 } 111 }