Skip to content

Commit 680b57b

Browse files
committed
For platform directory, Give ENV['JRUBY_HOME'] priority over executable path
Also don't fail if specified platform directory + /bin/jruby does not exist.
1 parent 62a6d5e commit 680b57b

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

argparser.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,42 @@ void ArgParser::addEnvVarToOptions(std::list<std::string> & optionsList, const c
115115
#include <mach-o/dyld.h>
116116
#endif
117117

118+
#ifndef PATH_MAX
119+
#define PATH_MAX MAX_PATH
120+
#endif
121+
118122
bool ArgParser::initPlatformDir() {
119-
#ifdef WIN32
120-
char path[MAX_PATH] = "";
121-
getCurrentModulePath(path, MAX_PATH);
122-
#else
123-
char path[PATH_MAX] = "";
124123
bool found = false;
124+
char path[PATH_MAX] = "";
125125

126-
// first try via linux /proc/self/exe
127-
logMsg("initPlatformDir: trying /proc/self/exe");
128-
found = readlink("/proc/self/exe", path, PATH_MAX) != -1;
126+
if (getenv("JRUBY_HOME") != NULL) {
127+
logMsg("initPlatformDir: using JRUBY_HOME environment variable");
128+
char sep[2] = { FILE_SEP, NULL };
129+
strncpy(path, getenv("JRUBY_HOME"), PATH_MAX - 11);
130+
strncpy(path + strlen(path), sep, 1);
131+
strncpy(path + strlen(path), "bin", 3);
132+
strncpy(path + strlen(path), sep, 1);
133+
strncpy(path + strlen(path), "jruby", 5);
134+
found = true;
135+
}
136+
137+
#ifdef WIN32
138+
139+
if (!found) {
140+
getCurrentModulePath(path, PATH_MAX);
141+
}
142+
143+
#else // !WIN32
144+
145+
if (!found) {
146+
// first try via linux /proc/self/exe
147+
logMsg("initPlatformDir: trying /proc/self/exe");
148+
found = readlink("/proc/self/exe", path, PATH_MAX) != -1;
149+
}
129150

130151
#ifdef __MACH__
131152
uint32_t sz = PATH_MAX;
132-
if (_NSGetExecutablePath(path, &sz) == 0) { // OSX-specific
153+
if (!found && _NSGetExecutablePath(path, &sz) == 0) { // OSX-specific
133154
logMsg("initPlatformDir: using _NSGetExecutablePath");
134155
string tmpPath(path);
135156
realpath(tmpPath.c_str(), path);
@@ -139,7 +160,7 @@ bool ArgParser::initPlatformDir() {
139160

140161
#ifdef __SUNOS__
141162
const char* execname = getexecname();
142-
if (execname) {
163+
if (!found && execname) {
143164
logMsg("initPlatformDir: using getexecname");
144165
char * dst = path;
145166
if (execname[0] != '/') {
@@ -165,6 +186,7 @@ bool ArgParser::initPlatformDir() {
165186
strncpy(path + strlen(path), platformDir.c_str(), platformDir.length());
166187
found = true;
167188
}
189+
#endif // WIN32
168190

169191
if (!found) { // try via PATH search
170192
logMsg("initPlatformDir: trying to find executable on PATH");
@@ -173,21 +195,8 @@ bool ArgParser::initPlatformDir() {
173195
found = true;
174196
}
175197

176-
if (!found) { // try via JRUBY_HOME
177-
if (getenv("JRUBY_HOME") != NULL) {
178-
logMsg("initPlatformDir: trying JRUBY_HOME environment variable");
179-
strncpy(path, getenv("JRUBY_HOME"), PATH_MAX - 11);
180-
strncpy(path + strlen(path), "/bin/jruby", 10);
181-
found = true;
182-
}
183-
}
184-
185-
if (!fileExists(path)) {
186-
printToConsole("Could not figure out a proper location for JRuby.\n"
187-
"Try `jruby -Xtrace trace.log ...` and view trace.log for details.");
188-
return false;
189-
}
190-
#endif
198+
// Check if bin/jruby file exists; this logs a message if not found
199+
fileExists(path);
191200

192201
logMsg("Module: %s", path);
193202
char *bslash = strrchr(path, FILE_SEP);

spec/launcher_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,32 @@
210210
end
211211
end
212212

213+
it "should honor JRUBY_HOME" do
214+
with_environment "JRUBY_HOME" => "/tmp" do
215+
jruby_launcher_args("").should include("-Djruby.home=/tmp")
216+
end
217+
end
218+
219+
context "JRUBY_HOME set and JRUBY_HOME/lib/jruby.jar exists" do
220+
let(:jruby_home) do
221+
require 'tempfile'
222+
t = Tempfile.new("jruby_home")
223+
t.path.tap { t.close! }
224+
end
225+
226+
before do
227+
FileUtils.mkdir_p(File.join(jruby_home, "lib"))
228+
FileUtils.touch(File.join(jruby_home, "lib", "jruby.jar"))
229+
end
230+
after { FileUtils.rm_rf jruby_home }
231+
232+
it "should add jruby.jar to the bootclasspath" do
233+
with_environment "JRUBY_HOME" => jruby_home do
234+
jruby_launcher_args("").should include("-Xbootclasspath/a:#{jruby_home}/lib/jruby.jar")
235+
end
236+
end
237+
end
238+
213239
it "should place user-supplied options after default options" do
214240
args = jruby_launcher_args("-J-Djruby.home=/tmp")
215241
home_args = args.select {|x| x =~ /^-Djruby\.home/ }

0 commit comments

Comments
 (0)