Skip to content

Commit

Permalink
refactor(build): include lpeg as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
bfredl committed Apr 27, 2023
1 parent 9f0762f commit 45bcf83
Show file tree
Hide file tree
Showing 19 changed files with 376 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ globals = {

exclude_files = {
'test/functional/fixtures/lua/syntax_error.lua',
'runtime/lua/vim/treesitter/_meta.lua'
'runtime/lua/vim/treesitter/_meta.lua',
'runtime/lua/vim/re.lua',
}
1 change: 1 addition & 0 deletions .styluaignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/scripts
/src
/test
/runtime/lua/vim/re.lua
13 changes: 3 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,20 @@ foreach(CFGNAME ${CMAKE_CONFIGURATION_TYPES})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CFGNAME} ${CMAKE_BINARY_DIR}/lib)
endforeach()

set(LUA_DEPENDENCIES lpeg)
if(NOT LUA_PRG)
foreach(CURRENT_LUA_PRG luajit lua5.1 lua5.2 lua)
unset(_CHECK_LUA_PRG CACHE)
unset(LUA_PRG_WORKS)
find_program(_CHECK_LUA_PRG ${CURRENT_LUA_PRG})

if(_CHECK_LUA_PRG)
check_lua_deps(${_CHECK_LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS)
if(LUA_PRG_WORKS)
set(LUA_PRG "${_CHECK_LUA_PRG}" CACHE FILEPATH "Path to a program.")
break()
endif()
set(LUA_PRG "${_CHECK_LUA_PRG}" CACHE FILEPATH "Path to a program.")
break()
endif()
endforeach()
unset(_CHECK_LUA_PRG CACHE)
else()
check_lua_deps(${LUA_PRG} "${LUA_DEPENDENCIES}" LUA_PRG_WORKS)
endif()

if(NOT LUA_PRG_WORKS)
if(NOT LUA_PRG)
message(FATAL_ERROR "Failed to find a Lua 5.1-compatible interpreter")
endif()

Expand Down
9 changes: 9 additions & 0 deletions cmake.deps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(DEPS_BIN_DIR "${DEPS_INSTALL_DIR}/bin")
set(DEPS_LIB_DIR "${DEPS_INSTALL_DIR}/lib")
set(DEPS_BUILD_DIR "${CMAKE_BINARY_DIR}/build")
set(DEPS_DOWNLOAD_DIR "${DEPS_BUILD_DIR}/downloads")
set(DEPS_INCLUDE_FLAGS "-I${DEPS_INSTALL_DIR}/include -I${DEPS_INSTALL_DIR}/include/luajit-2.1")

list(APPEND DEPS_CMAKE_ARGS -D CMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR})

