Add support for configuration path relocation on Unix

Get the installation prefix from the program dir. We have code
to obtain the directory where the executable resides for all
platforms we support, Linux, BSDs, Apple, etc.

On less well-known platforms where this isn't true (POSIX does not
define any standard interfaces for this) we fallback on
using a hard-coded installation prefix, like we have been doing
until now.

The path relocation allows the whole installation tree to be moved
without having to recompile the program. But note there are other
requirements for shared libraries to have full support for relocation.
This is only partial support.

We now use a header to pass the relative path definitions to avoid
excessively long compilation command lines as the number of #defines
increases.
This commit is contained in:
João Valverde 2023-01-25 20:15:16 +00:00
parent db010b5a27
commit abdaed1103
3 changed files with 51 additions and 46 deletions

View File

@ -7,28 +7,20 @@
# SPDX-License-Identifier: GPL-2.0-or-later
#
if(UNIX OR USE_MSYSTEM)
if(USE_MSYSTEM)
# Windows binaries are relocatable and do not need a prefix.
file(TO_NATIVE_PATH "${PLUGIN_INSTALL_LIBDIR}" _dir)
string(REPLACE "\\" "\\\\" _plugin_dir "${_dir}")
file(TO_NATIVE_PATH "${EXTCAP_INSTALL_LIBDIR}" _dir)
string(REPLACE "\\" "\\\\" _extcap_dir "${_dir}")
file(TO_NATIVE_PATH "${CMAKE_INSTALL_DATADIR}" _dir)
string(REPLACE "\\" "\\\\" _data_dir "${_dir}")
file(TO_NATIVE_PATH "${CMAKE_INSTALL_DOCDIR}" _dir)
string(REPLACE "\\" "\\\\" _doc_dir "${_dir}")
elseif(UNIX)
set(_plugin_dir "${PLUGIN_INSTALL_FULL_LIBDIR}")
set(_extcap_dir "${EXTCAP_INSTALL_FULL_LIBDIR}")
set(_data_dir "${CMAKE_INSTALL_FULL_DATADIR}")
set(_doc_dir "${CMAKE_INSTALL_FULL_DOCDIR}")
endif()
add_definitions(-DPLUGIN_DIR=\"${_plugin_dir}\")
add_definitions(-DEXTCAP_DIR=\"${_extcap_dir}\")
add_definitions(-DDATA_DIR=\"${_data_dir}\")
add_definitions(-DDOC_DIR=\"${_doc_dir}\")
endif()
file(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}" PATH_INSTALL_PREFIX)
string(REPLACE "\\" "\\\\" PATH_INSTALL_PREFIX "${PATH_INSTALL_PREFIX}")
file(TO_NATIVE_PATH "${CMAKE_INSTALL_DATADIR}" PATH_DATA_DIR)
string(REPLACE "\\" "\\\\" PATH_DATA_DIR "${PATH_DATA_DIR}")
file(TO_NATIVE_PATH "${CMAKE_INSTALL_DOCDIR}" PATH_DOC_DIR)
string(REPLACE "\\" "\\\\" PATH_DOC_DIR "${PATH_DOC_DIR}")
file(TO_NATIVE_PATH "${PLUGIN_INSTALL_LIBDIR}" PATH_PLUGIN_DIR)
string(REPLACE "\\" "\\\\" PATH_PLUGIN_DIR "${PATH_PLUGIN_DIR}")
file(TO_NATIVE_PATH "${EXTCAP_INSTALL_LIBDIR}" PATH_EXTCAP_DIR)
string(REPLACE "\\" "\\\\" PATH_EXTCAP_DIR "${PATH_EXTCAP_DIR}")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/path_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/path_config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_subdirectory(wmem)

View File

@ -47,6 +47,8 @@
#include <wiretap/wtap.h> /* for WTAP_ERR_SHORT_WRITE */
#include "path_config.h"
#define PROFILES_DIR "profiles"
#define PLUGINS_DIR_NAME "plugins"
#define EXTCAP_DIR_NAME "extcap"
@ -79,9 +81,7 @@ char *doc_dir = NULL;
/* Directory from which the executable came. */
static char *progfile_dir = NULL;
#ifdef __MINGW64__
static char *install_prefix = NULL;
#endif
static gboolean do_store_persconffiles = FALSE;
static GHashTable *profile_files = NULL;
@ -536,7 +536,6 @@ static void trim_progfile_dir(void)
g_free(extcap_progfile_dir);
}
#ifdef __MINGW64__
static char *
trim_last_dir_from_path(const char *_path)
{
@ -547,7 +546,6 @@ trim_last_dir_from_path(const char *_path)
}
return path;
}
#endif
/*
* Construct the path name of a non-extcap Wireshark executable file,
@ -680,6 +678,9 @@ configuration_init_posix(const char* arg0)
char *path;
char *dir_end;
/* Hard-coded value used if we cannot obtain the path of the running executable. */
install_prefix = g_strdup(INSTALL_PREFIX);
/*
* Check whether XXX_RUN_FROM_BUILD_DIRECTORY is set in the
* environment; if so, set running_in_build_directory_flag if we
@ -888,7 +889,6 @@ configuration_init_posix(const char* arg0)
*/
progfile_dir = prog_pathname;
trim_progfile_dir();
return NULL;
} else {
/*
* This "shouldn't happen"; we apparently
@ -899,6 +899,24 @@ configuration_init_posix(const char* arg0)
g_free(prog_pathname);
return retstr;
}
/*
* We already have the program_dir. Find the installation prefix.
* This is one level up from the bin_dir. If the program_dir does
* not end with "bin" then assume we are running in the build directory
* and the "installation prefix" (staging directory) is the same as
* the program_dir.
*/
g_free(install_prefix);
if (g_str_has_suffix(progfile_dir, _S"bin")) {
install_prefix = trim_last_dir_from_path(progfile_dir);
}
else {
install_prefix = g_strdup(progfile_dir);
running_in_build_directory_flag = TRUE;
}
return NULL;
}
#endif /* ?_WIN32 */
@ -1035,12 +1053,7 @@ get_datafile_dir(void)
*/
datafile_dir = g_strdup(progfile_dir);
} else {
/*
* XXX We might want want to make this relative to progfile_dir, which would
* allow installation into arbitrary directories and provide better AppImage
* support.
*/
datafile_dir = g_strdup(DATA_DIR);
datafile_dir = g_build_filename(install_prefix, DATA_DIR, (char *)NULL);
}
#endif
return datafile_dir;
@ -1093,7 +1106,7 @@ get_doc_dir(void)
*/
doc_dir = g_strdup(progfile_dir);
} else {
doc_dir = g_strdup(DOC_DIR);
doc_dir = g_build_filename(install_prefix, DOC_DIR, (char *)NULL);
}
#endif
return doc_dir;
@ -1203,12 +1216,7 @@ init_plugin_dir(void)
}
#endif
else {
/*
* XXX We might want want to make this relative to progfile_dir, which would
* allow installation into arbitrary directories and provide better AppImage
* support.
*/
plugin_dir = g_strdup(PLUGIN_DIR);
plugin_dir = g_build_filename(install_prefix, PLUGIN_DIR, (char *)NULL);
}
}
#endif
@ -1343,12 +1351,7 @@ init_extcap_dir(void)
}
#endif
else {
/*
* XXX We might want want to make this relative to progfile_dir, which would
* allow installation into arbitrary directories and provide better AppImage
* support.
*/
extcap_dir = g_strdup(EXTCAP_DIR);
extcap_dir = g_build_filename(install_prefix, EXTCAP_DIR, (char *)NULL);
}
#endif
}

10
wsutil/path_config.h.in Normal file
View File

@ -0,0 +1,10 @@
#ifndef __PATH_CONFIG_H__
#define __PATH_CONFIG_H__
#define INSTALL_PREFIX "@PATH_INSTALL_PREFIX@"
#define DATA_DIR "@PATH_DATA_DIR@"
#define DOC_DIR "@PATH_DOC_DIR@"
#define PLUGIN_DIR "@PATH_PLUGIN_DIR@"
#define EXTCAP_DIR "@PATH_EXTCAP_DIR@"
#endif