Skip to content

Commit

Permalink
Rework cmake variables and defaults. (#13)
Browse files Browse the repository at this point in the history
- C Lua is the default rather then C++ Lua
- Turn off test by default
- Rename cmake control variables and prepend LUABIND
- Reword and simplify readme.
  • Loading branch information
davits authored Oct 3, 2023
1 parent 7688e3a commit 742c3d0
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 87 deletions.
30 changes: 13 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,38 @@ cmake_minimum_required(VERSION 3.21)

project(luabind)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# variable to use custom library
set(LUA_LIB_NAME luabind_lua CACHE STRING "lua library name")

# variable to include tests to build phase
set(LUABIND_TEST ON CACHE BOOL "variable to determine whether tests are included in build phase or not")
set(LUABIND_LUA_LIB_NAME luabind_lua CACHE STRING "CMake name of lua library, luabind should be linked with.")
set(LUABIND_LUA_CPP OFF CACHE BOOL "Whether lua was compiled as C++ and headers should be included without extern 'C'.")

option(CODE_COVERAGE "Enable coverage reporting" OFF)
option(LUABIND_TESTS "Enable tests." OFF)
option(LUABIND_CODE_COVERAGE "Enable coverage reporting in tests" OFF)

add_subdirectory(third_party)

add_library(luabind INTERFACE)
target_include_directories(luabind INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(luabind INTERFACE ${LUA_LIB_NAME})
target_link_libraries(luabind INTERFACE ${LUABIND_LUA_LIB_NAME})

set(INCLUDE_LUA_LIB_WITH_EXTERN_C ON CACHE BOOL "flag to determine whether lua library is included with extern 'C'")
if(INCLUDE_LUA_LIB_WITH_EXTERN_C)
add_compile_definitions(INCLUDE_LUA_LIB_WITH_EXTERN_C)
target_compile_definitions(luabind INTERFACE INCLUDE_LUA_LIB_WITH_EXTERN_C)
endif(INCLUDE_LUA_LIB_WITH_EXTERN_C)
if(LUABIND_LUA_CPP)
add_compile_definitions(LUABIND_LUA_CPP)
target_compile_definitions(luabind INTERFACE LUABIND_LUA_CPP)
endif(LUABIND_LUA_CPP)

if(LUABIND_TEST)
if(LUABIND_TESTS)
target_compile_options(luabind INTERFACE -Wall -Wextra -Wnewline-eof -Wformat -Werror)

if(CODE_COVERAGE)
if(LUABIND_CODE_COVERAGE)
# Add required flags (GCC & LLVM/Clang)
target_compile_options(luabind INTERFACE
-O0 # no optimization
-g # generate debug info
--coverage # sets all required flags
)
target_link_options(luabind INTERFACE --coverage)
endif(CODE_COVERAGE)
endif(LUABIND_CODE_COVERAGE)

enable_testing()
add_subdirectory(tests)
Expand Down
7 changes: 2 additions & 5 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 19,
"minor": 21,
"patch": 0
},
"configurePresets": [
Expand All @@ -14,10 +14,7 @@
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"INCLUDE_LUA_LIB_WITH_EXTERN_C": "ON",
"LUABIND_TEST": "ON",
"LUA_LIB_NAME": "luabind_lua"
"LUABIND_TESTS": "ON"
}
}
],
Expand Down
69 changes: 27 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Introduction
Luabind is a small header only library aiming to provide easy means to expose C++ functionality in Lua.
Currently it supports inheritance, binding of class member functions, member variables and global functions.
Currently it supports inheritance, binding of class member functions, static functions, member variables and global functions.

It uses variadic template magic to generate code for you, which handles function arguments transition from Lua to C++, function call, and transition result back to Lua.

## Example
```cpp
Expand All @@ -11,19 +13,22 @@ public:
void credit(int amount);
void debit(int amount);

private:
public: // public for demonstative purposes
int balance;
};

class SpecialAccount : public Account {
public:
SpecialAccount(int balance, int overdraft);

int getOverdraft() const;

private:
int overdraft;
};
```
Binding to lua
```cpp
lua_State* L = luaL_newstate();
luaL_openlibs(L);
Expand All @@ -35,17 +40,20 @@ luabind::class_<Account>(L, "Account")
luabind::class_<SpecialAccount, Account>(L, "SpecialAccount")
.constructor<int, int>("new")
.property_readonly<&SpecialAccount::overdraft>("overdraft");
.construct_shared<int, int>("create")
.property_readonly<&SpecialAccount::getOverdraft>("overdraft");
luaL_dostring(L, R"--(
a = Account:new(10)
a:credit(3)
a:debit(2)
s = SpecialAccount:new(50, 10)
s = SpecialAccount:new(50, 10) " Memory is allocated on lua stack.
s:credit(20)
s = SpecialAccount:create(50, 10) " Represented by C++ shared_ptr on lua stack.
s:credit(20)
)--");
```
## How to install, confugure, build and run
## How to install, configure, build and run

