2015-10-03 12:28:23 -04:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2019-08-28 03:40:57 -04:00
|
|
|
#include "ruby/config.h"
|
2018-07-16 21:00:05 -04:00
|
|
|
#include <stdio.h>
|
2015-10-03 11:09:47 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2018-11-06 23:02:14 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-10-03 11:09:47 -04:00
|
|
|
|
2016-04-13 21:30:12 -04:00
|
|
|
#include "ruby-runner.h"
|
2015-10-31 19:34:17 -04:00
|
|
|
|
2018-12-07 20:49:02 -05:00
|
|
|
#ifdef MAKE_MJIT_BUILD_DIR
|
2018-12-07 20:50:39 -05:00
|
|
|
const char MJIT_HEADER[] = BUILDDIR "/" MJIT_MIN_HEADER;
|
2018-12-07 20:49:02 -05:00
|
|
|
#else
|
|
|
|
|
2015-10-31 19:34:17 -04:00
|
|
|
#define STRINGIZE(expr) STRINGIZE0(expr)
|
|
|
|
#define STRINGIZE0(expr) #expr
|
2015-10-03 11:09:47 -04:00
|
|
|
|
2017-09-23 21:48:25 -04:00
|
|
|
static void
|
2020-02-22 23:31:56 -05:00
|
|
|
insert_env_path(const char *envname, const char *paths, size_t size, int prepend)
|
2017-09-23 21:48:25 -04:00
|
|
|
{
|
|
|
|
const char *env = getenv(envname);
|
|
|
|
char c = 0;
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
if (env) {
|
|
|
|
while ((c = *env) == PATH_SEP) ++env;
|
|
|
|
n = strlen(env);
|
|
|
|
while (n > 0 && env[n-1] == PATH_SEP) --n;
|
|
|
|
}
|
|
|
|
if (c) {
|
|
|
|
char *e = malloc(size+n+1);
|
|
|
|
size_t pos = 0;
|
|
|
|
if (prepend) {
|
|
|
|
memcpy(e, paths, pos = size-1);
|
|
|
|
e[pos++] = PATH_SEP;
|
|
|
|
}
|
|
|
|
memcpy(e+pos, env, n);
|
|
|
|
pos += n;
|
|
|
|
if (!prepend) {
|
|
|
|
e[pos++] = PATH_SEP;
|
|
|
|
memcpy(e+pos, paths, size-1);
|
|
|
|
pos += size-1;
|
|
|
|
}
|
|
|
|
e[pos] = '\0';
|
|
|
|
env = e;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
env = paths;
|
|
|
|
}
|
|
|
|
setenv(envname, env, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EXTOUT_DIR BUILDDIR"/"EXTOUT
|
2015-10-03 11:09:47 -04:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
static const char builddir[] = BUILDDIR;
|
2017-05-29 02:39:41 -04:00
|
|
|
static const char rubypath[] = BUILDDIR"/"STRINGIZE(RUBY_INSTALL_NAME);
|
2017-09-23 21:48:25 -04:00
|
|
|
static const char rubylib[] =
|
|
|
|
ABS_SRCDIR"/lib"
|
|
|
|
PATH_SEPARATOR
|
|
|
|
EXTOUT_DIR"/common"
|
|
|
|
PATH_SEPARATOR
|
|
|
|
EXTOUT_DIR"/"ARCH
|
|
|
|
;
|
2018-11-07 02:21:00 -05:00
|
|
|
#ifndef LOAD_RELATIVE
|
2018-11-06 23:02:14 -05:00
|
|
|
static const char mjit_build_dir[] = BUILDDIR"/mjit_build_dir."SOEXT;
|
2018-11-07 02:21:00 -05:00
|
|
|
struct stat stbuf;
|
|
|
|
#endif
|
2017-05-29 02:39:41 -04:00
|
|
|
const size_t dirsize = sizeof(builddir);
|
|
|
|
const size_t namesize = sizeof(rubypath) - dirsize;
|
|
|
|
const char *rubyname = rubypath + dirsize;
|
|
|
|
char *arg0 = argv[0], *p;
|
2015-10-31 22:50:59 -04:00
|
|
|
|
2020-02-22 23:31:56 -05:00
|
|
|
insert_env_path(LIBPATHENV, builddir, dirsize, 1);
|
|
|
|
insert_env_path("RUBYLIB", rubylib, sizeof(rubylib), 0);
|
2018-11-07 02:21:00 -05:00
|
|
|
#ifndef LOAD_RELATIVE
|
2018-11-06 23:02:14 -05:00
|
|
|
if (PRELOADENV[0] && stat(mjit_build_dir, &stbuf) == 0) {
|
2020-02-22 23:31:56 -05:00
|
|
|
insert_env_path(PRELOADENV, mjit_build_dir, sizeof(mjit_build_dir), 1);
|
2018-11-06 23:02:14 -05:00
|
|
|
setenv("MJIT_SEARCH_BUILD_DIR", "true", 0);
|
|
|
|
}
|
2018-11-07 02:21:00 -05:00
|
|
|
#endif
|
2017-05-29 02:39:41 -04:00
|
|
|
|
|
|
|
if (!(p = strrchr(arg0, '/'))) p = arg0; else p++;
|
|
|
|
if (strlen(p) < namesize - 1) {
|
|
|
|
argv[0] = malloc(p - arg0 + namesize);
|
|
|
|
memcpy(argv[0], arg0, p - arg0);
|
2017-09-15 15:25:46 -04:00
|
|
|
p = argv[0] + (p - arg0);
|
2017-05-29 02:39:41 -04:00
|
|
|
}
|
2017-09-15 15:25:46 -04:00
|
|
|
memcpy(p, rubyname, namesize);
|
2017-05-29 02:39:41 -04:00
|
|
|
|
|
|
|
execv(rubypath, argv);
|
2018-07-16 21:00:05 -04:00
|
|
|
perror(rubypath);
|
2015-10-03 11:09:47 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-07 20:49:02 -05:00
|
|
|
|
|
|
|
#endif /* MAKE_MJIT_BUILD_DIR */
|