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.IOException;
 25 import java.io.File;
 26 import java.io.UncheckedIOException;
 27 import java.nio.file.Path;
 28 import java.nio.file.Paths;
 29 import java.util.ArrayList;
 30 import java.util.Arrays;
 31 import java.util.regex.Matcher;
 32 import java.util.regex.Pattern;
 33 
 34 public class JtregJextract {
 35     private final Path inputDir;
 36     private final Path outputDir;
 37 
 38     JtregJextract() {
 39         this(null, null);
 40     }
 41 
 42     JtregJextract(Path input, Path output) {
 43         inputDir = (input != null) ? input :
 44                 Paths.get(System.getProperty("test.src", "."));
 45         outputDir = (output != null) ? output :
 46                 Paths.get(System.getProperty("test.classes", "."));
 47 
 48     }
 49 
 50     protected String[] processArgs(String... args) {
 51         Pattern sysPropPattern = Pattern.compile("'?\\$\\((.*)\\)'?");
 52         ArrayList<String> jextrOpts = new ArrayList<>();
 53 
 54         jextrOpts.clear();
 55         jextrOpts.add("-C-nostdinc");
 56         jextrOpts.add("-I");
 57         jextrOpts.add(inputDir.toAbsolutePath().toString());
 58         jextrOpts.add("-d");
 59         jextrOpts.add(outputDir.toAbsolutePath().toString());
 60 
 61         int i = 0;
 62         while (i < args.length) {
 63             String opt = args[i++];
 64             if ("--".equals(opt)) {
 65                 break;
 66             }
 67 
 68             if ("-libpath".equals(opt)) {
 69                 String lib = args[i];
 70                 jextrOpts.add("-l");
 71                 String libpath = System.getProperty("test.nativepath") + File.separator + System.mapLibraryName(lib);
 72                 System.err.println("jextract driver libpath passed: " + libpath);
 73                 jextrOpts.add(libpath);
 74                 i++;
 75                 continue;
 76             }
 77 
 78             if ("-d".equals(opt)) {
 79                 i++;
 80                 continue;
 81             }
 82             // Pattern $(system.property.name) is replaced with the
 83             // value of the System property of that name.
 84             Matcher m = sysPropPattern.matcher(opt);
 85             if (m.matches()) {
 86                 jextrOpts.add(System.getProperty(m.group(1)));
 87             } else {
 88                 jextrOpts.add(opt);
 89             }
 90         }
 91 
 92         while (i < args.length) {
 93             jextrOpts.add(getInputFilePath(args[i++]).toString());
 94         }
 95 
 96         return jextrOpts.toArray(String[]::new);
 97     }
 98 
 99     protected int jextract(String... options) {
100         String[] args = processArgs(options);
101         String[] commands = new String[args.length + 1];
102         commands[0] = Paths.get(System.getProperty("test.jdk"), "bin", "jextract").toString();
103         System.arraycopy(args, 0, commands, 1, args.length);
104         try {
105             Process proc = new ProcessBuilder(commands).inheritIO().start();
106             int result = proc.waitFor();
107             if (result != 0) {
108                 throw new RuntimeException("jextract returns non-zero value");
109             }
110             return result;
111         } catch (IOException ioExp) {
112             throw new UncheckedIOException(ioExp);
113         } catch (InterruptedException intExp) {
114             throw new RuntimeException(intExp);
115         }
116     }
117 
118     private Path getInputFilePath(String filename) {
119         return inputDir.resolve(filename).toAbsolutePath();
120     }
121 
122     public static int main(String[] args) {
123         JtregJextract jj =  new JtregJextract();
124         return jj.jextract(args);
125     }
126 }