内容纲要

premake5 主要由 lua 写成,修改了 lua 加载脚本的方式,将工程用到的 lua 脚本统统转换为 C 代码的字节数组并加载之。

对于一些变动不大的通用的库的来说,改为字节数组加载更方便。

premake5 提供了一个 premake-core/scripts/embed.lua 用于生成脚本到字节数组,我们修改之为己所用。

修改过的 embed.lua

--
-- Embed the Lua scripts into src/host/scripts.c as static data buffers.
-- 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.
--    

    local scriptCount = 0

    local function loadScript(fname)
        fname = path.getabsolute(fname)
        local f = io.open(fname, "rb")
        local s = assert(f:read("*all"))
        f:close()
        return s
    end


    local function stripScript(s)
        -- strip tabs
        local result = s:gsub("[\t]", "")

        -- strip any CRs
        result = result:gsub("[\r]", "")

        -- strip out block comments
        result = result:gsub("[^\"']%-%-%[%[.-%]%]", "")
        result = result:gsub("[^\"']%-%-%[=%[.-%]=%]", "")
        sresult = result:gsub("[^\"']%-%-%[==%[.-%]==%]", "")

        -- strip out inline comments
        result = result:gsub("\n%-%-[^\n]*", "\n")

        -- strip duplicate line feeds
        result = result:gsub("\n+", "\n")

        -- strip out leading comments
        result = result:gsub("^%-%-[^\n]*\n", "")

        return result
    end


    local function outputScript(result, script)
        local data   = script.data
        local length = #data

        if length > 0 then
            script.table = string.format("builtin_script_%d", scriptCount)
            scriptCount = scriptCount + 1

            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)
                end
            end

            buffered.writeln(result, "};")
            buffered.writeln(result)
        end
    end


    local function addScript(result, filename, name, data)
        if not data then
            if _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)
                else
                    print(err)
                    print("Embedding source instead.")
                    data = stripScript(loadScript(filename))
                end
            else
                data = stripScript(loadScript(filename))
            end
        end

        local script = {}
        script.filename = filename
        script.name     = name
        script.data     = data
        table.insert(result, script)
    end


-- Prepare the file header

    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, "")
    buffered.writeln(result, '#include "host/premake.h"')
    buffered.writeln(result, "")

-- Find all of the _manifest.lua files within the project

    local mask = path.join(_MAIN_SCRIPT_DIR, "**/_manifest.lua")
    local manifests = os.matchfiles(mask)


-- Generate table of embedded content.
    local contentTable = {}

    print("Compiling... ")
    for mi = 1, #manifests do
        local manifestName = manifests[mi]
        local manifestDir  = path.getdirectory(manifestName)
        local baseDir      = path.getdirectory(manifestDir)      

        local files = dofile(manifests[mi])
        for fi = 1, #files do
            local filename = path.join(manifestDir, files[fi])            
            addScript(contentTable, filename, path.getrelative(baseDir, filename))
        end
    end

    --addScript(contentTable, path.join(_SCRIPT_DIR, "../src/_premake_main.lua"), "src/_premake_main.lua")
    addScript(contentTable, path.join(_SCRIPT_DIR, "./lua/_manifest.lua"), "lua/_manifest.lua")



-- Embed the actual script contents

    print("Embedding...")
    for mi = 1, #contentTable do
        outputScript(result, contentTable[mi])
    end

-- 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[] = {")

    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
    end

    buffered.writeln(result, "\t{NULL, NULL, 0}")
    buffered.writeln(result, "};")
    buffered.writeln(result, "")

-- Write it all out. Check against the current contents of scripts.c first,
-- and only overwrite it if there are actual changes.

    print("Writing...")
    local scriptsFile = path.getabsolute(path.join(_SCRIPT_DIR, "./scripts.c"))
    local output = buffered.tostring(result)

    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))
    end

我们的脚本目录结构

lua\util001.lua
lua\_manifest.lua
embed.lua

在 _manifest.lua 中修改需要生成的脚本文件名

return
    {              
        "util001.lua"       
    }

运行以下命令会在 embed.lua 所在目录生成 scripts.c

premake5 --file=embed.lua

生成的 scripts.c

/* Premake's Lua scripts, as static data buffers for release mode builds */
/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */
/* To regenerate this file, run: premake5 embed */

#include "host/premake.h"

// lua/util001.lua
static const unsigned char builtin_script_0[] = {
108, 111,  99,  97, 108,  32, 117, 116, 105, 108,  32,  61,  32, 123, 125,  10,  32,  10, 102, 117, 110,  99, 116, 105, 111, 110,  32, 117, 116, 105, 108,  46, 
116, 101, 115, 116,  40,  97, 114, 103,  41,  10,  32,  32,  32,  32, 112, 114, 105, 110, 116,  40,  97, 114, 103,  41,  10, 101, 110, 100,  10,  32,  10, 114, 
101, 116, 117, 114, 110,  32, 117, 116, 105, 108, };

// lua/_manifest.lua
static const unsigned char builtin_script_1[] = {
114, 101, 116, 117, 114, 110,  10, 123,  10,  32,  32,  32,  32,  32,  32,  32,  32,  45,  45,  32,  99, 106, 115, 111, 110,  10,  32,  32,  32,  32,  32,  32, 
 32,  32,  45,  45,  34,  99, 106, 115, 111, 110,  47, 117, 116, 105, 108,  46, 108, 117,  97,  34,  44,  10,  32,  32,  32,  32,  32,  32,  32,  32,  45,  45, 
 34,  66, 105, 110,  68, 101,  99,  72, 101, 120,  46, 108, 117,  97,  34,  44,  10,  32,  32,  32,  32,  32,  32,  32,  32,  34, 117, 116, 105, 108,  48,  48, 
 49,  46, 108, 117,  97,  34,  10, 125,  10, };

const buildin_mapping builtin_scripts[] = {
    {"lua/util001.lua", builtin_script_0, sizeof(builtin_script_0)},
    {"lua/_manifest.lua", builtin_script_1, sizeof(builtin_script_1)},
    {NULL, NULL, 0}
};

下一节修改自己的 lua 引擎加载代码。

发表评论

电子邮件地址不会被公开。 必填项已用*标注