001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
015: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
016: * Copyright (C) 2005 Charles O Nutter
017: * Copyright (C) 2006 Nick Sieger
018: *
019: * Alternatively, the contents of this file may be used under the terms of
020: * either of the GNU General Public License Version 2 or later (the "GPL"),
021: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022: * in which case the provisions of the GPL or the LGPL are applicable instead
023: * of those above. If you wish to allow use of your version of this file only
024: * under the terms of either the GPL or the LGPL, and not to allow others to
025: * use your version of this file under the terms of the CPL, indicate your
026: * decision by deleting the provisions above and replace them with the notice
027: * and other provisions required by the GPL or the LGPL. If you do not delete
028: * the provisions above, a recipient may use your version of this file under
029: * the terms of any one of the CPL, the GPL or the LGPL.
030: ***** END LICENSE BLOCK *****/package org.jruby.libraries;
031:
032: import java.io.IOException;
033: import java.net.URL;
034: import java.util.regex.Matcher;
035: import java.util.regex.Pattern;
036:
037: import org.jruby.Ruby;
038: import org.jruby.RubyHash;
039: import org.jruby.RubyModule;
040: import org.jruby.runtime.Constants;
041: import org.jruby.runtime.load.Library;
042: import org.jruby.util.NormalizedFile;
043:
044: public class RbConfigLibrary implements Library {
045: /**
046: * Just enough configuration settings (most don't make sense in Java) to run the rubytests
047: * unit tests. The tests use <code>bindir</code>, <code>RUBY_INSTALL_NAME</code> and
048: * <code>EXEEXT</code>.
049: */
050: public void load(Ruby runtime) {
051: RubyModule configModule = runtime.defineModule("Config");
052: RubyHash configHash = RubyHash.newHash(runtime);
053: configModule.defineConstant("CONFIG", configHash);
054:
055: String[] versionParts = Constants.RUBY_VERSION.split("\\.");
056: setConfig(configHash, "MAJOR", versionParts[0]);
057: setConfig(configHash, "MINOR", versionParts[1]);
058: setConfig(configHash, "TEENY", versionParts[2]);
059: setConfig(configHash, "ruby_version", versionParts[0] + '.'
060: + versionParts[1]);
061: setConfig(configHash, "arch", System.getProperty("os.arch")
062: + "-java"
063: + System.getProperty("java.specification.version"));
064:
065: String normalizedHome = new NormalizedFile(runtime
066: .getJRubyHome()).getAbsolutePath();
067: setConfig(configHash, "bindir", new NormalizedFile(
068: normalizedHome, "bin").getAbsolutePath());
069: setConfig(configHash, "RUBY_INSTALL_NAME", jruby_script());
070: setConfig(configHash, "ruby_install_name", jruby_script());
071: setConfig(configHash, "SHELL", jruby_shell());
072: setConfig(configHash, "prefix", normalizedHome);
073: setConfig(configHash, "exec_prefix", normalizedHome);
074:
075: setConfig(configHash, "host_os", System.getProperty("os.name"));
076: setConfig(configHash, "host_vendor", System
077: .getProperty("java.vendor"));
078: setConfig(configHash, "host_cpu", System.getProperty("os.arch"));
079:
080: setConfig(configHash, "target_os", System
081: .getProperty("os.name"));
082: setConfig(configHash, "target_cpu", System
083: .getProperty("os.arch"));
084:
085: String jrubyJarFile = "jruby.jar";
086: URL jrubyPropertiesUrl = Ruby.class.getClassLoader()
087: .getResource("jruby.properties");
088: if (jrubyPropertiesUrl != null) {
089: Pattern jarFile = Pattern
090: .compile("jar:file:.*?([a-zA-Z0-9.\\-]+\\.jar)!/jruby.properties");
091: Matcher jarMatcher = jarFile.matcher(jrubyPropertiesUrl
092: .toString());
093: jarMatcher.find();
094: if (jarMatcher.matches()) {
095: jrubyJarFile = jarMatcher.group(1);
096: }
097: }
098: setConfig(configHash, "LIBRUBY", jrubyJarFile);
099: setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
100:
101: setConfig(configHash, "build", Constants.BUILD);
102: setConfig(configHash, "target", Constants.TARGET);
103:
104: String libdir = System.getProperty("jruby.lib");
105: if (libdir == null) {
106: libdir = new NormalizedFile(normalizedHome, "lib")
107: .getAbsolutePath();
108: } else {
109: try {
110: // Our shell scripts pass in non-canonicalized paths, but even if we didn't
111: // anyone who did would become unhappy because Ruby apps expect no relative
112: // operators in the pathname (rubygems, for example).
113: libdir = new NormalizedFile(libdir).getCanonicalPath();
114: } catch (IOException e) {
115: libdir = new NormalizedFile(libdir).getAbsolutePath();
116: }
117: }
118:
119: setConfig(configHash, "libdir", libdir);
120: setConfig(configHash, "rubylibdir", new NormalizedFile(libdir,
121: "ruby/1.8").getAbsolutePath());
122: setConfig(configHash, "sitedir", new NormalizedFile(libdir,
123: "ruby/site_ruby").getAbsolutePath());
124: setConfig(configHash, "sitelibdir", new NormalizedFile(libdir,
125: "ruby/site_ruby/1.8").getAbsolutePath());
126: setConfig(configHash, "sitearchdir", new NormalizedFile(libdir,
127: "ruby/site_ruby/1.8/java").getAbsolutePath());
128: setConfig(configHash, "archdir", new NormalizedFile(libdir,
129: "ruby/site_ruby/1.8/java").getAbsolutePath());
130: setConfig(configHash, "configure_args", "");
131: setConfig(configHash, "datadir", new NormalizedFile(
132: normalizedHome, "share").getAbsolutePath());
133: setConfig(configHash, "mandir", new NormalizedFile(
134: normalizedHome, "man").getAbsolutePath());
135: setConfig(configHash, "sysconfdir", new NormalizedFile(
136: normalizedHome, "etc").getAbsolutePath());
137: setConfig(configHash, "DLEXT", "jar");
138:
139: if (isWindows()) {
140: setConfig(configHash, "EXEEXT", ".exe");
141: } else {
142: setConfig(configHash, "EXEEXT", "");
143: }
144: }
145:
146: private static void setConfig(RubyHash configHash, String key,
147: String value) {
148: Ruby runtime = configHash.getRuntime();
149: configHash.aset(runtime.newString(key), runtime
150: .newString(value));
151: }
152:
153: private static boolean isWindows() {
154: return System.getProperty("os.name", "").startsWith("Windows");
155: }
156:
157: private String jruby_script() {
158: return System.getProperty("jruby.script",
159: isWindows() ? "jruby.bat" : "jruby").replace('\\', '/');
160: }
161:
162: private String jruby_shell() {
163: return System.getProperty("jruby.shell",
164: isWindows() ? "cmd.exe" : "/bin/sh").replace('\\', '/');
165: }
166: }
|