changeset 44761:36aae904cea9

8175819: OS name and arch in JMOD files should match the values as in the bundle names Reviewed-by: erikj, ihse
author mchung
date Thu, 20 Apr 2017 08:00:35 -0700
parents 61b03b960583
children 1d562a53fd19
files jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Platform.java jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ExcludeVMPlugin.java jdk/test/tools/jlink/plugins/SystemModuleDescriptors/SystemModulesTest.java
diffstat 4 files changed, 110 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java	Mon Mar 20 07:38:52 2017 -0400
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java	Thu Apr 20 08:00:35 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -59,6 +59,7 @@
 
 import jdk.tools.jlink.internal.BasicImageWriter;
 import jdk.tools.jlink.internal.ExecutableImage;
+import jdk.tools.jlink.internal.Platform;
 import jdk.tools.jlink.plugin.ResourcePool;
 import jdk.tools.jlink.plugin.ResourcePoolEntry;
 import jdk.tools.jlink.plugin.ResourcePoolEntry.Type;
@@ -133,7 +134,7 @@
     private final Map<String, String> launchers;
     private final Path mdir;
     private final Set<String> modules = new HashSet<>();
-    private String targetOsName;
+    private Platform targetPlatform;
 
     /**
      * Default image builder constructor.
@@ -151,11 +152,14 @@
     @Override
     public void storeFiles(ResourcePool files) {
         try {
-            this.targetOsName = files.moduleView().
-                findModule("java.base").get().osName();
-            if (this.targetOsName == null) {
+            String targetOsName = files.moduleView()
+                                       .findModule("java.base")
+                                       .map(ResourcePoolModule::osName)
+                                       .orElse(null);
+            if (targetOsName == null) {
                 throw new PluginException("ModuleTarget attribute is missing for java.base module");
             }
+            this.targetPlatform = Platform.toPlatform(targetOsName);
 
             checkResourcePool(files);
 
@@ -476,7 +480,7 @@
     }
 
     private boolean isWindows() {
-        return targetOsName.startsWith("Windows");
+        return targetPlatform == Platform.WINDOWS;
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Platform.java	Thu Apr 20 08:00:35 2017 -0700
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.tools.jlink.internal;
+
+import jdk.tools.jlink.plugin.ResourcePoolModule;
+
+import java.util.Locale;
+
+/**
+ * Supported platforms
+ */
+public enum Platform {
+    WINDOWS,
+    LINUX,
+    SOLARIS,
+    MACOS,
+    AIX,
+    UNKNOWN;
+
+    /**
+     * Returns the {@code Platform} of the given OS name specified
+     * in the {@code ModuleTarget} attribute.
+     *
+     * @param osName OS name in ModuleTarget attribute
+     */
+    public static Platform toPlatform(String osName) {
+        try {
+            return Platform.valueOf(osName.toUpperCase(Locale.ENGLISH));
+        } catch (IllegalArgumentException e) {
+            return Platform.UNKNOWN;
+        }
+    }
+
+    /**
+     * Returns the {@code Platform} to which the given module is target to.
+     */
+    public static Platform getTargetPlatform(ResourcePoolModule module) {
+        String osName = module.osName();
+        if (osName != null) {
+            return toPlatform(osName);
+        } else {
+            return Platform.UNKNOWN;
+        }
+    }
+}
--- a/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ExcludeVMPlugin.java	Mon Mar 20 07:38:52 2017 -0400
+++ b/jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ExcludeVMPlugin.java	Thu Apr 20 08:00:35 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,8 @@
 import java.util.TreeSet;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
+
+import jdk.tools.jlink.internal.Platform;
 import jdk.tools.jlink.plugin.Plugin;
 import jdk.tools.jlink.plugin.ResourcePool;
 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
@@ -115,7 +117,7 @@
     @Override
     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
         ResourcePoolModule javaBase = in.moduleView().findModule("java.base").get();
-        String[] jvmlibs = jvmlibs(javaBase.osName());
+        String[] jvmlibs = jvmlibs(javaBase);
         TreeSet<Jvm> existing = new TreeSet<>(new JvmComparator());
         TreeSet<Jvm> removed = new TreeSet<>(new JvmComparator());
         if (!keepAll) {
@@ -257,21 +259,15 @@
         return orig.copyWithContent(content);
     }
 
-    private static String[] jvmlibs(String osName) {
-        if (isWindows(osName)) {
-            return new String[] { "jvm.dll" };
-        } else if (isMac(osName)) {
-            return new String[] { "libjvm.dylib", "libjvm.a" };
-        } else {
-            return new String[] { "libjvm.so", "libjvm.a" };
+    private static String[] jvmlibs(ResourcePoolModule module) {
+        Platform platform = Platform.getTargetPlatform(module);
+        switch (platform) {
+            case WINDOWS:
+                return new String[] { "jvm.dll" };
+            case MACOS:
+                return new String[] { "libjvm.dylib", "libjvm.a" };
+            default:
+                return new String[] { "libjvm.so", "libjvm.a" };
         }
     }
-
-    private static boolean isWindows(String osName) {
-        return osName.startsWith("Windows");
-    }
-
-    private static boolean isMac(String osName) {
-        return osName.startsWith("Mac OS") || osName.startsWith("Darwin");
-    }
 }
--- a/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/SystemModulesTest.java	Mon Mar 20 07:38:52 2017 -0400
+++ b/jdk/test/tools/jlink/plugins/SystemModuleDescriptors/SystemModulesTest.java	Thu Apr 20 08:00:35 2017 -0700
@@ -71,17 +71,23 @@
                     .forEach(this::checkAttributes);
     }
 
-    // JMOD files are created with osName and osArch that may be different
-    // than os.name and os.arch system property
+    // JMOD files are created with OS name and arch matching the bundle name
     private boolean checkOSName(String name) {
-        if (name.equals(OS_NAME))
-            return true;
+        if (OS_NAME.startsWith("Windows")) {
+            return name.equals("windows");
+        }
 
-        if (OS_NAME.startsWith("Windows")) {
-            return name.startsWith("Windows");
-        } else {
-            System.err.println("ERROR: " + name + " but expected: " + OS_NAME);
-            return false;
+        switch (OS_NAME) {
+            case "Linux":
+                return name.equals("linux");
+            case "SunOS":
+                return name.equals("solaris");
+            case "Mac OS X":
+                return name.equals("macos");
+            default:
+                // skip validation on unknown platform
+                System.out.println("Skip checking OS name in ModuleTarget: " + name);
+                return true;
         }
     }
 
@@ -94,10 +100,12 @@
             case "x86":
                 return name.equals("x86");
             case "amd64":
-                return name.equals("x86_64");
+            case "x86_64":
+                return name.equals("amd64");
             default:
-                System.err.println("ERROR: " + name + " but expected: " + OS_ARCH);
-                return false;
+                // skip validation on unknown platform
+                System.out.println("Skip checking OS arch in ModuleTarget: " + name);
+                return true;
         }
     }