Skip to content

Commit

Permalink
Make system paths configurable (#3370)
Browse files Browse the repository at this point in the history
  • Loading branch information
oitel authored Sep 23, 2024
1 parent 304c10c commit a0b765c
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 126 deletions.
2 changes: 2 additions & 0 deletions source/MRMesh/MRMesh.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
<ClInclude Include="MRSphere.h" />
<ClInclude Include="MRStacktrace.h" />
<ClInclude Include="MRSymMatrix4.h" />
<ClInclude Include="MRSystemPath.h" />
<ClInclude Include="MRTiffIO.h" />
<ClInclude Include="MRRectIndexer.h" />
<ClInclude Include="MRRestoringStreamsSink.h" />
Expand Down Expand Up @@ -559,6 +560,7 @@
<ClCompile Include="MRSeparationPoint.cpp" />
<ClCompile Include="MRSolarRadiation.cpp" />
<ClCompile Include="MRStacktrace.cpp" />
<ClCompile Include="MRSystemPath.cpp" />
<ClCompile Include="MRTiffIO.cpp" />
<ClCompile Include="MRRectIndexer.cpp" />
<ClCompile Include="MRSceneColors.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions source/MRMesh/MRMesh.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,9 @@
<ClInclude Include="MRPointsLoadSettings.h">
<Filter>Source Files\IO</Filter>
</ClInclude>
<ClInclude Include="MRSystemPath.h">
<Filter>Source Files\System</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MRObject.cpp">
Expand Down Expand Up @@ -2012,6 +2015,9 @@
<ClCompile Include="MRUniqueTemporaryFolder.cpp">
<Filter>Source Files\IO</Filter>
</ClCompile>
<ClCompile Include="MRSystemPath.cpp">
<Filter>Source Files\System</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\.editorconfig" />
Expand Down
115 changes: 10 additions & 105 deletions source/MRMesh/MRSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "MRSystem.h"
#include "MRStringConvert.h"
#include "MRSystemPath.h"
#include "MRConfig.h"
#include "MRStacktrace.h"
#include "MRDirectory.h"
#include "MRRestoringStreamsSink.h"
#include "MRPch/MRSpdlog.h"
#include "MRPch/MRSuppressWarning.h"
#include <cstring>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -89,16 +91,6 @@ void removeOldLogs( const std::filesystem::path& dir, int hours = 24 )
namespace MR
{

#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// If true, the resources should be loaded from the executable directory, rather than from the system directories.
[[nodiscard]] static bool resourcesAreNearExe()
{
auto opt = std::getenv("MR_LOCAL_RESOURCES");
return opt && std::string_view(opt) == "1";
}
#endif


void SetCurrentThreadName( const char * name )
{
#ifdef _MSC_VER
Expand Down Expand Up @@ -142,117 +134,27 @@ void SetCurrentThreadName( const char * name )

std::filesystem::path GetExeDirectory()
{
#ifdef _WIN32
HMODULE hm = GetModuleHandleA( "MRMesh.dll" );
wchar_t szPath[MAX_PATH];
GetModuleFileNameW( hm, szPath, MAX_PATH );
#else
#ifdef __EMSCRIPTEN__
return "/";
#endif
char szPath[PATH_MAX];
#ifdef __APPLE__
uint32_t size = PATH_MAX + 1;

if (_NSGetExecutablePath(szPath, &size) != 0) {
// Buffer size is too small.
spdlog::error( "Executable directory is too long" );
return {};
}
szPath[size] = '\0';
#else
ssize_t count = readlink( "/proc/self/exe", szPath, PATH_MAX );
if( count < 0 )
{
spdlog::error( "Executable directory was not found" );
return {};
}
if( count >= PATH_MAX )
{
spdlog::error( "Executable directory is too long" );
return {};
}
szPath[count] = '\0';
#endif
#endif
auto res = std::filesystem::path{ szPath }.parent_path() / "";
return res;
return SystemPath::getExecutableDirectory().value_or( std::filesystem::path{} );
}

std::filesystem::path GetResourcesDirectory()
{
auto exePath = GetExeDirectory();
#if defined(_WIN32) || defined(__EMSCRIPTEN__)
return exePath;
#else
if ( resourcesAreNearExe() )
return exePath;
#ifdef __APPLE__
#ifdef MR_FRAMEWORK
return "/Library/Frameworks/" + std::string( MR_PROJECT_NAME ) + ".framework/Versions/Current/Resources/";
#else
return "/Applications/" + std::string( MR_PROJECT_NAME ) + ".app/Contents/Resources/";
#endif
#else
return "/usr/local/etc/" + std::string( MR_PROJECT_NAME ) + "/";
#endif
#endif
return SystemPath::getResourcesDirectory();
}

std::filesystem::path GetFontsDirectory()
{
auto exePath = GetExeDirectory();
#if defined(_WIN32) || defined(__EMSCRIPTEN__)
return exePath;
#else
if ( resourcesAreNearExe() )
return exePath;
#ifdef __APPLE__
return GetResourcesDirectory() / "fonts/";
#else
return "/usr/local/share/fonts/";
#endif
#endif
return SystemPath::getFontsDirectory();
}

std::filesystem::path GetLibsDirectory()
{
auto exePath = GetExeDirectory();
#if defined(_WIN32) || defined(__EMSCRIPTEN__)
return exePath;
#else
if ( resourcesAreNearExe() )
return exePath;
#ifdef __APPLE__
#ifdef MR_FRAMEWORK
return "/Library/Frameworks/" + std::string( MR_PROJECT_NAME ) + ".framework/Versions/Current/lib/";
#else
return "/Applications/" + std::string( MR_PROJECT_NAME ) + ".app/Contents/libs/";
#endif
#else
return "/usr/local/lib/" + std::string( MR_PROJECT_NAME ) + "/";
#endif
#endif
return SystemPath::getPluginsDirectory();
}

std::filesystem::path GetEmbeddedPythonDirectory()
{
auto exePath = GetExeDirectory();
#if defined(_WIN32) || defined(__EMSCRIPTEN__)
return exePath;
#else
if ( resourcesAreNearExe() )
return exePath;
#ifdef __APPLE__
#ifdef MR_FRAMEWORK
return "/Library/Frameworks/" + std::string( MR_PROJECT_NAME ) + ".framework/Versions/Current/Frameworks/";
#else
return "/Applications/" + std::string( MR_PROJECT_NAME ) + ".app/Contents/Frameworks/";
#endif
#else
return "/usr/local/lib/" + std::string( MR_PROJECT_NAME ) + "/";
#endif
#endif
return SystemPath::getPythonModulesDirectory();
}

std::filesystem::path getUserConfigDir()
Expand Down Expand Up @@ -333,7 +235,10 @@ std::filesystem::path GetHomeDirectory()
std::string GetMRVersionString()
{
#ifndef __EMSCRIPTEN__
MR_SUPPRESS_WARNING_PUSH
MR_SUPPRESS_WARNING( "-Wdeprecated-declarations", 4996 )
auto directory = GetResourcesDirectory();
MR_SUPPRESS_WARNING_POP
auto versionFilePath = directory / "mr.version";
std::error_code ec;
std::string configPrefix = "";
Expand Down
5 changes: 5 additions & 0 deletions source/MRMesh/MRSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ namespace MR
MRMESH_API void SetCurrentThreadName( const char * name );

// returns path of current exe directory
[[deprecated( "Use SystemPath::getExecutableDirectory() instead" )]]
[[nodiscard]] MRMESH_API std::filesystem::path GetExeDirectory();

// returns path of resource files directory
// .json and .png files
[[deprecated( "Use SystemPath::getResourcesDirectory() instead" )]]
[[nodiscard]] MRMESH_API std::filesystem::path GetResourcesDirectory();

// returns path of font files directory
// .ttf files
[[deprecated( "Use SystemPath::getFontsDirectory() instead" )]]
[[nodiscard]] MRMESH_API std::filesystem::path GetFontsDirectory();

// returns path of lib files directory
// .dll .so files
[[deprecated( "Use SystemPath::getPluginsDirectory() instead" )]]
[[nodiscard]] MRMESH_API std::filesystem::path GetLibsDirectory();

// returns path of embedded python modules files directory
// .dll .so files
[[deprecated( "Use SystemPath::getPythonModulesDirectory() instead" )]]
[[nodiscard]] MRMESH_API std::filesystem::path GetEmbeddedPythonDirectory();

// return path to the folder with user config file(s)
Expand Down
Loading

0 comments on commit a0b765c

Please sign in to comment.