1 /*
   2  * Copyright (c) 2018, 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 package vm.mlvm.share;
  25 
  26 import jdk.test.lib.JDKToolLauncher;
  27 import jdk.test.lib.Utils;
  28 import jdk.test.lib.process.ProcessTools;
  29 import nsk.share.jdi.sde.InstallSDE;
  30 import vm.mlvm.tools.StratumAP;
  31 
  32 import java.io.IOException;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.util.Arrays;
  37 import java.util.stream.Stream;
  38 
  39 public class StratumClassesBuilder {
  40     public static void main(String[] args) {
  41         Path root = testRoot();
  42         Arrays.stream(args)
  43               .map(root::resolve)
  44               .forEach(StratumClassesBuilder::build);
  45     }
  46 
  47     private static void build(Path file) {
  48         if (Files.notExists(file)) {
  49             throw new Error("can't find " + file);
  50         }
  51         Path dst = Paths.get("bin")
  52                         .resolve("classes");
  53         mkdir(dst);
  54         compile(file, dst);
  55         if (file.getFileName().toString().contains("SDE_")) {
  56             IndifiedClassesBuilder.main(dst.toAbsolutePath().toString());
  57         }
  58         addStratum(dst);
  59     }
  60 
  61     private static void mkdir(Path dst) {
  62         try {
  63             Files.createDirectories(dst);
  64         } catch (IOException e) {
  65             throw new Error("can't create dir " + dst, e);
  66         }
  67     }
  68 
  69     private static void compile(Path file, Path dst) {
  70         JDKToolLauncher javac = JDKToolLauncher.create("javac")
  71                                                .addToolArg("-d")
  72                                                .addToolArg(dst.toString())
  73                                                .addToolArg("-cp")
  74                                                .addToolArg(Utils.TEST_CLASS_PATH)
  75                                                .addToolArg("-processor")
  76                                                .addToolArg(StratumAP.class.getName())
  77                                                .addToolArg(file.toAbsolutePath().toString());
  78 
  79         String[] command = javac.getCommand();
  80         try {
  81             ProcessTools.executeCommand(command)
  82                         .shouldHaveExitValue(0);
  83         } catch (Error | RuntimeException e) {
  84             throw e;
  85         } catch (Throwable e) {
  86             throw new Error("execution of javac(" + Arrays.toString(command) + ") failed", e);
  87         }
  88     }
  89 
  90     private static void addStratum(Path dst) {
  91         try (Stream<Path> files = Files.walk(dst)) {
  92             files.map(Path::toAbsolutePath)
  93                  .filter(p -> p.getFileName().toString().contains("SDE_"))
  94                  .filter(p -> p.toString().endsWith(".class"))
  95                  .forEach(p -> {
  96                      try {
  97                          InstallSDE.install(
  98                                  p.toFile(),
  99                                  classToSmap(p).toFile(),
 100                                  p.toFile(),
 101                                  true);
 102                      } catch (IOException e) {
 103                          throw new Error("can't install sde for " + p);
 104                      }
 105                  });
 106         } catch (IOException e) {
 107             throw new Error("can't traverse " + dst, e);
 108         }
 109     }
 110 
 111     private static Path classToSmap(Path file) {
 112         String filename = file.getFileName().toString();
 113         return file.getParent()
 114                    .resolve(filename.replaceFirst("\\.class$", ".smap"));
 115     }
 116 
 117     private static Path testRoot() {
 118         Path p = Paths.get(Utils.TEST_SRC);
 119         while (!Files.exists(p.resolve("TEST.ROOT"))) {
 120             p = p.getParent();
 121         }
 122         return p;
 123     }
 124 }