From b4c4c4e6143ffda97abed0c61d8657fd77328df0 Mon Sep 17 00:00:00 2001 From: Tom van Dijck Date: Tue, 20 Jun 2017 17:21:13 -0700 Subject: [PATCH] Allow modules to register C code too. only works for embedded modules. --- premake5.lua | 1 + scripts/embed.lua | 25 +++++++++++++++++++++++++ src/host/premake.c | 11 ++++++++--- src/host/premake.h | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/premake5.lua b/premake5.lua index f25c0889..75966215 100644 --- a/premake5.lua +++ b/premake5.lua @@ -144,6 +144,7 @@ { "*.txt", "**.lua", "src/**.h", "src/**.c", + "modules/**.h", "modules/**.c", } excludes diff --git a/scripts/embed.lua b/scripts/embed.lua index ba8ab53d..bd912e2c 100644 --- a/scripts/embed.lua +++ b/scripts/embed.lua @@ -116,11 +116,13 @@ -- Generate table of embedded content. local contentTable = {} + local nativeTable = {} print("Compiling... ") for mi = 1, #manifests do local manifestName = manifests[mi] local manifestDir = path.getdirectory(manifestName) + local moduleName = path.getbasename(manifestDir) local baseDir = path.getdirectory(manifestDir) local files = dofile(manifests[mi]) @@ -128,6 +130,15 @@ local filename = path.join(manifestDir, files[fi]) addScript(contentTable, filename, path.getrelative(baseDir, filename)) end + + -- 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 end addScript(contentTable, path.join(_SCRIPT_DIR, "../src/_premake_main.lua"), "src/_premake_main.lua") @@ -167,6 +178,20 @@ buffered.writeln(result, "};") buffered.writeln(result, "") +-- 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, "{") + for _, name in ipairs(nativeTable) do + buffered.writeln(result, string.format("\tregister%s(L);", name)) + end + 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. diff --git a/src/host/premake.c b/src/host/premake.c index 648045f5..450ff63c 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -215,13 +215,18 @@ int premake_init(lua_State* L) lua_newtable(L); lua_setglobal(L, "premake"); +#if !defined(PREMAKE_NO_BUILTIN_SCRIPTS) + /* let native modules initialize themselves */ + registerModules(L); +#endif + return OKAY; } static int getErrorColor(lua_State* L) { - int color; + int color; lua_getglobal(L, "term"); lua_pushstring(L, "errorColor"); @@ -421,7 +426,7 @@ int premake_test_file(lua_State* L, const char* filename, int searchMask) if (path && do_locate(L, filename, path)) return OKAY; } - #if !defined(PREMAKE_NO_BUILTIN_SCRIPTS) +#if !defined(PREMAKE_NO_BUILTIN_SCRIPTS) if ((searchMask & TEST_EMBEDDED) != 0) { /* Try to locate a record matching the filename */ if (premake_find_embedded_script(filename) != NULL) { @@ -431,7 +436,7 @@ int premake_test_file(lua_State* L, const char* filename, int searchMask) return OKAY; } } - #endif +#endif return !OKAY; } diff --git a/src/host/premake.h b/src/host/premake.h index 3c3b2b09..f60ca61e 100644 --- a/src/host/premake.h +++ b/src/host/premake.h @@ -181,7 +181,7 @@ typedef struct } buildin_mapping; extern const buildin_mapping builtin_scripts[]; - +extern void registerModules(lua_State* L); int premake_init(lua_State* L); int premake_execute(lua_State* L, int argc, const char** argv, const char* script);