premake/scripts/embed.lua

105 lines
2.5 KiB
Lua
Raw Normal View History

2009-07-16 15:20:15 +00:00
--
-- Embed the Lua scripts into src/host/scripts.c as static data buffers.
-- I embed the actual scripts, rather than Lua bytecodes, because the
-- bytecodes are not portable to different architectures, which causes
2009-07-16 15:20:15 +00:00
-- issues in Mac OS X Universal builds.
--
-- remember where I live, so I can find the files I need
local basedir = path.getdirectory(os.getcwd())
local function stripfile(fname)
2009-07-16 15:20:15 +00:00
local f = io.open(fname)
local s = assert(f:read("*a"))
2009-07-16 15:20:15 +00:00
f:close()
-- strip tabs
s = s:gsub("[\t]", "")
2009-07-16 15:20:15 +00:00
-- strip any CRs
s = s:gsub("[\r]", "")
-- strip out block comments
s = s:gsub("%-%-%[%[.-%-%-%]%]", "")
-- strip out inline comments
s = s:gsub("\n%-%-[^\n]*", "")
2009-07-16 15:20:15 +00:00
-- escape backslashes
s = s:gsub("\\", "\\\\")
-- strip duplicate line feeds
s = s:gsub("\n+", "\n")
-- strip out leading comments
s = s:gsub("^%-%-\n", "")
-- escape line feeds
s = s:gsub("\n", "\\n")
2009-07-16 15:20:15 +00:00
-- escape double quote marks
s = s:gsub("\"", "\\\"")
return s
end
local function writeline(out, s, continues)
2009-07-16 15:20:15 +00:00
out:write("\t\"")
out:write(s)
out:write(iif(continues, "\"\n", "\",\n"))
2009-07-16 15:20:15 +00:00
end
local function writefile(out, fname, contents)
local max = 1024
out:write("\t/* " .. fname .. " */\n")
-- break up large strings to fit in Visual Studio's string length limit
local start = 1
local len = contents:len()
while start <= len do
local n = len - start
if n > max then n = max end
local finish = start + n
-- make sure I don't cut an escape sequence
while contents:sub(finish, finish) == "\\" do
finish = finish - 1
end
writeline(out, contents:sub(start, finish), finish < len)
start = finish + 1
end
out:write("\n")
end
2009-07-16 15:20:15 +00:00
function doembed()
local srcdir = path.join(basedir, "src")
-- load the manifest of script files
scripts = dofile(path.join(srcdir, "_manifest.lua"))
-- main script always goes first
table.insert(scripts, 1, "_premake_main.lua")
-- open scripts.c and write the file header
local out = io.open(path.join(srcdir, "host/scripts.c"), "w+b")
out:write("/* Premake's Lua scripts, as static data buffers for release mode builds */\n")
out:write("/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */\n")
out:write("/* To regenerate this file, run: premake5 embed */ \n\n")
out:write("const char* builtin_scripts[] = {\n")
for i,fn in ipairs(scripts) do
print(fn)
local s = stripfile(path.join(srcdir, fn))
writefile(out, fn, s)
end
out:write("\t0\n};\n");
out:close()
2009-07-16 15:20:15 +00:00
end