From 4917aef04ec531c110b3485838ff6acfa15dca05 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 6 Feb 2018 10:30:53 +0000 Subject: mjit.c: build dir prefix * mjit.c (init_header_filename): prepend basedir to header build dir too, so that the header can be found when running not in the build directory. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62248 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- mjit.c | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'mjit.c') diff --git a/mjit.c b/mjit.c index a02a020507..3f056a1893 100644 --- a/mjit.c +++ b/mjit.c @@ -1043,12 +1043,32 @@ mjit_get_iseq_func(const struct rb_iseq_constant_body *body) } /* A name of the header file included in any C file generated by MJIT for iseqs. */ -#define RUBY_MJIT_HEADER_FILE ("rb_mjit_min_header-" RUBY_VERSION ".h") +#define RUBY_MJIT_HEADER_FILE "rb_mjit_min_header-" RUBY_VERSION ".h" /* GCC and CLANG executable paths. TODO: The paths should absolute ones to prevent changing C compiler for security reasons. */ #define GCC_PATH "gcc" #define CLANG_PATH "clang" +#define append_str2(p, str, len) ((char *)memcpy((p), str, (len))+(len)) +#define append_str(p, str) append_str2(p, str, sizeof(str)-1) + +static char * +build_header_path(const char *basedir, size_t baselen, const char *dir, size_t dirlen) +{ + static const char header_basename[] = "/" RUBY_MJIT_HEADER_FILE; + char *p, *path = xmalloc(baselen + dirlen + sizeof(header_basename)); + if (path == NULL) + return NULL; + p = path; + p = append_str2(p, basedir, baselen); + p = append_str2(p, dir, dirlen); + append_str2(p, header_basename, sizeof(header_basename)); + return path; +} + +#define BUILD_HEADER_PATH(basedir, baselen, dir) \ + build_header_path(basedir, baselen, dir, rb_strlen_lit(dir)) + static void init_header_filename(void) { @@ -1056,27 +1076,20 @@ init_header_filename(void) /* Root path of the running ruby process. Equal to RbConfig::TOPDIR. */ VALUE basedir_val; char *basedir; + size_t baselen; basedir_val = rb_const_get(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX")); - basedir = StringValueCStr(basedir_val); + basedir = StringValuePtr(basedir_val); + baselen = RSTRING_LEN(basedir_val); - header_file = xmalloc(strlen(MJIT_HEADER_BUILD_DIR) + 2 + strlen(RUBY_MJIT_HEADER_FILE)); + header_file = BUILD_HEADER_PATH(basedir, baselen, "/"MJIT_HEADER_BUILD_DIR); if (header_file == NULL) return; - strcpy(header_file, MJIT_HEADER_BUILD_DIR); - strcat(header_file, "/"); - strcat(header_file, RUBY_MJIT_HEADER_FILE); - if ((f = fopen(header_file, "r")) == NULL) { xfree(header_file); - header_file = xmalloc(strlen(basedir) + 1 + strlen(MJIT_HEADER_INSTALL_DIR) + 1 + strlen(RUBY_MJIT_HEADER_FILE) + 1); + header_file = BUILD_HEADER_PATH(basedir, baselen, "/"MJIT_HEADER_INSTALL_DIR); if (header_file == NULL) return; - strcpy(header_file, basedir); - strcat(header_file, "/"); - strcat(header_file, MJIT_HEADER_INSTALL_DIR); - strcat(header_file, "/"); - strcat(header_file, RUBY_MJIT_HEADER_FILE); if ((f = fopen(header_file, "r")) == NULL) { xfree(header_file); header_file = NULL; @@ -1086,13 +1099,14 @@ init_header_filename(void) fclose(f); #ifdef _WIN32 - libruby_build = xmalloc(2 + strlen(basedir) + 1); - strcpy(libruby_build, "-L"); - strcat(libruby_build, basedir); - libruby_installed = xmalloc(2 + strlen(basedir) + 4 + 1); - strcpy(libruby_installed, "-L"); - strcat(libruby_installed, basedir); - strcat(libruby_installed, "/lib"); + p = libruby_build = xmalloc(2 + baselen + 1); + p = append_str(p, "-L"); + p = append_str2(p, basedir, baselen); + *p = '\0'; + libruby_installed = xmalloc(2 + baselen + 4 + 1); + p = append_str2(libruby_installed, libruby_build, p - libruby_build); + p = append_str(p, "/lib"); + *p = '\0'; #endif } -- cgit v1.2.3