Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5853aeae1775f9775cc410aa164da94427d712c1
[simgrid.git] / src / bindings / java / org / simgrid / NativeLib.java
1 /* Copyright (c) 2014-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 package org.simgrid;
7
8 import java.io.InputStream;
9 import java.io.IOException;
10 import java.io.File;
11 import java.nio.file.Files;
12 import java.nio.file.Path;
13 import java.util.stream.Stream;
14
15 /** Helper class loading the native functions of SimGrid that we use for downcalls
16  *
17  * Almost all org.simgrid.msg.* classes contain a static bloc (thus executed when the class is loaded)
18  * containing a call to this.
19  */
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
23
24         /** A static-only "class" don't need no constructor */
25         private NativeLib() {
26                 throw new IllegalAccessError("Utility class");
27         }
28
29         /** Hidden debug main() function
30          *
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.
34          */
35         public static void main(String[] args) {
36                 System.out.println("This jarfile searches the native code under: " +getPath());
37         }
38         
39         /** Main function loading all the native classes that we need */
40         public static void nativeInit() {
41                 if (isNativeInited)
42                         return;
43
44                 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
45                         NativeLib.nativeInit("winpthread-1");
46
47                 NativeLib.nativeInit("simgrid");
48                 NativeLib.nativeInit("simgrid-java");
49                 isNativeInited = true;
50         }
51
52         /** Helper function trying to load one requested library */
53         public static void nativeInit(String name) {
54                 Throwable cause = null;
55                 try {
56                         /* Prefer the version of the library bundled into the jar file and use it */
57                         if (loadLibAsStream(name))
58                                 return;
59                 } catch (UnsatisfiedLinkError|SecurityException|IOException e) {
60                         cause = e;
61                 }
62                 
63                 /* If not found, try to see if we can find a version on disk */
64                 try {
65                         System.loadLibrary(name);
66                         return;
67                 } catch (UnsatisfiedLinkError systemException) { /* don't care */ }
68                 
69                 System.err.println("\nCannot load the bindings to the "+name+" library in path "+getPath()+" and no usable SimGrid installation found on disk.");
70                 if (cause != null) {
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).");
75                         else
76                                 System.err.println("Try to install the missing dependencies, if any. Read carefully the following error message.");
77
78                         System.err.println();
79                         cause.printStackTrace();
80                 } else {
81                         System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk for "+name+".");
82                 }
83                 System.exit(1);
84         }
85
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();
89                 
90                 // We must write the lib onto the disk before loading it -- stupid operating systems
91                 if (tempDir == null) {
92
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);
104
105                                 }
106                         }
107
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())));
111                 }
112                 
113                 /* For each possible filename of the given library on all possible OSes, try it */
114                 for (String filename : new String[]
115                    { name,
116                      "lib"+name+".so",               /* linux */
117                      name+".dll", "lib"+name+".dll", /* windows (pure and mingw) */
118                      "lib"+name+".dylib"             /* macOS */}) {
119                                                 
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);
123                         ) {
124                                 if (in != null) {
125                                         /* copy the library in position */
126                                         Files.copy(in, fileOut.toPath());
127
128                                         /* load that library */
129                                         System.load(fileOut.getAbsolutePath());
130
131                                         /* It loaded! we're good */
132                                         return true;
133                                 }
134                         }
135                 }
136                 
137                 /* No suitable name found */
138                 return false;
139         }
140
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");
147
148                 if (arch.matches("^i[3-6]86$"))
149                         arch = "x86";
150                 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
151                         arch = "amd64";
152
153                 if (os.toLowerCase().startsWith("win")) {
154                         os = "Windows";
155                 } else if (os.contains("OS X")) {
156                         os = "Darwin";
157                 }
158                 os = os.replace(' ', '_');
159                 arch = arch.replace(' ', '_');
160
161                 return prefix + "/" + os + "/" + arch + "/";
162         }
163         
164         /** A hackish mechanism used to remove the file containing our library when the JVM shuts down */
165         private static class FileCleaner implements Runnable {
166                 private File dir;
167                 public FileCleaner(File dir) {
168                         this.dir = dir;
169                 }
170                 @Override
171                 public void run() {
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());
180                                 e.printStackTrace();
181                         }
182                 }
183         }
184 }