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 02:45:13 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
2013-12-23 01:12:31 +00:00
|
|
|
#include "util/exception.h"
|
2013-12-23 02:45:13 +00:00
|
|
|
#include "util/sstream.h"
|
2013-12-23 01:12:31 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
#if defined(LEAN_WINDOWS)
|
|
|
|
// Windows version
|
|
|
|
#include <windows.h>
|
2013-12-23 02:45:13 +00:00
|
|
|
static char g_path_sep = ';';
|
|
|
|
static char g_sep = '\\';
|
2013-12-23 03:38:32 +00:00
|
|
|
static char g_bad_sep = '/';
|
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 03:09:33 +00:00
|
|
|
std::wstring pathstr(path);
|
2013-12-23 05:27:12 +00:00
|
|
|
return std::string(pathstr.begin(), pathstr.end());
|
2013-12-23 01:12:31 +00:00
|
|
|
}
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
// OSX version
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#include <limits.h>
|
2013-12-23 02:45:13 +00:00
|
|
|
static char g_path_sep = ':';
|
|
|
|
static char g_sep = '/';
|
2013-12-23 03:38:32 +00:00
|
|
|
static char g_bad_sep = '\\';
|
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 02:45:13 +00:00
|
|
|
static char g_path_sep = ':';
|
|
|
|
static char g_sep = '/';
|
2013-12-23 03:38:32 +00:00
|
|
|
static char g_bad_sep = '\\';
|
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 03:38:32 +00:00
|
|
|
|
|
|
|
std::string normalize_path(std::string f) {
|
|
|
|
for (auto & c : f) {
|
|
|
|
if (c == g_bad_sep)
|
|
|
|
c = g_sep;
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2013-12-23 05:27:12 +00:00
|
|
|
std::string get_path(std::string f) {
|
|
|
|
while (true) {
|
|
|
|
if (f.empty())
|
|
|
|
throw exception("failed to locate Lean executable location");
|
|
|
|
if (f.back() == g_sep) {
|
|
|
|
f.pop_back();
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
f.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 02:45:13 +00:00
|
|
|
static std::string g_lean_path;
|
|
|
|
static std::vector<std::string> g_lean_path_vector;
|
2013-12-23 01:56:53 +00:00
|
|
|
struct init_lean_path {
|
|
|
|
init_lean_path() {
|
|
|
|
char * r = getenv("LEAN_PATH");
|
2013-12-23 02:45:13 +00:00
|
|
|
if (r == nullptr) {
|
|
|
|
g_lean_path = ".";
|
|
|
|
g_lean_path += g_path_sep;
|
2013-12-23 05:27:12 +00:00
|
|
|
g_lean_path += get_path(get_exe_location());
|
2013-12-23 02:45:13 +00:00
|
|
|
} else {
|
2013-12-23 01:56:53 +00:00
|
|
|
g_lean_path = r;
|
2013-12-23 02:45:13 +00:00
|
|
|
}
|
2013-12-23 03:38:32 +00:00
|
|
|
g_lean_path = normalize_path(g_lean_path);
|
2013-12-23 02:45:13 +00:00
|
|
|
unsigned i = 0;
|
|
|
|
unsigned j = 0;
|
|
|
|
unsigned sz = g_lean_path.size();
|
|
|
|
for (; j < sz; j++) {
|
|
|
|
if (g_lean_path[j] == g_path_sep) {
|
|
|
|
if (j > i)
|
|
|
|
g_lean_path_vector.push_back(g_lean_path.substr(i, j - i));
|
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 03:38:32 +00:00
|
|
|
if (j > i)
|
|
|
|
g_lean_path_vector.push_back(g_lean_path.substr(i, j - i));
|
2013-12-23 01:56:53 +00:00
|
|
|
}
|
2013-12-23 01:12:31 +00:00
|
|
|
};
|
2013-12-23 01:56:53 +00:00
|
|
|
static init_lean_path g_init_lean_path;
|
2013-12-23 02:45:13 +00:00
|
|
|
|
|
|
|
std::string find_file(char const * fname) {
|
2013-12-23 03:38:32 +00:00
|
|
|
std::string nfname = normalize_path(std::string(fname));
|
2013-12-23 02:45:13 +00:00
|
|
|
for (auto path : g_lean_path_vector) {
|
2013-12-23 03:38:32 +00:00
|
|
|
std::string file = path + g_sep + nfname;
|
2013-12-23 02:45:13 +00:00
|
|
|
std::ifstream ifile(file);
|
|
|
|
if (ifile)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
throw exception(sstream() << "file '" << fname << "' not found in the LEAN_PATH");
|
|
|
|
}
|
|
|
|
|
2013-12-23 01:56:53 +00:00
|
|
|
char const * get_lean_path() {
|
|
|
|
return g_lean_path.c_str();
|
2013-12-23 01:12:31 +00:00
|
|
|
}
|
|
|
|
}
|