Thursday, July 11, 2019

Windows 10: Compile the Lua interpreter from source and install it for the local user

Objective


Compile the latest version of the Lua language interpreter from source (in this case version 5.3.5). Build a locally installed Lua interpreter for the current user only, in order to have the possibility to safely remove it when necessary.

Motivation


As I already mentioned in my previous post "Linux Mint: Compile the Lua interpreter from source and build your own Debian package to install it", I wanted to have a scripting language that smoothly can be integrated and combined with C++. I figured that Lua would be very handy for that job. This time, I didn't just want to have the language interpreter build for my main operating system, which is Linux Mint, but also for Windows 10. I wanted to have the installation build from the same sources for both operating systems. This article describes the process to get a local portable installation of the Lua interpreter on per user basis. It tries to describe how to compile your own Lua interpreter on Windows 10 using the (command line interface) CLI compiler from Microsoft e.g. Visual Studio 2019 Community Edition.

Prerequisites


  • Windows 10 (64 Bit)
  • Microsoft Visual Studio 2019

Solution


I decided to do the whole build and temporary stuff within a "tmp" folder in my home account.
The boostrap toolchain will reside in the sub-directory "lua" within that "tmp" folder.

Get the latest source of the Lua interpreter from their homepage


Open a cmd shell, by right-clicking the Windows icon in the lower-left corner of your screen. Select "Run" from the context-menu and type "cmd".
You should get a command shell, where the current directory is already your home-directory, but to be sure, navigate to that location and create the sub-directories "tmp\lua" by typing:

$> cd %HOMEDRIVE%\%HOMEPATH%
$> mkdir tmp\lua
$> cd tmp\lua

To see, where "%HOMEDRIVE%\%HOMEPATH%" is pointing to, type in your cmd-shell:

$> echo %HOMEDRIVE%\%HOMEPATH%

C:\Users\cschmidt>

Now, open your preferred browser and navigate to the Lua FTP download page. Download the latest version of thte source code, and save it in your home-directory under the newly created folder "%HOMEDRIVE%\%HOMEPATH%\tmp\lua". Im my case the current source package of Lua was "lua-5.3.5.tar.gz".

Now you have to unpack the newly downloaded package "lua-5.3.5.tar.gz" into the "tmp\lua" folder. I am not sure if the standard Window 10 unzip mechanism is able to unpack archives packed by "*.tar.gz" compression, which usually is used on *nix-like systems e.g. Linux. I recommend to install an archive manager like 7-Zip anyway.

Once you have successfully unpacked the "lua-5.3.5.tar.gz" source code package, you should get an additional directory named "lua-5.3.5" within your "tmp\lua" folder. If you just have an intermediate file called "lua-5.3.5.tar", you have to unpack that file again.

$> dir

Directory of C:\Users\cschmidt\tmp\lua

11.07.2019 14:19 <DIR> .
11.07.2019 14:19 <DIR> ..
26.06.2018 18:21 <DIR> lua-5.3.5
21.06.2019 15:11 303.543 lua-5.3.5.tar.gz
1 File(s) 303.543 bytes
3 Dir(s) 160.112.115.712 bytes free

Everything from the source code package will be unpacked into the sub-directory "lua-5.3.5"
To see, what we got, type:

$> cd lua-5.3.5
$> dir /s

Volume in drive C has no label.
Volume Serial Number is 62D3-5614

Directory of C:\Users\cschmidt\tmp\lua\lua-5.3.5

[...]

Directory of C:\Users\cschmidt\tmp\lua\lua-5.3.5\doc

[...]

Directory of C:\Users\cschmidt\tmp\lua\lua-5.3.5\src

11.07.2019 14:53 <DIR> .
11.07.2019 14:53 <DIR> ..
06.12.2017 20:35 31.352 lapi.c
19.04.2017 19:20 545 lapi.h
19.04.2017 19:20 30.495 lauxlib.c
19.04.2017 19:20 8.632 lauxlib.h
[...]
19.04.2017 19:20 1.305 lualib.h
19.04.2017 19:20 6.179 lundump.c
19.04.2017 19:20 803 lundump.h
19.04.2017 19:29 7.075 lutf8lib.c
19.04.2017 19:39 44.393 lvm.c
19.04.2017 19:20 3.685 lvm.h
19.04.2017 19:20 1.365 lzio.c
19.04.2017 19:20 1.481 lzio.h
25.06.2018 19:46 6.911 Makefile
63 File(s) 694.458 bytes

