Skip to content

Commit

Permalink
Add function osgDB::executableFilePath() to find the executable path …
Browse files Browse the repository at this point in the history
…platform independent

The implementation has been taken from the VulkanSceneGraph project.

(cherry picked from commit 644f552)
  • Loading branch information
rhabacker committed Nov 18, 2022
1 parent 6c1cbb1 commit 74cbe71
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/osgDB/FileUtils
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ extern OSGDB_EXPORT bool setCurrentWorkingDirectory( const std::string &newCurre
/** return true if a file exists. */
extern OSGDB_EXPORT bool fileExists(const std::string& filename);

/** returns the path/filename of the currently executed program. */
extern OSGDB_EXPORT std::string executableFilePath();

enum FileType
{
FILE_NOT_FOUND,
Expand Down
61 changes: 61 additions & 0 deletions src/osgDB/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ typedef char TCHAR;
#ifndef F_OK
#define F_OK 4
#endif
#ifdef _MSC_VER
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#endif

#else // unix

#if defined( __APPLE__ )
// I'm not sure how we would handle this in raw Darwin
// without the AvailablilityMacros.
#include <AvailabilityMacros.h>
#include <libgen.h>
#include <mach-o/dyld.h>

//>OSG_IOS
//IOS includes
Expand Down Expand Up @@ -116,6 +123,7 @@ typedef char TCHAR;
#include <osgDB/Registry>

#include <errno.h>
#include <limits.h>
#include <string.h>

#include <stack>
Expand Down Expand Up @@ -526,6 +534,59 @@ std::string osgDB::findFileInDirectory(const std::string& fileName,const std::st
return "";
}

/* This function has be taken from the VSG project */
std::string osgDB::executableFilePath()
{
std::string path;

#if defined(WIN32)
TCHAR buf[PATH_MAX + 1];
DWORD result = GetModuleFileName(NULL, buf, static_cast<DWORD>(std::size(buf) - 1));
if (result && result < std::size(buf))
path = buf;
#elif defined(__linux__)

std::vector<char> buffer(1024);
ssize_t len = 0;
while ((len = ::readlink("/proc/self/exe", buffer.data(), buffer.size())) == static_cast<ssize_t>(buffer.size()))
{
buffer.resize(buffer.size() * 2);
}

// add terminator to string.
buffer[len] = '\0';

return buffer.data();

#elif defined(__APPLE__)
# if TARGET_OS_MAC
char realPathName[PATH_MAX + 1];
char buf[PATH_MAX + 1];
uint32_t size = (uint32_t)sizeof(buf);

if (!_NSGetExecutablePath(buf, &size))
{
realpath(buf, realPathName);
path = realPathName;
}
# elif TARGET_IPHONE_SIMULATOR
// iOS, tvOS, or watchOS Simulator
// Not currently implemented
# elif TARGET_OS_MACCATALYST
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
// Not currently implemented
# elif TARGET_OS_IPHONE
// iOS, tvOS, or watchOS device
// Not currently implemented
# else
# error "Unknown Apple platform"
# endif
#elif defined(__ANDROID__)
// Not currently implemented
#endif
return path;
}

static void appendInstallationLibraryFilePaths(osgDB::FilePathList& filepath)
{
#ifdef OSG_DEFAULT_LIBRARY_PATH
Expand Down

0 comments on commit 74cbe71

Please sign in to comment.