]> gerrit.simantics Code Review - simantics/platform.git/blob
31702fd10e63d42828acb3163db619d3ed43bf9e
[simantics/platform.git] /
1 package org.simantics.msvc.runtime;
2
3 import java.io.File;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URL;
6 import java.net.URLDecoder;
7
8 import org.eclipse.core.runtime.Path;
9
10 /**
11  * @author Tuukka Lehtonen
12  */
13 public final class Runtimes {
14
15     public static final String VC_2010 = "vc2010";
16
17     static boolean vc100Initialized = false;
18
19     public static void initialize(String vc) {
20         OSType os = OSType.calculate();
21         if (os == OSType.WINDOWS) {
22             if (VC_2010.equals(vc) && !vc100Initialized) {
23                 initializeVC100();
24             }
25         }
26     }
27
28     static synchronized void initializeVC100() {
29         try {
30             // Works in OSGi environment.
31             System.loadLibrary("msvcr100");
32             System.loadLibrary("msvcp100");
33             vc100Initialized = true;
34         } catch (UnsatisfiedLinkError e) {
35             // Try to load from a workspace in POJO environment.
36             try {
37                 String archStr = ARCHType.calculate().toString().toLowerCase();
38                 URL url = Runtimes.class.getResource(".");
39                 File dir = new File(URLDecoder.decode(url.getPath(), "UTF-8") + "../../../../../../org.simantics.msvc.runtime." + archStr);
40                 dir = new Path(dir.getAbsolutePath()).toFile();
41                 String vcr = new File(dir, "msvcr100.dll").getAbsolutePath();
42                 String vcp = new File(dir, "msvcp100.dll").getAbsolutePath();
43                 System.load(vcr);
44                 System.load(vcp);
45                 vc100Initialized = true;
46             } catch (UnsupportedEncodingException e1) {
47                 throw new Error("UTF-8 not supported, impossible");
48             }
49         }
50     }
51
52     public enum ARCHType {
53         PPC, PPC_64, SPARC, X86, X86_64, UNKNOWN;
54
55         public static ARCHType calculate() {
56             String osArch = System.getProperty("os.arch");
57             assert osArch != null;
58             osArch = osArch.toLowerCase();
59             if (osArch.equals("i386") || osArch.equals("i586") || osArch.equals("i686") || osArch.equals("x86"))
60                 return X86;
61             if (osArch.startsWith("amd64") || osArch.startsWith("x86_64"))
62                 return X86_64;
63             if (osArch.equals("ppc"))
64                 return PPC;
65             if (osArch.startsWith("ppc"))
66                 return PPC_64;
67             if (osArch.startsWith("sparc"))
68                 return SPARC;
69             return UNKNOWN;
70         }
71     }
72
73     public enum OSType {
74         APPLE, LINUX, SUN, WINDOWS, UNKNOWN;
75
76         public static OSType calculate() {
77             String osName = System.getProperty("os.name");
78             assert osName != null;
79             osName = osName.toLowerCase();
80             if (osName.startsWith("mac os x"))
81                 return APPLE;
82             if (osName.startsWith("windows"))
83                 return WINDOWS;
84             if (osName.startsWith("linux"))
85                 return LINUX;
86             if (osName.startsWith("sun"))
87                 return SUN;
88             return UNKNOWN;
89         }
90     }
91
92 }