From 501ad55982a8f92a7a95c76c75944d644870181d Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Thu, 12 Mar 2020 15:16:04 +0100 Subject: [PATCH] load: Make Guile run-time relocatable using /proc/self/exe. Import from http://git.savannah.gnu.org/cgit/guix.git/commit/?id=920f2c42ce3345dc1355a41377ebf01a33fdae51 --- libguile/load.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/libguile/load.c b/libguile/load.c index c2ee5093a..128cdf95a 100644 --- a/libguile/load.c +++ b/libguile/load.c @@ -26,6 +26,7 @@ #include #include +#include #include "libguile/_scm.h" #include "libguile/alist.h" @@ -325,6 +326,32 @@ scm_init_load_path () SCM cpath = SCM_EOL; #ifdef SCM_LIBRARY_DIR + char *program, *bin_dir, *prefix, *module_dir, *ccache_dir; + + /* Determine the source and compiled module directories at run-time, + relative to the executable's location. + + Note: Use /proc/self/exe instead of argv[0] because the latter is + not necessarily an absolute, nor a valid file name. */ + + program = scm_gc_malloc_pointerless (256, "string"); + readlink ("/proc/self/exe", program, 256); + + bin_dir = dirname (strdupa (program)); + + prefix = scm_gc_malloc_pointerless (strlen (bin_dir) + 4, "string"); + strcpy (prefix, bin_dir); + strcat (prefix, "/.."); + prefix = canonicalize_file_name (prefix); + + module_dir = scm_gc_malloc_pointerless (strlen (prefix) + 50, "string"); + strcpy (module_dir, prefix); + strcat (module_dir, "/share/guile/" SCM_EFFECTIVE_VERSION); + + ccache_dir = scm_gc_malloc_pointerless (strlen (prefix) + 50, "string"); + strcpy (ccache_dir, prefix); + strcat (ccache_dir, "/lib/guile/" SCM_EFFECTIVE_VERSION "/ccache"); + env = scm_i_mirror_backslashes (getenv ("GUILE_SYSTEM_PATH")); if (env && strcmp (env, "") == 0) /* special-case interpret system-path=="" as meaning no system path instead @@ -333,10 +360,7 @@ scm_init_load_path () else if (env) path = scm_parse_path (scm_from_locale_string (env), path); else - path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR), - scm_from_locale_string (SCM_SITE_DIR), - scm_from_locale_string (SCM_GLOBAL_SITE_DIR), - scm_from_locale_string (SCM_PKGDATA_DIR)); + path = scm_list_1 (scm_from_locale_string (module_dir)); env = scm_i_mirror_backslashes (getenv ("GUILE_SYSTEM_COMPILED_PATH")); if (env && strcmp (env, "") == 0) @@ -346,8 +370,7 @@ scm_init_load_path () cpath = scm_parse_path (scm_from_locale_string (env), cpath); else { - cpath = scm_list_2 (scm_from_locale_string (SCM_CCACHE_DIR), - scm_from_locale_string (SCM_SITE_CCACHE_DIR)); + cpath = scm_list_1 (scm_from_locale_string (ccache_dir)); } #endif /* SCM_LIBRARY_DIR */ -- 2.24.0