Ordinary prcedure when developing and testing `luabind` library.

Expand All @@ -61,44 +69,21 @@ cmake --build --preset luabind
ctest --preset unit_tests
```

There are some specifications when you want to use `luabind` as submodule. As a library it consist of submodules such as: `lua` and `googletest`. You can encounter to issues like `'multiple definition of googletest'` etc.. To avoid this conflicts on CMake level variables are defined to prevent such problems.

| CMake variable | Purpose |
| ------ | ------ |
| LUA_LIB_NAME (STRING) | variable to link specific library (luabind_lua is set as default) |
| INCLUDE_LUA_LIB_WITH_EXTERN_C (BOOL) | variable to add/remove extern "C" when including lua headers |
| LUABIND_TEST (BOOL) | variable for inclusion/exclusion of test into build phase |
## Integrate to CMake project


## Command line build steps

- To use custom `lua` library one can run with the following command.
```sh
cmake --preset luabind -DLUA_LIB_NAME=[your-custom-lua-library]
```
> Note: `-DLUA_LIB_NAME=luabind_lua` is set by default.

- To exclude `luabind` tests and `googletest` library from build phase run the following command.
```sh
cmake --preset luabind -DLUABIND_TEST=OFF
```
> Note: `-DLUABIND_TEST=ON` is set by default.
- To wrap/unwrap lua includes with `extern "C"` use the variable to do so.
```sh
cmake --preset luabind -DINCLUDE_LUA_LIB_WITH_EXTERN_C=OFF
```
> Note: `-DINCLUDE_LUA_LIB_WITH_EXTERN_C=ON` is set by default.
## Integrate to CMake

In this example `luabind` is used as submodule.
```
set(INCLUDE_LUA_LIB_WITH_EXTERN_C ON)
set(LUA_LIB_NAME lua)
set(LUABIND_TEST OFF)
add_subdirectory(luabind)
Simple example to use `luabind` as a submodule to a project which already has Lua integrated.
``` cmake
# Given that "lua" is the cmake name of Lua library in the top project
# And it has lua's path added to include path, such as:
# target_include_directories(lua SYSTEM INTERFACE ${LUA_SOURCE_DIR})
set(LUABIND_LUA_LIB_NAME lua)
add_subdirectory(luabind)
```

Here are some control variables, which will help to fine tune integration:

| CMake variable | Description |
| ------ | ------ |
| LUABIND_LUA_LIB_NAME (STRING) | cmake name of the Lua library to use (default: luabind_lua) |
| LUABIND_LUA_CPP (BOOL) | option indicating whether Lua headers should be included as C++ code. (default: OFF) |
| LUABIND_TESTS (BOOL) | option to enable luabind tests (default: OFF) |
8 changes: 4 additions & 4 deletions include/luabind/lua.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#ifndef LUABIND_LUA_HPP
#define LUABIND_LUA_HPP

#ifdef INCLUDE_LUA_LIB_WITH_EXTERN_C
#ifndef LUABIND_LUA_CPP
extern "C" {
#endif // INCLUDE_LUA_LIB_WITH_EXTERN_C
#endif // LUABIND_LUA_CPP

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#ifdef INCLUDE_LUA_LIB_WITH_EXTERN_C
#ifndef LUABIND_LUA_CPP
}
#endif // INCLUDE_LUA_LIB_WITH_EXTERN_C
#endif // LUABIND_LUA_CPP

#endif // LUABIND_LUA_HPP
13 changes: 6 additions & 7 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@

include(CTest)

add_executable(base_class_binding base_class_binding.cpp lua_test.hpp)
target_link_libraries(base_class_binding luabind ${LUA_LIB_NAME} gtest_main)
target_link_libraries(base_class_binding luabind gtest_main)
add_test(NAME base_class_binding_test COMMAND base_class_binding)

add_executable(binding binding.cpp lua_test.hpp)
target_link_libraries(binding luabind ${LUA_LIB_NAME} gtest_main)
target_link_libraries(binding luabind gtest_main)
add_test(NAME binding_test COMMAND binding)

add_executable(const_binding const_binding.cpp lua_test.hpp)
target_link_libraries(const_binding luabind ${LUA_LIB_NAME} gtest_main)
target_link_libraries(const_binding luabind gtest_main)
add_test(NAME const_binding_test COMMAND const_binding)

add_executable(explicit_delete explicit_delete.cpp lua_test.hpp)
target_link_libraries(explicit_delete luabind ${LUA_LIB_NAME} gtest_main)
target_link_libraries(explicit_delete luabind gtest_main)
add_test(NAME explicit_delete_test COMMAND explicit_delete)

add_executable(custom_functions custom_functions.cpp lua_test.hpp)
target_link_libraries(custom_functions luabind ${LUA_LIB_NAME} gtest_main)
target_link_libraries(custom_functions luabind gtest_main)
add_test(NAME custom_functions_test COMMAND custom_functions)

