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.io.File; 25 import java.io.IOException; 26 import java.io.UncheckedIOException; 27 import java.lang.reflect.Array; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.stream.Collectors; 34 35 public class JtregJextractSources { 36 private static Path getJextractSourcePath() { 37 Path testSrc = Path.of(System.getProperty("test.file")); 38 return Path.of(testSrc.toFile().getName() + "_sources"); 39 } 40 41 public static int main(String[] args) throws IOException { 42 System.err.println("jextract --source mode"); 43 Path sourcePath = getJextractSourcePath(); 44 JtregJextract jj = new JtregJextract(null, sourcePath); 45 String[] newArgs = new String[args.length + 1]; 46 newArgs[0] = "--source"; 47 System.arraycopy(args, 0, newArgs, 1, args.length); 48 jj.jextract(newArgs); 49 50 Path outputDir = Paths.get(System.getProperty("test.classes", ".")); 51 52 List<String> files = Files.find(sourcePath.toAbsolutePath(), 999, (path, ignored) -> path.toString().endsWith(".java")) 53 .map(p -> p.toAbsolutePath().toString()) 54 .collect(Collectors.toList()); 55 56 System.err.println("compiling jextracted sources @ " + sourcePath.toAbsolutePath()); 57 List<String> commands = new ArrayList<>(); 58 commands.add(Paths.get(System.getProperty("test.jdk"), "bin", "javac").toString()); 59 commands.add("-parameters"); 60 commands.add("--add-modules"); 61 commands.add("jdk.incubator.foreign"); 62 commands.add("-d"); 63 commands.add(outputDir.toAbsolutePath().toString()); 64 commands.addAll(files); 65 66 try { 67 Process proc = new ProcessBuilder(commands).inheritIO().start(); 68 int result = proc.waitFor(); 69 if (result != 0) { 70 throw new RuntimeException("javac returns non-zero value: " + result); 71 } 72 return result; 73 } catch (IOException ioExp) { 74 throw new UncheckedIOException(ioExp); 75 } catch (InterruptedException intExp) { 76 throw new RuntimeException(intExp); 77 } 78 } 79 }