Skip to content

Commit

Permalink
added MSVC project configuration for host-side tools (#2665)
Browse files Browse the repository at this point in the history
Added MSVC project configuration (@ziggurat29) and support of MinGW (@TerryE) for host-side`luac.cross` tool
  • Loading branch information
ziggurat29 authored and TerryE committed Feb 23, 2019
1 parent 30ff0a1 commit 62789da
Show file tree
Hide file tree
Showing 15 changed files with 713 additions and 16 deletions.
12 changes: 12 additions & 0 deletions app/include/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@
#define MODULE_EXPAND_PASTE_(x,y) MODULE_PASTE_(x,y)

#ifdef LUA_CROSS_COMPILER
#ifdef _MSC_VER
//on msvc it is necessary to go through more pre-processor hoops to get the
//section name built; string merging does not happen in the _declspecs.
//NOTE: linker magic is invoked via the magical '$' character. Caveat editor.
#define __TOKIFY(s) .rodata1$##s
#define __TOTOK(s) __TOKIFY(s)
#define __STRINGIFY(x) #x
#define __TOSTRING(x) __STRINGIFY(x)
#define __ROSECNAME(s) __TOSTRING(__TOTOK(s))
#define LOCK_IN_SECTION(s) __declspec ( allocate( __ROSECNAME(s) ) )
#else
#define LOCK_IN_SECTION(s) __attribute__((used,unused,section(".rodata1." #s)))
#endif
#else
#define LOCK_IN_SECTION(s) __attribute__((used,unused,section(".lua_" #s)))
#endif
Expand Down
7 changes: 7 additions & 0 deletions app/lua/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

#include "lua.h"
#include C_HEADER_CTYPE
#ifdef __MINGW__
#include <errno.h>
#else
#ifdef _MSC_VER //msvc #defines errno, which interferes with our #include macro
#undef errno
#endif
#include C_HEADER_ERRNO
#endif
#include C_HEADER_STDIO
#include C_HEADER_STDLIB
#include C_HEADER_STRING
Expand Down
14 changes: 14 additions & 0 deletions app/lua/linit.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ extern const luaR_entry lua_rotable_base[];
#define LUA_LIBS lua_libs_core
#endif

#ifdef _MSC_VER
//MSVC requires us to declare these sections before we refer to them
#pragma section(__ROSECNAME(A), read)
#pragma section(__ROSECNAME(zzzzzzzz), read)
#pragma section(__ROSECNAME(libs), read)
#pragma section(__ROSECNAME(rotable), read)

//These help us to find the beginning and ending of the RO data. NOTE: linker
//magic is used; the section names are lexically sorted, so 'a' and 'z' are
//important to keep the other sections lexically between these two dummy
//variables. Check your mapfile output if you need to fiddle with this stuff.
const LOCK_IN_SECTION(A) int _ro_start = 0;
const LOCK_IN_SECTION(zzzzzzzz) int _ro_end = 0;
#endif
static const LOCK_IN_SECTION(libs) luaL_reg LUA_LIBS[] = {
{"", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
Expand Down
4 changes: 4 additions & 0 deletions app/lua/lrotable.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#include "lobject.h"
#include "lapi.h"

#ifdef _MSC_VER
#define ALIGNED_STRING (__declspec( align( 4 ) ) char*)
#else
#define ALIGNED_STRING (__attribute__((aligned(4))) char *)
#endif
#define LA_LINES 16
#define LA_SLOTS 4
//#define COLLECT_STATS
Expand Down
10 changes: 9 additions & 1 deletion app/lua/lrotable.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,29 @@ int luaR_isrotable(void *p);
*/
#if defined(LUA_CROSS_COMPILER)

#if defined(_MSC_VER)
//msvc build uses these dummy vars to locate the beginning and ending addresses of the RO data
extern cons char _ro_start[], _ro_end[];
#define IN_RODATA_AREA(p) (((const char*)(p)) >= _ro_start && ((const char *)(p)) <= _ro_end)
#else /* one of the POSIX variants */
#if defined(__CYGWIN__)
#define _RODATA_END __end__
#elif defined(__MINGW32__)
#define _RODATA_END end
#else
#define _RODATA_END _edata
#endif
extern const char _RODATA_END[];
#define IN_RODATA_AREA(p) (((const char *)(p)) < _RODATA_END)
#endif /* defined(_MSC_VER) */

#else /* xtensa tool chain for ESP target */

extern const char _irom0_text_start[];
extern const char _irom0_text_end[];
#define IN_RODATA_AREA(p) (((const char *)(p)) >= _irom0_text_start && ((const char *)(p)) <= _irom0_text_end)

#endif
#endif /* defined(LUA_CROSS_COMPILER) */

/* Return 1 if the given pointer is a rotable */
#define luaR_isrotable(p) IN_RODATA_AREA(p)
Expand Down
4 changes: 4 additions & 0 deletions app/lua/luac_cross/lflashimg.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ static uint *flashAddrTag = flashImage + LUA_MAX_FLASH_SIZE;
#define getFlashAddrTag(v) ((flashAddrTag[_TW(v)]&_TB(v)) != 0)

#define fatal luac_fatal
#ifdef _MSC_VER
extern void __declspec( noreturn ) luac_fatal( const char* message );
#else
extern void __attribute__((noreturn)) luac_fatal(const char* message);
#endif

#ifdef LOCAL_DEBUG
#define DBG_PRINT(...) printf(__VA_ARGS__)
Expand Down
4 changes: 4 additions & 0 deletions app/lua/luac_cross/luac.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ static TString *corename(lua_State *L, const TString *filename)
{
const char *fn = getstr(filename)+1;
const char *s = strrchr(fn, '/');
if (!s) s = strrchr(fn, '\\');
s = s ? s + 1 : fn;
while (*s == '.') s++;
const char *e = strchr(s, '.');
Expand Down Expand Up @@ -272,6 +273,9 @@ struct Smain {
char** argv;
};

#if defined(_MSC_VER) || defined(__MINGW32__)
typedef unsigned int uint;
#endif
extern uint dumpToFlashImage (lua_State* L,const Proto *main, lua_Writer w,
void* data, int strip,
lu_int32 address, lu_int32 maxSize);
Expand Down
72 changes: 72 additions & 0 deletions app/lua/luac_cross/mingw32-Makefile.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#
# This is a minimal Make file designed to be called from within a MinGW Cmd prompt.
# So if the distro ZIP file has been unpacked into C:\nodemcu-firmware then
#
# C:\nodemcu-firmware\app\lua\luac_cross> mingw32-make -f mingw32-makefile.mak
#
# will create the WinX EXE luac.cross.exe within the root C:\nodemcu-firmware folder.
# This make has been stripped down to use the basic non-graphics MinGW32 install and
# standard Windows commands available at the Cmd> prompt. This make is quite separate
# from the normal toolchain build.

.NOTPARALLEL:

CCFLAGS:= -I.. -I../../include -I../../libc -I../../uzlib -Wall
LDFLAGS:= -lm -Wl,-Map=mapfile
DEFINES += -DLUA_CROSS_COMPILER -DLUA_OPTIMIZE_MEMORY=2

CFLAGS = $(CCFLAGS) $(DEFINES) $(EXTRA_CCFLAGS) $(STD_CFLAGS) $(INCLUDES)

TARGET = host
CC := gcc

ifeq ($(FLAVOR),debug)
CCFLAGS += -O0 -g
TARGET_LDFLAGS += -O0 -g
DEFINES += -DLUA_DEBUG_BUILD
else
FLAVOR = release
CCFLAGS += -O2
TARGET_LDFLAGS += -O2
endif
#
# C files needed to compile luac.cross
#
LUACSRC := luac.c lflashimg.c liolib.c loslib.c print.c
LUASRC := lapi.c lauxlib.c lbaselib.c lcode.c ldblib.c ldebug.c \
ldo.c ldump.c lfunc.c lgc.c linit.c llex.c \
lmathlib.c lmem.c loadlib.c lobject.c lopcodes.c lparser.c \
lrotable.c lstate.c lstring.c lstrlib.c ltable.c ltablib.c \
ltm.c lundump.c lvm.c lzio.c
LIBCSRC := c_stdlib.c
UZSRC := uzlib_deflate.c crc32.c
#
# This relies on the files being unique on the vpath
#
SRC := $(LUACSRC) $(LUASRC) $(LIBCSRC) $(UZSRC)

vpath %.c .:..:../../libc:../../uzlib

INCS := -I.. -I../.. -I../../libc -I../../uzlib
ODIR := .output\obj
OBJS := $(SRC:%.c=$(ODIR)/%.o)
IMAGE := ../../../luac.cross.exe

.PHONY: test clean all

all: $(DEPS) $(IMAGE)

$(IMAGE) : $(OBJS)
$(CC) $(OBJS) -o $@ $(LDFLAGS)

test :
@echo CC: $(CC)
@echo SRC: $(SRC)
@echo OBJS: $(OBJS)

clean :
del /s /q $(ODIR)

$(ODIR)/%.o: %.c
@mkdir $(ODIR) || echo .
$(CC) $(INCS) $(CFLAGS) -o $@ -c $<
28 changes: 16 additions & 12 deletions app/lua/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#endif


#if defined(LUA_CROSS_COMPILER)
#if defined(LUA_CROSS_COMPILER) && !defined(_MSC_VER) && !defined(__MINGW32__)
#define LUA_USE_LINUX
#endif

Expand Down Expand Up @@ -264,7 +264,7 @@
#include <io.h>
#ifdef LUA_CROSS_COMPILER
#include <stdio.h>
else
#else
#include "c_stdio.h"
#endif

Expand Down Expand Up @@ -631,7 +631,11 @@ extern int readline4lua(const char *prompt, char *buffer, int length);
#define lua_str2number(s,p) c_strtoll((s), (p), 10)
#endif // #if !defined LUA_INTEGRAL_LONGLONG
#else
#ifdef _MSC_VER //what's wrong with stdlib strtod?
#define lua_str2number(s,p) strtod((s), (p))
#else
#define lua_str2number(s,p) c_strtod((s), (p))
#endif
#endif // #if defined LUA_NUMBER_INTEGRAL

/*
Expand Down Expand Up @@ -760,18 +764,18 @@ union luai_Cast { double l_d; long l_l; };
{ if ((c)->status == 0) (c)->status = -1; }
#define luai_jmpbuf int /* dummy variable */

#elif defined(LUA_USE_ULONGJMP)
#else
#if defined(LUA_USE_ULONGJMP)
/* in Unix, try _longjmp/_setjmp (more efficient) */
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf

#define LONGJMP(a,b) _longjmp(a,b)
#define SETJMP(a) _setjmp(a)
#else
/* default handling with long jumps */
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
#define LONGJMP(a,b) longjmp(a,b)
#define SETJMP(a) setjmp(a)
#endif
#define LUAI_THROW(L,c) LONGJMP((c)->b, 1)
#define LUAI_TRY(L,c,a) if (SETJMP((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf

#endif


Expand Down Expand Up @@ -910,4 +914,4 @@ union luai_Cast { double l_d; long l_l; };
#error "Pipes not supported in aggresive optimization mode (LUA_OPTIMIZE_MEMORY=2)"
#endif

#endif
#endif
13 changes: 10 additions & 3 deletions app/uzlib/uzlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@
#define uz_malloc os_malloc
#define uz_free os_free

#else /* POSIX */
#else /* Host */

#include <stdint.h>
#include <stdlib.h>

extern int dbg_break(void);
#if defined(_MSC_VER) || defined(__MINGW32__) //msvc requires old name for longjmp
#define UZLIB_THROW(v) {dbg_break();longjmp(unwindAddr, (v));}
#define UZLIB_SETJMP(n) setjmp(n)
#else
#define UZLIB_THROW(v) {dbg_break();_longjmp(unwindAddr, (v));}
#define UZLIB_SETJMP _setjmp
#define UZLIB_SETJMP(n) _setjmp(n)
#endif

#define uz_malloc malloc
#define uz_free free

#endif
#endif /* defined(__XTENSA__) */

extern jmp_buf unwindAddr;

Expand Down
8 changes: 8 additions & 0 deletions msvc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.vs/
*/Win32/
*/x64/

*.vcxproj.user

*.lua
*.img
30 changes: 30 additions & 0 deletions msvc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
These are MSVC (Visual Studio 2017) project files for the host-side tools,
namely 'luac.cross' and 'spiffsimg'. Some may find these convenient if they
already have MSVC instead of, say, setting up a Cygwin or MingW build
system.

To build 'luac.cross', you must first edit app/include/user_config.h to make
some choices about the kind of cross-compiler you are generating.

In particular, the definition of
LUA_FLASH_STORE
should be enabled if you are creating a cross-compiler for generating images
for the Lua File Storage (LFS). The specific value of this define is not
critical for luac.cross, but it's existence is if you want to be able to
generate appropriate code for LFS.

Be aware that the codebase, as checked in, has LUA_FLASH_STORE undefined.
Since it is expected that most folks wanting a host-side luac.cross is
for LFS use, you will want to first make sure that is changed to be
defined.

Secondly, if you are wanting to generate code that is appropriate for an
integer-only build, you should ensure that
LUA_NUMBER_INTEGRAL
is defined.

After altering those settings, you can build using the hosttools.sln file in
the Visual Studio UI, or directly on the command line. x86 and x64 targets
are provisioned, though there isn't anything to be gained with the 64-bit
build.

31 changes: 31 additions & 0 deletions msvc/hosttools.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.168
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "luac-cross", "luac-cross\luac-cross.vcxproj", "{78A3411A-A18F-41A4-B4A7-D76B273F0E44}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Debug|x64.ActiveCfg = Debug|x64
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Debug|x64.Build.0 = Debug|x64
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Debug|x86.ActiveCfg = Debug|Win32
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Debug|x86.Build.0 = Debug|Win32
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Release|x64.ActiveCfg = Release|x64
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Release|x64.Build.0 = Release|x64
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Release|x86.ActiveCfg = Release|Win32
{78A3411A-A18F-41A4-B4A7-D76B273F0E44}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FC69D912-B682-4325-8FBC-1A887364B511}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 62789da

Please sign in to comment.