Windows port(wingw cross compiler targets)

This commit is contained in:
Łukasz Magiera 2016-02-27 15:30:34 +01:00
parent 0c3c3d58ce
commit 7dab3da74b
13 changed files with 266 additions and 23 deletions

View File

@ -27,20 +27,35 @@ BUILDDIRECTORIES = $(patsubst $(SOURCE)/%, $(BUILD)%, $(SRCDIRECTORIES))
CFILES = $(shell find $(SOURCE) -type f -name '*.c')
OBJECTS := $(patsubst $(SOURCE)/%.c, $(BUILD)%.c.o, $(CFILES))
OUTNAME = lupi
# Targets
# Pseudo Targets
debug: CFLAGS+= -g -DLOGGING -DDEBUG
debug: build
winexe: $(BUILD)$(OUTNAME)
cp $(BUILD)$(OUTNAME) $(BUILD)$(OUTNAME).exe
win: LIBS+= -lws2_32
win: all winexe
win-build: LIBS+= -lws2_32
win-build: build winexe
win-debug: LIBS+= -lws2_32
win-debug: debug winexe
$(BUILDDIRECTORIES):
mkdir -p $@
build: smallclean $(BUILDDIRECTORIES) resources $(BUILD)lupi
build: smallclean $(BUILDDIRECTORIES) resources $(BUILD)$(OUTNAME)
all: clean build
$(BUILD)lupi: $(OBJECTS)
$(BUILD)$(OUTNAME): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBS)
$(BUILD)%.c.o: $(SOURCE)/%.c

View File

@ -7,13 +7,13 @@ OUT=$TOOL
if [ $# -lt 1 ]
then
echo "Usage : $0 [all|arm32|x86_64|i486] <libevent|libressl>"
echo "Usage : $0 [all|arm32|x86_64|i486|x86_64-win|i686-win] <libevent|libressl>"
exit
fi
case "$1" in
all )
TARGETS=(arm32 i486 x86_64)
TARGETS=(arm32 i486 x86_64 x86_64-win i686-win)
for i in ${TARGETS[@]}; do
./$0 $i $2
done
@ -21,19 +21,23 @@ case "$1" in
arm32 )
TOOL=arm-linux-musleabihf
OUT=$TOOL
OPENSSL_TARGET=linux-generic32
;;
i486 )
TOOL=i486-linux-musl
OUT=$TOOL
OPENSSL_TARGET=linux-generic32
;;
x86_64 )
TOOL=x86_64-linux-musl
OUT=$TOOL
OPENSSL_TARGET=linux-generic64
;;
x86_64-win )
TOOL=x86_64-w64-mingw32
OUT=$TOOL
;;
i686-win )
TOOL=i686-w64-mingw32
OUT=$TOOL
;;
*) echo "Invalid target!" ; exit 1
;;
esac

View File

@ -5,6 +5,10 @@
#include <stdlib.h>
#include <unistd.h>
#ifdef _WIN32
#define WIN32
#endif
#include <event2/event.h>
#include <event2/event_struct.h>

View File

@ -1,4 +1,6 @@
#include "lupi.h"
#ifndef _WIN32
#include <sys/mount.h>
#include <sys/types.h>
#include <unistd.h>
@ -9,3 +11,8 @@ void lupi_init() {
mount(NULL, "/proc", "procfs", 0, NULL);
}
}
#else
void lupi_init() {
}
#endif

View File

