| 1 | From 501ad55982a8f92a7a95c76c75944d644870181d Mon Sep 17 00:00:00 2001 |
| 2 | From: Ludovic Courtès <ludo@gnu.org> |
| 3 | Date: Thu, 12 Mar 2020 15:16:04 +0100 |
| 4 | Subject: [PATCH] load: Make Guile run-time relocatable using /proc/self/exe. |
| 5 | |
| 6 | Import from |
| 7 | |
| 8 | http://git.savannah.gnu.org/cgit/guix.git/commit/?id=920f2c42ce3345dc1355a41377ebf01a33fdae51 |
| 9 | --- |
| 10 | libguile/load.c | 35 +++++++++++++++++++++++++++++------ |
| 11 | 1 file changed, 29 insertions(+), 6 deletions(-) |
| 12 | |
| 13 | diff --git a/libguile/load.c b/libguile/load.c |
| 14 | index c2ee5093a..128cdf95a 100644 |
| 15 | --- a/libguile/load.c |
| 16 | +++ b/libguile/load.c |
| 17 | @@ -26,6 +26,7 @@ |
| 18 | |
| 19 | #include <string.h> |
| 20 | #include <stdio.h> |
| 21 | +#include <libgen.h> |
| 22 | |
| 23 | #include "libguile/_scm.h" |
| 24 | #include "libguile/alist.h" |
| 25 | @@ -325,6 +326,32 @@ scm_init_load_path () |
| 26 | SCM cpath = SCM_EOL; |
| 27 | |
| 28 | #ifdef SCM_LIBRARY_DIR |
| 29 | + char *program, *bin_dir, *prefix, *module_dir, *ccache_dir; |
| 30 | + |
| 31 | + /* Determine the source and compiled module directories at run-time, |
| 32 | + relative to the executable's location. |
| 33 | + |
| 34 | + Note: Use /proc/self/exe instead of argv[0] because the latter is |
| 35 | + not necessarily an absolute, nor a valid file name. */ |
| 36 | + |
| 37 | + program = scm_gc_malloc_pointerless (256, "string"); |
| 38 | + readlink ("/proc/self/exe", program, 256); |
| 39 | + |
| 40 | + bin_dir = dirname (strdupa (program)); |
| 41 | + |
| 42 | + prefix = scm_gc_malloc_pointerless (strlen (bin_dir) + 4, "string"); |
| 43 | + strcpy (prefix, bin_dir); |
| 44 | + strcat (prefix, "/.."); |
| 45 | + prefix = canonicalize_file_name (prefix); |
| 46 | + |
| 47 | + module_dir = scm_gc_malloc_pointerless (strlen (prefix) + 50, "string"); |
| 48 | + strcpy (module_dir, prefix); |
| 49 | + strcat (module_dir, "/share/guile/" SCM_EFFECTIVE_VERSION); |
| 50 | + |
| 51 | + ccache_dir = scm_gc_malloc_pointerless (strlen (prefix) + 50, "string"); |
| 52 | + strcpy (ccache_dir, prefix); |
| 53 | + strcat (ccache_dir, "/lib/guile/" SCM_EFFECTIVE_VERSION "/ccache"); |
| 54 | + |
| 55 | env = scm_i_mirror_backslashes (getenv ("GUILE_SYSTEM_PATH")); |
| 56 | if (env && strcmp (env, "") == 0) |
| 57 | /* special-case interpret system-path=="" as meaning no system path instead |
| 58 | @@ -333,10 +360,7 @@ scm_init_load_path () |
| 59 | else if (env) |
| 60 | path = scm_parse_path (scm_from_locale_string (env), path); |
| 61 | else |
| 62 | - path = scm_list_4 (scm_from_locale_string (SCM_LIBRARY_DIR), |
| 63 | - scm_from_locale_string (SCM_SITE_DIR), |
| 64 | - scm_from_locale_string (SCM_GLOBAL_SITE_DIR), |
| 65 | - scm_from_locale_string (SCM_PKGDATA_DIR)); |
| 66 | + path = scm_list_1 (scm_from_locale_string (module_dir)); |
| 67 | |
| 68 | env = scm_i_mirror_backslashes (getenv ("GUILE_SYSTEM_COMPILED_PATH")); |
| 69 | if (env && strcmp (env, "") == 0) |
| 70 | @@ -346,8 +370,7 @@ scm_init_load_path () |
| 71 | cpath = scm_parse_path (scm_from_locale_string (env), cpath); |
| 72 | else |
| 73 | { |
| 74 | - cpath = scm_list_2 (scm_from_locale_string (SCM_CCACHE_DIR), |
| 75 | - scm_from_locale_string (SCM_SITE_CCACHE_DIR)); |
| 76 | + cpath = scm_list_1 (scm_from_locale_string (ccache_dir)); |
| 77 | } |
| 78 | |
| 79 | #endif /* SCM_LIBRARY_DIR */ |
| 80 | -- |
| 81 | 2.24.0 |
| 82 | |