Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function osgDB::executableFilePath() to find the executable path platform independent #1172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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