1 /* Copyright (c) 2014-2019. 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.InputStream;
9 import java.io.IOException;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.util.stream.Stream;
15 /** Helper class loading the native functions of SimGrid that we use for downcalls
17 * Almost all org.simgrid.msg.* classes contain a static bloc (thus executed when the class is loaded)
18 * containing a call to this.
20 public final class NativeLib {
21 private static boolean isNativeInited = false;
22 private static Path tempDir = null; // where the embeeded libraries are unpacked before loading them
24 /** A static-only "class" don't need no constructor */
26 throw new IllegalAccessError("Utility class");
29 /** Hidden debug main() function
31 * It is not the Main-Class defined in src/bindings/java/MANIFEST.in (org.simgrid.msg.Msg is),
32 * so it won't get executed by default. But that's helpful to debug linkage errors, if you
33 * know that it exists. It's used by cmake during the configure, to inform the user.
35 public static void main(String[] args) {
36 System.out.println("This jarfile searches the native code under: " +getPath());
39 /** Main function loading all the native classes that we need */
40 public static void nativeInit() {
44 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
45 NativeLib.nativeInit("winpthread-1");
47 NativeLib.nativeInit("simgrid");
48 NativeLib.nativeInit("simgrid-java");
49 isNativeInited = true;
52 /** Helper function trying to load one requested library */
53 public static void nativeInit(String name) {
54 Throwable cause = null;
56 /* Prefer the version of the library bundled into the jar file and use it */
57 if (loadLibAsStream(name))
59 } catch (UnsatisfiedLinkError|SecurityException|IOException e) {
63 /* If not found, try to see if we can find a version on disk */
65 System.loadLibrary(name);
67 } catch (UnsatisfiedLinkError systemException) { /* don't care */ }
69 System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath()+" and no usable SimGrid installation found on disk.");
71 if (cause.getMessage().contains("libcgraph.so"))
72 System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
73 else if (cause.getMessage().contains("libboost_context.so"))
74 System.err.println("HINT: Try to install the boost-context package (sudo apt-get install libboost-context-dev).");
76 System.err.println("Try to install the missing dependencies, if any. Read carefully the following error message.");
79 cause.printStackTrace();
81 System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk for "+name+".");
86 /** Try to extract the library from the jarfile before loading it */
87 private static boolean loadLibAsStream (String name) throws IOException, UnsatisfiedLinkError {
88 String path = NativeLib.getPath();
90 // We must write the lib onto the disk before loading it -- stupid operating systems
91 if (tempDir == null) {
93 if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
94 // The cleanup at exit fails on Windows where it is impossible to delete files which are still in
95 // use. Try to remove stale temporary files from previous executions, and limit disk usage.
96 Path tmpdir = (new File(System.getProperty("java.io.tmpdir"))).toPath();
97 try (Stream<Path> paths = Files.find(tmpdir, 1,
98 (Path p, java.nio.file.attribute.BasicFileAttributes a) ->
99 a.isDirectory() && !p.equals(tmpdir) &&
100 p.getFileName().toString().startsWith("simgrid-java-"))) {
101 paths.map(Path::toFile)
102 .map(FileCleaner::new)
103 .forEach(FileCleaner::run);
108 tempDir = Files.createTempDirectory("simgrid-java-");
109 // don't leak the files on disk, but remove it on JVM shutdown
110 Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
113 /* For each possible filename of the given library on all possible OSes, try it */
114 for (String filename : new String[]
116 "lib"+name+".so", /* linux */
117 name+".dll", "lib"+name+".dll", /* windows (pure and mingw) */
118 "lib"+name+".dylib" /* macOS */}) {
120 File fileOut = new File(tempDir.toFile(), filename);
121 try ( // Try-with-resources. These stream will be autoclosed when needed.
122 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
125 /* copy the library in position */
126 Files.copy(in, fileOut.toPath());
128 /* load that library */
129 System.load(fileOut.getAbsolutePath());
131 /* It loaded! we're good */
137 /* No suitable name found */
141 /** Find where to search for the library in the jar -- keep it aligned with where cmake puts it! */
142 private static String getPath() {
143 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
144 String prefix = "NATIVE";
145 String os = System.getProperty("os.name");
146 String arch = System.getProperty("os.arch");
148 if (arch.matches("^i[3-6]86$"))
150 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
153 if (os.toLowerCase().startsWith("win")) {
155 } else if (os.contains("OS X")) {
158 os = os.replace(' ', '_');
159 arch = arch.replace(' ', '_');
161 return prefix + "/" + os + "/" + arch + "/";
164 /** A hackish mechanism used to remove the file containing our library when the JVM shuts down */
165 private static class FileCleaner implements Runnable {
167 public FileCleaner(File dir) {
172 try (Stream<Path> paths = Files.walk(dir.toPath())) {
173 paths.sorted(java.util.Comparator.reverseOrder())
174 .map(java.nio.file.Path::toFile)
175 //.peek(System.err::println) // Prints what gets removed
176 .forEach(java.io.File::delete);
177 } catch(Exception e) {
178 System.err.println("Error while cleaning temporary file " + dir.getAbsolutePath() +
179 ": " + e.getCause());