├── .gitmodules ├── LICENSE ├── CMakeLists.txt └── README.md /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "upstream"] 2 | path = upstream 3 | url = https://github.com/lua/lua.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Lukas Böger 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.12) 3 | project(lua-cmake VERSION 1.0.0 DESCRIPTION "Lua build for a project subdirectory" LANGUAGES C) 4 | 5 | option(BUILD_SHARED_LIBS "Build as shared library" ON) 6 | 7 | set(srcDir upstream) 8 | 9 | set(srcFiles lapi.c lauxlib.c lbaselib.c lcode.c lcorolib.c lctype.c ldblib.c ldebug.c ldo.c 10 | ldump.c lfunc.c lgc.c linit.c liolib.c llex.c lmathlib.c lmem.c loadlib.c lobject.c lopcodes.c 11 | loslib.c lparser.c lstate.c lstring.c lstrlib.c ltable.c ltablib.c ltests.c ltm.c lundump.c 12 | lutf8lib.c lvm.c lzio.c) 13 | 14 | set(publicHeaderFiles lauxlib.h lua.h luaconf.h lualib.h) 15 | 16 | list(TRANSFORM srcFiles PREPEND ${srcDir}/) 17 | list(TRANSFORM publicHeaderFiles PREPEND ${srcDir}/) 18 | 19 | add_library(lua 20 | ${srcFiles}) 21 | 22 | target_compile_definitions(lua 23 | PRIVATE 24 | $<$:LUA_USE_LINUX LUA_COMPAT_5_2>) 25 | 26 | target_compile_options(lua 27 | PRIVATE 28 | $<$,$,$>: 29 | -Wextra -Wshadow -Wsign-compare -Wundef -Wwrite-strings -Wredundant-decls 30 | -Wdisabled-optimization -Waggregate-return -Wdouble-promotion -Wdeclaration-after-statement 31 | -Wmissing-prototypes -Wnested-externs -Wstrict-prototypes -Wc++-compat -Wold-style-definition>) 32 | 33 | set(includeDir ${CMAKE_CURRENT_BINARY_DIR}/include) 34 | file(COPY ${publicHeaderFiles} DESTINATION ${includeDir}) 35 | 36 | add_library(lua-header 37 | INTERFACE) 38 | 39 | target_include_directories(lua-header 40 | INTERFACE 41 | ${includeDir}) 42 | 43 | target_link_libraries(lua 44 | INTERFACE 45 | lua-header) 46 | 47 | add_library(lua::lib ALIAS lua) 48 | add_library(lua::header ALIAS lua-header) 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Easily embed lua into applications managed with CMake 3 | 4 | Due to its simplicity and portability, [Lua](https://www.lua.org) became a popular choice for 5 | extending applications through a scripting language. The interpreter is available on most platforms, 6 | however, depending on globally installed packages is not always reliable (outdated versions, static 7 | vs. dynamic library etc.). As compile times of the interpreter are neglible, embedding it as a **git 8 | submodule** can be preferrable. To simplify the Lua integration into `cmake`-managed projects, this 9 | repository offers two simple facilities. 10 | 11 | - The `CMakeLists.txt` contains instructions for building the interpreter as a library. It is 12 | intended to be used via `cmake`'s `add_subdirectory` command, and sets up the `lua::lib` target 13 | that can be linked against. Public headers are taken care of and are propagated as a usage 14 | requirement. 15 | - A git clone of the upstream [sources](https://github.com/lua/lua) is configured as a submodule, 16 | such that recursive initialization of this repository pulls them in. 17 | 18 | The Lua version pulled in is the current v5.4.1. The library is built as C (not C++, which is 19 | possible), with default upstream compiler flags. Whether a shared or static library is built depends 20 | on `cmake`'s 21 | [`BUILD_SHARED_LIBS`](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html) flag. No 22 | `install` target is configured and no standalone executable is built, given the intended use case. 23 | 24 | Here's how to use it: 25 | ```bash 26 | cd path/to/your/project 27 | 28 | git submodule add https://github.com/lubgr/lua-cmake external/lua 29 | 30 | # Necessary to automatically pull the upstream Lua sources: 31 | git submodule update --init --recursive external/lua 32 | ``` 33 | In the `CMakeLists.txt` of your application, add 34 | ``` 35 | add_subdirectory(external/lua) 36 | 37 | target_link_libraries(yourTarget PRIVATE lua::lib) 38 | ``` 39 | That's it. Further integration with a library to facilitate the bindings (e.g. 40 | [sol2](https://github.com/ThePhD/sol2)) is straightfoward. 41 | --------------------------------------------------------------------------------