lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



We have a bunch of lua files in a directory
structure with 'requires' to load code as
necessary, using the lua_path environment
variable. Now we want to precompile the code
instead of keeping it all as source. Is there
a good way to define a "parallel" directory
structure for the compiled code that could
be generated automatically from the source?

Assuming that you don't want to do what LHF suggested (i.e. to build a large binary with all the code bundled inside it, which is a lot less flexible), you could just redefine your package.path from, say:

'/opt/me/?.lua;/opt/me/?/init.lua'

(adjust to Windows flavour if desired) to:

'/opt/me/?.lua;/opt/me/?.luac;/opt/me/?/init.lua;/opt/me/?/init.luac'

When you're ready to make a release build, just compile all the .lua files to .luac recursively in the /opt/me tree, and package only the .luac versions into the release distro.

When you unpack the distro file on the target computer, there won't be any .lua files, so the .luac files will be found and used instead. Whenever you want to debug one of the files from source, just drop the .lua file in the same directory as its corresponding .luac and it will be loaded in preference.

Then, when you want one of your users to test a new .lua file with the rest of the regular software build, they can drop it in place until the testing is done, and delete it to revert to the "official" compiled version.

Alternatively, reverse the precedence of .lua and .luac in the lua_path. Then the .luac files, if they exist, will be found first. Whenever you want to switch back to running with a .lua file, simply delete its .luac companion. When finished hacking on the .lua file, simply recompile it to .luac to start running and testing with the updated precompiled version.

When ready to ship, leave out the .lua files from the shipping tarball/installer/BLOB and your users won't get the source code (and so won't be tempted to muck around with it), but everything will work.

If you are using bash (which you can easily do natively on Windows using MinGW's MSYS) then something like:

for f in $(find /opt/me -name '*.lua'); do
luac -o ${f%.lua}.luac $f; done

should do the recursive compile in one shot. Or just use a makefile to rebuild only changed .lua source files. (The compiler is superquick so unless your proejct is huge a bulk rebuild is no dig deal.)

This way allows you to keep the .lua and .luac files in the same directory tree on your build computers. Recursively delete *.luac to go to source-only operation. The deleted files can trivially be regenerated in place.

[Apologies if this has been answered. I am currently reading the list off the lua-users website and the updates are quite laggy. Daily, I presume.]