Skip to content

Commit 02866be

Browse files
committed
Premake support for UTF-8
1 parent b01da7d commit 02866be

38 files changed

Lines changed: 895 additions & 536 deletions

Bootstrap.mak

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ mingw-clean: nix-clean
106106

107107
mingw: mingw-clean
108108
mkdir -p build/bootstrap
109-
$(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_STATICLIB -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $(SRC) -lole32 -lversion
109+
$(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_STATICLIB -DUNICODE -D_UNICODE -municode -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $(SRC) -lole32 -lversion
110110
./build/bootstrap/premake_bootstrap embed
111111
./build/bootstrap/premake_bootstrap --arch=$(PLATFORM) --os=windows --to=build/bootstrap --cc=gcc $(PREMAKE_OPTS) gmake
112112
$(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` config=$(CONFIG)_$(PLATFORM:x86=win32)
@@ -162,7 +162,7 @@ haiku: haiku-clean
162162

163163
windows-base: windows-clean
164164
if not exist build\bootstrap (mkdir build\bootstrap)
165-
cl /Fo.\build\bootstrap\ /Fe.\build\bootstrap\premake_bootstrap.exe /DPREMAKE_NO_BUILTIN_SCRIPTS /DLUA_STATICLIB /I"$(LUA_DIR)" /I"$(LUASHIM_DIR)" user32.lib ole32.lib advapi32.lib $(SRC)
165+
cl /Fo.\build\bootstrap\ /Fe.\build\bootstrap\premake_bootstrap.exe /DPREMAKE_NO_BUILTIN_SCRIPTS /DLUA_STATICLIB /DUNICODE /D_UNICODE /I"$(LUA_DIR)" /I"$(LUASHIM_DIR)" user32.lib ole32.lib advapi32.lib $(SRC)
166166
.\build\bootstrap\premake_bootstrap.exe embed
167167
.\build\bootstrap\premake_bootstrap --arch=$(PLATFORM) --to=build/bootstrap $(PREMAKE_OPTS) $(MSDEV)
168168

binmodules/luasocket/src/inet.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
#include "inet.h"
1414

15+
#ifdef _WIN32 /* PREMAKE: Fix for unicode Windows build */
16+
#undef gai_strerror
17+
#define gai_strerror gai_strerrorA
18+
#endif
19+
1520
/*=========================================================================*\
1621
* Internal function prototypes.
1722
\*=========================================================================*/

binmodules/luasocket/src/udp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#define MAX(x, y) ((x) > (y) ? x : y)
2424
#endif
2525

26+
#ifdef _WIN32 /* PREMAKE: Fix for unicode Windows build */
27+
#undef gai_strerror
28+
#define gai_strerror gai_strerrorA
29+
#endif
30+
2631
/*=========================================================================*\
2732
* Internal function prototypes
2833
\*=========================================================================*/

binmodules/luasocket/src/wsocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ const char *socket_gaistrerror(int err) {
427427
#ifdef EAI_SYSTEM
428428
case EAI_SYSTEM: return strerror(errno);
429429
#endif
430-
default: return gai_strerror(err);
430+
default: return gai_strerrorA(err);
431431
}
432432
}
433433

premake5.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224

225225
multiprocessorcompile "On"
226226
warnings "Extra"
227+
symbols "On"
227228

228229
filter { "options:not zlib-src=none" }
229230
defines { "PREMAKE_COMPRESSION" }
@@ -273,7 +274,6 @@
273274

274275
filter "configurations:Debug"
275276
defines "_DEBUG"
276-
symbols "On"
277277

278278
filter "configurations:Release"
279279
defines "NDEBUG"
@@ -287,6 +287,14 @@
287287
filter { "system:windows", "configurations:Release" }
288288
incrementallink "Off"
289289

290+
filter { "system:windows", "action:vs*" }
291+
characterset "Unicode"
292+
293+
filter { "system:windows", "action:not vs*" } -- TODO: mingw doesn't support `characterset "Unicode"`
294+
defines { "UNICODE", "_UNICODE" }
295+
buildoptions { "-municode" }
296+
linkoptions { "-municode" }
297+
290298
-- MinGW AR does not handle LTO out of the box and need a plugin to be setup
291299
filter { "system:windows", "configurations:Release", "toolset:not gcc" }
292300
linktimeoptimization "On"
@@ -351,7 +359,7 @@
351359
targetdir "bin/release"
352360

353361
filter "system:windows"
354-
links { "ole32", "ws2_32", "advapi32", "version" }
362+
links { "ole32", "ws2_32", "advapi32", "version", "user32" }
355363

356364
filter { "system:windows", "toolset:msc*" }
357365
files { "src/**.rc" }

src/base/help.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
printf("Premake %s, a build script generator", _PREMAKE_VERSION)
1212
printf(_PREMAKE_COPYRIGHT)
1313
printf("%s %s", _VERSION, _COPYRIGHT)
14+
if _UTF8_ENABLED then printf("UTF-8 support enabled") end
1415
printf("")
1516
printf("Usage: premake5 [options] action [arguments]")
1617
printf("")

src/host/http_download.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ int http_download(lua_State* L)
2222
long responseCode = 0;
2323

2424
FILE* fp;
25+
#if PLATFORM_WINDOWS
26+
const wchar_t *file = luaL_checkconvertstring(L, 2);
27+
fp = _wfopen(file, L"wb");
28+
lua_pop(L, 1);
29+
#else
2530
const char* file = luaL_checkstring(L, 2);
26-
2731
fp = fopen(file, "wb");
32+
#endif
2833
if (!fp)
2934
{
3035
lua_pushstring(L, "Unable to open file.");

src/host/lua_auxlib.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ int premake_luaL_loadfilex (lua_State* L, const char* filename, const char* mode
103103
* script chunk on the stack. Turn these into a closure that will call my
104104
* wrapper below when the loaded script needs to be executed. */
105105

106-
assert(lua_gettop(L) == bottom + 2);
106+
assert(lua_gettop(L) == bottom + (z == OKAY ? 2 : 0));
107107

108108
if (z == OKAY) {
109109
/* if we are called with an env, then our caller, luaB_loadfile, will
@@ -149,7 +149,7 @@ static int chunk_wrapper(lua_State* L)
149149
/* Remember the current _SCRIPT and working directory so I can
150150
* restore them after this new chunk has been run. */
151151

152-
do_getcwd(cwd, PATH_MAX);
152+
do_getcwd(cwd, sizeof(cwd) / sizeof(cwd[0]));
153153
lua_getglobal(L, "_SCRIPT");
154154
lua_getglobal(L, "_SCRIPT_DIR");
155155

@@ -265,11 +265,19 @@ int premake_luaB_dofile(lua_State *L)
265265
/**
266266
* Copy of readable from loadlib.c in Lua source
267267
*/
268-
static int readable(const char *filename) {
269-
FILE *f = fopen(filename, "r"); /* try to open file */
270-
if (f == NULL) return 0; /* open failed */
271-
fclose(f);
272-
return 1;
268+
static int readable(lua_State *L, const char *filename) {
269+
#if PLATFORM_WINDOWS
270+
const wchar_t *wfilename = luaL_convertstring(L, filename);
271+
if (wfilename == NULL) return 0; /* conversion failed */
272+
FILE *f = _wfopen(wfilename, L"r");
273+
lua_pop(L, 1);
274+
#else
275+
(void)(L);
276+
FILE *f = fopen(filename, "r");
277+
#endif
278+
if (f == NULL) return 0; /* open failed */
279+
fclose(f);
280+
return 1;
273281
}
274282

275283

@@ -302,7 +310,7 @@ static const char *searchpath(lua_State *L, const char *name,
302310
const char *filename = luaL_gsub(L, lua_tostring(L, -1),
303311
LUA_PATH_MARK, name);
304312
lua_remove(L, -2); /* remove path template */
305-
if (readable(filename)) /* does file exist and is readable? */
313+
if (readable(L, filename)) /* does file exist and is readable? */
306314
return filename; /* return that file name */
307315
lua_pushfstring(L, "\n\tno file '%s'", filename);
308316
lua_remove(L, -2); /* remove file name */

src/host/os_chdir.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ int do_chdir(lua_State* L, const char* path)
1212
int z;
1313

1414
#if PLATFORM_WINDOWS
15-
wchar_t wide_buffer[PATH_MAX];
16-
if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_buffer, PATH_MAX) == 0)
17-
{
18-
lua_pushstring(L, "unable to encode path");
19-
return lua_error(L);
20-
}
21-
22-
z = SetCurrentDirectoryW(wide_buffer);
15+
const wchar_t *wpath = luaL_convertstring(L, path);
16+
if (!wpath) return luaL_error(L, "unable to encode path");
17+
z = SetCurrentDirectoryW(wpath);
18+
lua_pop(L, 1);
2319
#else
2420
(void)(L); /* warning: unused parameter */
2521

src/host/os_comparefiles.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,20 @@ int os_comparefiles(lua_State* L)
1919
size_t read;
2020
char firstBuffer[4096];
2121
char secondBuffer[4096];
22+
23+
#if PLATFORM_WINDOWS
24+
// if we read the first argument first, it might push to the stack obscuring
25+
// a missing second argument. So read the second argument first.
26+
const wchar_t *secondPath = luaL_checkconvertstring(L, 2);
27+
const wchar_t *firstPath = luaL_checkconvertstring(L, 1);
28+
firstFile = _wfopen(firstPath, L"rb");
29+
secondFile = _wfopen(secondPath, L"rb");
30+
#else
2231
const char* firstPath = luaL_checkstring(L, 1);
2332
const char* secondPath = luaL_checkstring(L, 2);
24-
25-
#if PLATFORM_WINDOWS
26-
wchar_t wide_firstPath[PATH_MAX];
27-
if (MultiByteToWideChar(CP_UTF8, 0, firstPath, -1, wide_firstPath, PATH_MAX) == 0)
28-
{
29-
lua_pushnil(L);
30-
lua_pushstring(L, "unable to encode first path");
31-
return 2;
32-
}
33-
34-
wchar_t wide_secondPath[PATH_MAX];
35-
if (MultiByteToWideChar(CP_UTF8, 0, secondPath, -1, wide_secondPath, PATH_MAX) == 0)
36-
{
37-
lua_pushnil(L);
38-
lua_pushstring(L, "unable to encode second path");
39-
return 2;
40-
}
41-
42-
firstFile = _wfopen(wide_firstPath, L"rb");
43-
secondFile = _wfopen(wide_secondPath, L"rb");
44-
#else
4533
firstFile = fopen(firstPath, "rb");
4634
secondFile = fopen(secondPath, "rb");
47-
#endif
35+
#endif
4836

4937
if (!firstFile)
5038
{

0 commit comments

Comments
 (0)