# HG changeset patch
# User Alexander De Sousa <aphanic@hotmail.com>
# Date 1406492126 -7200
#      Sun Jul 27 22:15:26 2014 +0200
# Node ID b21cde6870b728724b326e8ee6453e5a378b0ff8
# Parent  2869e38eab7eedc31bb9abdd22b839c756f9fa90
Add CRT startup code for Unicode console programs.

diff -r 2869e38eab7e -r b21cde6870b7 CMakeLists.txt
--- a/CMakeLists.txt	Sun Jul 27 22:14:53 2014 +0200
+++ b/CMakeLists.txt	Sun Jul 27 22:15:26 2014 +0200
@@ -95,7 +95,7 @@
 set(ARM_SOURCES arm_gen.c)
 
 set(LIBTCC1_I386_SOURCES lib/alloca86.S lib/alloca86-bt.S)
-set(LIBTCC1_WIN_SOURCES win32/lib/crt1.c win32/lib/wincrt1.c win32/lib/dllcrt1.c win32/lib/dllmain.c win32/lib/chkstk.S)
+set(LIBTCC1_WIN_SOURCES win32/lib/crt1.c win32/lib/wcrt1.c win32/lib/wincrt1.c win32/lib/dllcrt1.c win32/lib/dllmain.c win32/lib/chkstk.S)
 if(NOT WIN32)
   set(LIBTCC1_I386_SOURCES ${LIBTCC1_I386_SOURCES} lib/bcheck.c)
 endif()
diff -r 2869e38eab7e -r b21cde6870b7 lib/Makefile
--- a/lib/Makefile	Sun Jul 27 22:14:53 2014 +0200
+++ b/lib/Makefile	Sun Jul 27 22:15:26 2014 +0200
@@ -47,8 +47,8 @@
 I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BCHECK_O)
 X86_64_O = libtcc1.o alloca86_64.o
 ARM_O = libtcc1.o armeabi.o alloca-arm.o
-WIN32_O = $(I386_O) crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o
-WIN64_O = $(X86_64_O) crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o
+WIN32_O = $(I386_O) crt1.o wcrt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o
+WIN64_O = $(X86_64_O) crt1.o wcrt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o
 
 # build TCC runtime library to contain PIC code, so it can be linked
 # into shared libraries
diff -r 2869e38eab7e -r b21cde6870b7 win32/build-tcc.bat
--- a/win32/build-tcc.bat	Sun Jul 27 22:14:53 2014 +0200
+++ b/win32/build-tcc.bat	Sun Jul 27 22:15:26 2014 +0200
@@ -39,6 +39,7 @@
 :libtcc1.a
 .\tcc %target% -c ../lib/libtcc1.c
 .\tcc %target% -c lib/crt1.c
+.\tcc %target% -c lib/wcrt1.c
 .\tcc %target% -c lib/wincrt1.c
 .\tcc %target% -c lib/dllcrt1.c
 .\tcc %target% -c lib/dllmain.c
@@ -49,12 +50,12 @@
 .\tcc %target% -c ../lib/alloca86.S
 .\tcc %target% -c ../lib/alloca86-bt.S
 .\tcc %target% -c ../lib/bcheck.c
-tiny_libmaker lib/libtcc1.a libtcc1.o alloca86.o alloca86-bt.o crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o bcheck.o
+tiny_libmaker lib/libtcc1.a libtcc1.o alloca86.o alloca86-bt.o crt1.o wcrt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o bcheck.o
 @goto the_end
 
 :lib64
 .\tcc %target% -c ../lib/alloca86_64.S
-tiny_libmaker lib/libtcc1.a libtcc1.o alloca86_64.o crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o
+tiny_libmaker lib/libtcc1.a libtcc1.o alloca86_64.o crt1.o wcrt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o
 
 :the_end
 del *.o
diff -r 2869e38eab7e -r b21cde6870b7 win32/lib/wcrt1.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/win32/lib/wcrt1.c	Sun Jul 27 22:15:26 2014 +0200
@@ -0,0 +1,34 @@
+// =============================================
+// wcrt1.c
+
+#include <stdlib.h>
+
+#define __UNKNOWN_APP    0
+#define __CONSOLE_APP    1
+#define __GUI_APP        2
+void __set_app_type(int);
+void _controlfp(unsigned a, unsigned b);
+
+typedef struct
+{
+    int newmode;
+} _startupinfo;
+
+void __wgetmainargs(int *pargc, wchar_t ***pargv, wchar_t ***penv, int globb, _startupinfo*);
+int wmain(int argc, wchar_t **argv, wchar_t **env);
+
+int _wstart(void)
+{
+    __TRY__
+    int argc; wchar_t **argv; wchar_t **env; int ret;
+    _startupinfo start_info = {0};
+
+    _controlfp(0x10000, 0x30000);
+    __set_app_type(__CONSOLE_APP);
+    __wgetmainargs(&argc, &argv, &env, 0, &start_info);
+
+    ret = wmain(argc, argv, env);
+    exit(ret);
+}
+
+// =============================================
