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