tdlight-java/src/main/java/it/tdlight/common/utils/LoadLibrary.java

330 lines
9.9 KiB
Java
Raw Normal View History

2018-07-18 12:24:37 +02:00
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.common.utils;
2018-07-18 12:24:37 +02:00
import java.io.IOException;
import java.io.InputStream;
2021-10-22 12:54:28 +02:00
import java.lang.reflect.InvocationTargetException;
2018-11-26 16:17:57 +01:00
import java.nio.ByteOrder;
2020-04-19 16:32:49 +02:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2021-10-23 01:25:23 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2021-09-27 19:27:13 +02:00
import java.util.Objects;
2021-10-23 01:25:23 +02:00
import java.util.StringJoiner;
2018-07-18 12:24:37 +02:00
import java.util.concurrent.ConcurrentHashMap;
/**
* The class to load the libraries needed to run Tdlib
*/
2021-09-27 19:27:13 +02:00
public final class LoadLibrary {
2021-01-10 19:06:47 +01:00
private static final ConcurrentHashMap<String, Boolean> libraryLoaded = new ConcurrentHashMap<>();
private static final Path librariesPath = Paths.get(".cache");
2021-09-27 19:27:13 +02:00
private static final String libsVersion =
LibraryVersion.IMPLEMENTATION_NAME + "-" + LibraryVersion.VERSION + "-" + LibraryVersion.NATIVES_VERSION;
2020-04-19 16:32:49 +02:00
static {
if (Files.notExists(librariesPath)) {
try {
Files.createDirectories(librariesPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Load a library installed in the system (priority choice) or a library included in the jar.
*
* @param libname The name of the library.
* @throws CantLoadLibrary An exception that is thrown when the LoadLibrary class fails to load the library.
*/
2020-08-30 00:46:28 +02:00
public static void load(String libname) throws CantLoadLibrary {
2020-04-19 16:32:49 +02:00
if (libname == null || libname.trim().isEmpty()) {
throw new IllegalArgumentException();
}
if (libraryLoaded.containsKey(libname)) {
if (libraryLoaded.get(libname)) {
return;
}
}
loadLibrary(libname);
libraryLoaded.put(libname, true);
}
2020-08-30 00:46:28 +02:00
private static void loadLibrary(String libname) throws CantLoadLibrary {
Arch arch = getCpuArch();
Os os = getOs();
2020-04-19 16:32:49 +02:00
if (arch == Arch.UNKNOWN) {
throw new CantLoadLibrary("Arch: \"" + System.getProperty("os.arch") + "\" is unknown");
2020-04-19 16:32:49 +02:00
}
if (os == Os.UNKNOWN) {
throw new CantLoadLibrary("Os: \"" + System.getProperty("os.name") + "\" is unknown");
2020-04-19 16:32:49 +02:00
}
try {
loadJarLibrary(libname, arch, os);
} catch (CantLoadLibrary | UnsatisfiedLinkError e) {
2020-08-23 15:36:45 +02:00
if (loadSysLibrary(libname)) {
return;
}
2020-08-30 00:46:28 +02:00
throw (CantLoadLibrary) new CantLoadLibrary().initCause(e);
2020-04-19 16:32:49 +02:00
}
}
private static boolean loadSysLibrary(String libname) {
try {
System.loadLibrary(libname);
} catch (UnsatisfiedLinkError e) {
return false;
}
return true;
}
2021-10-23 01:25:23 +02:00
private static String removeLastPackageParts(String clazz, int count, String className) {
List<String> parts = new ArrayList<>(Arrays.asList(clazz.split("\\.")));
parts.remove(parts.size() - 1);
for (int i = 0; i < count; i++) {
parts.remove(parts.size() - 1);
}
StringJoiner joiner = new StringJoiner(".");
for (String part : parts) {
joiner.add(part);
}
joiner.add(className);
return joiner.toString();
}
private static void loadJarLibrary(String libname, Arch arch, Os os) throws CantLoadLibrary {
Path tempPath;
try {
tempPath = Files.createDirectories(librariesPath.resolve("version-" + libsVersion).resolve(libname));
} catch (IOException e) {
throw new CantLoadLibrary("Can't create temporary files", e);
}
2020-04-19 16:32:49 +02:00
Path tempFile = Paths.get(tempPath.toString(), libname + getExt(os));
2020-08-21 00:01:01 +02:00
Class<?> classForResource = null;
switch (os) {
case LINUX:
2020-08-21 00:01:01 +02:00
switch (arch) {
case AMD64:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_AMD64_CLASS, os, arch);
2020-08-21 00:01:01 +02:00
break;
case I386:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_X86_CLASS, os, arch);
2020-12-29 23:38:50 +01:00
break;
case AARCH64:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_AARCH64_CLASS, os, arch);
2020-08-21 00:01:01 +02:00
break;
case ARMHF:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_ARMHF_CLASS, os, arch);
2020-12-29 23:38:50 +01:00
break;
case S390X:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_S390X_CLASS, os, arch);
2020-12-29 23:38:50 +01:00
break;
case PPC64LE:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.LINUX_PPC64LE_CLASS, os, arch);
2020-12-29 23:38:50 +01:00
break;
2020-08-21 00:01:01 +02:00
}
break;
case OSX:
if (arch == Arch.AMD64) {
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.OSX_AMD64_CLASS, os, arch);
2020-08-23 19:48:42 +02:00
}
break;
case WINDOWS:
2020-12-29 23:38:50 +01:00
switch (arch) {
case AMD64:
2021-10-23 01:46:27 +02:00
classForResource = tryLoadLibraryVersionClass(LibraryVersion.WINDOWS_AMD64_CLASS, os, arch);
2020-12-29 23:38:50 +01:00
break;
case I386:
2020-12-29 23:38:50 +01:00
break;
2020-08-21 00:01:01 +02:00
}
break;
}
if (classForResource == null) {
2021-10-23 01:46:27 +02:00
throw new CantLoadLibrary("Native libraries for platform " + os + "-" + arch + " not found!"
+ " Required version: " + getRequiredVersionName(os, arch));
2020-08-21 00:01:01 +02:00
}
InputStream libInputStream;
try {
2021-09-27 19:27:13 +02:00
libInputStream = Objects.requireNonNull((InputStream) classForResource
.getDeclaredMethod("getLibraryAsStream")
.invoke(InputStream.class));
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | NullPointerException e) {
throw new CantLoadLibrary("Native libraries for platform " + os + "-" + arch + " not found!", e);
}
2020-04-19 16:32:49 +02:00
if (Files.notExists(tempFile)) {
try {
Files.copy(libInputStream, tempFile);
} catch (IOException e) {
throw new CantLoadLibrary("Can't copy native libraries into temporary files", e);
}
}
try {
libInputStream.close();
} catch (IOException e) {
throw new CantLoadLibrary("Can't load the native libraries", e);
2020-04-19 16:32:49 +02:00
}
System.load(tempFile.toFile().getAbsolutePath());
}
2021-10-23 01:46:27 +02:00
private static Class<?> tryLoadLibraryVersionClass(String classForResource, Os os, Arch arch) throws CantLoadLibrary {
2021-10-23 01:25:23 +02:00
try {
return Class.forName(classForResource);
} catch (ClassNotFoundException e1) {
// exact library not found
//Check if a wrong version is installed
try {
Class<?> foundAnotherVersion = Class.forName(removeFromVersion(classForResource));
throw new CantLoadLibrary("Can't load the native libraries."
+ " A different version of the native libraries was found."
2021-10-23 01:46:27 +02:00
+ " Please make sure that you installed the correct one."
+ " Required version: " + getRequiredVersionName(os, arch), e1);
2021-10-23 01:25:23 +02:00
} catch (ClassNotFoundException e2) {
// not found arch
//Check if a wrong arch is installed
try {
Class<?> foundAnotherArch = Class.forName(removeFromArch(classForResource));
throw new CantLoadLibrary("Can't load the native libraries."
+ " A different architecture of the native libraries was found."
2021-10-23 01:46:27 +02:00
+ " Please make sure that you installed the correct one."
+ " Required version: " + getRequiredVersionName(os, arch), e1);
2021-10-23 01:25:23 +02:00
} catch (ClassNotFoundException e3) {
// not found os
//Check if a wrong os is installed
try {
Class<?> foundAnotherOs = Class.forName(removeFromOs(classForResource));
throw new CantLoadLibrary("Can't load the native libraries."
+ " A different OS of the native libraries was found."
2021-10-23 01:46:27 +02:00
+ " Please make sure that you installed the correct one."
+ " Required version: " + getRequiredVersionName(os, arch), e1);
2021-10-23 01:25:23 +02:00
} catch (ClassNotFoundException e4) {
// not found anything
// No library was found, return
return null;
}
}
}
}
}
2021-10-23 01:46:27 +02:00
private static String getRequiredVersionName(Os os, Arch arch) {
return LibraryVersion.IMPLEMENTATION_NAME + " " + os + " " + arch + " 4.0." + LibraryVersion.NATIVES_VERSION;
}
2021-10-23 01:25:23 +02:00
private static String removeFromVersion(String libraryVersionClass) {
return removeLastPackageParts(libraryVersionClass, 1, "AnyVersion");
}
private static String removeFromArch(String libraryVersionClass) {
return removeLastPackageParts(libraryVersionClass, 2, "AnyArch");
}
private static String removeFromOs(String libraryVersionClass) {
return removeLastPackageParts(libraryVersionClass, 3, "AnyOs");
}
2020-04-19 16:32:49 +02:00
private static Arch getCpuArch() {
String architecture = System.getProperty("os.arch").trim();
2020-04-19 16:32:49 +02:00
switch (architecture) {
case "amd64":
case "x86_64":
return Arch.AMD64;
2020-04-19 16:32:49 +02:00
case "i386":
case "x86":
case "386":
case "i686":
case "686":
return Arch.I386;
2020-12-29 23:38:50 +01:00
case "armv6":
2020-04-19 16:32:49 +02:00
case "arm":
case "armhf":
2020-12-29 23:38:50 +01:00
case "aarch32":
case "armv7":
case "armv7l":
return Arch.ARMHF;
2020-04-19 16:32:49 +02:00
case "arm64":
case "aarch64":
case "armv8":
case "armv8l":
return Arch.AARCH64;
2020-12-29 23:38:50 +01:00
case "powerpc":
case "powerpc64":
case "powerpc64le":
case "powerpc64el":
case "ppc":
2020-04-19 16:32:49 +02:00
case "ppc64":
case "ppc64le":
case "ppc64el":
2021-09-27 19:27:13 +02:00
if (ByteOrder
.nativeOrder()
.equals(ByteOrder.LITTLE_ENDIAN)) // Java always returns ppc64 for all 64-bit powerpc but
{
return Arch.PPC64LE; // powerpc64le (our target) is very different, it uses this condition to accurately identify the architecture
2021-09-27 19:27:13 +02:00
} else {
return Arch.UNKNOWN;
2021-09-27 19:27:13 +02:00
}
2020-04-19 16:32:49 +02:00
default:
return Arch.UNKNOWN;
2020-04-19 16:32:49 +02:00
}
}
public static Os getOs() {
String os = System.getProperty("os.name").toLowerCase().trim();
2021-09-27 19:27:13 +02:00
if (os.contains("linux")) {
return Os.LINUX;
2021-09-27 19:27:13 +02:00
}
if (os.contains("windows")) {
return Os.WINDOWS;
2021-09-27 19:27:13 +02:00
}
if (os.contains("mac")) {
return Os.OSX;
2021-09-27 19:27:13 +02:00
}
if (os.contains("darwin")) {
return Os.OSX;
2021-09-27 19:27:13 +02:00
}
return Os.UNKNOWN;
2020-04-19 16:32:49 +02:00
}
private static String getExt(Os os) {
2020-08-23 19:34:08 +02:00
switch (os) {
case WINDOWS:
2020-08-23 19:34:08 +02:00
return ".dll";
case OSX:
2020-08-23 19:34:08 +02:00
return ".dylib";
case LINUX:
case UNKNOWN:
2020-08-23 19:34:08 +02:00
default:
return ".so";
}
2020-04-19 16:32:49 +02:00
}
2018-07-18 12:24:37 +02:00
}