-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple codegen for lua c library static inclusion (#178)
- Loading branch information
Showing
14 changed files
with
175 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
* | ||
|
||
!.gitignore | ||
!SCsub | ||
!SConscript | ||
!codegen.py | ||
!codegen.py | ||
!README.md | ||
!lua_libraries.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## Lua Libraries | ||
This is a code gen module which allows you to statically compile a lua C library into this addon. Its as simple as downloading the source for the addon, and adding it as a folder with the library name in this folder. | ||
|
||
It is important that the folder name be exactly the library name in lowercase. No version numbers included. | ||
|
||
The code gen will auto detect it next time you build the addon. Either as a module or for GDExtension. | ||
|
||
This is very new and potentially very prone to failure. So for lpeg has been tested and confirmed to work. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Import('env') | ||
from codegen import code_gen | ||
|
||
code_gen(env['luaapi_luaver'] == 'jit') | ||
|
||
sources = ['lua_libraries.gen.cpp'] | ||
library_name = "liblualibraries{}{}".format(env['suffix'], env["LIBSUFFIX"]) | ||
|
||
env.Append(sources=["lua_libraries/lua_libraries.gen.cpp"]) | ||
env.Append(CPPPATH=[Dir('.').abspath]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Import('env') | ||
Import('env_lua') | ||
|
||
from codegen import code_gen | ||
|
||
code_gen(env['luaapi_luaver'] == 'jit') | ||
|
||
env_lua.add_source_files(env.modules_sources, "lua_libraries.gen.cpp") | ||
env_lua.Append(CPPPATH=[Dir('.').abspath]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import os | ||
|
||
def code_gen(luaJIT=False): | ||
lua_libraries = [ | ||
"base", | ||
"coroutine", | ||
"debug", | ||
"io", | ||
"math", | ||
"os", | ||
"package", | ||
"string", | ||
"table", | ||
"utf8" | ||
] | ||
|
||
luajit_libraries = [ | ||
"base", | ||
"bit", | ||
"debug", | ||
"ffi", | ||
"io", | ||
"jit", | ||
"math", | ||
"os", | ||
"package", | ||
"string", | ||
"string_buffer", | ||
"table" | ||
] | ||
|
||
libraries = lua_libraries | ||
lib_source_files = [] | ||
|
||
if luaJIT: | ||
libraries = luajit_libraries | ||
|
||
for library in os.listdir("./"): | ||
if not os.path.isdir(library) or library == "__pycache__" or library == "bin": | ||
continue | ||
|
||
libraries.append(library) | ||
|
||
for source_file in os.listdir("./%s" % library): | ||
if source_file.endswith(".cpp") or source_file.endswith(".c"): | ||
lib_source_files.append(os.path.join(library, source_file)) | ||
|
||
luaLibraries_gen_cpp = "#include \"lua_libraries.h\"\n#include <map>\n#include <string>\n\n" | ||
|
||
if len(lib_source_files) > 0: | ||
for source_file in lib_source_files: | ||
luaLibraries_gen_cpp += "#include \"%s\"\n" % source_file | ||
luaLibraries_gen_cpp += "\n" | ||
|
||
luaLibraries_gen_cpp += "std::map<std::string, lua_CFunction> luaLibraries = {\n" | ||
|
||
for library in libraries: | ||
luaLibraries_gen_cpp += "\t{ \"%s\", luaopen_%s },\n" % (library, library) | ||
|
||
luaLibraries_gen_cpp += "};\n" | ||
if luaJIT: | ||
luaLibraries_gen_cpp += """ | ||
bool loadLuaLibrary(lua_State *L, String libraryName) { | ||
const char *lib_c_str = libraryName.ascii().get_data(); | ||
if (luaLibraries[lib_c_str] == nullptr) { | ||
return false; | ||
} | ||
lua_pushcfunction(L, luaLibraries[lib_c_str]); | ||
if (libraryName == "base") { | ||
lua_pushstring(L, ""); | ||
} else { | ||
lua_pushstring(L, lib_c_str); | ||
} | ||
lua_call(L, 1, 0); | ||
return true; | ||
} | ||
""" | ||
else: | ||
luaLibraries_gen_cpp += """ | ||
bool loadLuaLibrary(lua_State *L, String libraryName) { | ||
const char *lib_c_str = libraryName.ascii().get_data(); | ||
if (luaLibraries[lib_c_str] == nullptr) { | ||
return false; | ||
} | ||
luaL_requiref(L, lib_c_str, luaLibraries[lib_c_str], 1); | ||
lua_pop(L, 1); | ||
return true; | ||
} | ||
""" | ||
|
||
gen_file = open("lua_libraries.gen.cpp", "w") | ||
gen_file.write(luaLibraries_gen_cpp) | ||
gen_file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef LUA_LIBRARIES_H | ||
#define LUA_LIBRARIES_H | ||
|
||
#include <lua/lua.hpp> | ||
|
||
#ifndef LAPI_GDEXTENSION | ||
#include "core/string/ustring.h" | ||
|
||
#include "core/variant/variant.h" | ||
#else | ||
#include <godot_cpp/variant/string.hpp> | ||
|
||
using namespace godot; | ||
#endif | ||
|
||
bool loadLuaLibrary(lua_State *L, String libraryName); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters