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("--add-modules");
60         commands.add("jdk.incubator.foreign");
61         commands.add("-d");
62         commands.add(outputDir.toAbsolutePath().toString());
63         commands.addAll(files);
64 
65         try {
66             Process proc = new ProcessBuilder(commands).inheritIO().start();
67             int result = proc.waitFor();
68             if (result != 0) {
69                 throw new RuntimeException("javac returns non-zero value: " + result);
70             }
71             return result;
72         } catch (IOException ioExp) {
73             throw new UncheckedIOException(ioExp);
74         } catch (InterruptedException intExp) {
75             throw new RuntimeException(intExp);
76         }
77     }
78 }