Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / bindings / java / org / simgrid / NativeLib.java
1 /* Copyright (c) 2014-2022. 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 block (thus executed when the class is loaded)
18  * containing a call to this.
19  */
20 public final class NativeLib {
21         private static final boolean WINDOWS_OS = System.getProperty("os.name").toLowerCase().startsWith("win");
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 (WINDOWS_OS)
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().contains("libcgraph.so"))
73                                 System.err.println("HINT: Try to install the libcgraph package (sudo apt-get install libcgraph).");
74                         else if (cause.getMessage().contains("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 for "+name+".");
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                         final String tempPrefix = "simgrid-java-";
94
95                         if (WINDOWS_OS) {
96                                 // The cleanup at exit fails on Windows where it is impossible to delete files which are still in
97                                 // use.  Try to remove stale temporary files from previous executions, and limit disk usage.
98                                 Path tmpdir = (new File(System.getProperty("java.io.tmpdir"))).toPath();
99                                 try (Stream<Path> paths = Files.find(tmpdir, 1,
100                                         (Path p, java.nio.file.attribute.BasicFileAttributes a) ->
101                                                 a.isDirectory() && !p.equals(tmpdir) &&
102                                                 p.getFileName().toString().startsWith(tempPrefix))) {
103                                         paths.map(Path::toFile)
104                                              .map(FileCleaner::new)
105                                              .forEach(FileCleaner::run);
106
107                                 }
108                         }
109
110                         tempDir = Files.createTempDirectory(tempPrefix);
111                         // don't leak the files on disk, but remove it on JVM shutdown
112                         Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
113                 }
114
115                 /* For each possible filename of the given library on all possible OSes, try it */
116                 for (String filename : new String[]
117                    { name,
118                      "lib"+name+".so",               /* linux */
119                      name+".dll", "lib"+name+".dll", /* windows (pure and mingw) */
120                      "lib"+name+".dylib"             /* macOS */}) {
121
122                         File fileOut = new File(tempDir.toFile(), filename);
123                         try ( // Try-with-resources. These stream will be autoclosed when needed.
124                                 InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
125                         ) {
126                                 if (in != null) {
127                                         /* copy the library in position */
128                                         Files.copy(in, fileOut.toPath());
129
130                                         /* load that library */
131                                         System.load(fileOut.getAbsolutePath());
132
133                                         /* It loaded! we're good */
134                                         return true;
135                                 }
136                         }
137                 }
138
139                 /* No suitable name found */
140                 return false;
141         }
142
143         /** Find where to search for the library in the jar -- keep it aligned with where cmake puts it! */
144         private static String getPath() {
145                 // Inspiration: https://github.com/xerial/snappy-java/blob/develop/src/main/java/org/xerial/snappy/OSInfo.java
146                 String prefix = "NATIVE";
147                 String os = System.getProperty("os.name");
148                 String arch = System.getProperty("os.arch");
149
150                 if (arch.matches("^i[3-6]86$"))
151                         arch = "x86";
152                 else if ("x86_64".equalsIgnoreCase(arch) || "AMD64".equalsIgnoreCase(arch))
153                         arch = "amd64";
154
155                 if (os.toLowerCase().startsWith("win")) {
156                         os = "Windows";
157                 } else if (os.contains("OS X")) {
158                         os = "Darwin";
159                 }
160                 os = os.replace(' ', '_');
161                 arch = arch.replace(' ', '_');
162
163                 return prefix + "/" + os + "/" + arch + "/";
164         }
165
166         /** A hackish mechanism used to remove the file containing our library when the JVM shuts down */
167         private static class FileCleaner implements Runnable {
168                 private File dir;
169                 public FileCleaner(File dir) {
170                         this.dir = dir;
171                 }
172                 @Override
173                 public void run() {
174                         try (Stream<Path> paths = Files.walk(dir.toPath())) {
175                                 paths.sorted(java.util.Comparator.reverseOrder())
176                                      .map(java.nio.file.Path::toFile)
177                                      //.peek(System.err::println) // Prints what gets removed
178                                      .forEach(java.io.File::delete);
179                         } catch(Exception e) {
180                                 System.err.println("Error while cleaning temporary file " + dir.getAbsolutePath() +
181                                                    ": " + e.getCause());
182                                 e.printStackTrace();
183                         }
184                 }
185         }
186 }