add_executable(errors errors.cpp lua_test.hpp)
target_link_libraries(errors luabind ${LUA_LIB_NAME} gtest_main)
target_link_libraries(errors luabind gtest_main)
add_test(NAME errors_test COMMAND errors)


4 changes: 2 additions & 2 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if(${LUA_LIB_NAME} STREQUAL luabind_lua)
if(${LUABIND_LUA_LIB_NAME} STREQUAL luabind_lua)
add_subdirectory(lua)
endif()

if(LUABIND_TEST)
if(LUABIND_TESTS)
add_subdirectory(googletest)
endif()
13 changes: 4 additions & 9 deletions third_party/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
set(LUA_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lua)

file(GLOB luabind_lua_header_files ${LUA_SOURCE_DIR}/*.h)
file(GLOB luabind_lua_sources ${LUA_SOURCE_DIR}/*.c)
list(REMOVE_ITEM luabind_lua_sources "${LUA_SOURCE_DIR}/lua.c" "${LUA_SOURCE_DIR}/luac.c" "${LUA_SOURCE_DIR}/onelua.c" "${LUA_SOURCE_DIR}/ltests.c")
file(GLOB LUABIND_LUA_SOURCES ${LUA_SOURCE_DIR}/*.c)
list(REMOVE_ITEM LUABIND_LUA_SOURCES "${LUA_SOURCE_DIR}/lua.c" "${LUA_SOURCE_DIR}/luac.c" "${LUA_SOURCE_DIR}/onelua.c" "${LUA_SOURCE_DIR}/ltests.c")

add_library(luabind_lua_headers INTERFACE ${luabind_lua_header_files})
add_library(luabind_lua STATIC ${luabind_lua_sources})

target_include_directories(luabind_lua_headers SYSTEM INTERFACE ${LUA_SOURCE_DIR})

target_link_libraries(luabind_lua PUBLIC luabind_lua_headers)
add_library(luabind_lua STATIC ${LUABIND_LUA_SOURCES})
target_include_directories(luabind_lua SYSTEM INTERFACE ${LUA_SOURCE_DIR})
2 changes: 1 addition & 1 deletion third_party/lua/lua
Submodule lua updated 75 files
+1 −1 all
+123 −126 lapi.c
+10 −7 lapi.h
+31 −25 lauxlib.c
+88 −49 lcode.c
+2 −2 lcorolib.c
+43 −37 ldebug.c
+1 −1 ldebug.h
+128 −101 ldo.c
+13 −4 ldo.h
+6 −2 ldump.c
+27 −27 lfunc.c
+3 −3 lfunc.h
+57 −49 lgc.c
+11 −8 lgc.h
+3 −3 llex.c
+17 −4 llimits.h
+5 −5 lmathlib.c
+41 −27 lmem.c
+7 −2 loadlib.c
+24 −14 lobject.c
+17 −2 lobject.h
+1 −1 lopcodes.h
+17 −19 loslib.c
+16 −15 lparser.c
+35 −30 lstate.c
+15 −10 lstate.h
+1 −1 lstrlib.c
+4 −4 ltable.c
+0 −1 ltable.h
+1 −1 ltablib.c
+18 −18 ltests.c
+7 −0 ltests.h
+19 −19 ltm.c
+3 −2 ltm.h
+26 −13 lua.c
+16 −11 lua.h
+10 −3 luaconf.h
+5 −3 lundump.c
+16 −11 lutf8lib.c
+148 −87 lvm.c
+5 −0 lvm.h
+7 −3 makefile
+1 −1 manual/2html
+173 −100 manual/manual.of
+17 −3 onelua.c
+1 −0 testes/all.lua
+61 −51 testes/api.lua
+23 −11 testes/attrib.lua
+1 −1 testes/big.lua
+12 −0 testes/bitwise.lua
+49 −19 testes/calls.lua
+4 −2 testes/closure.lua
+3 −3 testes/code.lua
+45 −16 testes/constructs.lua
+41 −30 testes/coroutine.lua
+26 −0 testes/cstack.lua
+18 −9 testes/db.lua
+55 −36 testes/errors.lua
+3 −0 testes/events.lua
+13 −2 testes/files.lua
+23 −19 testes/gc.lua
+5 −5 testes/libs/makefile
+17 −16 testes/literals.lua
+47 −3 testes/locals.lua
+32 −7 testes/main.lua
+3 −3 testes/math.lua
+30 −10 testes/nextvar.lua
+17 −15 testes/pm.lua
+12 −11 testes/sort.lua
+10 −5 testes/strings.lua
+1 −1 testes/tpack.lua
+12 −2 testes/utf8.lua
+9 −9 testes/vararg.lua
+6 −6 testes/verybig.lua

0 comments on commit 742c3d0

Please sign in to comment.