feat(util/exe_location): add function for finding the location of the executable
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
65f7217935
commit
df1b21a03d
3 changed files with 69 additions and 1 deletions
|
@ -8,6 +8,6 @@ add_library(util trace.cpp debug.cpp name.cpp name_set.cpp
|
|||
exception.cpp interrupt.cpp hash.cpp escaped.cpp bit_tricks.cpp
|
||||
safe_arith.cpp ascii.cpp memory.cpp shared_mutex.cpp realpath.cpp
|
||||
script_state.cpp script_exception.cpp splay_map.cpp lua.cpp
|
||||
luaref.cpp stackinfo.cpp ${THREAD_CPP})
|
||||
luaref.cpp stackinfo.cpp exe_location.cpp ${THREAD_CPP})
|
||||
|
||||
target_link_libraries(util ${LEAN_LIBS})
|
||||
|
|
58
src/util/exe_location.cpp
Normal file
58
src/util/exe_location.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
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>
|
||||
#include "util/exception.h"
|
||||
|
||||
namespace lean {
|
||||
static std::string g_exe_location;
|
||||
#if defined(LEAN_WINDOWS)
|
||||
// Windows version
|
||||
#include <windows.h>
|
||||
static void init_exe_location() {
|
||||
HMODULE hModule = GetModuleHandleW(NULL);
|
||||
WCHAR path[MAX_PATH];
|
||||
GetModuleFileNameW(hModule, path, MAX_PATH);
|
||||
g_exe_location = path;
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
// OSX version
|
||||
#include <mach-o/dyld.h>
|
||||
#include <limits.h>
|
||||
static void init_exe_location() {
|
||||
char buf[PATH_MAX];
|
||||
uint32_t bufsize = PATH_MAX;
|
||||
if (_NSGetExecutablePath(buf, &bufsize) != 0)
|
||||
throw exception("failed to locate Lean executable location");
|
||||
g_exe_location = buf;
|
||||
}
|
||||
#else
|
||||
// Linux version
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <limits.h> // NOLINT
|
||||
#include <stdio.h>
|
||||
static void init_exe_location() {
|
||||
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 {
|
||||
g_exe_location = dest;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
struct init_exe_location_proc {
|
||||
init_exe_location_proc() { init_exe_location(); }
|
||||
};
|
||||
static init_exe_location_proc g_init_exe_location_proc;
|
||||
char const * get_exe_location() {
|
||||
return g_exe_location.c_str();
|
||||
}
|
||||
}
|
10
src/util/exe_location.h
Normal file
10
src/util/exe_location.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
/*
|
||||
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
Author: Leonardo de Moura
|
||||
*/
|
||||
#pragma once
|
||||
namespace lean {
|
||||
char const * get_exe_location();
|
||||
}
|
Loading…
Reference in a new issue