luvit/luvit — Issue #115: can not build on Windows
Author: starwing
Created: 2012-02-06 04:29:13+00:00
State: open

several issues in sources and Makefile, includes:
- uv need a MSVC make variable, or set uname_S to MINGW
- Windows don't have dl and pthread, and missing psapi and iphlpapi on Windows
- luajit don't support mixed BUILDMODE in windows, so add BUILDMODE=static to makefile
- In windows, -std=c89 hides putenv function, makes lenv.c fail.
- luajit don't have 10ef10 commit, don't know why :-(
- variable overwrite in file lenv.c:91 isn't use in Windows version, makes -Werror fail.
- luv.c use uv-private/ev.h, which don't exists in Windows version.
- typo in luvit_main.c, WIN32 should be _WIN32

after apply this patch the Windows version compiles, but still failed to run:
sw@WX-ACER /d/Work/Sources/luvit (master)
$ build/luvit
Welcome to the Luvit repl
> Assertion failed: loop->refs == 0, file src/win/core.c, line 246

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

unlucky :-(

this is the fix patch:

diff --git a/Makefile b/Makefile
index 673d81b..e556ea0 100644
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,17 @@ LDFLAGS+=-L${BUILDDIR} -lluvit
LDFLAGS+=${LUADIR}/src/libluajit.a
LDFLAGS+=${UVDIR}/uv.a
LDFLAGS+=${YAJLDIR}/yajl.a
-LDFLAGS+=-Wall -lm -ldl -lpthread
+LDFLAGS+=-Wall -lm
+
+ifdef MSVC
+OS_NAME=MINGW
+endif
+
+ifneq (${OS_NAME},MINGW)
+LDFLAGS+=-ldl -lpthread
+else
+LDFLAGS+=-lws2_32 -lpsapi -liphlpapi
+endif

ifeq (${OS_NAME},Linux)
LDFLAGS+= -lrt
@@ -111,7 +121,7 @@ ${LUADIR}/Makefile:

${LUADIR}/src/libluajit.a: ${LUADIR}/Makefile
touch -c ${LUADIR}/src/*.h
-	$(MAKE) -C ${LUADIR}
+	$(MAKE) -C ${LUADIR} BUILDMODE=static

${YAJLDIR}/CMakeLists.txt:
git submodule update --init ${YAJLDIR}
@@ -128,7 +138,7 @@ ${UVDIR}/Makefile:
git submodule update --init ${UVDIR}

${UVDIR}/uv.a: ${UVDIR}/Makefile
-	$(MAKE) -C ${UVDIR} uv.a
+	$(MAKE) -C ${UVDIR} uv.a uname_S=${OS_NAME}

${HTTPDIR}/Makefile:
git submodule update --init ${HTTPDIR}
@@ -147,7 +157,7 @@ ${BUILDDIR}/libluvit.a: ${GENDIR} ${ALLOBJS} deps

${BUILDDIR}/%.o: src/%.c deps
mkdir -p ${BUILDDIR}
-	$(CC) --std=c89 -D_GNU_SOURCE -g -Wall -Werror -c $< -o $@ -I${HTTPDIR} -I${UVDIR}/include -I${LUADIR}/src -I${YAJLDIR}/src/api -I${YAJLDIR}/src -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHTTP_VERSION=\"${HTTP_VERSION}\" -DUV_VERSION=\"${UV_VERSION}\" -DYAJL_VERSIONISH=\"${YAJL_VERSION}\" -DLUVIT_VERSION=\"${VERSION}\" -DLUAJIT_VERSION=\"${LUAJIT_VERSION}\"
+	$(CC) -D_GNU_SOURCE -g -Wall -Werror -c $< -o $@ -I${HTTPDIR} -I${UVDIR}/include -I${UVDIR}/src/ares -I${LUADIR}/src -I${YAJLDIR}/src/api -I${YAJLDIR}/src -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHTTP_VERSION=\"${HTTP_VERSION}\" -DUV_VERSION=\"${UV_VERSION}\" -DYAJL_VERSIONISH=\"${YAJL_VERSION}\" -DLUVIT_VERSION=\"${VERSION}\" -DLUAJIT_VERSION=\"${LUAJIT_VERSION}\"

${BUILDDIR}/luvit: ${LUVITOBJS} ${LUVITLIB}
$(CC) -g -o ${BUILDDIR}/luvit ${LUVITOBJS} ${LDFLAGS}
diff --git a/deps/luajit b/deps/luajit
index 10ef109..248cf2f 160000
--- a/deps/luajit
+++ b/deps/luajit
@@ -1 +1 @@
-Subproject commit 10ef109eefdf5c1742ea4c0a62e7a127bf1bb43b
+Subproject commit 248cf2ffa9f43e9645c345d58ae2822fd2225979
diff --git a/src/lenv.c b/src/lenv.c
index cd7f2e8..d44ed48 100644
--- a/src/lenv.c
+++ b/src/lenv.c
@@ -88,13 +88,13 @@ static int lenv_put(lua_State* L) {
static int lenv_set(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
const char* value = luaL_checkstring(L, 2);
- int overwrite = luaL_checkint(L, 3);

#ifdef _WIN32
if (SetEnvironmentVariable(name, value) != 0) {
return luaL_error(L, "Failed to set environment variable");
}
#else
+ int overwrite = luaL_checkint(L, 3);
if (setenv(name, value, overwrite)) {
return luaL_error(L, "Insufficient space in environment.");
}
diff --git a/src/luv.c b/src/luv.c
index d06d9eb..c4560da 100644
--- a/src/luv.c
+++ b/src/luv.c
@@ -20,7 +20,9 @@
#include
#include
#include
+#ifndef _WIN32
#include "uv-private/ev.h"
+#endif

#include "luv_fs.h"
#include "luv_dns.h"
diff --git a/src/luvit_main.c b/src/luvit_main.c
index fdac894..22bbe02 100644
--- a/src/luvit_main.c
+++ b/src/luvit_main.c
@@ -24,7 +24,7 @@
#include "lualib.h"
#include "lauxlib.h"

-#ifndef WIN32
+#ifndef _WIN32
#include
#endif
#include "uv.h"