1 /* Copyright (c) 2014-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 import java.io.FileOutputStream;
9 import java.io.InputStream;
10 import java.io.IOException;
11 import java.io.OutputStream;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
16 /** Helper class loading the native functions of SimGrid that we use for downcalls
18 * Almost all org.simgrid.msg.* classes contain a static bloc (thus executed when the class is loaded)
19 * containing a call to this.
21 public final class NativeLib {
22 private static boolean isNativeInited = false;
23 static Path tempDir = null; // where the embeeded libraries are unpacked before loading them
25 /** A static-only "class" don't need no constructor */
27 throw new IllegalAccessError("Utility class");
30 /** Hidden debug main() function
32 * It is not the Main-Class defined in src/bindings/java/MANIFEST.in (org.simgrid.msg.Msg is),
33 * so it won't get executed by default. But that's helpful to debug linkage errors, if you
34 * know that it exists. It's used by cmake during the configure, to inform the user.
36 public static void main(String[] args) {
37 System.out.println("This jarfile searches the native code under: " +getPath());
40 /** Main function loading all the native classes that we need */
41 public static void nativeInit() {
45 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
46 NativeLib.nativeInit("winpthread-1");
48 NativeLib.nativeInit("simgrid");
49 NativeLib.nativeInit("simgrid-java");
50 isNativeInited = true;
53 /** Helper function trying to load one requested library */
54 public static void nativeInit(String name) {
55 Throwable cause = null;
57 /* Prefer the version of the library bundled into the jar file and use it */
58 if (loadLibAsStream(name))
60 } catch (UnsatisfiedLinkError|SecurityException|IOException e) {
64 /* If not found, try to see if we can find a version on disk */
66 System.loadLibrary(name);
68 } catch (UnsatisfiedLinkError systemException) { /* don't care */ }
70 System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath()+" and no usable SimGrid installation found on disk.");
72 if (cause.getMessage().matches(".*libcgraph.so.*"))
73 System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
74 else if (cause.getMessage().matches(".*libboost_context.so.*"))
75 System.err.println("HINT: Try to install the boost-context package (sudo apt-get install libboost-context-dev).");
77 System.err.println("Try to install the missing dependencies, if any. Read carefully the following error message.");
80 cause.printStackTrace();
82 System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk.");
87 /** Try to extract the library from the jarfile before loading it */
88 private static boolean loadLibAsStream (String name) throws SecurityException, IOException, UnsatisfiedLinkError {
89 String path = NativeLib.getPath();
91 /* For each possible filename of the given library on all possible OSes, try it */
92 for (String filename : new String[]
94 "lib"+name+".so", /* linux */
95 name+".dll", "lib"+name+".dll", /* windows (pure and mingw) */
96 "lib"+name+".dylib" /* mac osx */}) {
98 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
100 continue; // Try the next name: no such file found
102 OutputStream out = null;
104 // We must write the lib onto the disk before loading it -- stupid operating systems
105 if (tempDir == null) {
106 tempDir = Files.createTempDirectory("simgrid-java-");
107 // don't leak the files on disk, but remove it on JVM shutdown
108 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
110 File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
112 /* copy the library in position */
113 out = new FileOutputStream(fileOut);
114 byte[] buffer = new byte[4096];
116 while ((bytesRead = in.read(buffer)) != -1) // Read until EOF
117 out.write(buffer, 0, bytesRead);
120 System.load(fileOut.getAbsolutePath());
122 /* It loaded! we're good */
126 /* Always close all descriptors, no matter success or error */
130 } catch (IOException e) {
131 /* Too bad. I dont care. */
136 /* No suitable name found */
140 /** Find where to search for the library in the jar -- keep it aligned with where cmake puts it! */
141 private static String getPath() {
142 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
143 String prefix = "NATIVE";
144 String os = System.getProperty("os.name");
145 String arch = System.getProperty("os.arch");
147 if (arch.matches("^i[3-6]86$"))
149 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
152 if (os.toLowerCase().startsWith("win")){
154 } else if (os.contains("OS X"))
157 os = os.replace(' ', '_');
158 arch = arch.replace(' ', '_');
160 return prefix + "/" + os + "/" + arch + "/";
163 /** A hackish mechanism used to remove the file containing our library when the JVM shuts down */
164 private static class FileCleaner implements Runnable {
166 public FileCleaner(File dir) {
172 for (File f : dir.listFiles())
174 System.err.println("Unable to clean temporary file "+f.getAbsolutePath()+" during shutdown.");
176 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");
177 } catch(Exception e) {
178 System.err.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown: "+e.getCause());