1 31 package org.jruby.libraries; 32 33 import java.io.IOException ; 34 35 import org.jruby.Ruby; 36 import org.jruby.RubyHash; 37 import org.jruby.RubyModule; 38 import org.jruby.runtime.Constants; 39 import org.jruby.runtime.load.Library; 40 import org.jruby.util.NormalizedFile; 41 42 public class RbConfigLibrary implements Library { 43 48 public void load(Ruby runtime) { 49 RubyModule configModule = runtime.defineModule("Config"); 50 RubyHash configHash = RubyHash.newHash(runtime); 51 configModule.defineConstant("CONFIG", configHash); 52 53 String [] versionParts = Constants.RUBY_VERSION.split("\\."); 54 setConfig(configHash, "MAJOR", versionParts[0]); 55 setConfig(configHash, "MINOR", versionParts[1]); 56 setConfig(configHash, "TEENY", versionParts[2]); 57 setConfig(configHash, "ruby_version", versionParts[0] + '.' + versionParts[1]); 58 setConfig(configHash, "arch", "java"); 59 60 setConfig(configHash, "bindir", new NormalizedFile(runtime.getJRubyHome(), "bin").getAbsolutePath()); 61 setConfig(configHash, "RUBY_INSTALL_NAME", jruby_script()); 62 setConfig(configHash, "ruby_install_name", jruby_script()); 63 setConfig(configHash, "SHELL", jruby_shell()); 64 setConfig(configHash, "prefix", new NormalizedFile(runtime.getJRubyHome()).getAbsolutePath()); 65 66 setConfig(configHash, "host_os", System.getProperty("os.name")); 67 setConfig(configHash, "LIBRUBY", "jruby"); 68 setConfig(configHash, "LIBRUBY_SO", "jruby"); 69 setConfig(configHash, "target", "java"); 70 setConfig(configHash, "build", "java"); 71 setConfig(configHash, "host_vendor", System.getProperty("java.vendor")); 72 setConfig(configHash, "host_cpu", System.getProperty("os.arch")); 73 setConfig(configHash, "target_cpu", System.getProperty("os.arch")); 74 75 String libdir = System.getProperty("jruby.lib"); 76 if (libdir == null) { 77 libdir = new NormalizedFile(runtime.getJRubyHome(), "lib").getAbsolutePath(); 78 } else { 79 try { 80 libdir = new NormalizedFile(libdir).getCanonicalPath(); 84 } catch (IOException e) { 85 libdir = new NormalizedFile(libdir).getAbsolutePath(); 86 } 87 } 88 89 setConfig(configHash, "libdir", libdir); 90 setConfig(configHash, "rubylibdir", new NormalizedFile(libdir, "ruby/1.8").getAbsolutePath()); 91 setConfig(configHash, "sitedir", new NormalizedFile(libdir, "ruby/site_ruby").getAbsolutePath()); 92 setConfig(configHash, "sitelibdir", new NormalizedFile(libdir, "ruby/site_ruby/1.8").getAbsolutePath()); 93 setConfig(configHash, "sitearchdir", new NormalizedFile(libdir, "ruby/site_ruby/1.8/java").getAbsolutePath()); 94 setConfig(configHash, "configure_args", ""); 95 setConfig(configHash, "datadir", new NormalizedFile(runtime.getJRubyHome(), "share").getAbsolutePath()); 96 setConfig(configHash, "mandir", new NormalizedFile(runtime.getJRubyHome(), "man").getAbsolutePath()); 97 setConfig(configHash, "sysconfdir", new NormalizedFile(runtime.getJRubyHome(), "etc").getAbsolutePath()); 98 99 if (isWindows()) { 100 setConfig(configHash, "EXEEXT", ".exe"); 101 } else { 102 setConfig(configHash, "EXEEXT", ""); 103 } 104 } 105 106 private static void setConfig(RubyHash configHash, String key, String value) { 107 Ruby runtime = configHash.getRuntime(); 108 configHash.aset(runtime.newString(key), runtime.newString(value)); 109 } 110 111 private static boolean isWindows() { 112 return System.getProperty("os.name", "").startsWith("Windows"); 113 } 114 115 private String jruby_script() { 116 return System.getProperty("jruby.script", isWindows() ? "jruby.bat" : "jruby").replace('\\', '/'); 117 } 118 119 private String jruby_shell() { 120 return System.getProperty("jruby.shell", isWindows() ? "cmd.exe" : "/bin/sh").replace('\\', '/'); 121 } 122 } 123 | Popular Tags |