Skip to content

Commit

Permalink
added MSVC project configuration for host-side tools. modified code t…
Browse files Browse the repository at this point in the history
…o also build with MSVC. at present, only luac.cross. later, spiffsimg.
  • Loading branch information
XxX123xXxm committed Feb 12, 2019
1 parent 30744af commit cc6f163
Show file tree
Hide file tree
Showing 14 changed files with 625 additions and 2 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
3 changes: 3 additions & 0 deletions app/lua/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include "lua.h"
#include C_HEADER_CTYPE
#ifdef _MSC_VER //msvc #defines errno, which interferes with our #include macro
#undef errno
#endif
#include C_HEADER_ERRNO
#include C_HEADER_STDIO
#include C_HEADER_STDLIB
Expand Down
15 changes: 15 additions & 0 deletions app/lua/linit.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ 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: 10 additions & 0 deletions app/lua/lrotable.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ int luaR_isrotable(void *p);
extern const char _RODATA_END[];
#define IN_RODATA_AREA(p) (((const char *)(p)) < _RODATA_END)

#if defined(_MSC_VER)
//msvc build uses dummy vars to locate the beginning and ending addresses of the RO data
extern const int _ro_start;
extern const int _ro_end;
//and this is how we see if something is in the RO section
#undef IN_RODATA_AREA
#define IN_RODATA_AREA(p) (((const int*)(p)) >= &_ro_start && ((const int *)(p)) <= &_ro_end)
#endif


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

extern const char _irom0_text_start[];
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
3 changes: 3 additions & 0 deletions app/lua/luac_cross/luac.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ struct Smain {
char** argv;
};

#ifdef _MSC_VER
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
11 changes: 9 additions & 2 deletions app/lua/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@


#if defined(LUA_CROSS_COMPILER)
#ifdef _MSC_VER
#else
#define LUA_USE_LINUX
#endif
#endif

#if defined(LUA_USE_LINUX)
#define LUA_USE_POSIX
Expand Down Expand Up @@ -264,7 +267,7 @@
#include <io.h>
#ifdef LUA_CROSS_COMPILER
#include <stdio.h>
else
#else
#include "c_stdio.h"
#endif

Expand Down Expand Up @@ -631,7 +634,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 @@ -910,4 +917,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
4 changes: 4 additions & 0 deletions app/uzlib/uzlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
#include <stdint.h>
#include <stdlib.h>
extern int dbg_break(void);
#ifdef _MSC_VER //msvc requires old name for longjmp
#define UZLIB_THROW(v) {dbg_break();longjmp(unwindAddr, (v));}
#else
#define UZLIB_THROW(v) {dbg_break();_longjmp(unwindAddr, (v));}
#endif
#define UZLIB_SETJMP _setjmp
#define uz_malloc malloc
#define uz_free free
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 cc6f163

Please sign in to comment.