lua-users home
lua-l archive

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


2014-09-18 14:07 GMT+01:00 Tony Papadimitriou <tonyp@acm.org>:
> How can one #include a bunch of files so that when compiled, a single
> self-contained executable is produced.

You can concatenate the files in a certain way with a bit of
boilerplate between each. I'm sure others will chime in to offer
suggestions of tools doing that (I can't remember any).

> 'require' does not qualify as equivalent because it needs to find the files
> at run-time.

require can look for modules in plenty of places, not only in separate
files. If your modules and main script already use require, you can
concatenate them as suggested above without modification. For example:

local main = "main.lua"
local modules = {"foo", "foo.fff", "bar"}
local output = "all.lua"
----------
local file = io.open(output, 'wb')
for _,modname in ipairs(modules) do
    local modfile = modname:gsub('%.', '/')..".lua"
    file:write("package.preload['"..modname.."'] = function(...)\n")
    file:write(io.open(modfile, 'rb'):read('*all'))
    file:write("end\n")
end
file:write(io.open(main, 'rb'):read('*all'))
file:close()