From 47fea74335776d0d05de6225fcb3a8baa33e7f01 Mon Sep 17 00:00:00 2001 From: Fabio Mascarenhas Date: Thu, 21 Jan 2010 15:12:31 -0200 Subject: mingw32 build target --- src/bin/rclauncher.c | 118 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/build/builtin.lua | 32 ++++++++++- src/luarocks/cfg.lua | 37 ++++++++++++- 3 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 src/bin/rclauncher.c (limited to 'src') diff --git a/src/bin/rclauncher.c b/src/bin/rclauncher.c new file mode 100644 index 00000000..4ff23e4a --- /dev/null +++ b/src/bin/rclauncher.c @@ -0,0 +1,118 @@ + +/* +** Simple Lua interpreter. +** This program is used to run a Lua file embedded as a resource. +** It creates a Lua state, opens all its standard libraries, and run +** the Lua file in a protected environment just to redirect the error +** messages to stdout and stderr. +** +** $Id: rclauncher.c,v 1.1 2008/06/30 14:29:59 carregal Exp $ +*/ + +#include +#include + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include +#include +#include + +/* +** Report error message. +** Assumes that the error message is on top of the stack. +*/ +static int report (lua_State *L) { + fprintf (stderr, "lua: fatal error: `%s'\n", lua_tostring (L, -1)); + fflush (stderr); + printf ("Content-type: text/plain\n\nConfiguration fatal error: see error log!\n"); + printf ("%s", lua_tostring(L, -1)); + return 1; +} + +static int runlua (lua_State *L, const char *lua_string, int argc, char *argv[]) { + int err_func; + int err; + + lua_pushliteral(L, "debug"); + lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */ + lua_pushliteral(L, "traceback"); + lua_gettable(L, -2); + err_func = lua_gettop (L); + err = luaL_loadstring (L, lua_string); + if(!err) { + int i; + for(i = 1; i < argc; i++) + lua_pushstring(L, argv[i]); + return lua_pcall (L, argc - 1, LUA_MULTRET, err_func); + } else return err; +} + +static DWORD GetModulePath( HINSTANCE hInst, LPTSTR pszBuffer, DWORD dwSize ) +// +// Return the size of the path in bytes. +{ + DWORD dwLength = GetModuleFileName( hInst, pszBuffer, dwSize ); + if( dwLength ) + { + while( dwLength && pszBuffer[ dwLength ] != '.' ) + { + dwLength--; + } + + if( dwLength ) + pszBuffer[ dwLength ] = '\000'; + } + return dwLength; +} + + +/* +** MAIN +*/ +int main (int argc, char *argv[]) { + char name[ MAX_PATH ]; + DWORD dwLength; + int size; + luaL_Buffer b; + int i; +#ifdef UNICODE + TCHAR lua_wstring[4098]; +#endif + char lua_string[4098]; + lua_State *L = lua_open(); + (void)argc; /* avoid "unused parameter" warning */ + luaL_openlibs(L); + dwLength = GetModulePath( NULL, name, MAX_PATH ); + if(dwLength) { /* Optional bootstrap */ + strcat(name, ".lua"); + if(!luaL_loadfile (L, name)) { + if(lua_pcall (L, 0, LUA_MULTRET, 0)) { + report (L); + lua_close (L); + return EXIT_FAILURE; + } + } + } + luaL_buffinit(L, &b); + for(i = 1; ; i++) { +#ifdef UNICODE + size = LoadString(GetModuleHandle(NULL), i, lua_wstring, + sizeof(lua_string)/sizeof(TCHAR)); + if(size > 0) wcstombs(lua_string, lua_wstring, size + 1); +#else + size = LoadString(GetModuleHandle(NULL), i, lua_string, + sizeof(lua_string)/sizeof(char)); +#endif + if(size) luaL_addlstring(&b, lua_string, size); else break; + } + luaL_pushresult(&b); + if (runlua (L, lua_tostring(L, -1), argc, argv)) { + report (L); + lua_close (L); + return EXIT_FAILURE; + } + lua_close (L); + return EXIT_SUCCESS; +} diff --git a/src/luarocks/build/builtin.lua b/src/luarocks/build/builtin.lua index b874b82d..dacaad0a 100644 --- a/src/luarocks/build/builtin.lua +++ b/src/luarocks/build/builtin.lua @@ -76,7 +76,37 @@ function run(rockspec) end end - if is_platform("win32") then + if is_platform("mingw32") then + compile_object = function(object, source, defines, incdirs) + local extras = {} + add_flags(extras, "-D%s", defines) + add_flags(extras, "-I%s", incdirs) + return execute(variables.CC.." "..variables.CFLAGS, "-c", "-o", object, "-I"..variables.LUA_INCDIR, source, unpack(extras)) + end + compile_library = function(library, objects, libraries, libdirs, name) + local extras = { unpack(objects) } + add_flags(extras, "-L%s", libdirs) + add_flags(extras, "-l%s", libraries) + extras[#extras+1] = "-L" .. variables.LUA_LIBDIR + extras[#extras+1] = "-llua51" + extras[#extras+1] = "-lmsvcrt" + local ok = execute(variables.LD.." "..variables.LIBFLAG, "-o", library, unpack(extras)) + return ok + end + compile_wrapper_binary = function(fullname, name) + local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\") + local basename = name:gsub("%.lua$", ""):gsub("/", "\\") + local rcname = basename..".rc" + local resname = basename..".o" + local wrapname = basename..".exe" + make_rc(fullname, fullbasename..".rc") + local ok = execute(variables.RC, "-o", resname, rcname) + if not ok then return ok end + ok = execute(variables.LD, "-o", wrapname, resname, variables.WRAPPER, + "-L"..variables.LUA_LIBDIR, "-llua51", "-lmsvcrt", "-luser32") + return ok, wrapname + end + elseif is_platform("win32") then compile_object = function(object, source, defines, incdirs) local extras = {} add_flags(extras, "-D%s", defines) diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 220e7b29..5ec87bf5 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -74,6 +74,8 @@ elseif system and system:match("^CYGWIN") then detected.cygwin = true elseif system and system:match("^Windows") then detected.windows = true +elseif system and system:match("^MINGW") then + detected.mingw32 = true else detected.unix = true -- Fall back to Unix in unknown systems. @@ -82,7 +84,7 @@ end -- Path configuration: local sys_config_file, home_config_file, home_tree -if detected.windows then +if detected.windows or detected.mingw32 then home = os.getenv("APPDATA") or "c:" sys_config_file = "c:/luarocks/config.lua" home_config_file = home.."/luarocks/config.lua" @@ -185,6 +187,39 @@ if detected.windows then defaults.local_cache = home.."/cache/luarocks" end +if detected.mingw32 then + home_config_file = home_config_file:gsub("\\","/") + defaults.arch = "mingw32-"..proc + defaults.platforms = { "win32", "mingw32" } + defaults.lib_extension = "dll" + defaults.external_lib_extension = "dll" + defaults.obj_extension = "o" + defaults.external_deps_dirs = { "c:/external/" } + defaults.variables.LUA_BINDIR = config.LUA_BINDIR and config.LUA_BINDIR:gsub("\\", "/") or "c:/lua5.1/bin" + defaults.variables.LUA_INCDIR = config.LUA_INCDIR and config.LUA_INCDIR:gsub("\\", "/") or "c:/lua5.1/include" + defaults.variables.LUA_LIBDIR = config.LUA_LIBDIR and config.LUA_LIBDIR:gsub("\\", "/") or "c:/lua5.1/lib" + defaults.cmake_generator = "MinGW Makefiles" + defaults.make = "mingw32-make" -- TODO: Split Windows flavors between mingw and msvc + defaults.makefile = "Makefile.win" + defaults.variables.CC = "gcc" + defaults.variables.RC = "windres" + defaults.variables.WRAPPER = config.LUAROCKS_PREFIX .. "\\2.0\\rclauncher.o" + defaults.variables.LD = "gcc" + defaults.variables.CFLAGS = "-O2" + defaults.variables.LIBFLAG = "-shared --dll --export-all-symbols" + defaults.external_deps_patterns = { + bin = { "?.exe", "?.bat" }, + lib = { "lib?.a", "?.dll", "lib?.dll" }, + include = { "?.h" } + } + defaults.runtime_external_deps_patterns = { + bin = { "?.exe", "?.bat" }, + lib = { "?.dll", "lib?.dll" }, + include = { "?.h" } + } + defaults.local_cache = home.."/cache/luarocks" +end + if detected.unix then defaults.lib_extension = "so" defaults.external_lib_extension = "so" -- cgit v1.2.3-55-g6feb