From 35fb0e8633217f602360a9987af51c4b960e7850 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 18 Jan 2009 13:00:14 +0100 Subject: [PATCH] Compute prefix at runtime if RUNTIME_PREFIX is set This commit adds support for relocatable binaries (called RUNTIME_PREFIX). Such binaries can be moved together with the system configuration files to a different directory, as long as the relative paths from the binary to the configuration files is preserved. This functionality is essential on Windows where we deliver git binaries with an installer that allows to freely choose the installation location. If RUNTIME_PREFIX is unset we use the static prefix. This will be the default on Unix. Thus, the behavior on Unix will remain identical to the old implementation, which used to add the prefix in the Makefile. If RUNTIME_PREFIX is set the prefix is computed from the location of the executable. In this case, system_path() tries to strip known directories that executables can be located in from the path of the executable. If the path is successfully stripped it is used as the prefix. For example, if the executable is "/msysgit/bin/git" and BINDIR is "bin", then the prefix computed is "/msysgit". If the runtime prefix computation fails, we fall back to the static prefix specified in the makefile. This can be the case if the executable is not installed at a known location. Note that our test system sets GIT_CONFIG_NOSYSTEM to tell git to ignore global configuration files during testing. Hence testing does not trigger the fall back. Note that RUNTIME_PREFIX only works on Windows, though adding support on Unix should not be too hard. The implementation requires argv0_path to be set to an absolute path. argv0_path must point to the directory of the executable. We use assert() to verify this in debug builds. On Windows, the wrapper for main() (see compat/mingw.h) guarantees that argv0_path is correctly initialized. On Unix, further work is required before RUNTIME_PREFIX can be enabled. Signed-off-by: Steffen Prohaska Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- Makefile | 3 +++ exec_cmd.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/Makefile b/Makefile index 1d2060aa3..fbe52c9c6 100644 --- a/Makefile +++ b/Makefile @@ -1031,6 +1031,9 @@ ifdef INTERNAL_QSORT COMPAT_CFLAGS += -DINTERNAL_QSORT COMPAT_OBJS += compat/qsort.o endif +ifdef RUNTIME_PREFIX + COMPAT_CFLAGS += -DRUNTIME_PREFIX +endif ifdef NO_PTHREADS THREADED_DELTA_SEARCH = diff --git a/exec_cmd.c b/exec_cmd.c index 23f4873b2..f234066de 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -9,12 +9,56 @@ static const char *argv0_path; const char *system_path(const char *path) { +#ifdef RUNTIME_PREFIX + static const char *prefix; +#else static const char *prefix = PREFIX; +#endif struct strbuf d = STRBUF_INIT; if (is_absolute_path(path)) return path; +#ifdef RUNTIME_PREFIX + assert(argv0_path); + assert(is_absolute_path(argv0_path)); + + if (!prefix) { + const char *strip[] = { + GIT_EXEC_PATH, + BINDIR, + 0 + }; + const char **s; + + for (s = strip; *s; s++) { + const char *sargv = argv0_path + strlen(argv0_path); + const char *ss = *s + strlen(*s); + while (argv0_path < sargv && *s < ss + && (*sargv == *ss || + (is_dir_sep(*sargv) && is_dir_sep(*ss)))) { + sargv--; + ss--; + } + if (*s == ss) { + struct strbuf d = STRBUF_INIT; + /* We also skip the trailing directory separator. */ + assert(sargv - argv0_path - 1 >= 0); + strbuf_add(&d, argv0_path, sargv - argv0_path - 1); + prefix = strbuf_detach(&d, NULL); + break; + } + } + } + + if (!prefix) { + prefix = PREFIX; + fprintf(stderr, "RUNTIME_PREFIX requested, " + "but prefix computation failed. " + "Using static fallback '%s'.\n", prefix); + } +#endif + strbuf_addf(&d, "%s/%s", prefix, path); path = strbuf_detach(&d, NULL); return path; -- 2.26.2