2013-12-23 01:12:31 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <string>
|
2013-12-23 01:56:53 +00:00
|
|
|
#include <cstdlib>
|
2013-12-23 01:12:31 +00:00
|
|
|
#include "util/exception.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
#if defined(LEAN_WINDOWS)
|
|
|
|
// Windows version
|
|
|
|
#include <windows.h>
|
2013-12-23 01:56:53 +00:00
|
|
|
static std::string get_exe_location() {
|
2013-12-23 01:12:31 +00:00
|
|
|
HMODULE hModule = GetModuleHandleW(NULL);
|
|
|
|
WCHAR path[MAX_PATH];
|
|
|
|
GetModuleFileNameW(hModule, path, MAX_PATH);
|
2013-12-23 01:56:53 +00:00
|
|
|
return std::string(path);
|
2013-12-23 01:12:31 +00:00
|
|
|
}
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
// OSX version
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#include <limits.h>
|
2013-12-23 01:56:53 +00:00
|
|
|
static std::string get_exe_location() {
|
2013-12-23 01:12:31 +00:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
uint32_t bufsize = PATH_MAX;
|
|
|
|
if (_NSGetExecutablePath(buf, &bufsize) != 0)
|
|
|
|
throw exception("failed to locate Lean executable location");
|
2013-12-23 01:56:53 +00:00
|
|
|
return std::string(buf);
|
2013-12-23 01:12:31 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Linux version
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <limits.h> // NOLINT
|
|
|
|
#include <stdio.h>
|
2013-12-23 01:56:53 +00:00
|
|
|
static std::string get_exe_location() {
|
2013-12-23 01:12:31 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
char dest[PATH_MAX];
|
|
|
|
pid_t pid = getpid();
|
|
|
|
snprintf(path, PATH_MAX, "/proc/%d/exe", pid);
|
|
|
|
if (readlink(path, dest, PATH_MAX) == -1) {
|
|
|
|
throw exception("failed to locate Lean executable location");
|
|
|
|
} else {
|
2013-12-23 01:56:53 +00:00
|
|
|
return std::string(dest);
|
2013-12-23 01:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-23 01:56:53 +00:00
|
|
|
static std::string g_lean_path;
|
|
|
|
struct init_lean_path {
|
|
|
|
init_lean_path() {
|
|
|
|
char * r = getenv("LEAN_PATH");
|
|
|
|
if (r == nullptr)
|
|
|
|
g_lean_path = get_exe_location();
|
|
|
|
else
|
|
|
|
g_lean_path = r;
|
|
|
|
}
|
2013-12-23 01:12:31 +00:00
|
|
|
};
|
2013-12-23 01:56:53 +00:00
|
|
|
static init_lean_path g_init_lean_path;
|
|
|
|
char const * get_lean_path() {
|
|
|
|
return g_lean_path.c_str();
|
2013-12-23 01:12:31 +00:00
|
|
|
}
|
|
|
|
}
|