1 /* 2 * Copyright (c) 2000, 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 25 package sun.jvm.hotspot.utilities; 26 27 /** Provides canonicalized OS and CPU information for the rest of the 28 system. */ 29 30 public class PlatformInfo { 31 /* Returns "win32" if Windows; "linux" if Linux. */ getOS()32 public static String getOS() throws UnsupportedPlatformException { 33 String os = System.getProperty("os.name"); 34 if (os.equals("Linux")) { 35 return "linux"; 36 } else if (os.equals("DragonFly") || os.equals("DragonFlyBSD") ) { 37 return "bsd"; 38 } else if (os.equals("FreeBSD")) { 39 return "bsd"; 40 } else if (os.equals("NetBSD")) { 41 return "bsd"; 42 } else if (os.equals("OpenBSD")) { 43 return "bsd"; 44 } else if (os.contains("Darwin") || os.contains("OS X")) { 45 return "darwin"; 46 } else if (os.startsWith("Windows")) { 47 return "win32"; 48 } else { 49 throw new UnsupportedPlatformException("Operating system " + os + " not yet supported"); 50 } 51 } 52 knownCPU(String cpu)53 public static boolean knownCPU(String cpu) { 54 final String[] KNOWN = 55 new String[] {"i386", "x86", "x86_64", "amd64", "ppc64", "ppc64le", "aarch64"}; 56 57 for(String s : KNOWN) { 58 if(s.equals(cpu)) 59 return true; 60 } 61 62 return false; 63 } 64 65 /* Returns "x86" for x86 based platforms and x86_64 for 64bit x86 66 based platform. Otherwise returns the value of os.arch. If the 67 value is not recognized as supported, an exception is thrown 68 instead. */ 69 getCPU()70 public static String getCPU() throws UnsupportedPlatformException { 71 String cpu = System.getProperty("os.arch"); 72 73 // Check that CPU is supported 74 if (!knownCPU(cpu)) { 75 throw new UnsupportedPlatformException("CPU type " + cpu + " not yet supported"); 76 } 77 78 // Tweeks 79 if (cpu.equals("i386")) 80 return "x86"; 81 82 if (cpu.equals("x86_64")) 83 return "amd64"; 84 85 if (cpu.equals("ppc64le")) 86 return "ppc64"; 87 88 return cpu; 89 90 } 91 92 // this main is invoked from Makefile to make platform specific agent Makefile(s). main(String[] args)93 public static void main(String[] args) { 94 System.out.println(getOS()); 95 } 96 } 97