Total Files Listed:
75 File(s) 1.088.257 bytes
8 Dir(s) 160.102.793.216 bytes free

Compile the Lua interpreter from the sources


In order to compile the Lua interpreters source code, we need a compiler. I assume that you have already a "Visual Studio 2019" (Community Edition will be sufficient) or similar installed.

We now need a new command shell with the compiler tool chain set up. The easiest way, to get this "special" shell, is to start up Visual Studio and launch the command shell from the menu:
"Tools --> Visual Studio Command Prompt"

In that shell, navigate again to our source code folder:

$> cd %HOMEDRIVE%\%HOMEPATH%\tmp\lua\lua-5.3.5\src

To compile the source, type:

$> cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
$> ren lua.obj lua.o
$> ren luac.obj luac.o
$> link /DLL /IMPLIB:lua5.3.5.lib /OUT:lua5.3.5.dll *.obj
$> link /OUT:lua.exe lua.o lua5.3.5.lib
$> lib /OUT:lua5.3.5-static.lib *.obj
$> link /OUT:luac.exe luac.o lua5.3.5-static.lib

Alternatively, you can put all the instructions in a file named "buildLua5.3.5.bat" and just execute that file:

cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
ren lua.obj lua.o
ren luac.obj luac.o
link /DLL /IMPLIB:lua5.3.5.lib /OUT:lua5.3.5.dll *.obj
link /OUT:lua.exe lua.o lua5.3.5.lib
lib /OUT:lua5.3.5-static.lib *.obj
link /OUT:luac.exe luac.o lua5.3.5-static.lib

$> buildLua5.3.5.bat

C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.21.27702.2 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

lapi.c
lauxlib.c
lbaselib.c
lbitlib.c
[...]
lua.c
luac.c
lundump.c
lutf8lib.c
lvm.c
lzio.c
Generating Code...

C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>ren lua.obj lua.o

C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>ren luac.obj luac.o

C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>link /DLL /IMPLIB:lua5.3.5.lib /OUT:lua5.3.5.dll *.obj
Microsoft (R) Incremental Linker Version 14.21.27702.2
Copyright (C) Microsoft Corporation. All rights reserved.

Creating library lua5.3.5.lib and object lua5.3.5.exp

C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>link /OUT:lua.exe lua.o lua5.3.5.lib
Microsoft (R) Incremental Linker Version 14.21.27702.2
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>lib /OUT:lua5.3.5-static.lib *.obj
Microsoft (R) Library Manager Version 14.21.27702.2
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Users\cschmidt\tmp\lua\lua-5.3.5\src>link /OUT:luac.exe luac.o lua5.3.5-static.lib
Microsoft (R) Incremental Linker Version 14.21.27702.2
Copyright (C) Microsoft Corporation. All rights reserved.

Creating library luac.lib and object luac.exp

Fine, everything is build locally into the current folder "src". If we want to use the result of this compilation, we can just move everything we need to a folder called "lua" in our home-directory:

$> mkdir %HOMEDRIVE%\%HOMEPATH%\lua
$> copy *.dll %HOMEDRIVE%\%HOMEPATH%\lua\
$> copy *.h %HOMEDRIVE%\%HOMEPATH%\lua\
$> copy *.hpp %HOMEDRIVE%\%HOMEPATH%\lua\
$> copy *.exp %HOMEDRIVE%\%HOMEPATH%\lua\
$> copy *.lib %HOMEDRIVE%\%HOMEPATH%\lua\
$> copy *.exe %HOMEDRIVE%\%HOMEPATH%\lua\

Finally we need to expand the "PATH" environment variable (in the system-settings) by that location to the Lua interpreter binary.


After doing so, we open a new command shell, as described at the beginning of this article, in order to test if everything is working:

$> lua

> Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio

To exit the interpreter, just type "CTRL-C".

Fine! Now, we can safely delete the folder "tmp\lua".

References:

  1. Lua homepage
  2. How to compile Lua 5.3.0 for Windows