2009-07-16 15:20:15 +00:00
|
|
|
--
|
|
|
|
-- Embed the Lua scripts into src/host/scripts.c as static data buffers.
|
2013-09-10 20:24:39 +00:00
|
|
|
-- 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.
|
|
|
|
--
|
|
|
|
|
2013-11-07 14:39:46 +00:00
|
|
|
-- remember where I live, so I can find the files I need
|
|
|
|
local basedir = path.getdirectory(os.getcwd())
|
|
|
|
|
2010-01-07 15:36:03 +00:00
|
|
|
local function stripfile(fname)
|
2009-07-16 15:20:15 +00:00
|
|
|
local f = io.open(fname)
|
2011-08-24 20:07:53 +00:00
|
|
|
local s = assert(f:read("*a"))
|
2009-07-16 15:20:15 +00:00
|
|
|
f:close()
|
|
|
|
|
|
|
|
-- strip tabs
|
|
|
|
s = s:gsub("[\t]", "")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2009-07-16 15:20:15 +00:00
|
|
|
-- strip any CRs
|
|
|
|
s = s:gsub("[\r]", "")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2012-02-13 20:21:28 +00:00
|
|
|
-- strip out block comments
|
2013-12-01 20:30:28 +00:00
|
|
|
s = s:gsub("[^\"']%-%-%[%[.-%]%]", "")
|
|
|
|
s = s:gsub("[^\"']%-%-%[=%[.-%]=%]", "")
|
|
|
|
s = s:gsub("[^\"']%-%-%[==%[.-%]==%]", "")
|
2012-02-13 20:21:28 +00:00
|
|
|
|
|
|
|
-- strip out inline comments
|
2013-12-01 20:30:28 +00:00
|
|
|
s = s:gsub("\n%-%-[^\n]*", "\n")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
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
|
2013-12-01 20:30:28 +00:00
|
|
|
s = s:gsub("^%-%-[^\n]*\n", "")
|
2009-07-16 15:20:15 +00:00
|
|
|
|
|
|
|
-- escape line feeds
|
|
|
|
s = s:gsub("\n", "\\n")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2009-07-16 15:20:15 +00:00
|
|
|
-- escape double quote marks
|
|
|
|
s = s:gsub("\"", "\\\"")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2010-01-07 15:36:03 +00:00
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-01-08 17:42:16 +00:00
|
|
|
local function writeline(out, s, continues)
|
2009-07-16 15:20:15 +00:00
|
|
|
out:write("\t\"")
|
|
|
|
out:write(s)
|
2010-01-08 17:42:16 +00:00
|
|
|
out:write(iif(continues, "\"\n", "\",\n"))
|
2009-07-16 15:20:15 +00:00
|
|
|
end
|
2013-09-10 20:24:39 +00:00
|
|
|
|
|
|
|
|
2010-01-07 15:36:03 +00:00
|
|
|
local function writefile(out, fname, contents)
|
2010-01-08 17:42:16 +00:00
|
|
|
local max = 1024
|
2010-01-07 15:36:03 +00:00
|
|
|
|
2010-01-08 17:42:16 +00:00
|
|
|
out:write("\t/* " .. fname .. " */\n")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
|
|
|
-- break up large strings to fit in Visual Studio's string length limit
|
2010-01-07 15:36:03 +00:00
|
|
|
local start = 1
|
|
|
|
local len = contents:len()
|
|
|
|
while start <= len do
|
|
|
|
local n = len - start
|
2010-01-07 16:24:17 +00:00
|
|
|
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
|
2013-09-10 20:24:39 +00:00
|
|
|
end
|
2010-01-07 16:24:17 +00:00
|
|
|
|
2010-01-08 17:42:16 +00:00
|
|
|
writeline(out, contents:sub(start, finish), finish < len)
|
2010-01-07 16:24:17 +00:00
|
|
|
start = finish + 1
|
2013-09-10 20:24:39 +00:00
|
|
|
end
|
2010-01-07 15:36:03 +00:00
|
|
|
|
2010-01-08 17:42:16 +00:00
|
|
|
out:write("\n")
|
2010-01-07 15:36:03 +00:00
|
|
|
end
|
2009-07-16 15:20:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
function doembed()
|
2013-11-07 14:39:46 +00:00
|
|
|
|
2013-12-01 20:24:25 +00:00
|
|
|
-- Find and run the manifest file. Checks the normal search paths to
|
|
|
|
-- allow for manifest and _premake_main customizations, then falls
|
|
|
|
-- back to the canonical version at ../src if not found.
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2013-12-01 20:24:25 +00:00
|
|
|
local dir = os.pathsearch("_manifest.lua", _OPTIONS["scripts"], os.getenv("PREMAKE_PATH"))
|
|
|
|
if not dir then
|
|
|
|
dir = path.join(basedir, "src")
|
|
|
|
end
|
|
|
|
|
|
|
|
scripts = dofile(path.join(dir, "_manifest.lua"))
|
|
|
|
|
|
|
|
-- Main script always goes first
|
2012-10-19 22:53:03 +00:00
|
|
|
table.insert(scripts, 1, "_premake_main.lua")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2013-12-01 20:24:25 +00:00
|
|
|
-- Embed all the scripts to a temporary file first
|
2013-12-01 21:03:08 +00:00
|
|
|
local file = io.tmpfile()
|
|
|
|
file:write("/* Premake's Lua scripts, as static data buffers for release mode builds */\n")
|
|
|
|
file:write("/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */\n")
|
|
|
|
file:write("/* To regenerate this file, run: premake5 embed */ \n\n")
|
|
|
|
file:write("const char* builtin_scripts[] = {\n")
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2013-12-01 21:03:08 +00:00
|
|
|
for i, fn in ipairs(scripts) do
|
2013-12-01 20:24:25 +00:00
|
|
|
local s = stripfile(path.join(dir, fn))
|
2013-12-01 21:03:08 +00:00
|
|
|
writefile(file, fn, s)
|
2010-01-07 15:36:03 +00:00
|
|
|
end
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2013-12-01 21:03:08 +00:00
|
|
|
file:write("\t0\n};\n");
|
2013-12-01 20:24:25 +00:00
|
|
|
|
|
|
|
-- Now read it back in and compare it to the current scripts.c; only
|
|
|
|
-- write it out if changed.
|
|
|
|
|
2013-12-01 21:03:08 +00:00
|
|
|
file:seek("set", 0)
|
|
|
|
local newVersion = file:read("*a")
|
|
|
|
file:close()
|
2013-12-01 20:24:25 +00:00
|
|
|
|
|
|
|
local oldVersion
|
2013-12-01 21:03:08 +00:00
|
|
|
local scriptsFile = path.join(basedir, "src/host/scripts.c")
|
|
|
|
|
|
|
|
local file = io.open(scriptsFile, "r")
|
|
|
|
if file then
|
|
|
|
oldVersion = file:read("*a")
|
|
|
|
file:close()
|
2013-12-01 20:24:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if newVersion ~= oldVersion then
|
|
|
|
print("Writing scripts.c")
|
2013-12-01 21:03:08 +00:00
|
|
|
file = io.open(scriptsFile, "w+b")
|
|
|
|
file:write(newVersion)
|
|
|
|
file:close()
|
2013-12-01 20:24:25 +00:00
|
|
|
end
|
|
|
|
|
2009-07-16 15:20:15 +00:00
|
|
|
end
|