feat(util/lean_path): add support for LEAN_PATH

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-12-22 17:56:53 -08:00
parent df1b21a03d
commit 777582380f
5 changed files with 30 additions and 15 deletions

View file

@ -13,6 +13,8 @@ add_test(lean_version1 ${CMAKE_CURRENT_BINARY_DIR}/lean --version)
add_test(lean_version2 ${CMAKE_CURRENT_BINARY_DIR}/lean --v)
add_test(lean_ghash1 ${CMAKE_CURRENT_BINARY_DIR}/lean -g)
add_test(lean_ghash2 ${CMAKE_CURRENT_BINARY_DIR}/lean --githash)
add_test(lean_path1 ${CMAKE_CURRENT_BINARY_DIR}/lean -p)
add_test(lean_path2 ${CMAKE_CURRENT_BINARY_DIR}/lean --path)
add_test(lean_luahook1 ${CMAKE_CURRENT_BINARY_DIR}/lean --luahook=100 "${LEAN_SOURCE_DIR}/../tests/lua/big.lua")
add_test(lean_luahook2 ${CMAKE_CURRENT_BINARY_DIR}/lean -c 100 "${LEAN_SOURCE_DIR}/../tests/lua/big.lua")
add_test(lean_lua1 ${LEAN_SOURCE_DIR}/cmake/redirect.sh "${CMAKE_CURRENT_BINARY_DIR}/lean" "--lua" "${LEAN_SOURCE_DIR}/../tests/lua/single.lua")

View file

@ -15,6 +15,7 @@ Author: Leonardo de Moura
#include "util/interrupt.h"
#include "util/script_state.h"
#include "util/thread.h"
#include "util/lean_path.h"
#include "kernel/printer.h"
#include "kernel/environment.h"
#include "library/io_state.h"
@ -55,6 +56,7 @@ static void display_help(std::ostream & out) {
std::cout << " --help -h display this message\n";
std::cout << " --version -v display version number\n";
std::cout << " --githash display the git commit hash number used to build this binary\n";
std::cout << " --path display the path used for finding Lean libraries and extensions\n";
std::cout << " --luahook=num -c how often the Lua interpreter checks the interrupted flag,\n";
std::cout << " it is useful for interrupting non-terminating user scripts,\n";
std::cout << " 0 means 'do not check'.\n";
@ -82,6 +84,7 @@ static struct option g_long_options[] = {
{"help", no_argument, 0, 'h'},
{"lean", no_argument, 0, 'l'},
{"lua", no_argument, 0, 'u'},
{"path", no_argument, 0, 'p'},
{"luahook", required_argument, 0, 'c'},
{"githash", no_argument, 0, 'g'},
#if defined(LEAN_USE_BOOST)
@ -95,7 +98,7 @@ int main(int argc, char ** argv) {
lean::register_modules();
input_kind default_k = input_kind::Lean; // default
while (true) {
int c = getopt_long(argc, argv, "lugvhc:012s:012", g_long_options, NULL);
int c = getopt_long(argc, argv, "lupgvhc:012s:012", g_long_options, NULL);
if (c == -1)
break; // end of command line
switch (c) {
@ -117,6 +120,9 @@ int main(int argc, char ** argv) {
case 'c':
script_state::set_check_interrupt_freq(atoi(optarg));
break;
case 'p':
std::cout << lean::get_lean_path() << "\n";
return 0;
case 's':
lean::set_thread_stack_size(atoi(optarg)*1024);
break;

View file

@ -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 exe_location.cpp ${THREAD_CPP})
luaref.cpp stackinfo.cpp lean_path.cpp ${THREAD_CPP})
target_link_libraries(util ${LEAN_LIBS})

View file

@ -5,29 +5,29 @@ Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <string>
#include <cstdlib>
#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() {
static std::string get_exe_location() {
HMODULE hModule = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);
g_exe_location = path;
return std::string(path);
}
#elif defined(__APPLE__)
// OSX version
#include <mach-o/dyld.h>
#include <limits.h>
static void init_exe_location() {
static std::string get_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;
return std::string(buf);
}
#else
// Linux version
@ -36,7 +36,7 @@ static void init_exe_location() {
#include <sys/stat.h>
#include <limits.h> // NOLINT
#include <stdio.h>
static void init_exe_location() {
static std::string get_exe_location() {
char path[PATH_MAX];
char dest[PATH_MAX];
pid_t pid = getpid();
@ -44,15 +44,22 @@ static void init_exe_location() {
if (readlink(path, dest, PATH_MAX) == -1) {
throw exception("failed to locate Lean executable location");
} else {
g_exe_location = dest;
return std::string(dest);
}
}
#endif
struct init_exe_location_proc {
init_exe_location_proc() { init_exe_location(); }
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;
}
};
static init_exe_location_proc g_init_exe_location_proc;
char const * get_exe_location() {
return g_exe_location.c_str();
static init_lean_path g_init_lean_path;
char const * get_lean_path() {
return g_lean_path.c_str();
}
}

View file

@ -6,5 +6,5 @@ Author: Leonardo de Moura
*/
#pragma once
namespace lean {
char const * get_exe_location();
char const * get_lean_path();
}