Skip to content

Commit

Permalink
Improved folder handling, And add a workaround when failed to resolve…
Browse files Browse the repository at this point in the history
… armhf architecture #32
  • Loading branch information
xerial committed May 7, 2015
1 parent 2dab606 commit 6b46954
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 29 deletions.
Binary file modified lib/org/sqlite/OSInfo.class
Binary file not shown.
122 changes: 93 additions & 29 deletions src/main/java/org/sqlite/util/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,66 @@
//--------------------------------------
package org.sqlite.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;

/**
* Provides OS name and architecture name.
*
*
* @author leo
*
*
*/
public class OSInfo
{
private static HashMap<String, String> archMapping = new HashMap<String, String>();

public static final String X86 = "x86";
public static final String X86_64 = "x86_64";
public static final String IA64_32 = "ia64_32";
public static final String IA64 = "ia64";
public static final String PPC = "ppc";
public static final String PPC64 = "ppc64";

static {
// x86 mappings
archMapping.put(X86, X86);
archMapping.put("i386", X86);
archMapping.put("i486", X86);
archMapping.put("i586", X86);
archMapping.put("i686", X86);
archMapping.put("pentium", X86);

// x86_64 mappings
archMapping.put(X86_64, X86_64);
archMapping.put("amd64", X86_64);
archMapping.put("em64t", X86_64);
archMapping.put("universal", X86_64); // Needed for openjdk7 in Mac

// Itenium 64-bit mappings
archMapping.put(IA64, IA64);
archMapping.put("ia64w", IA64);

// Itenium 32-bit mappings, usually an HP-UX construct
archMapping.put(IA64_32, IA64_32);
archMapping.put("ia64n", IA64_32);

// PowerPC mappings
archMapping.put(PPC, PPC);
archMapping.put("power", PPC);
archMapping.put("powerpc", PPC);
archMapping.put("power_pc", PPC);
archMapping.put("power_rs", PPC);

// TODO: PowerPC 64bit mappings
archMapping.put(PPC64, PPC64);
archMapping.put("power64", PPC64);
archMapping.put("powerpc64", PPC64);
archMapping.put("power_pc64", PPC64);
archMapping.put("power_rs64", PPC64);
}


public static void main(String[] args) {
if (args.length >= 1) {
if ("--os".equals(args[0])) {
Expand All @@ -47,38 +99,50 @@ else if ("--arch".equals(args[0])) {
System.out.print(getNativeLibFolderPathForCurrentOS());
}

/**
* @return Native library fold path for the current operation system in the format
* of <code>OS_name/OS_architecture_name</code>
*/
public static String getNativeLibFolderPathForCurrentOS() {
return getOSName() + "/" + getArchName();
}

/**
* @return Operating system name: <code>"Windows"</code>, <code>"Mac"</code>, or <code>"Linux"</code>.
*/
public static String getOSName() {
return translateOSNameToFolderName(System.getProperty("os.name"));
}

/**
* @return Operation system architecture name.
*/
public static String getArchName() {
// Java 1.8 introduces a system property to determine armel or armhf
if (System.getProperty("sun.arch.abi") != null && System.getProperty("sun.arch.abi").startsWith("gnueabihf")) {
return translateArchNameToFolderName("armhf");
}
return translateArchNameToFolderName(System.getProperty("os.arch"));
String osArch = System.getProperty("os.arch");
if(osArch.startsWith("arm")) {
// Java 1.8 introduces a system property to determine armel or armhf
if(System.getProperty("sun.arch.abi") != null && System.getProperty("sun.arch.abi").startsWith("gnueabihf")) {
return translateArchNameToFolderName("armhf");
}
// For java7, we stil need to if run some shell commands to determine ABI of JVM
if(System.getProperty("os.name").contains("Linux")) {
String javaHome = System.getProperty("java.home");
try {
// determine if first JVM found uses ARM hard-float ABI
String[] cmdarray = {"/bin/sh", "-c", "find '" + javaHome +
"' -name 'libjvm.so' | head -1 | xargs readelf -A | " +
"grep 'Tag_ABI_VFP_args: VFP registers'"};
int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
if(exitCode == 0)
return translateArchNameToFolderName("armhf");
}
catch(IOException e) {
// ignored: fall back to "arm" arch (soft-float ABI)
}
catch(InterruptedException e) {
// ignored: fall back to "arm" arch (soft-float ABI)
}
}
}
else {
String lc = osArch.toLowerCase(Locale.US);
if(archMapping.containsKey(lc))
return archMapping.get(lc);
}
return translateArchNameToFolderName(osArch);
}

/**
* Extracts operating system name from given string.
* @param osName The given string that contains operating system name.
* @return Operating system name: <code>"Windows"</code>, <code>"Mac"</code>, or <code>"Linux"</code>.
*/
public static String translateOSNameToFolderName(String osName) {
static String translateOSNameToFolderName(String osName) {
if (osName.contains("Windows")) {
return "Windows";
}
Expand All @@ -88,16 +152,16 @@ else if (osName.contains("Mac") || osName.contains("Darwin")) {
else if (osName.contains("Linux")) {
return "Linux";
}
else if (osName.contains("AIX")) {
return "AIX";
}

else {
return osName.replaceAll("\\W", "");
}
}

/**
* @param archName
* @return Operation system architecture name.
*/
public static String translateArchNameToFolderName(String archName) {
static String translateArchNameToFolderName(String archName) {
return archName.replaceAll("\\W", "");
}
}
}

0 comments on commit 6b46954

Please sign in to comment.