mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 14:27:16 +00:00
Codechange: Drop libxdg-basedir dependency in favour of finding the directories ourselves
This commit is contained in:
parent
c288eba813
commit
3dfee979a7
1
.github/workflows/ci-build.yml
vendored
1
.github/workflows/ci-build.yml
vendored
@ -94,7 +94,6 @@ jobs:
|
|||||||
liblzo2-dev \
|
liblzo2-dev \
|
||||||
libsdl1.2-dev \
|
libsdl1.2-dev \
|
||||||
libsdl2-dev \
|
libsdl2-dev \
|
||||||
libxdg-basedir-dev \
|
|
||||||
zlib1g-dev \
|
zlib1g-dev \
|
||||||
# EOF
|
# EOF
|
||||||
echo "::endgroup::"
|
echo "::endgroup::"
|
||||||
|
@ -122,7 +122,6 @@ if(NOT WIN32)
|
|||||||
find_package(Fluidsynth)
|
find_package(Fluidsynth)
|
||||||
find_package(Fontconfig)
|
find_package(Fontconfig)
|
||||||
find_package(ICU OPTIONAL_COMPONENTS i18n lx)
|
find_package(ICU OPTIONAL_COMPONENTS i18n lx)
|
||||||
find_package(XDG_basedir)
|
|
||||||
else()
|
else()
|
||||||
find_package(Iconv)
|
find_package(Iconv)
|
||||||
|
|
||||||
@ -208,7 +207,6 @@ link_package(PNG TARGET PNG::PNG ENCOURAGED)
|
|||||||
link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
|
link_package(ZLIB TARGET ZLIB::ZLIB ENCOURAGED)
|
||||||
link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
|
link_package(LIBLZMA TARGET LibLZMA::LibLZMA ENCOURAGED)
|
||||||
link_package(LZO)
|
link_package(LZO)
|
||||||
link_package(XDG_basedir)
|
|
||||||
|
|
||||||
if(NOT OPTION_DEDICATED)
|
if(NOT OPTION_DEDICATED)
|
||||||
link_package(Fluidsynth)
|
link_package(Fluidsynth)
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
#[=======================================================================[.rst:
|
|
||||||
FindXDG_basedir
|
|
||||||
-------
|
|
||||||
|
|
||||||
Finds the xdg-basedir library.
|
|
||||||
|
|
||||||
Result Variables
|
|
||||||
^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
This will define the following variables:
|
|
||||||
|
|
||||||
``XDG_basedir_FOUND``
|
|
||||||
True if the system has the xdg-basedir library.
|
|
||||||
``XDG_basedir_INCLUDE_DIRS``
|
|
||||||
Include directories needed to use xdg-basedir.
|
|
||||||
``XDG_basedir_LIBRARIES``
|
|
||||||
Libraries needed to link to xdg-basedir.
|
|
||||||
``XDG_basedir_VERSION``
|
|
||||||
The version of the xdg-basedir library which was found.
|
|
||||||
|
|
||||||
Cache Variables
|
|
||||||
^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
The following cache variables may also be set:
|
|
||||||
|
|
||||||
``XDG_basedir_INCLUDE_DIR``
|
|
||||||
The directory containing ``xdg-basedir.h``.
|
|
||||||
``XDG_basedir_LIBRARY``
|
|
||||||
The path to the xdg-basedir library.
|
|
||||||
|
|
||||||
#]=======================================================================]
|
|
||||||
|
|
||||||
find_package(PkgConfig QUIET)
|
|
||||||
pkg_check_modules(PC_XDG_basedir QUIET libxdg-basedir)
|
|
||||||
|
|
||||||
find_path(XDG_basedir_INCLUDE_DIR
|
|
||||||
NAMES basedir.h
|
|
||||||
PATHS ${PC_XDG_basedir_INCLUDE_DIRS}
|
|
||||||
)
|
|
||||||
|
|
||||||
find_library(XDG_basedir_LIBRARY
|
|
||||||
NAMES xdg-basedir
|
|
||||||
PATHS ${PC_XDG_basedir_LIBRARY_DIRS}
|
|
||||||
)
|
|
||||||
|
|
||||||
set(XDG_basedir_VERSION ${PC_XDG_basedir_VERSION})
|
|
||||||
|
|
||||||
include(FindPackageHandleStandardArgs)
|
|
||||||
find_package_handle_standard_args(XDG_basedir
|
|
||||||
FOUND_VAR XDG_basedir_FOUND
|
|
||||||
REQUIRED_VARS
|
|
||||||
XDG_basedir_LIBRARY
|
|
||||||
XDG_basedir_INCLUDE_DIR
|
|
||||||
VERSION_VAR XDG_basedir_VERSION
|
|
||||||
)
|
|
||||||
|
|
||||||
if(XDG_basedir_FOUND)
|
|
||||||
set(XDG_basedir_LIBRARIES ${XDG_basedir_LIBRARY})
|
|
||||||
set(XDG_basedir_INCLUDE_DIRS ${XDG_basedir_INCLUDE_DIR})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
mark_as_advanced(
|
|
||||||
XDG_basedir_INCLUDE_DIR
|
|
||||||
XDG_basedir_LIBRARY
|
|
||||||
)
|
|
102
src/fileio.cpp
102
src/fileio.cpp
@ -28,10 +28,6 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#ifdef WITH_XDG_BASEDIR
|
|
||||||
#include <basedir.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
|
||||||
/** Size of the #Fio data buffer. */
|
/** Size of the #Fio data buffer. */
|
||||||
@ -977,6 +973,27 @@ bool DoScanWorkingDirectory()
|
|||||||
return _searchpaths[SP_PERSONAL_DIR] != tmp;
|
return _searchpaths[SP_PERSONAL_DIR] != tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the home directory of the user.
|
||||||
|
* May return an empty string in the unlikely scenario that the home directory cannot be found.
|
||||||
|
* @return User's home directory
|
||||||
|
*/
|
||||||
|
static std::string GetHomeDir()
|
||||||
|
{
|
||||||
|
#ifdef __HAIKU__
|
||||||
|
BPath path;
|
||||||
|
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
||||||
|
return std::string(path.Path());
|
||||||
|
#else
|
||||||
|
const char *home_env = getenv("HOME"); // Stack var, shouldn't be freed
|
||||||
|
if (home_env != nullptr) return std::string(home_env);
|
||||||
|
|
||||||
|
const struct passwd *pw = getpwuid(getuid());
|
||||||
|
if (pw != nullptr) return std::string(pw->pw_dir);
|
||||||
|
#endif
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine the base (personal dir and game data dir) paths
|
* Determine the base (personal dir and game data dir) paths
|
||||||
* @param exe the path to the executable
|
* @param exe the path to the executable
|
||||||
@ -984,48 +1001,38 @@ bool DoScanWorkingDirectory()
|
|||||||
void DetermineBasePaths(const char *exe)
|
void DetermineBasePaths(const char *exe)
|
||||||
{
|
{
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
const std::string homedir = GetHomeDir();
|
||||||
const char *xdg_data_home = xdgDataHome(nullptr);
|
#ifdef USE_XDG
|
||||||
tmp = xdg_data_home;
|
const char *xdg_data_home = getenv("XDG_DATA_HOME");
|
||||||
tmp += PATHSEP;
|
if (xdg_data_home != nullptr) {
|
||||||
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
tmp = xdg_data_home;
|
||||||
free(xdg_data_home);
|
tmp += PATHSEP;
|
||||||
|
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
||||||
|
AppendPathSeparator(tmp);
|
||||||
|
|
||||||
AppendPathSeparator(tmp);
|
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
||||||
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
} else if (!homedir.empty()) {
|
||||||
|
tmp = homedir;
|
||||||
|
tmp += PATHSEP ".local" PATHSEP "share" PATHSEP;
|
||||||
|
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
||||||
|
AppendPathSeparator(tmp);
|
||||||
|
|
||||||
|
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
||||||
|
} else {
|
||||||
|
_searchpaths[SP_PERSONAL_DIR_XDG].clear();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(OS2) || !defined(WITH_PERSONAL_DIR)
|
#if defined(OS2) || !defined(WITH_PERSONAL_DIR)
|
||||||
_searchpaths[SP_PERSONAL_DIR].clear();
|
_searchpaths[SP_PERSONAL_DIR].clear();
|
||||||
#else
|
#else
|
||||||
#ifdef __HAIKU__
|
if (!homedir.empty()) {
|
||||||
BPath path;
|
|
||||||
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
|
|
||||||
const char *homedir = stredup(path.Path());
|
|
||||||
#else
|
|
||||||
/* getenv is highly unsafe; duplicate it as soon as possible,
|
|
||||||
* or at least before something else touches the environment
|
|
||||||
* variables in any way. It can also contain all kinds of
|
|
||||||
* unvalidated data we rather not want internally. */
|
|
||||||
const char *homedir = getenv("HOME");
|
|
||||||
if (homedir != nullptr) {
|
|
||||||
homedir = stredup(homedir);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (homedir == nullptr) {
|
|
||||||
const struct passwd *pw = getpwuid(getuid());
|
|
||||||
homedir = (pw == nullptr) ? nullptr : stredup(pw->pw_dir);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (homedir != nullptr) {
|
|
||||||
ValidateString(homedir);
|
|
||||||
tmp = homedir;
|
tmp = homedir;
|
||||||
tmp += PATHSEP;
|
tmp += PATHSEP;
|
||||||
tmp += PERSONAL_DIR;
|
tmp += PERSONAL_DIR;
|
||||||
AppendPathSeparator(tmp);
|
AppendPathSeparator(tmp);
|
||||||
|
|
||||||
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
||||||
free(homedir);
|
|
||||||
} else {
|
} else {
|
||||||
_searchpaths[SP_PERSONAL_DIR].clear();
|
_searchpaths[SP_PERSONAL_DIR].clear();
|
||||||
}
|
}
|
||||||
@ -1111,13 +1118,20 @@ void DeterminePaths(const char *exe)
|
|||||||
{
|
{
|
||||||
DetermineBasePaths(exe);
|
DetermineBasePaths(exe);
|
||||||
|
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#ifdef USE_XDG
|
||||||
const char *xdg_config_home = xdgConfigHome(nullptr);
|
std::string config_home;
|
||||||
std::string config_home(xdg_config_home);
|
const std::string homedir = GetHomeDir();
|
||||||
config_home += PATHSEP;
|
const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
|
||||||
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
if (xdg_config_home != nullptr) {
|
||||||
free(xdg_config_home);
|
config_home = xdg_config_home;
|
||||||
|
config_home += PATHSEP;
|
||||||
|
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
||||||
|
} else if (!homedir.empty()) {
|
||||||
|
/* Defaults to ~/.config */
|
||||||
|
config_home = homedir;
|
||||||
|
config_home += PATHSEP ".config" PATHSEP;
|
||||||
|
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
||||||
|
}
|
||||||
AppendPathSeparator(config_home);
|
AppendPathSeparator(config_home);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1137,7 +1151,7 @@ void DeterminePaths(const char *exe)
|
|||||||
if (end != std::string::npos) personal_dir.erase(end + 1);
|
if (end != std::string::npos) personal_dir.erase(end + 1);
|
||||||
config_dir = personal_dir;
|
config_dir = personal_dir;
|
||||||
} else {
|
} else {
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#ifdef USE_XDG
|
||||||
/* No previous configuration file found. Use the configuration folder from XDG. */
|
/* No previous configuration file found. Use the configuration folder from XDG. */
|
||||||
config_dir = config_home;
|
config_dir = config_home;
|
||||||
#else
|
#else
|
||||||
@ -1165,7 +1179,7 @@ void DeterminePaths(const char *exe)
|
|||||||
extern std::string _windows_file;
|
extern std::string _windows_file;
|
||||||
_windows_file = config_dir + "windows.cfg";
|
_windows_file = config_dir + "windows.cfg";
|
||||||
|
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#ifdef USE_XDG
|
||||||
if (config_dir == config_home) {
|
if (config_dir == config_home) {
|
||||||
/* We are using the XDG configuration home for the config file,
|
/* We are using the XDG configuration home for the config file,
|
||||||
* then store the rest in the XDG data home folder. */
|
* then store the rest in the XDG data home folder. */
|
||||||
|
@ -131,7 +131,7 @@ enum Subdirectory {
|
|||||||
enum Searchpath : unsigned {
|
enum Searchpath : unsigned {
|
||||||
SP_FIRST_DIR,
|
SP_FIRST_DIR,
|
||||||
SP_WORKING_DIR = SP_FIRST_DIR, ///< Search in the working directory
|
SP_WORKING_DIR = SP_FIRST_DIR, ///< Search in the working directory
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#ifdef USE_XDG
|
||||||
SP_PERSONAL_DIR_XDG, ///< Search in the personal directory from the XDG specification
|
SP_PERSONAL_DIR_XDG, ///< Search in the personal directory from the XDG specification
|
||||||
#endif
|
#endif
|
||||||
SP_PERSONAL_DIR, ///< Search in the personal directory
|
SP_PERSONAL_DIR, ///< Search in the personal directory
|
||||||
|
@ -348,6 +348,11 @@ typedef unsigned char byte;
|
|||||||
# define PERSONAL_DIR ""
|
# define PERSONAL_DIR ""
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Define the the platforms that use XDG */
|
||||||
|
#if defined(WITH_PERSONAL_DIR) && defined(UNIX) && !defined(__APPLE__)
|
||||||
|
# define USE_XDG
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Check if the types have the bitsizes like we are using them */
|
/* Check if the types have the bitsizes like we are using them */
|
||||||
static_assert(sizeof(uint64) == 8);
|
static_assert(sizeof(uint64) == 8);
|
||||||
static_assert(sizeof(uint32) == 4);
|
static_assert(sizeof(uint32) == 4);
|
||||||
|
Loading…
Reference in New Issue
Block a user