Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@
# Debug files
*.dSYM/
*.su

# Build directories
bin/
build/
dist/
out/
182 changes: 174 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
cmake_minimum_required(VERSION 3.1...3.12)
cmake_policy(SET CMP0077 NEW)
project(lua LANGUAGES C VERSION 5.4.7)
project(lua LANGUAGES C VERSION 5.5.0)

option(LUA_SUPPORT_DL "Support dynamic loading of compiled modules" OFF)
option(LUA_BUILD_AS_CXX "Build lua as C++" OFF)
option(LUA_ENABLE_SHARED "Build dynamic liblua" ON)
option(LUA_ENABLE_TESTING "Build and run tests" ON)

enable_language(CXX)
if(LUA_ENABLE_TESTING)
enable_testing()
endif()

if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
set(TOP_LEVEL TRUE)
Expand All @@ -26,8 +22,178 @@ else()
option(LUA_BUILD_COMPILER "Build luac compiler" ON)
endif()

add_subdirectory(lua-${PROJECT_VERSION})
set(LUA_LIB_SRCS
"src/lapi.c"
"src/lcode.c"
"src/lctype.c"
"src/ldebug.c"
"src/ldo.c"
"src/ldump.c"
"src/lfunc.c"
"src/lgc.c"
"src/llex.c"
"src/lmem.c"
"src/lobject.c"
"src/lopcodes.c"
"src/lparser.c"
"src/lstate.c"
"src/lstring.c"
"src/ltable.c"
"src/ltm.c"
"src/lundump.c"
"src/lvm.c"
"src/lzio.c"
"src/lauxlib.c"
"src/lbaselib.c"
"src/lcorolib.c"
"src/ldblib.c"
"src/liolib.c"
"src/lmathlib.c"
"src/loadlib.c"
"src/loslib.c"
"src/lstrlib.c"
"src/ltablib.c"
"src/lutf8lib.c"
"src/linit.c"
)

set(TARGETS_TO_INSTALL lua_internal lua_include)

set(LUA_LINKED_LIBRARIES)

if(LUA_BUILD_AS_CXX)
set_source_files_properties(${LUA_LIB_SRCS} "src/lua.c" "src/luac.c" PROPERTIES LANGUAGE CXX )
endif()

add_library(lua_internal INTERFACE)

add_library(lua_include INTERFACE)

