1 /*
   2  * Copyright (c) 2016, 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 compiler.aot;
  25 
  26 import jdk.test.lib.Platform;
  27 import jdk.test.lib.artifacts.Artifact;
  28 import jdk.test.lib.artifacts.ArtifactResolver;
  29 import jdk.test.lib.process.OutputAnalyzer;
  30 import java.io.File;
  31 import java.io.FileNotFoundException;
  32 import java.io.IOException;
  33 import java.lang.annotation.Annotation;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.nio.file.StandardOpenOption;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 import jdk.test.lib.JDKToolLauncher;
  42 import jdk.test.lib.Utils;
  43 import jdk.test.lib.process.ProcessTools;
  44 
  45 /**
  46  * A simple class calling AOT compiler over requested items
  47  */
  48 public class AotCompiler {
  49 
  50     private final static String METHODS_LIST_FILENAME = "methodsList.txt";
  51 
  52     public static void main(String args[]) {
  53         String className = null;
  54         List<String> compileList = new ArrayList<>();
  55         String libName = null;
  56         List<String> extraopts = new ArrayList<>();
  57         for (int i = 0; i < args.length; i++) {
  58             switch (args[i]) {
  59                 case "-class":
  60                     className = args[++i];
  61                     break;
  62                 case "-compile":
  63                     compileList.add("compileOnly " + args[++i]);
  64                     break;
  65                 case "-libname":
  66                     libName = args[++i];
  67                     break;
  68                 case "-extraopt":
  69                     extraopts.add(args[++i]);
  70                     break;
  71                 default:
  72                     throw new Error("Unknown option: " + args[i]);
  73             }
  74         }
  75         extraopts.add("-classpath");
  76         extraopts.add(Utils.TEST_CLASS_PATH + File.pathSeparator + Utils.TEST_SRC);
  77         if (className != null && libName != null) {
  78             OutputAnalyzer oa = launchCompiler(libName, className, extraopts, compileList);
  79             oa.shouldHaveExitValue(0);
  80         } else {
  81             printUsage();
  82             throw new Error("Mandatory arguments aren't passed");
  83         }
  84     }
  85 
  86     public static OutputAnalyzer launchCompilerSimple(String... args) {
  87         return launchJaotc(Arrays.asList(args), null);
  88     }
  89 
  90     public static OutputAnalyzer launchCompiler(String libName, String item, List<String> extraopts,
  91             List<String> compList) {
  92         Path file = null;
  93         if (compList != null && !compList.isEmpty()) {
  94             file = Paths.get(METHODS_LIST_FILENAME);
  95             try {
  96                 Files.write(file, compList, StandardOpenOption.CREATE);
  97             } catch (IOException e) {
  98                 throw new Error("Couldn't write " + METHODS_LIST_FILENAME + " " + e, e);
  99             }
 100         }
 101         List<String> args = new ArrayList<>();
 102         args.add("--compile-with-assertions");
 103         args.add("--output");
 104         args.add(libName);
 105         if (file != null) {
 106             args.add("--compile-commands");
 107             args.add(file.toString());
 108         }
 109         args.add("--class-name");
 110         args.add(item);
 111         String linker = resolveLinker();
 112         if (linker != null) {
 113             args.add("--linker-path");
 114             args.add(linker);
 115         }
 116         // Execute with asserts
 117         args.add("-J-ea");
 118         args.add("-J-esa");
 119         return launchJaotc(args, extraopts);
 120     }
 121 
 122     private static OutputAnalyzer launchJaotc(List<String> args, List<String> extraVmOpts) {
 123         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jaotc");
 124         for (String vmOpt : Utils.getTestJavaOpts()) {
 125             launcher.addVMArg(vmOpt);
 126         }
 127         if (extraVmOpts != null) {
 128             for (String vmOpt : extraVmOpts) {
 129                 launcher.addVMArg(vmOpt);
 130             }
 131         }
 132         for (String arg : args) {
 133             launcher.addToolArg(arg);
 134         }
 135         try {
 136             return ProcessTools.executeCommand(new ProcessBuilder(launcher.getCommand()).redirectErrorStream(true));
 137         } catch (Throwable e) {
 138             throw new Error("Can't start test process: " + e, e);
 139         }
 140     }
 141 
 142     public static void printUsage() {
 143         System.err.println("Usage: " + AotCompiler.class.getName()
 144                 + " -class <class> -libname <.so name>"
 145                 + " [-compile <compileItems>]* [-extraopt <java option>]*");
 146     }
 147 
 148     // runs ld -v (or ld -V on solaris) and check its exit code
 149     private static boolean checkLd(Path bin) {
 150         try {
 151             return 0 == ProcessTools.executeCommand(bin.toString(),
 152                                                     Platform.isSolaris() ? "-V" : "-v")
 153                                     .getExitValue();
 154         } catch (Throwable t) {
 155                         // any errors mean ld doesn't work
 156             return false;
 157         }
 158     }
 159 
 160     public static String resolveLinker() {
 161         Path linker = null;
 162         // if non windows, 1st, check if PATH has ld
 163         if (!Platform.isWindows()) {
 164             String bin = "ld";
 165             for (String path : System.getenv("PATH").split(File.pathSeparator)) {
 166                     Path ld = Paths.get(path).resolve("ld");
 167                     if (Files.exists(ld)) {
 168                         // there is ld in PATH
 169                         if (checkLd(ld)) {
 170                             System.out.println("found working linker: " + ld);
 171                             // ld works, jaotc is supposed to find and use it
 172                             return null;
 173                         } else {
 174                             System.out.println("found broken linker: " + ld);
 175                             // ld exists in PATH, but doesn't work, have to use devkit
 176                             break;
 177                         }
 178                     }
 179                         }
 180         }
 181         // there is no ld in PATH, will use ld from devkit
 182         // artifacts are got from common/conf/jib-profiles.js
 183         try {
 184             if (Platform.isWindows()) {
 185                 if (Platform.isX64()) {
 186                     @Artifact(organization = "jpg.infra.builddeps",
 187                             name = "devkit-windows_x64",
 188                             revision = "VS2013SP4+1.0",
 189                             extension = "tar.gz")
 190                     class DevkitWindowsX64 { }
 191                     String artifactName = "jpg.infra.builddeps."
 192                             + "devkit-windows_x64-"
 193                             + "VS2013SP4+1.0";
 194                     Path devkit = ArtifactResolver.resolve(DevkitWindowsX64.class)
 195                                                   .get(artifactName);
 196                     linker = devkit.resolve("VC")
 197                                    .resolve("bin")
 198                                    .resolve("amd64")
 199                                    .resolve("link.exe");
 200                 }
 201             } else if (Platform.isOSX()) {
 202                 @Artifact(organization =  "jpg.infra.builddeps",
 203                         name = "devkit-macosx_x64",
 204                         revision = "Xcode6.3-MacOSX10.9+1.0",
 205                         extension = "tar.gz")
 206                 class DevkitMacosx { }
 207                 String artifactName = "jpg.infra.builddeps."
 208                         + "devkit-macosx_x64-"
 209                         + "Xcode6.3-MacOSX10.9+1.0";
 210                 Path devkit = ArtifactResolver.resolve(DevkitMacosx.class)
 211                                               .get(artifactName);
 212                 linker = devkit.resolve("Xcode.app")
 213                                .resolve("Contents")
 214                                .resolve("Developer")
 215                                .resolve("Toolchains")
 216                                .resolve("XcodeDefault.xctoolchain")
 217                                .resolve("usr")
 218                                .resolve("bin")
 219                                .resolve("ld");
 220             } else if (Platform.isSolaris()) {
 221                 if (Platform.isSparc()) {
 222                     @Artifact(organization =  "jpg.infra.builddeps",
 223                             name = "devkit-solaris_sparcv9",
 224                             revision = "SS12u4-Solaris11u1+1.0",
 225                             extension = "tar.gz")
 226                     class DevkitSolarisSparc { }
 227 
 228                     String artifactName = "jpg.infra.builddeps."
 229                             + "devkit-solaris_sparcv9-"
 230                             + "SS12u4-Solaris11u1+1.0";
 231                     Path devkit = ArtifactResolver.resolve(DevkitSolarisSparc.class)
 232                                                   .get(artifactName);
 233                     linker = devkit.resolve("SS12u4-Solaris11u1")
 234                                    .resolve("gnu")
 235                                    .resolve("bin")
 236                                    .resolve("ld");
 237                 } else if (Platform.isX64()) {
 238                     @Artifact(organization =  "jpg.infra.builddeps",
 239                             name = "devkit-solaris_x64",
 240                             revision = "SS12u4-Solaris11u1+1.0",
 241                             extension = "tar.gz")
 242                     class DevkitSolarisX64 { }
 243 
 244                     String artifactName = "jpg.infra.builddeps."
 245                             + "devkit-solaris_x64-"
 246                             + "SS12u4-Solaris11u1+1.0";
 247                     Path devkit = ArtifactResolver.resolve(DevkitSolarisX64.class)
 248                                                   .get(artifactName);
 249                     linker = devkit.resolve("SS12u4-Solaris11u1")
 250                                    .resolve("bin")
 251                                    .resolve("amd64")
 252                                    .resolve("ld");
 253                 }
 254             } else if (Platform.isLinux()) {
 255                 if (Platform.isAArch64()) {
 256                     @Artifact(organization = "jpg.infra.builddeps",
 257                             name = "devkit-linux_aarch64",
 258                             revision = "gcc-linaro-aarch64-linux-gnu-4.8-2013.11_linux+1.0",
 259                             extension = "tar.gz")
 260                     class DevkitLinuxAArch64 { }
 261 
 262                     String artifactName = "jpg.infra.builddeps."
 263                             + "devkit-linux_aarch64-"
 264                             + "gcc-linaro-aarch64-linux-gnu-4.8-2013.11_linux+1.0";
 265                     Path devkit = ArtifactResolver.resolve(DevkitLinuxAArch64.class)
 266                                                   .get(artifactName);
 267                     linker = devkit.resolve("aarch64-linux-gnu")
 268                                    .resolve("bin")
 269                                    .resolve("ld");
 270                 } else if (Platform.isARM()) {
 271                     @Artifact(organization = "jpg.infra.builddeps",
 272                             name = "devkit-linux_arm",
 273                             revision = "gcc-linaro-arm-linux-gnueabihf-raspbian-2012.09-20120921_linux+1.0",
 274                             extension = "tar.gz")
 275                     class DevkitLinuxARM { }
 276 
 277                     String artifactName = "jpg.infra.builddeps."
 278                             + "devkit-linux_arm-"
 279                             + "gcc-linaro-arm-linux-gnueabihf-raspbian-2012.09-20120921_linux+1.0";
 280                     Path devkit = ArtifactResolver.resolve(DevkitLinuxARM.class)
 281                                                   .get(artifactName);
 282                     linker = devkit.resolve("arm-linux-gnueabihf")
 283                                    .resolve("bin")
 284                                    .resolve("ld");
 285                 } else if (Platform.isX64()) {
 286                     @Artifact(organization = "jpg.infra.builddeps",
 287                             name = "devkit-linux_x64",
 288                             revision = "gcc4.9.2-OEL6.4+1.1",
 289                             extension = "tar.gz")
 290                     class DevkitLinuxX64 { }
 291 
 292                     String artifactName = "jpg.infra.builddeps."
 293                             + "devkit-linux_x64-"
 294                             + "gcc4.9.2-OEL6.4+1.1";
 295                     Path devkit = ArtifactResolver.resolve(DevkitLinuxX64.class)
 296                                                   .get(artifactName);
 297                     linker = devkit.resolve("bin")
 298                                    .resolve("ld");
 299                 }
 300             }
 301         } catch (FileNotFoundException e) {
 302             System.err.println("artifact resolution error: " + e);
 303             // let jaotc try to find linker
 304             return null;
 305         }
 306         if (linker != null) {
 307             return linker.toAbsolutePath().toString();
 308         }
 309         return null;
 310     }
 311 }