Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit e9129f2

Browse files
authored
Simple codegen for lua c library static inclusion (#178)
1 parent 5a71e71 commit e9129f2

14 files changed

Lines changed: 175 additions & 94 deletions

File tree

SConstruct

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ env.tools=['mingw']
99
env.Append(CPPDEFINES = ['LAPI_GDEXTENSION'])
1010
env.Append(CPPPATH = [Dir('src').abspath, Dir('external').abspath])
1111

12+
env['sources'] = []
13+
Export('env')
14+
SConscript("lua_libraries/SConscript")
15+
1216
sources = Glob('*.cpp')
1317
sources.append(Glob('src/*.cpp'))
1418
sources.append(Glob('src/classes/*.cpp'))
19+
sources.append(env['sources'])
1520

1621
if env["luaapi_luaver"] == 'jit':
1722
env.Append(CPPDEFINES=['LAPI_LUAJIT'])

SCsub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ if env["luaapi_luaver"] == "jit" and env['platform']=='javascript':
1818

1919
Export('env_lua')
2020
SConscript('external/SCsub')
21+
SConscript('lua_libraries/SCsub')
2122

2223
if env["luaapi_luaver"] == 'jit':
2324
env_lua.Append(CPPDEFINES=['LAPI_LUAJIT'])

doc_classes/LuaAPI.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
</description>
2323
</method>
2424
<method name="bind_libraries">
25-
<return type="void" />
25+
<return type="LuaError" />
2626
<param index="0" name="Array" type="Array" />
2727
<description>
2828
Bind lua libraries to the LuaAPI Object.
29+
Returns an error if a library fails to bind.
2930
Note: in C#, the param Array must be a [code]Godot.Collections.Array[/code] and not a [code]System.Array[/code] class.
3031
</description>
3132
</method>

external/SCsub

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Import('env')
32
Import('env_lua')
43

@@ -74,6 +73,7 @@ if luaver == "jit":
7473
else:
7574
env.Append(LIBPATH=[Dir("luaJIT/src").abspath])
7675
env.Append(LIBS=['libluajit'])
76+
env_lua.Append(CPPPATH=[Dir('luaJIT/src').abspath])
7777

7878
elif luaver == "5.1":
7979
env_lua.Append(CPPDEFINES='MAKE_LIB')
@@ -87,7 +87,8 @@ elif luaver == "5.1":
8787
env_lua['CFLAGS'].remove('-std=gnu11')
8888
env_lua.Append(CFLAGS=['-std=c99'])
8989

90-
env_lua.add_source_files(env.modules_sources,'lua/*.c')
90+
env_lua.Append(CPPPATH=[Dir('lua51').abspath])
91+
env_lua.add_source_files(env.modules_sources,'lua51/*.c')
9192

9293
else:
9394
env_lua.Append(CPPDEFINES='MAKE_LIB')
@@ -101,5 +102,6 @@ else:
101102
env_lua['CFLAGS'].remove('-std=gnu11')
102103
env_lua.Append(CFLAGS=['-std=c99'])
103104

104-
env_lua.add_source_files(env.modules_sources,'lua/onelua.c')
105+
env_lua.Append(CPPPATH=[Dir('lua').abspath])
106+
env_lua.add_source_files(env.modules_sources, 'lua/onelua.c')
105107

lua_libraries/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*
2+
3+
!.gitignore
4+
!SCsub
5+
!SConscript
6+
!codegen.py
7+
!codegen.py
8+
!README.md
9+
!lua_libraries.h

lua_libraries/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Lua Libraries
2+
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.
3+
4+
It is important that the folder name be exactly the library name in lowercase. No version numbers included.
5+
6+
The code gen will auto detect it next time you build the addon. Either as a module or for GDExtension.
7+
8+
This is very new and potentially very prone to failure. So for lpeg has been tested and confirmed to work.

lua_libraries/SConscript

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Import('env')
2+
from codegen import code_gen
3+
4+
code_gen(env['luaapi_luaver'] == 'jit')
5+
6+
sources = ['lua_libraries.gen.cpp']
7+
library_name = "liblualibraries{}{}".format(env['suffix'], env["LIBSUFFIX"])
8+
9+
env.Append(sources=["lua_libraries/lua_libraries.gen.cpp"])
10+
env.Append(CPPPATH=[Dir('.').abspath])

lua_libraries/SCsub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Import('env')
2+
Import('env_lua')
3+
4+
from codegen import code_gen
5+
6+
code_gen(env['luaapi_luaver'] == 'jit')
7+
8+
env_lua.add_source_files(env.modules_sources, "lua_libraries.gen.cpp")
9+
env_lua.Append(CPPPATH=[Dir('.').abspath])

lua_libraries/codegen.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import os
2+
3+
def code_gen(luaJIT=False):
4+
lua_libraries = [
5+
"base",
6+
"coroutine",
7+
"debug",
8+
"io",
9+
"math",
10+
"os",
11+
"package",
12+
"string",
13+
"table",
14+
"utf8"
15+
]
16+
17+
luajit_libraries = [
18+
"base",
19+
"bit",
20+
"debug",
21+
"ffi",
22+
"io",
23+
"jit",
24+
"math",
25+
"os",
26+
"package",
27+
"string",
28+
"string_buffer",
29+
"table"
30+
]
31+
32+
libraries = lua_libraries
33+
lib_source_files = []
34+
35+
if luaJIT:
36+
libraries = luajit_libraries
37+
38+
for library in os.listdir("./"):
39+
if not os.path.isdir(library) or library == "__pycache__" or library == "bin":
40+
continue
41+
42+
libraries.append(library)
43+
44+
for source_file in os.listdir("./%s" % library):
45+
if source_file.endswith(".cpp") or source_file.endswith(".c"):
46+
lib_source_files.append(os.path.join(library, source_file))
47+
48+
luaLibraries_gen_cpp = "#include \"lua_libraries.h\"\n#include <map>\n#include <string>\n\n"
49+
50+
if len(lib_source_files) > 0:
51+
for source_file in lib_source_files:
52+
luaLibraries_gen_cpp += "#include \"%s\"\n" % source_file
53+
luaLibraries_gen_cpp += "\n"
54+
55+
luaLibraries_gen_cpp += "std::map<std::string, lua_CFunction> luaLibraries = {\n"
56+
57+
for library in libraries:
58+
luaLibraries_gen_cpp += "\t{ \"%s\", luaopen_%s },\n" % (library, library)
59+
60+
luaLibraries_gen_cpp += "};\n"
61+
if luaJIT:
62+
luaLibraries_gen_cpp += """
63+
bool loadLuaLibrary(lua_State *L, String libraryName) {
64+
const char *lib_c_str = libraryName.ascii().get_data();
65+
if (luaLibraries[lib_c_str] == nullptr) {
66+
return false;
67+
}
68+
69+
lua_pushcfunction(L, luaLibraries[lib_c_str]);
70+
if (libraryName == "base") {
71+
lua_pushstring(L, "");
72+
} else {
73+
lua_pushstring(L, lib_c_str);
74+
}
75+
lua_call(L, 1, 0);
76+
return true;
77+
}
78+
"""
79+
else:
80+
luaLibraries_gen_cpp += """
81+
bool loadLuaLibrary(lua_State *L, String libraryName) {
82+
const char *lib_c_str = libraryName.ascii().get_data();
83+
if (luaLibraries[lib_c_str] == nullptr) {
84+
return false;
85+
}
86+
87+
luaL_requiref(L, lib_c_str, luaLibraries[lib_c_str], 1);
88+
lua_pop(L, 1);
89+
return true;
90+
}
91+
"""
92+
93+
gen_file = open("lua_libraries.gen.cpp", "w")
94+
gen_file.write(luaLibraries_gen_cpp)
95+
gen_file.close()

lua_libraries/lua_libraries.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef LUA_LIBRARIES_H
2+
#define LUA_LIBRARIES_H
3+
4+
#include <lua/lua.hpp>
5+
6+
#ifndef LAPI_GDEXTENSION
7+
#include "core/string/ustring.h"
8+
9+
#include "core/variant/variant.h"
10+
#else
11+
#include <godot_cpp/variant/string.hpp>
12+
13+
using namespace godot;
14+
#endif
15+
16+
bool loadLuaLibrary(lua_State *L, String libraryName);
17+
18+
#endif

0 commit comments

Comments
 (0)