Solgen – C++ code generator for creating Lua bindings using sol2 library.
Warning
There is no any guarantee that this project will work for you as you expect. solgen was created as a part of algine infrastructure, so its main goal is to automate Lua bindings generation for this project. However, solgen is open to contributions.
I created this code generator mainly for my project algine since I noticed, that writing class bindings by hand is too boring, takes a lot of time and hard to maintain (especially when you have advanced inheritance).
Note: it is very possible that something will not work for you.
- Lua bindings generation for C++ classes (inner classes, constructors, operators, methods, fields, enums)
- Ability to generate custom factories instead of constructors for selected classes
- Ability to bind custom implementations for selected methods
- Properties generation based on getters & setters
- Operators bindings (temporary limited)
- Default function arguments support
- "Bad" (i.e. not auto-convertible from / to Lua table) arguments detector (e.g. such as
const std::vector<T> &
) - You can specify additional options for classes, methods, fields etc by using
///
or//!
comments or by creating conf file. - Can rebuild only changed files (by default). See
--regenerate
,--regenerate-derived
for more info - For more info see
--help
Solgen has only one dependency - libclang
.
Fedora: sudo dnf install clang-devel
In order to build solgen
on Windows, you should install LLVM.
Note
If you have installed LLVM in the default location (i.e., $Env:ProgramW6432\LLVM
),
there is no need to specify the LLVM location.
C++20 compatible compiler.
After the building process, a symlink to the libclang.dll
will be created in order
to ensure that solgen
can be executed even when LLVM is not included in the PATH.
By default, in Windows, symlinks can be created only with elevated privileges. To change this, you need to enable Developer mode in the settings.
For Windows 11: Settings -> Privacy & security -> For developers -> Developer Mode (on).
Flag | Description | Platform | Mandatory | Default |
---|---|---|---|---|
LLVM_PATH |
Specifies LLVM location | Windows | No | $Env:ProgramW6432\LLVM |
Tested on:
- Fedora 38
- Windows 11, clang version 16.0.0, target: x86_64-pc-windows-msvc
It's important to run solgen from your project's root and pass absolute paths or relative from the project root.
If you see the error stddef.h: No such file or directory
(or something similar),
you should add your compiler's include directories to solgen includes (--includes
).
You can get that directories using the following commands:
-
clang++ -E -x c++ - -v < /dev/null
-
gcc -E -x c++ - -v < /dev/null
Most limitations are coming from the sol2 library itself.
For example, you can't bind std::set
or std::vector
in some cases.
Possible workarounds: you can provide your own custom implementations for these types
(i.e. convert them to lua table and vice versa) or you can just ignore them. Possible
solution - detect these types and generate custom bindings for them.
- High Priority: refactor: move functionality from
SolGen.cpp
to the corresponding classes (Class
,Enum
etc) - Add templates support
- Add Windows support
- Add ability to disable properties (flag
--disable-properties
and optionprop_disable
) - Register types that were used in class (args, fields, return types etc)
- Link getters of type
const std::string&
and setters of typestd::string_view
- Add multiline support for conf
- Generate bindings for enums located in namespaces
- Detect deleted constructors
- Complete TODOs in the project
- Make conf files less type sensitive (?)
I will update this list when I will need more features & functionality for my own projects.
Solgen generates only source (cpp
) files which contain specialized templates.
To use them you can do something like that:
// NOTE: you can specify OutputNamespace by using --output-namespace
// By default it is solgen
namespace OutputNamespace {
template<typename T> void registerLuaUsertype(sol::table &table);
}
void registerMyTypes(sol::table &table) {
registerLuaUsertype<Foo>(table);
registerLuaUsertype<Bar>(table);
registerLuaUsertype<Baz>(table);
}
You can get list of generated files by setting --print-paths
flag.
This option is very useful for build systems like CMake.
[canonical-signature-1]
option1 value1
option2 value2
switcher0
# comment
[canonical-signature-2]
option1 value1
option2 value2
switcher0
Canonical signature means that you should write full type names, e.g.:
const std::string& MyClass::modify(std::string_view str)
// becomes
const std::basic_string<char> &MyNamespace::MyClass::modify(std::basic_string_view<char>)
Available options you can find in --help-options