CDSoft / luax

luax is a Lua interpreter and REPL based on Lua 5.4, augmented with some useful packages. It is also a "compiler" that produces standalone executables from Lua scripts.
https://cdelord.fr/luax
GNU General Public License v3.0
167 stars 8 forks source link

compile with "-t lua" failed #5

Closed moky80 closed 5 hours ago

moky80 commented 2 months ago

I want to compile my test.lua into test.exe using target "lua". So I installed lua-5.4.2 and luax-7.1.5 in my Windows 10.

Both paths to luax and lua were added to the system PATH environment variable and I could verify that they started successfully in cmd. But listing targets and compiling for target "lua" did not work. Please see the log below:

Listing targets:

>luax compile -t list
Target                Interpeter / LuaX archive
--------------------- -------------------------
luax                  luax [NOT FOUND]
lua                   lua [NOT FOUND]
pandoc                pandoc [NOT FOUND]

Compile for target "lua":

>luax compile -o test.exe test.lua -t lua
 _               __  __  |  https://cdelord.fr/luax
| |   _   _  __ _\ \/ /  |
| |  | | | |/ _` |\  /   |  Version 7.1.5 (2024-09-08)
| |__| |_| | (_| |/  \   |  Powered by Lua 5.4
|_____\__,_|\__,_/_/\_\  |
                         |  Windows x86_64 gnu

scripts: test.lua

target : lua
output : test.exe
$luax:luax/luax_bundle.lua:240: attempt to get length of a nil value (local 'runtime')
stack traceback:
        $luax:luax/luax_bundle.lua:240: in function 'luax_bundle.bundle'
        $luax:luax/luax_cmd_compile.lua:158: in local 'compile_lua'
        $luax:luax/luax_cmd_compile.lua:287: in main chunk
        [C]: in ?
        [C]: in function 'require'
        $luax:luax/luax.lua:28: in function <$luax:luax/luax.lua:26>
        (...tail calls...)
CDSoft commented 2 months ago

Target list

I guess the target list on Windows can not list the interpreters because the files are searched without the .exe extension (I'll fix it) but this should not be a problem.

Installation

The error message is not explicit (I'll fix it too). In fact it does not find luax.lar. Your installation may not be complete. You need to install at least luax.exe in the bin directory and luax.lar in the lib directory. A full installation looks like:

some_path\bin\luax.exe
some_path\bin\luax.lua
some_path\bin\luax-pandoc.lua
some_path\lib\libluax.dll
some_path\lib\luax.lar
some_path\lib\luax.lua

and %PATH% shall contain some_path\bin.

Did you install luax manually?

There is usually two ways to install luax but on Windows the first one is not tested and is likely not to work (on Windows I would suggest to work with WSL ;-)

  1. installation from sources (ok on Linux and MacOS, may work on Cygwin and WSL) (see README.md)
./bootstrap.sh
ninja install

This will install everything in ~/.local/bin and ~/.local/lib

  1. installation of precompiled binaries

Just download and unzip https://cdelord.fr/pub/luax-full-windows-x86_64.zip. Then add the bin directory to PATH.

Note that the zip file contains more than just luax but you can keep luax files only.

Targets

With the lua target, luax produces a Lua script that contains a subset of the LuaX runtime implemented in pure Lua. The resulting script can be interpreted by a standard Lua interpreter.

If you want to make a native windows executable, you need the native or windows-x86_64 target. luax will download zig to compile the scripts and link them with the luax precompiled runtime (found in lib/luax.lar). curl, tar and xz must be installed (which may not be very windows-friendly).

Oops, while testing it I see that zig is distributed as a zip archive. This won't work with the current luax implementation.

When this is fixed, it should be (:crossed_fingers:) possible to compile lua scripts like this:

luax compile -o test.exe test.lua -t native

Anyway on windows it should be easier to use luax to interpret luax script (possibly bundled for the target luax)

CDSoft commented 2 months ago

The version 7.1.6 should work better on Windows. To compile scripts on Windows (native or cross-compilation) it needs curl and tar, which should be installed on recent windows versions.

The first compilation may take time since zig has to compile the C library. Subsequent compilations will be faster.

moky80 commented 1 month ago

Thank you very much for your quick fix.

Using version 7.1.6, listing targets works for me:

>luax compile -t list
Target                Interpeter / LuaX archive
--------------------- -------------------------
luax                  D:\dev\rblx\lua\luax-windows-x86_64\bin\luax.exe
lua                   D:\dev\rblx\lua\lua-5.4.2_Win64_bin\lua.exe
pandoc                pandoc [NOT FOUND]

But compiling for native still does not work:

>luax compile test\test.lua -o test\test.exe -t native
 _               __  __  |  https://cdelord.fr/luax
| |   _   _  __ _\ \/ /  |
| |  | | | |/ _` |\  /   |  Version 7.1.6 (2024-09-18)
| |__| |_| | (_| |/  \   |  Powered by Lua 5.4
|_____\__,_|\__,_/_/\_\  |
                         |  Windows x86_64 gnu

scripts: test\test.lua

target : windows-x86_64
output : test\test.exe
Zig    : download and install Zig to C:\Users\tinyitchy\AppData\Local\zig\0.13.0
######################################################################################################################################################################################### 100.0% 

error: Compilation not available

Should I now see the target "native" when listing? I still see the same listing after zig was successfully installed.

CDSoft commented 1 month ago

The target list should contain these targets to be able to compile scripts:

Target                Interpeter / LuaX archive
--------------------- -------------------------
luax                  ~/.local/bin/luax
lua                   ~/.local/bin/lua
pandoc                ~/.local/bin/pandoc
native                ~/.local/lib/luax.lar
linux-x86_64          ~/.local/lib/luax.lar
linux-x86_64-musl     ~/.local/lib/luax.lar
linux-aarch64         ~/.local/lib/luax.lar
linux-aarch64-musl    ~/.local/lib/luax.lar
macos-x86_64          ~/.local/lib/luax.lar
macos-aarch64         ~/.local/lib/luax.lar
windows-x86_64        ~/.local/lib/luax.lar

If luax.lar is not found, you won't be able to compile scripts. In your case it should be installed in D:\dev\rblx\lua\luax-windows-x86_64\lib\luax.lar

moky80 commented 1 month ago

I did every steps mentioned in https://github.com/CDSoft/luax/issues/5#issuecomment-2359010265 and could verify:

But luax.lar is still not found in target listing. What else could I miss?

What I've noticed: In the output of my target listing, the target "native" or "windows-x86_64" are not NOT FOUND, they don't even exist:

D:\dev\rblx>luax compile -t list
Target                Interpeter / LuaX archive
--------------------- -------------------------
luax                  D:\dev\rblx\lua\luax-windows-x86_64\bin\luax.exe
lua                   D:\dev\rblx\lua\lua-5.4.2_Win64_bin\lua.exe
pandoc                pandoc [NOT FOUND]
CDSoft commented 1 month ago

Ok, I think it is still a difference between Linux and Windows. Luax search for luax in PATH but may not find it on windows since the program name is luax.exe.

Could you please try to add the .exe extension to confirm this is actually the problem:

luax.exe compile test\test.lua -o test\test.exe -t native

If this works, I will change the way to search for luax on windows.

CDSoft commented 1 month ago

I pushed a patch to find luax.lar on Windows. It should work better now.

moky80 commented 1 month ago

Thank you very much for your hint and patch.

With version 7.1.6, calling luax.exe (instead of luax) works.

With version 7.1.7, both luax and luax.exe invocations work.