2009-07-16 15:20:15 +00:00
|
|
|
--
|
|
|
|
-- Embed the Lua scripts into src/host/scripts.c as static data buffers.
|
2016-02-15 00:05:40 +00:00
|
|
|
-- Embeds minified versions of the actual scripts by default, rather than
|
|
|
|
-- bytecode, as bytecodes are not portable to different architectures. Use
|
|
|
|
-- the `--bytecode` flag to override.
|
2009-07-16 15:20:15 +00:00
|
|
|
--
|
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local scriptCount = 0
|
|
|
|
|
2014-09-26 13:25:14 +00:00
|
|
|
local function loadScript(fname)
|
2015-03-09 21:07:12 +00:00
|
|
|
fname = path.getabsolute(fname)
|
2015-10-05 19:41:11 +00:00
|
|
|
local f = io.open(fname, "rb")
|
|
|
|
local s = assert(f:read("*all"))
|
2015-04-01 17:42:15 +00:00
|
|
|
f:close()
|
2015-10-05 19:41:11 +00:00
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
2015-04-01 17:42:15 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local function stripScript(s)
|
2015-04-01 17:42:15 +00:00
|
|
|
-- strip tabs
|
2015-10-05 19:41:11 +00:00
|
|
|
local result = s:gsub("[\t]", "")
|
2015-03-29 19:35:43 +00:00
|
|
|
|
|
|
|
-- strip any CRs
|
2015-10-05 19:41:11 +00:00
|
|
|
result = result:gsub("[\r]", "")
|
2015-03-29 19:35:43 +00:00
|
|
|
|
2015-04-01 17:42:15 +00:00
|
|
|
-- strip out block comments
|
2015-10-05 19:41:11 +00:00
|
|
|
result = result:gsub("[^\"']%-%-%[%[.-%]%]", "")
|
|
|
|
result = result:gsub("[^\"']%-%-%[=%[.-%]=%]", "")
|
2017-05-17 21:21:15 +00:00
|
|
|
result = result:gsub("[^\"']%-%-%[==%[.-%]==%]", "")
|
2015-04-01 17:42:15 +00:00
|
|
|
|
|
|
|
-- strip out inline comments
|
2015-10-05 19:41:11 +00:00
|
|
|
result = result:gsub("\n%-%-[^\n]*", "\n")
|
2015-03-29 19:35:43 +00:00
|
|
|
|
2015-04-01 17:42:15 +00:00
|
|
|
-- strip duplicate line feeds
|
2015-10-05 19:41:11 +00:00
|
|
|
result = result:gsub("\n+", "\n")
|
2015-04-01 17:42:15 +00:00
|
|
|
|
|
|
|
-- strip out leading comments
|
2015-10-05 19:41:11 +00:00
|
|
|
result = result:gsub("^%-%-[^\n]*\n", "")
|
2015-04-01 17:42:15 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
return result
|
|
|
|
end
|
2015-03-29 19:35:43 +00:00
|
|
|
|
2015-03-29 19:43:40 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local function outputScript(result, script)
|
|
|
|
local data = script.data
|
|
|
|
local length = #data
|
2010-01-07 15:36:03 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
if length > 0 then
|
|
|
|
script.table = string.format("builtin_script_%d", scriptCount)
|
|
|
|
scriptCount = scriptCount + 1
|
2010-01-07 15:36:03 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
buffered.writeln(result, "// ".. script.name)
|
|
|
|
buffered.writeln(result, "static const unsigned char " .. script.table .. "[] = {")
|
|
|
|
|
|
|
|
for i = 1, length do
|
|
|
|
buffered.write(result, string.format("%3d, ", data:byte(i)))
|
|
|
|
if (i % 32 == 0) then
|
|
|
|
buffered.writeln(result)
|
2015-03-25 20:10:27 +00:00
|
|
|
end
|
2015-10-05 19:41:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
buffered.writeln(result, "};")
|
|
|
|
buffered.writeln(result)
|
|
|
|
end
|
|
|
|
end
|
2010-01-07 16:24:17 +00:00
|
|
|
|
2014-05-29 22:04:58 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local function addScript(result, filename, name, data)
|
|
|
|
if not data then
|
2017-08-02 21:03:58 +00:00
|
|
|
if not path.hasextension(filename, ".lua") then
|
|
|
|
data = loadScript(filename)
|
2017-08-07 15:52:18 +00:00
|
|
|
elseif _OPTIONS["bytecode"] then
|
|
|
|
verbosef("Compiling... " .. filename)
|
|
|
|
local output = path.replaceextension(filename, ".luac")
|
|
|
|
local res, err = os.compile(filename, output);
|
|
|
|
if res ~= nil then
|
|
|
|
data = loadScript(output)
|
|
|
|
os.remove(output)
|
2015-10-05 19:41:11 +00:00
|
|
|
else
|
2017-08-07 15:52:18 +00:00
|
|
|
print(err)
|
|
|
|
print("Embedding source instead.")
|
2015-10-05 19:41:11 +00:00
|
|
|
data = stripScript(loadScript(filename))
|
|
|
|
end
|
2017-08-07 15:52:18 +00:00
|
|
|
else
|
|
|
|
data = stripScript(loadScript(filename))
|
2015-03-25 20:10:27 +00:00
|
|
|
end
|
2013-09-10 20:24:39 +00:00
|
|
|
end
|
2010-01-07 15:36:03 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local script = {}
|
|
|
|
script.filename = filename
|
|
|
|
script.name = name
|
|
|
|
script.data = data
|
|
|
|
table.insert(result, script)
|
2010-01-07 15:36:03 +00:00
|
|
|
end
|
2009-07-16 15:20:15 +00:00
|
|
|
|
|
|
|
|
2014-09-26 13:25:14 +00:00
|
|
|
-- Prepare the file header
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local result = buffered.new()
|
|
|
|
buffered.writeln(result, "/* Premake's Lua scripts, as static data buffers for release mode builds */")
|
|
|
|
buffered.writeln(result, "/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */")
|
|
|
|
buffered.writeln(result, "/* To regenerate this file, run: premake5 embed */")
|
|
|
|
buffered.writeln(result, "")
|
2015-12-17 20:56:56 +00:00
|
|
|
buffered.writeln(result, '#include "host/premake.h"')
|
2015-10-05 19:41:11 +00:00
|
|
|
buffered.writeln(result, "")
|
2013-12-01 20:24:25 +00:00
|
|
|
|
2014-09-26 13:25:14 +00:00
|
|
|
-- Find all of the _manifest.lua files within the project
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2014-09-26 13:25:14 +00:00
|
|
|
local mask = path.join(_MAIN_SCRIPT_DIR, "**/_manifest.lua")
|
|
|
|
local manifests = os.matchfiles(mask)
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2015-08-24 16:08:43 +00:00
|
|
|
-- Find all of the _user_modules.lua files within the project
|
|
|
|
|
|
|
|
local userModuleFiles = {}
|
|
|
|
userModuleFiles = table.join(userModuleFiles, os.matchfiles(path.join(_MAIN_SCRIPT_DIR, "**/_user_modules.lua")))
|
|
|
|
userModuleFiles = table.join(userModuleFiles, os.matchfiles(path.join(_MAIN_SCRIPT_DIR, "_user_modules.lua")))
|
2013-09-10 20:24:39 +00:00
|
|
|
|
2013-12-01 20:24:25 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
-- Generate table of embedded content.
|
|
|
|
local contentTable = {}
|
2017-06-21 00:21:13 +00:00
|
|
|
local nativeTable = {}
|
2013-12-01 20:24:25 +00:00
|
|
|
|
2015-10-05 22:13:50 +00:00
|
|
|
print("Compiling... ")
|
2014-09-26 13:25:14 +00:00
|
|
|
for mi = 1, #manifests do
|
|
|
|
local manifestName = manifests[mi]
|
2015-10-05 19:41:11 +00:00
|
|
|
local manifestDir = path.getdirectory(manifestName)
|
2017-06-21 00:21:13 +00:00
|
|
|
local moduleName = path.getbasename(manifestDir)
|
2015-10-05 19:41:11 +00:00
|
|
|
local baseDir = path.getdirectory(manifestDir)
|
2014-09-26 13:25:14 +00:00
|
|
|
|
|
|
|
local files = dofile(manifests[mi])
|
|
|
|
for fi = 1, #files do
|
|
|
|
local filename = path.join(manifestDir, files[fi])
|
2015-10-05 19:41:11 +00:00
|
|
|
addScript(contentTable, filename, path.getrelative(baseDir, filename))
|
2013-12-01 20:24:25 +00:00
|
|
|
end
|
2017-06-21 00:21:13 +00:00
|
|
|
|
|
|
|
-- find native code in modules.
|
|
|
|
if moduleName ~= "src" then
|
|
|
|
local nativeFile = path.join(manifestDir, 'native', moduleName .. '.c')
|
|
|
|
if os.isfile(nativeFile) then
|
|
|
|
local pretty_name = moduleName:gsub("^%l", string.upper)
|
|
|
|
table.insert(nativeTable, pretty_name)
|
|
|
|
end
|
|
|
|
end
|
2014-09-26 13:25:14 +00:00
|
|
|
end
|
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
addScript(contentTable, path.join(_SCRIPT_DIR, "../src/_premake_main.lua"), "src/_premake_main.lua")
|
|
|
|
addScript(contentTable, path.join(_SCRIPT_DIR, "../src/_manifest.lua"), "src/_manifest.lua")
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
-- Add the list of modules
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local modules = dofile("../src/_modules.lua")
|
|
|
|
for _, userModules in ipairs(userModuleFiles) do
|
|
|
|
modules = table.join(modules, dofile(userModules))
|
|
|
|
end
|
2013-12-01 20:24:25 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
addScript(contentTable, "_modules.lua", "src/_modules.lua", "return {" .. table.implode(modules, '"', '"', ', ') .. "}")
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
-- Embed the actual script contents
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
print("Embedding...")
|
|
|
|
for mi = 1, #contentTable do
|
|
|
|
outputScript(result, contentTable[mi])
|
2009-07-16 15:20:15 +00:00
|
|
|
end
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
-- Generate an index of the script file names. Script names are stored
|
|
|
|
-- relative to the directory containing the manifest, i.e. the main
|
|
|
|
-- Xcode script, which is at $/modules/xcode/xcode.lua is stored as
|
|
|
|
-- "xcode/xcode.lua".
|
|
|
|
buffered.writeln(result, "const buildin_mapping builtin_scripts[] = {")
|
2015-08-24 16:08:43 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
for mi = 1, #contentTable do
|
|
|
|
if contentTable[mi].table then
|
|
|
|
buffered.writeln(result, string.format('\t{"%s", %s, sizeof(%s)},', contentTable[mi].name, contentTable[mi].table, contentTable[mi].table))
|
|
|
|
else
|
|
|
|
buffered.writeln(result, string.format('\t{"%s", NULL, 0},', contentTable[mi].name))
|
|
|
|
end
|
2015-08-24 16:08:43 +00:00
|
|
|
end
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
buffered.writeln(result, "\t{NULL, NULL, 0}")
|
|
|
|
buffered.writeln(result, "};")
|
|
|
|
buffered.writeln(result, "")
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2017-06-21 00:21:13 +00:00
|
|
|
-- write out the registerModules method.
|
|
|
|
|
|
|
|
for _, name in ipairs(nativeTable) do
|
|
|
|
buffered.writeln(result, string.format("extern void register%s(lua_State* L);", name))
|
|
|
|
end
|
|
|
|
buffered.writeln(result, "")
|
|
|
|
buffered.writeln(result, "void registerModules(lua_State* L)")
|
|
|
|
buffered.writeln(result, "{")
|
2017-07-05 01:21:15 +00:00
|
|
|
buffered.writeln(result, "\t(void)(L);")
|
2017-06-21 00:21:13 +00:00
|
|
|
for _, name in ipairs(nativeTable) do
|
|
|
|
buffered.writeln(result, string.format("\tregister%s(L);", name))
|
|
|
|
end
|
|
|
|
buffered.writeln(result, "}")
|
|
|
|
buffered.writeln(result, "")
|
|
|
|
|
2014-09-26 13:25:14 +00:00
|
|
|
-- Write it all out. Check against the current contents of scripts.c first,
|
|
|
|
-- and only overwrite it if there are actual changes.
|
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
print("Writing...")
|
2015-12-17 20:56:56 +00:00
|
|
|
local scriptsFile = path.getabsolute(path.join(_SCRIPT_DIR, "../src/scripts.c"))
|
2015-10-05 19:41:11 +00:00
|
|
|
local output = buffered.tostring(result)
|
2014-09-26 13:25:14 +00:00
|
|
|
|
2015-10-05 19:41:11 +00:00
|
|
|
local f, err = os.writefile_ifnotequal(output, scriptsFile);
|
|
|
|
if (f < 0) then
|
|
|
|
error(err, 0)
|
|
|
|
elseif (f > 0) then
|
|
|
|
printf("Generated %s...", path.getrelative(os.getcwd(), scriptsFile))
|
2014-09-26 13:25:14 +00:00
|
|
|
end
|