diff --git a/README.md b/README.md index 86bc5db..24605ef 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,8 @@ if (lua_ADDED) endif() ``` +For a full example on using CPM to download and configure lua with sol2 see [here](examples/sol2). + ### Examples See the [examples directory](https://github.com/TheLartians/CPM/tree/master/examples) for more examples with source code. diff --git a/examples/sol2/CMakeLists.txt b/examples/sol2/CMakeLists.txt new file mode 100644 index 0000000..b9fad2d --- /dev/null +++ b/examples/sol2/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +# ---- Dependencies ---- + +include(../../cmake/CPM.cmake) + +CPMAddPackage( + NAME lua + GIT_REPOSITORY https://github.com/lua/lua.git + VERSION 5-3-4 + DOWNLOAD_ONLY YES +) + +if (lua_ADDED) + # lua has no CMakeLists, so we create our own target + + FILE(GLOB lua_sources ${lua_SOURCE_DIR}/*.c) + add_library(lua STATIC ${lua_sources}) + + target_include_directories(lua + PUBLIC + $ + ) +endif() + + +CPMAddPackage( + NAME sol2 + URL https://github.com/ThePhD/sol2/archive/v3.0.2.zip + VERSION 3.0.2 + DOWNLOAD_ONLY YES +) + +if (sol2_ADDED) + add_library(sol2 INTERFACE IMPORTED) + target_include_directories(sol2 INTERFACE ${sol2_SOURCE_DIR}/include) + target_link_libraries(sol2 INTERFACE lua) +endif() + +# ---- Executable ---- + +add_executable(CPMSol2Example "main.cpp") +set_target_properties(CPMSol2Example PROPERTIES CXX_STANDARD 17) +target_link_libraries(CPMSol2Example sol2) + diff --git a/examples/sol2/main.cpp b/examples/sol2/main.cpp new file mode 100644 index 0000000..8ed7451 --- /dev/null +++ b/examples/sol2/main.cpp @@ -0,0 +1,16 @@ +#include +#include + +struct vars { + int boop = 0; +}; + +int main() { + sol::state lua; + lua.open_libraries( sol::lib::base ); + lua.new_usertype("vars", "boop", &vars::boop); + lua.script("beep = vars.new()\n" + "beep.boop = 1\n" + "print('beep boop')"); + assert(lua.get("beep").boop == 1); +} \ No newline at end of file