@ -9,9 +9,6 @@
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <strings.h>
#include <fcntl.h>
#include <errno.h>
@ -19,6 +16,15 @@
#include <openssl/ssl.h>
#include <openssl/conf.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
SSL_CTX* ctx = NULL;
static int l_open(lua_State *L) { /* TODO: Any mem leaks? */
@ -54,8 +60,12 @@ static int l_open(lua_State *L) { /* TODO: Any mem leaks? */
close(sockfd);
continue;
}
#ifdef _WIN32
u_long blockmode = 1;
ioctlsocket(sockfd,FIONBIO,&blockmode);
#else
fcntl(sockfd, F_SETFL, O_NONBLOCK);
#endif
break;
}
@ -197,8 +207,15 @@ static void ssl_init() {
void internet_start(lua_State *L) {
ssl_init();
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#else
WSADATA wd;
if(WSAStartup(MAKEWORD(2,2), &wd)!=0) {
WSACleanup();
exit(1);
}
#endif
struct luaL_Reg netlib[] = {
{"open", l_open},
{"write", l_write},

119
src/c/lib/wcwidth.c Normal file
View File

@ -0,0 +1,119 @@
/*
* This is an implementation of wcwidth() and wcswidth() as defined in
* "The Single UNIX Specification, Version 2, The Open Group, 1997"
* <http://www.UNIX-systems.org/online.html>
*
* Markus Kuhn -- 2001-01-12 -- public domain
* ftp://ftp.maths.tcd.ie/src/windows/putty/wcwidth.c
*/
#include <wchar.h>
struct interval {
unsigned short first;
unsigned short last;
};
/* auxiliary function for binary search in interval table */
static int bisearch(wchar_t ucs, const struct interval *table, int max) {
int min = 0;
int mid;
if (ucs < table[0].first || ucs > table[max].last)
return 0;
while (max >= min) {
mid = (min + max) / 2;
if (ucs > table[mid].last)
min = mid + 1;
else if (ucs < table[mid].first)
max = mid - 1;
else
return 1;
}
return 0;
}
#ifdef _WIN32
int wcwidth(wchar_t ucs)
{
/* sorted list of non-overlapping intervals of non-spacing characters */
static const struct interval combining[] = {
{ 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
{ 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
{ 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
{ 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
{ 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
{ 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
{ 0x07A6, 0x07B0 }, { 0x0901, 0x0902 }, { 0x093C, 0x093C },
{ 0x0941, 0x0948 }, { 0x094D, 0x094D }, { 0x0951, 0x0954 },
{ 0x0962, 0x0963 }, { 0x0981, 0x0981 }, { 0x09BC, 0x09BC },
{ 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 },
{ 0x0A02, 0x0A02 }, { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 },
{ 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 },
{ 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 },
{ 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 },
{ 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
{ 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
{ 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
{ 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
{ 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
{ 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA },
{ 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 },
{ 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 },
{ 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD },
{ 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 },
{ 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 },
{ 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC },
{ 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, { 0x1032, 0x1032 },
{ 0x1036, 0x1037 }, { 0x1039, 0x1039 }, { 0x1058, 0x1059 },
{ 0x1160, 0x11FF }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
{ 0x17C9, 0x17D3 }, { 0x180B, 0x180E }, { 0x18A9, 0x18A9 },
{ 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x206A, 0x206F },
{ 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
{ 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF },
{ 0xFFF9, 0xFFFB }
};
/* test for 8-bit control characters */
if (ucs == 0)
return 0;
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
return -1;
/* binary search in table of non-spacing characters */
if (bisearch(ucs, combining,
sizeof(combining) / sizeof(struct interval) - 1))
return 0;
/* if we arrive here, ucs is not a combining or C0/C1 control character */
return 1 +
(ucs >= 0x1100 &&
(ucs <= 0x115f || /* Hangul Jamo init. consonants */
(ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
ucs != 0x303f) || /* CJK ... Yi */
(ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
(ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
(ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
(ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
(ucs >= 0xffe0 && ucs <= 0xffe6) ||
(ucs >= 0x20000 && ucs <= 0x2ffff)));
}
int wcswidth(const wchar_t *pwcs, size_t n)
{
int w, width = 0;
for (;*pwcs && n-- > 0; pwcs++)
if ((w = wcwidth(*pwcs)) < 0)
return -1;
else
width += w;
return width;
}
#endif

View File

@ -4,10 +4,14 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifndef _WIN32
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#else
#include <windows.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
@ -99,7 +103,11 @@ static int l_fs_exists (lua_State *L) {
static int l_fs_mkdir (lua_State *L) {
const char* fname = lua_tostring(L, 1);
#ifndef _WIN32
if( mkdir( fname, 0755 ) != -1 ) {
#else
if( mkdir( fname ) != -1 ) {
#endif
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
@ -125,12 +133,16 @@ static int l_fs_isdir (lua_State *L) {
static int l_fs_spaceUsed (lua_State *L) {
const char* fname = lua_tostring(L, 1);
#ifndef _WIN32
struct statvfs s;
if( statvfs(fname, &s) != -1 ) {
lua_pushnumber(L, s.f_bsize * s.f_bfree);
} else {
lua_pushnumber(L, -1);
}
#else
lua_pushnumber(L, -1);
#endif
return 1;
}
@ -186,12 +198,16 @@ static int l_fs_write (lua_State *L) {
static int l_fs_spaceTotal (lua_State *L) {
const char* fname = lua_tostring(L, 1);
#ifndef _WIN32
struct statvfs s;
if( statvfs(fname, &s) != -1 ) {
lua_pushnumber(L, s.f_frsize * s.f_blocks);
} else {
lua_pushnumber(L, -1);
}
#else
lua_pushnumber(L, -1);
#endif
return 1;
}
@ -319,6 +335,7 @@ static int l_fs_read (lua_State *L) {
static int l_beep (lua_State *L) {
int freq = lua_tonumber(L, 1);
int btime = lua_tonumber(L, 2);
#ifndef _WIN32
int console_fd = -1;
if((console_fd = open("/dev/console", O_WRONLY)) == -1) {
@ -334,6 +351,7 @@ static int l_beep (lua_State *L) {
usleep(1000 * btime);
ioctl(console_fd, KIOCSOUND, 0);
close(console_fd);
#endif /* TODO win32 implementation */
return 0;
}
@ -345,16 +363,40 @@ static int l_uptime (lua_State *L) { /* Return ms */
}
static int l_totalMemory (lua_State *L) {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status );
lua_pushnumber(L, status.dwTotalPhys);
#elif defined(_WIN32)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx( &status );
lua_pushnumber(L, (size_t)status.ullTotalPhys);
#else
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
lua_pushnumber(L, pages * page_size);
#endif
return 1;
}
static int l_freeMemory (lua_State *L) {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status );
lua_pushnumber(L, status.dwAvailPhys);
#elif defined(_WIN32)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx( &status );
lua_pushnumber(L, (size_t)status.ullAvailPhys);
#else
long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
lua_pushnumber(L, pages * page_size);
#endif
return 1;
}
@ -378,6 +420,15 @@ static int l_towlower (lua_State *L) {
return 1;
}
static int l_platform (lua_State *L) { /* returns platform identifiers separated by | */
#ifndef _WIN32
lua_pushstring(L, "unix|linux|other");
#else
lua_pushstring(L, "windows");
#endif
return 1;
}
#ifdef DEBUG
static int l_debug (lua_State *L) {
return 0;
@ -412,6 +463,7 @@ void luanative_start(lua_State *L) {
{"totalMemory", l_totalMemory},
{"freeMemory", l_freeMemory},
{"pull", l_pull},
{"platform", l_platform},
#ifdef DEBUG
{"debug", l_debug},
#endif

View File

@ -8,7 +8,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
//#include <sys/statvfs.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

View File

@ -3,9 +3,11 @@
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include <signal.h>
#ifndef _WIN32
#include <sys/ioctl.h>
#include <termios.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
@ -31,12 +33,23 @@ static int l_term_restore (lua_State *L) {
return 0;
}
#else
static int l_get_term_sz (lua_State *L) { return 0; }
static int l_term_restore (lua_State *L) { return 0; }
#endif
static int l_term_init (lua_State *L) {
#ifndef _WIN32
tcsetattr (STDOUT_FILENO, TCSAFLUSH, &new);
return 0;
lua_pushboolean(L, 1);
#else
lua_pushboolean(L, 0);
#endif
return 1;
}
void termutils_start(lua_State *L) {
#ifndef _WIN32
signal(SIGWINCH, handle_winch);
if (tcgetattr (STDOUT_FILENO, &old) != 0)
@ -49,6 +62,7 @@ void termutils_start(lua_State *L) {
new.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
new.c_cflag &= ~(CSIZE | PARENB);
new.c_cflag |= CS8;
#endif
struct luaL_Reg termlib[] = {
{"getSize", l_get_term_sz},

View File

@ -2,6 +2,11 @@ local boot = {}
function boot.boot()
local gpu = modules.component.api.proxy(modules.component.api.list("gpu", true)())
if not gpu then
gpu = setmetatable({}, {__index = function()
return function() return 0, 0 end
end})
end
local w, h = gpu.getResolution()
local function bsod(...)

View File

@ -57,7 +57,7 @@ function filesystem.register(basePath, uuid)
native.fs_mkdir(basePath)
end
if not native.fs_isdir(basePath) then
error("Filesystem root is not a directory!")
error("Filesystem root is not a directory! (basePath=".. tostring(basePath) ..")")
end
local function realpath(path)
checkArg(1, path, "string")

View File

@ -79,12 +79,14 @@ function main()
modules.gpio.register()
modules.internet.start()
modules.filesystem.register("root")
if native.debug then
if native.debug and native.platform():match("unix") then
modules.filesystem.register("/", "11111111-1111-1111-1111-111111111111")
end
modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid())
modules.textgpu.start()
local textgpuAddr, tgfail = modules.textgpu.start()
if not textgpuAddr then
lprint("Couldn't initialize text gpu: " .. tostring(tgfail))
end
if native.debug then
modules.debug.hook()
end

View File

@ -245,7 +245,9 @@ function textgpu.start()
return screenAddr
end
termutils.init()
if not termutils.init() then
return nil, "Cannot initialize terminal based gpu"
end
write("\x1b[?25l") --Disable cursor
local w, h = gpu.getResolution()
_height = h
@ -253,7 +255,7 @@ function textgpu.start()
gpu.setForeground(0xFFFFFF)
gpu.setBackground(0x000000)
modules.component.api.register(nil, "gpu", gpu)
local gpuaddr = modules.component.api.register(nil, "gpu", gpu)
screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c
modules.component.api.register("TODO:SetThisUuid", "keyboard", {})
@ -262,6 +264,8 @@ function textgpu.start()
io.flush()
termutils.restore()
end
return gpuaddr
end
return textgpu