lua-users home
lua-l archive

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


This is a modifed version of the excellent luac.lua from Luiz Henrique de Figueiredo, http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/#srlua. This version don't support lua 5.1, but it support lua 5.2 and lua 5.3 and also future versions of lua. Usage: lua luac-5.2-5.3.lua [-O output_file.luac] [file.lua]* [-L [module]*] [-S] You can specify the output file name with "-O". You can stripe debug information with "-S". And now for the module name, it is going to search in the same places as require does. The only thing is that it doesn't support is adding already precompiled lua files...
-- usage: lua luac.lua [-O output_file.luac] [file.lua]* [-L [module]*] [-S]
--
-- creates a precompiled chunk that preloads all modules listed after -L
-- and then runs all programs listed before -L.
--
-- Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
-- TheNain38 <the_nain_38@hotmail.fr>
-- Tue Aug  5 22:57:33 BRT 2008
-- This code is hereby placed in the public domain.

local STRIP = false
local USE_PACKAGES = false
local OUTPUT_NAME_OPTION = false

local NAME = "luac"

local OUTPUT = NAME..".out"
NAME = "=("..NAME..")"

local files = {}
local packages = {}
local b = {}
local f

for i = 1, #arg do
	if OUTPUT_NAME_OPTION then
		OUTPUT_NAME_OPTION = false
		OUTPUT = arg[i]
	elseif arg[i] == "-L" then
		USE_PACKAGES = true
	elseif arg[i] == "-S" then
		STRIP = true
	elseif arg[i] == "-O" then
		OUTPUT_NAME_OPTION = true
	elseif not USE_PACKAGES then
		files[#files+1] = arg[i]
	else
		packages[#packages+1] = arg[i]
	end
end

if USE_PACKAGES then
	b[#b+1] = "local t=package.preload;"
	for i = 1, #packages do
		b[#b+1] = "t['"..packages[i].."']=function()"
		for line in io.lines(assert(package.searchpath(packages[i], package.path))) do b[#b+1] = line end
		b[#b+1] = "end;"
	end
end

for i = 1, #files do
	b[#b+1] = "(function()"
	for line in io.lines(files[i]) do b[#b+1] = line end
	b[#b+1] = "end)();"
end

b = table.concat(b, "\n")
-- print(b)

b = string.dump(assert(load(b, NAME)), STRIP)

f = assert(io.open(OUTPUT, "wb"))
assert(f:write(b))
assert(f:close())