if(LUA_ENABLE_TESTING)
add_test(NAME lua-testsuite COMMAND lua -e "_U=true" all.lua WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lua-${CMAKE_PROJECT_VERSION}-tests)
target_include_directories(lua_include INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

target_link_libraries(lua_internal INTERFACE lua_include)

if(LUA_ENABLE_SHARED)
add_library(lua_shared SHARED ${LUA_LIB_SRCS})
target_link_libraries(lua_shared PRIVATE lua_internal PUBLIC lua_include)
set_target_properties(lua_shared PROPERTIES
VERSION "${PACKAGE_VERSION}"
OUTPUT_NAME "lua55"
)
if(WIN32)
target_compile_definitions(lua_shared PRIVATE LUA_BUILD_AS_DLL)
endif()
list(APPEND TARGETS_TO_INSTALL lua_shared)
if(BUILD_SHARED_LIBS)
add_library(Lua::Library ALIAS lua_shared)
elseif(NOT TOP_LEVEL)
set_target_properties(lua_shared PROPERTIES
EXCLUDE_FROM_ALL ON
)
endif()
endif()

add_library(lua_static STATIC ${LUA_LIB_SRCS})
target_link_libraries(lua_static PRIVATE lua_internal PUBLIC lua_include)
set_target_properties(lua_static PROPERTIES
VERSION "${PACKAGE_VERSION}"
OUTPUT_NAME "lua55"
)
list(APPEND TARGETS_TO_INSTALL lua_static)
if(NOT BUILD_SHARED_LIBS OR NOT LUA_ENABLE_SHARED)
add_library(Lua::Library ALIAS lua_static)
endif()


if(UNIX)
if(NOT EMSCRIPTEN)
find_library(LIBM m)
#TODO: Redo this with find_package
if(NOT LIBM)
message(FATAL_ERROR "libm not found and is required by lua")
endif()
target_compile_definitions(lua_internal INTERFACE "LUA_USE_POSIX")
target_link_libraries(lua_internal INTERFACE m)
list(APPEND LUA_LINKED_LIBRARIES m)
if(LUA_SUPPORT_DL)
find_library(LIBDL "${CMAKE_DL_LIBS}")
if(NOT LIBDL)
message(FATAL_ERROR "libdl not found and is required by lua")
endif()
target_compile_definitions(lua_internal INTERFACE "LUA_USE_DLOPEN")
target_link_libraries(lua_internal INTERFACE "${CMAKE_DL_LIBS}")
list(APPEND LUA_LINKED_LIBRARIES "${CMAKE_DL_LIBS}")
endif()
endif()

target_compile_options(lua_internal
INTERFACE "-Wall" "-Wextra"
)
elseif(WIN32)
target_compile_options(lua_internal
INTERFACE "/Wall"
)
endif()

if(LUA_BUILD_BINARY)
include(CheckIncludeFile)
CHECK_INCLUDE_FILE("readline/readline.h" HAVE_READLINE_READLINE_H)

add_executable(lua "src/lua.c")
# Can not use lua_shared because some symbols are not exported
target_link_libraries(lua PRIVATE lua_static)
set_target_properties(lua PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
OUTPUT_NAME "lua55"
)
if (HAVE_READLINE_READLINE_H)
target_compile_definitions(lua PRIVATE "LUA_USE_READLINE")
target_link_libraries(lua PUBLIC readline)
endif()
list(APPEND TARGETS_TO_INSTALL lua)
endif()

if(LUA_BUILD_COMPILER)
add_executable(luac "src/luac.c")
target_link_libraries(luac PRIVATE lua_static)
set_target_properties(luac PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
OUTPUT_NAME "luac55"
)
list(APPEND TARGETS_TO_INSTALL luac)
endif()

install(TARGETS ${TARGETS_TO_INSTALL}
EXPORT LuaTargets
)

install(DIRECTORY include/ TYPE INCLUDE)

include(CMakePackageConfigHelpers)

get_target_property(LUA_EXPORT_LIBRARY Lua::Library ALIASED_TARGET)
write_basic_package_version_file(
LuaConfigVersion.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion
)

install(EXPORT LuaTargets
FILE LuaTargets.cmake
DESTINATION "share/cmake/Lua"
NAMESPACE Lua::
)

configure_package_config_file(
LuaConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/LuaConfig.cmake"
INSTALL_DESTINATION "share/cmake/Lua"
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/LuaConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/LuaConfigVersion.cmake"
DESTINATION "share/cmake/Lua"
)
File renamed without changes.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Lua
CMake based build of Lua 5.4.7
| Build as C | Build as C++ |
| --: | --: |
| ![Build Linux](https://github.com/walterschell/Lua/actions/workflows/build-linux.yml/badge.svg?branch=master) | ![Build Linux as C++](https://github.com/walterschell/Lua/actions/workflows/build-linux-cxx.yml/badge.svg?branch=master) |
| ![Build Windows](https://github.com/walterschell/Lua/actions/workflows/build-windows.yml/badge.svg?branch=master) | ![Build Windows as C++](https://github.com/walterschell/Lua/actions/workflows/build-windows-cxx.yml/badge.svg?branch=master) |
| ![Build OSX](https://github.com/walterschell/Lua/actions/workflows/build-osx.yml/badge.svg?branch=master) | ![Build OSX as C++](https://github.com/walterschell/Lua/actions/workflows/build-osx-cxx.yml/badge.svg?branch=master) |
| ![Build Emscripten](https://github.com/walterschell/Lua/actions/workflows/build-emscripten.yml/badge.svg?branch=master) | ![Build Emscripten as C++](https://github.com/walterschell/Lua/actions/workflows/build-emscripten-cxx.yml/badge.svg?branch=master) |
CMake based build of Lua 5.5.0
# Usage
Inside of your project's CMakeLists.txt
```cmake
add_subdirectory(lua)
...
target_link_libraries(<YOURTARGET> lua_static)
```
target_link_libraries(<YOURTARGET> Lua::Library)
```
65 changes: 65 additions & 0 deletions include/lapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
** $Id: lapi.h $
** Auxiliary functions from Lua API
** See Copyright Notice in lua.h
*/

#ifndef lapi_h
#define lapi_h


#include "llimits.h"
#include "lstate.h"


#if defined(LUA_USE_APICHECK)
#include <assert.h>
#define api_check(l,e,msg) assert(e)
#else /* for testing */
#define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg))
#endif



/* Increments 'L->top.p', checking for stack overflows */
#define api_incr_top(L) \
(L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow"))


/*
** macros that are executed whenever program enters the Lua core
** ('lua_lock') and leaves the core ('lua_unlock')
*/
#if !defined(lua_lock)
#define lua_lock(L) ((void) 0)
#define lua_unlock(L) ((void) 0)
#endif



/*
** If a call returns too many multiple returns, the callee may not have
** stack space to accommodate all results. In this case, this macro
** increases its stack space ('L->ci->top.p').
*/
#define adjustresults(L,nres) \
{ if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
L->ci->top.p = L->top.p; }


/* Ensure the stack has at least 'n' elements */
#define api_checknelems(L,n) \
api_check(L, (n) < (L->top.p - L->ci->func.p), \
"not enough elements in the stack")


/* Ensure the stack has at least 'n' elements to be popped. (Some
** functions only update a slot after checking it for popping, but that
** is only an optimization for a pop followed by a push.)
*/
#define api_checkpop(L,n) \
api_check(L, (n) < L->top.p - L->ci->func.p && \
L->tbclist.p < L->top.p - (n), \
"not enough free elements in the stack")

#endif
46 changes: 8 additions & 38 deletions lua-5.4.7/include/lauxlib.h → include/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);

LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize,
size_t nsize);


/* predefined references */
#define LUA_NOREF (-2)
Expand All @@ -100,6 +103,8 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);

LUALIB_API lua_State *(luaL_newstate) (void);

LUALIB_API unsigned luaL_makeseed (lua_State *L);

LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);

LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
Expand Down Expand Up @@ -163,21 +168,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,


/* push the value used to represent failure/error */
#define luaL_pushfail(L) lua_pushnil(L)


/*
** Internal assertions for in-house debugging
*/
#if !defined(lua_assert)

#if defined LUAI_ASSERT
#include <assert.h>
#define lua_assert(c) assert(c)
#if defined(LUA_FAILISFALSE)
#define luaL_pushfail(L) lua_pushboolean(L, 0)
#else
#define lua_assert(c) ((void)0)
#endif

#define luaL_pushfail(L) lua_pushnil(L)
#endif


Expand Down Expand Up @@ -249,30 +243,6 @@ typedef struct luaL_Stream {

/* }====================================================== */

/*
** {==================================================================
** "Abstraction Layer" for basic report of messages and errors
** ===================================================================
*/

/* print a string */
#if !defined(lua_writestring)
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
#endif

/* print a newline and flush the output */
#if !defined(lua_writeline)
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
#endif

/* print an error message */
#if !defined(lua_writestringerror)
#define lua_writestringerror(s,p) \
(fprintf(stderr, (s), (p)), fflush(stderr))
#endif

/* }================================================================== */


/*
** {============================================================
Expand Down
Loading
Loading