Expand All @@ -54,6 +55,7 @@ option(USE_BUNDLED_MSGPACK "Use the bundled msgpack." ${USE_BUNDLED})
option(USE_BUNDLED_LUAJIT "Use the bundled version of luajit." ${USE_BUNDLED})
option(USE_BUNDLED_LUAROCKS "Use the bundled version of luarocks." ${USE_BUNDLED})
option(USE_BUNDLED_LUV "Use the bundled version of luv." ${USE_BUNDLED})
option(USE_BUNDLED_LPEG "Use the bundled lpeg." ${USE_BUNDLED})
#XXX(tarruda): Lua is only used for debugging the functional test client, don't
# build it unless explicitly requested
option(USE_BUNDLED_LUA "Use the bundled version of lua." OFF)
Expand Down Expand Up @@ -161,6 +163,9 @@ set(LIBVTERM_SHA256 25a8ad9c15485368dfd0a8a9dca1aec8fea5c27da3fa74ec518d5d3787f0
set(LUV_URL https://github.com/luvit/luv/archive/093a977b82077591baefe1e880d37dfa2730bd54.tar.gz)
set(LUV_SHA256 222b38b6425f0926218e14e7da81481fdde6f9660c1feac25a53e6fb52e886e6)

set(LPEG_URL http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz)
set(LPEG_SHA256 48d66576051b6c78388faad09b70493093264588fcd0f258ddaab1cdd4a15ffe)

set(LUA_COMPAT53_URL https://github.com/keplerproject/lua-compat-5.3/archive/v0.9.tar.gz)
set(LUA_COMPAT53_SHA256 ad05540d2d96a48725bb79a1def35cf6652a4e2ec26376e2617c8ce2baa6f416)

Expand Down Expand Up @@ -245,6 +250,10 @@ if(USE_BUNDLED_LUV)
include(BuildLuv)
endif()

if(USE_BUNDLED_LPEG)
include(BuildLpeg)
endif()

if(USE_BUNDLED_GETTEXT)
include(BuildGettext)
endif()
Expand Down
18 changes: 18 additions & 0 deletions cmake.deps/cmake/BuildLpeg.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
list(APPEND LPEG_CMAKE_ARGS "-DCMAKE_C_FLAGS:STRING=${DEPS_INCLUDE_FLAGS}")

ExternalProject_Add(lpeg
URL ${LPEG_URL}
URL_HASH SHA256=${LPEG_SHA256}
DOWNLOAD_NO_PROGRESS TRUE
DOWNLOAD_DIR ${DEPS_DOWNLOAD_DIR}/lpeg
PATCH_COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/cmake/LpegCMakeLists.txt
${DEPS_BUILD_DIR}/src/lpeg/CMakeLists.txt
CMAKE_ARGS ${DEPS_CMAKE_ARGS} ${LPEG_CMAKE_ARGS}
CMAKE_CACHE_ARGS ${DEPS_CMAKE_CACHE_ARGS})

if(USE_BUNDLED_LUAJIT)
add_dependencies(lpeg luajit)
elseif(USE_BUNDLED_LUA)
add_dependencies(lpeg lua)
endif()
2 changes: 0 additions & 2 deletions cmake.deps/cmake/BuildLuarocks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ function(Download ROCK VER)
set(CURRENT_DEP ${ROCK} PARENT_SCOPE)
endfunction()

Download(lpeg 1.0.2-1)

if(USE_BUNDLED_BUSTED)
if(WIN32)
set(BUSTED_EXE "${DEPS_BIN_DIR}/busted.bat")
Expand Down
6 changes: 1 addition & 5 deletions cmake.deps/cmake/BuildLuv.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
set(LUV_INCLUDE_FLAGS
"-I${DEPS_INSTALL_DIR}/include -I${DEPS_INSTALL_DIR}/include/luajit-2.1")

set(LUV_CMAKE_ARGS
-D LUA_BUILD_TYPE=System
-D LUA_COMPAT53_DIR=${DEPS_BUILD_DIR}/src/lua-compat-5.3
Expand All @@ -26,8 +23,7 @@ if(USE_BUNDLED_LIBUV)
list(APPEND LUV_CMAKE_ARGS -D CMAKE_PREFIX_PATH=${DEPS_INSTALL_DIR})
endif()

list(APPEND LUV_CMAKE_ARGS
"-DCMAKE_C_FLAGS:STRING=${LUV_INCLUDE_FLAGS}")
list(APPEND LUV_CMAKE_ARGS "-DCMAKE_C_FLAGS:STRING=${DEPS_INCLUDE_FLAGS}")
if(CMAKE_GENERATOR MATCHES "Unix Makefiles" AND
(CMAKE_SYSTEM_NAME MATCHES ".*BSD" OR CMAKE_SYSTEM_NAME MATCHES "DragonFly"))
list(APPEND LUV_CMAKE_ARGS -D CMAKE_MAKE_PROGRAM=gmake)
Expand Down
11 changes: 11 additions & 0 deletions cmake.deps/cmake/LpegCMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.10)
project (lpeg C)

include(GNUInstallDirs)

file(GLOB LPEG_SOURCES ${CMAKE_SOURCE_DIR}/*.c)
add_library(lpeg ${LPEG_SOURCES})

install(TARGETS lpeg ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

# vim: set ft=cmake:
14 changes: 14 additions & 0 deletions cmake/FindLpeg.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
find_library(LPEG_LIBRARY NAMES lpeg_a lpeg liblpeg_a)

# Ubuntu-specific workaround to find system paths
function(ubuntu)
set(CMAKE_FIND_LIBRARY_PREFIXES "")
find_library(LPEG_LIBRARY NAMES lpeg PATH_SUFFIXES lua/5.1)
endfunction()
ubuntu()

find_package_handle_standard_args(Lpeg DEFAULT_MSG LPEG_LIBRARY)
mark_as_advanced(LPEG_LIBRARY)

add_library(lpeg INTERFACE)
target_link_libraries(lpeg INTERFACE ${LPEG_LIBRARY})
23 changes: 0 additions & 23 deletions cmake/LuaHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,3 @@ function(check_lua_module LUA_PRG_PATH MODULE RESULT_VAR)
set(${RESULT_VAR} True PARENT_SCOPE)
endif()
endfunction()

# Check Lua interpreter for dependencies
function(check_lua_deps LUA_PRG_PATH MODULES RESULT_VAR)
# Check if the lua interpreter at the given path
# satisfies all Neovim dependencies
message(STATUS "Checking Lua interpreter: ${LUA_PRG_PATH}")
if(NOT EXISTS ${LUA_PRG_PATH})
message(STATUS
"[${LUA_PRG_PATH}] file not found")
endif()

foreach(module ${MODULES})
check_lua_module(${LUA_PRG_PATH} ${module} has_module)
if(NOT has_module)
message(STATUS
"[${LUA_PRG_PATH}] The '${module}' lua package is required for building Neovim")
set(${RESULT_VAR} False PARENT_SCOPE)
return()
endif()
endforeach()

set(${RESULT_VAR} True PARENT_SCOPE)
endfunction()
8 changes: 8 additions & 0 deletions runtime/doc/lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ regex:match_line({bufnr}, {line_idx} [, {start}, {end}]) *regex:match_line()*
|regex:match_str()|. If {start} is used, then the returned byte indices
will be relative {start}.

------------------------------------------------------------------------------
VIM.LPEG *lua-lpeg*

*vim.lpeg* *vim.re*
The Lpeg library for parsing expression grammars is being included as
`vim.lpeg` (https://www.inf.puc-rio.br/~roberto/lpeg/). In addition, its regex-like
interface is available as `vim.re` (https://www.inf.puc-rio.br/~roberto/lpeg/re.html).

------------------------------------------------------------------------------
VIM.DIFF *lua-diff*

Expand Down
1 change: 1 addition & 0 deletions runtime/lua/vim/_init_packages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ vim._submodules = {
version = true,
fs = true,
iter = true,
re = true,
}

-- These are for loading runtime modules in the vim namespace lazily.
Expand Down
Loading

0 comments on commit 45bcf83

Please sign in to comment.