Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid explicit call to close() on an auto-closeable resource.
[simgrid.git] / src / bindings / java / org / simgrid / NativeLib.java
1 /* Copyright (c) 2014-2018. 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.FileOutputStream;
9 import java.io.InputStream;
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.io.File;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15
16 /** Helper class loading the native functions of SimGrid that we use for downcalls
17  *
18  * Almost all org.simgrid.msg.* classes contain a static bloc (thus executed when the class is loaded)
19  * containing a call to this.
20  */
21 public final class NativeLib {
22         private static boolean isNativeInited = false;
23         private static Path tempDir = null; // where the embeeded libraries are unpacked before loading them
24
25         /** A static-only "class" don't need no constructor */
26         private NativeLib() {
27                 throw new IllegalAccessError("Utility class");
28         }
29
30         /** Hidden debug main() function
31          *
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.
35          */
36         public static void main(String[] args) {
37                 System.out.println("This jarfile searches the native code under: " +getPath());
38         }
39         
40         /** Main function loading all the native classes that we need */
41         public static void nativeInit() {
42                 if (isNativeInited)
43                         return;
44
45                 if (System.getProperty("os.name").toLowerCase().startsWith("win"))
46                         NativeLib.nativeInit("winpthread-1");
47
48                 NativeLib.nativeInit("simgrid");
49                 NativeLib.nativeInit("simgrid-java");
50                 isNativeInited = true;
51         }
52
53         /** Helper function trying to load one requested library */
54         public static void nativeInit(String name) {
55                 Throwable cause = null;
56                 try {
57                         /* Prefer the version of the library bundled into the jar file and use it */
58                         if (loadLibAsStream(name))
59                                 return;
60                 } catch (UnsatisfiedLinkError|SecurityException|IOException e) {
61                         cause = e;
62                 }
63                 
64                 /* If not found, try to see if we can find a version on disk */
65                 try {
66                         System.loadLibrary(name);
67                         return;
68                 } catch (UnsatisfiedLinkError systemException) { /* don't care */ }
69                 
70                 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 != null) {
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).");
76                         else
77                                 System.err.println("Try to install the missing dependencies, if any. Read carefully the following error message.");
78
79                         System.err.println();
80                         cause.printStackTrace();
81                 } else {
82                         System.err.println("This jar file does not seem to fit your system, and no usable SimGrid installation found on disk.");
83                 }
84                 System.exit(1);
85         }
86
87         /** Try to extract the library from the jarfile before loading it */
88         private static boolean loadLibAsStream (String name) throws IOException, UnsatisfiedLinkError {
89                 String path = NativeLib.getPath();
90                 
91                 // We must write the lib onto the disk before loading it -- stupid operating systems
92                 if (tempDir == null) {
93                         tempDir = Files.createTempDirectory("simgrid-java-");
94                         // don't leak the files on disk, but remove it on JVM shutdown
95                         Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
96                 }
97                 
98                 /* For each possible filename of the given library on all possible OSes, try it */
99                 for (String filename : new String[]
100                    { name,
101                      "lib"+name+".so",               /* linux */
102                      name+".dll", "lib"+name+".dll", /* windows (pure and mingw) */
103                      "lib"+name+".dylib"             /* mac osx */}) {
104                                                 
105                         File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
106                         boolean done = false;
107                         try ( // Try-with-resources. These stream will be autoclosed when needed.
108                                 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
109                                 OutputStream out = new FileOutputStream(fileOut);
110                         ) {
111                                 if (in == null)
112                                         continue; // Try the next name: no such file found
113                                 
114                                 /* copy the library in position */
115                                 byte[] buffer = new byte[4096];
116                                 int bytesRead;
117                                 while ((bytesRead = in.read(buffer)) != -1)     // Read until EOF
118                                         out.write(buffer, 0, bytesRead);
119                                 
120                                 done = true;
121                         }
122                         if (done) {
123                                 /* load that shit */
124                                 System.load(fileOut.getAbsolutePath());
125                                 
126                                 /* It loaded! we're good */
127                                 return true;
128                         }
129                 }
130                 
131                 /* No suitable name found */
132                 return false;
133         }
134
135         /** Find where to search for the library in the jar -- keep it aligned with where cmake puts it! */
136         private static String getPath() {
137                 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
138                 String prefix = "NATIVE";
139                 String os = System.getProperty("os.name");
140                 String arch = System.getProperty("os.arch");
141
142                 if (arch.matches("^i[3-6]86$"))
143                         arch = "x86";
144                 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
145                         arch = "amd64";
146
147                 if (os.toLowerCase().startsWith("win")){
148                         os = "Windows";
149                 } else if (os.contains("OS X"))
150                         os = "Darwin";
151
152                 os = os.replace(' ', '_');
153                 arch = arch.replace(' ', '_');
154
155                 return prefix + "/" + os + "/" + arch + "/";
156         }
157         
158         /** A hackish mechanism used to remove the file containing our library when the JVM shuts down */
159         private static class FileCleaner implements Runnable {
160                 private File dir;
161                 public FileCleaner(File dir) {
162                         this.dir = dir;
163                 }
164                 @Override
165                 public void run() {
166                         try {
167                                 for (File f : dir.listFiles())
168                                         if (! f.delete() && !f.getAbsolutePath().contains("appveyor")) // Be silent on AppVeyor to not break the tests. Ugly trick :)
169                                                 System.out.println("Unable to clean temporary file "+f.getAbsolutePath()+" during shutdown.");
170                                 if (! dir.delete() && !dir.getAbsolutePath().contains("appveyor") )
171                                         System.out.println("Unable to clean temporary file "+dir.getAbsolutePath()+" during shutdown.");                                
172                         } catch(Exception e) {
173                                 System.out.println("Error while cleaning temporary file "+dir.getAbsolutePath()+" during shutdown: "+e.getCause());
174                                 e.printStackTrace();
175                         }
176                 }
177         }
178 }