From 05b1de32917bda2aa9e2fc0e3379afefc962cb6a Mon Sep 17 00:00:00 2001 From: samehkhamis Date: Thu, 23 Jul 2015 17:35:37 -0700 Subject: Work under windows --- CMakeLists.txt | 3 +++ sys-1.0-0.rockspec | 2 +- sys-1.1-0.rockspec | 2 +- sys.c | 31 +++++++++++++++++++++++++------ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 029fd0f..e4a6db3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,3 +9,6 @@ SET(luasrc init.lua) ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}") TARGET_LINK_LIBRARIES(sys luaT TH) +IF(LUALIB) + TARGET_LINK_LIBRARIES(sys ${LUALIB}) +ENDIF() diff --git a/sys-1.0-0.rockspec b/sys-1.0-0.rockspec index bc34425..6054ae1 100644 --- a/sys-1.0-0.rockspec +++ b/sys-1.0-0.rockspec @@ -21,6 +21,6 @@ dependencies = { build = { type = "command", - build_command = [[cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], + build_command = [[mkdir && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], install_command = "cd build && $(MAKE) install" } diff --git a/sys-1.1-0.rockspec b/sys-1.1-0.rockspec index dd46ca2..22f5452 100644 --- a/sys-1.1-0.rockspec +++ b/sys-1.1-0.rockspec @@ -21,6 +21,6 @@ dependencies = { build = { type = "command", - build_command = [[cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], + build_command = [[cmake -E make_directory build && cd build && cmake .. -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], install_command = "cd build && $(MAKE) install" } diff --git a/sys.c b/sys.c index 489fdce..5425723 100644 --- a/sys.c +++ b/sys.c @@ -1,16 +1,30 @@ #include #include -#ifdef LUA_WIN +#ifdef _WIN32 + +#define WINDOWS_LEAN_AND_MEAN +#include +#include static int l_clock(lua_State *L) { - printf("warning: sys.clock not implemented on Windows\n"); - return 0; + static const uint64_t EPOCH = 116444736000000000ULL; + SYSTEMTIME systemtime; + FILETIME filetime; + uint64_t time; + GetSystemTime(&systemtime); + GetSystemTimeAsFileTime(&filetime); + time = (((uint64_t)filetime.dwHighDateTime) << 32) + ((uint64_t)filetime.dwLowDateTime); + double precise_time = (time - EPOCH) / 10000000.0; + lua_pushnumber(L, precise_time); + return 1; } static int l_usleep(lua_State *L) { - printf("warning: sys.usleep not implemented on Windows\n"); - return 0; + int time = 1; + if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1); + Sleep(time / 1000); + return 1; } #else @@ -50,7 +64,12 @@ static const struct luaL_Reg routines [] = { {NULL, NULL} }; -int luaopen_libsys(lua_State *L) +#if defined(_WIN32) + #define SYS_DLLEXPORT __declspec(dllexport) __cdecl +#else + #define SYS_DLLEXPORT +#endif +int SYS_DLLEXPORT luaopen_libsys(lua_State *L) { lua_newtable(L); #if LUA_VERSION_NUM == 501 -- cgit v1.2.3