Skip to content

Commit

Permalink
luasocket & luasocket: switch to cmake for building (#1935)
Browse files Browse the repository at this point in the history
Additionally, reuse libsocket's library for luasec (instead of using the later vendored-in copy).
  • Loading branch information
benoit-pierre authored Sep 22, 2024
1 parent 8e5be56 commit e65c7dd
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ declare_project(thirdparty/luajit)
declare_project(thirdparty/luajson)

# luasec
declare_project(thirdparty/luasec DEPENDS luajit openssl)
declare_project(thirdparty/luasec DEPENDS luajit luasocket openssl)

# luasocket
declare_project(thirdparty/luasocket DEPENDS luajit)
Expand Down
33 changes: 9 additions & 24 deletions thirdparty/luasec/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
# Build in source tree.
set(BINARY_DIR ${SOURCE_DIR})
list(APPEND PATCH_FILES visibility.patch)

string(APPEND CFLAGS " -DWITH_LUASOCKET -I. -I${LUAJIT_INC} -I${STAGING_DIR}/include")
list(APPEND CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE})

string(JOIN " " LIBS
luasocket/libluasocket.a
${STAGING_DIR}/lib/libcrypto${LIB_EXT}
${STAGING_DIR}/lib/libssl${LIB_EXT}
${LUAJIT_LIB}
)

set(MAKE_CMD
make
AR=${AR} RANLIB=${RANLIB}
CC=${CC} CFLAGS=${CFLAGS}
LDFLAGS=${DYNLIB_LDFLAGS}
LIBS=${LIBS} EXTRA=${LIBS}
)

list(APPEND BUILD_CMD COMMAND ${MAKE_CMD} -C src/luasocket)
list(APPEND BUILD_CMD COMMAND ${MAKE_CMD} -C src ssl.so)
list(APPEND BUILD_CMD COMMAND ninja)

append_binary_install_command(INSTALL_CMD src/ssl.so DESTINATION common)
append_install_commands(INSTALL_CMD src/ssl.lua DESTINATION common)
append_install_commands(INSTALL_CMD src/https.lua DESTINATION common/ssl)
append_binary_install_command(INSTALL_CMD ssl.so DESTINATION common)
append_install_commands(INSTALL_CMD ${SOURCE_DIR}/src/ssl.lua DESTINATION common)
append_install_commands(INSTALL_CMD ${SOURCE_DIR}/src/https.lua DESTINATION common/ssl)

external_project(
DOWNLOAD GIT v1.3.2
https://github.com/brunoos/luasec
PATCH_COMMAND ${PATCH_CMD}
PATCH_OVERLAY overlay
PATCH_FILES ${PATCH_FILES}
CMAKE_ARGS ${CMAKE_ARGS}
BUILD_COMMAND ${BUILD_CMD}
INSTALL_COMMAND ${INSTALL_CMD}
)
24 changes: 24 additions & 0 deletions thirdparty/luasec/overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.15)
project(luasec LANGUAGES C)

find_package(OpenSSL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LuaJIT luajit REQUIRED IMPORTED_TARGET)

add_library(ssl MODULE)
set_target_properties(ssl PROPERTIES C_VISIBILITY_PRESET hidden PREFIX "")
target_compile_definitions(ssl PRIVATE -DWITH_LUASOCKET)
target_include_directories(ssl PRIVATE ${CMAKE_INSTALL_PREFIX}/include)
target_link_libraries(ssl PRIVATE
${CMAKE_INSTALL_PREFIX}/lib/libluasocket.a
OpenSSL::Crypto OpenSSL::SSL
PkgConfig::LuaJIT
)
target_sources(ssl PRIVATE
src/options.c
src/x509.c
src/context.c
src/ssl.c
src/config.c
src/ec.c
)
11 changes: 11 additions & 0 deletions thirdparty/luasec/visibility.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- a/src/compat.h
+++ b/src/compat.h
@@ -15,7 +15,7 @@
#if defined(_WIN32)
#define LSEC_API __declspec(dllexport)
#else
-#define LSEC_API extern
+#define LSEC_API extern __attribute__ ((visibility("default")))
#endif

//------------------------------------------------------------------------------
80 changes: 36 additions & 44 deletions thirdparty/luasocket/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
# Build in source tree.
set(BINARY_DIR ${SOURCE_DIR})

list(APPEND PATCH_FILES
# Rename modules:
# `mime.core` → `mime.mcore`
# `socket.core` → `socket.score`
namespace.patch
# Compatibility with luajit.
luajit-compat.patch
# To avoid duplicated code in luasec.
export-api-for-luasec.patch
)
if(NOT ANDROID)
# Make sure socket fds are CLOEXEC on *nix.
list(APPEND PATCH_FILES luasocket-enforce-cloexec.patch)
list(APPEND PATCH_FILES enforce-cloexec.patch)
endif()

# Rename modules:
# `mime.core` → `mime.mcore`
# `socket.core` → `socket.score`
list(APPEND PATCH_CMD COMMAND sh -c "\"\$@\" src/*" --
${ISED} " "
-e "s|socket\\.core|socket.score|g"
-e "s|socket_core|socket_score|g"
-e "s|mime\\.core|mime.mcore|g"
-e "s|mime_core|mime_mcore|g"
-e "s|SOCKET_CDIR)/core|SOCKET_CDIR)/score|"
-e "s|MIME_CDIR)/core|MIME_CDIR)/mcore|"
)
list(APPEND CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE})

# Don't enforce CFLAGS.
list(APPEND PATCH_CMD COMMAND ${ISED} "s|-O2 -ggdb3 ||g" src/makefile)

if(APPLE)
set(PLAT macosx)
elseif(WIN32)
set(PLAT mingw)
else()
set(PLAT linux)
endif()

string(JOIN " " MYLDFLAGS ${LDFLAGS} ${LUAJIT_LIB})

list(APPEND BUILD_CMD COMMAND
make
"CC=${CC} ${CFLAGS}"
"LD=${CC} ${CFLAGS}"
LUAINC=${LUAJIT_INC}
MYLDFLAGS=${MYLDFLAGS}
PLAT=${PLAT}
SOCKET_SO=score.so MIME_SO=mcore.so
)
list(APPEND BUILD_CMD COMMAND ninja)

append_install_commands(
INSTALL_CMD
src/ltn12.lua src/mime.lua src/socket.lua
${SOURCE_DIR}/src/ltn12.lua
${SOURCE_DIR}/src/mime.lua
${SOURCE_DIR}/src/socket.lua
DESTINATION common
)
append_install_commands(
INSTALL_CMD
src/ftp.lua src/headers.lua src/http.lua
src/smtp.lua src/tp.lua src/url.lua
${SOURCE_DIR}/src/ftp.lua
${SOURCE_DIR}/src/headers.lua
${SOURCE_DIR}/src/http.lua
${SOURCE_DIR}/src/smtp.lua
${SOURCE_DIR}/src/tp.lua
${SOURCE_DIR}/src/url.lua
DESTINATION common/socket
)
append_binary_install_command(INSTALL_CMD src/score.so DESTINATION common/socket)
append_binary_install_command(INSTALL_CMD src/mcore.so DESTINATION common/mime)
append_install_commands(INSTALL_CMD DESTINATION ${STAGING_DIR}/include/luasocket
${SOURCE_DIR}/src/buffer.h
${SOURCE_DIR}/src/compat.h
${SOURCE_DIR}/src/io.h
${SOURCE_DIR}/src/luasocket.h
${SOURCE_DIR}/src/socket.h
${SOURCE_DIR}/src/timeout.h
${SOURCE_DIR}/src/usocket.h
)
append_install_commands(INSTALL_CMD libluasocket.a DESTINATION ${STAGING_DIR}/lib)
append_binary_install_command(INSTALL_CMD score.so DESTINATION common/socket)
append_binary_install_command(INSTALL_CMD mcore.so DESTINATION common/mime)

external_project(
DOWNLOAD GIT e3ca4a767a68d127df548d82669aba3689bd84f4
https://github.com/lunarmodules/luasocket
PATCH_OVERLAY overlay
PATCH_FILES ${PATCH_FILES}
PATCH_COMMAND ${PATCH_CMD}
CMAKE_ARGS ${CMAKE_ARGS}
BUILD_COMMAND ${BUILD_CMD}
INSTALL_COMMAND ${INSTALL_CMD}
)
File renamed without changes.
75 changes: 75 additions & 0 deletions thirdparty/luasocket/export-api-for-luasec.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
diff --git a/src/usocket.c b/src/usocket.c
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -18,11 +18,7 @@
* Wait for readable/writable/connected socket with timeout
\*-------------------------------------------------------------------------*/
#ifndef SOCKET_SELECT
-#include <sys/poll.h>

-#define WAITFD_R POLLIN
-#define WAITFD_W POLLOUT
-#define WAITFD_C (POLLIN|POLLOUT)
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int ret;
struct pollfd pfd;
@@ -41,10 +37,6 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
}
#else

-#define WAITFD_R 1
-#define WAITFD_W 2
-#define WAITFD_C (WAITFD_R|WAITFD_W)
-
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int ret;
fd_set rfds, wfds, *rp, *wp;
diff --git a/src/usocket.h b/src/usocket.h
--- a/src/usocket.h
+++ b/src/usocket.h
@@ -56,4 +56,15 @@ typedef struct sockaddr_storage t_sockaddr_storage;

#define SOCKET_INVALID (-1)

+#ifndef SOCKET_SELECT
+#include <sys/poll.h>
+#define WAITFD_R POLLIN
+#define WAITFD_W POLLOUT
+#define WAITFD_C (POLLIN|POLLOUT)
+#else
+#define WAITFD_R 1
+#define WAITFD_W 2
+#define WAITFD_C (WAITFD_R|WAITFD_W)
+#endif
+
#endif /* USOCKET_H */
diff --git a/src/wsocket.c b/src/wsocket.c
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -42,11 +42,6 @@ int socket_close(void) {
/*-------------------------------------------------------------------------*\
* Wait for readable/writable/connected socket with timeout
\*-------------------------------------------------------------------------*/
-#define WAITFD_R 1
-#define WAITFD_W 2
-#define WAITFD_E 4
-#define WAITFD_C (WAITFD_E|WAITFD_W)
-
int socket_waitfd(p_socket ps, int sw, p_timeout tm) {
int ret;
fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL;
diff --git a/src/wsocket.h b/src/wsocket.h
--- a/src/wsocket.h
+++ b/src/wsocket.h
@@ -16,6 +16,11 @@ typedef SOCKADDR_STORAGE t_sockaddr_storage;
typedef SOCKET t_socket;
typedef t_socket *p_socket;

+#define WAITFD_R 1
+#define WAITFD_W 2
+#define WAITFD_E 4
+#define WAITFD_C (WAITFD_E|WAITFD_W)
+
#ifndef IPV6_V6ONLY
#define IPV6_V6ONLY 27
#endif
13 changes: 13 additions & 0 deletions thirdparty/luasocket/luajit-compat.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/src/compat.h b/src/compat.h
--- a/src/compat.h
+++ b/src/compat.h
@@ -1,3 +1,4 @@
+#if 0 // not necessary for luajit
#ifndef COMPAT_H
#define COMPAT_H

@@ -20,3 +21,4 @@ void *luasocket_testudata ( lua_State *L, int arg, const char *tname);
#endif

#endif
+#endif
70 changes: 70 additions & 0 deletions thirdparty/luasocket/namespace.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
diff --git a/src/luasocket.c b/src/luasocket.c
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -96,7 +96,7 @@ static int base_open(lua_State *L) {
/*-------------------------------------------------------------------------*\
* Initializes all library modules.
\*-------------------------------------------------------------------------*/
-LUASOCKET_API int luaopen_socket_core(lua_State *L) {
+LUASOCKET_API int luaopen_socket_score(lua_State *L) {
int i;
base_open(L);
for (i = 0; mod[i].name; i++) mod[i].func(L);
diff --git a/src/luasocket.h b/src/luasocket.h
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -31,6 +31,6 @@
/*-------------------------------------------------------------------------*\
* Initializes the library.
\*-------------------------------------------------------------------------*/
-LUASOCKET_API int luaopen_socket_core(lua_State *L);
+LUASOCKET_API int luaopen_socket_score(lua_State *L);

#endif /* LUASOCKET_H */
diff --git a/src/mime.c b/src/mime.c
--- a/src/mime.c
+++ b/src/mime.c
@@ -167,7 +167,7 @@ static const UC b64unbase[] = {
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
-LUASOCKET_API int luaopen_mime_core(lua_State *L)
+LUASOCKET_API int luaopen_mime_mcore(lua_State *L)
{
lua_newtable(L);
luaL_setfuncs(L, func, 0);
diff --git a/src/mime.h b/src/mime.h
--- a/src/mime.h
+++ b/src/mime.h
@@ -17,6 +17,6 @@
#define MIME_COPYRIGHT "Copyright (C) 2004-2013 Diego Nehab"
#define MIME_AUTHORS "Diego Nehab"

-LUASOCKET_API int luaopen_mime_core(lua_State *L);
+LUASOCKET_API int luaopen_mime_mcore(lua_State *L);

#endif /* MIME_H */
diff --git a/src/mime.lua b/src/mime.lua
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -9,7 +9,7 @@
-----------------------------------------------------------------------------
local base = _G
local ltn12 = require("ltn12")
-local mime = require("mime.core")
+local mime = require("mime.mcore")
local _M = mime

-- encode, decode and wrap algorithm tables
diff --git a/src/socket.lua b/src/socket.lua
--- a/src/socket.lua
+++ b/src/socket.lua
@@ -9,7 +9,7 @@
local base = _G
local string = require("string")
local math = require("math")
-local socket = require("socket.core")
+local socket = require("socket.score")

local _M = socket

38 changes: 38 additions & 0 deletions thirdparty/luasocket/overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.15)
project(luasocket LANGUAGES C)

find_package(PkgConfig REQUIRED)
pkg_check_modules(LuaJIT luajit REQUIRED IMPORTED_TARGET)

add_library(luasocket STATIC)
set_target_properties(luasocket PROPERTIES C_VISIBILITY_PRESET hidden)
target_sources(luasocket PRIVATE
src/auxiliar.c
src/buffer.c
src/compat.c
src/except.c
src/inet.c
src/io.c
src/options.c
src/select.c
src/tcp.c
src/timeout.c
src/udp.c
)
if(APPLE)
target_sources(luasocket PRIVATE src/usocket.c)
elseif(LINUX)
target_sources(luasocket PRIVATE src/usocket.c)
elseif(WIN32)
# TODO
message(FATAL_ERROR "Windows is not supported")
endif()
target_link_libraries(luasocket PRIVATE PkgConfig::LuaJIT)

foreach(MOD mcore score)
add_library(${MOD} MODULE)
set_target_properties(${MOD} PROPERTIES C_VISIBILITY_PRESET hidden PREFIX "")
target_link_libraries(${MOD} PRIVATE luasocket PkgConfig::LuaJIT)
endforeach()
target_sources(mcore PRIVATE src/mime.c)
target_sources(score PRIVATE src/luasocket.c)

0 comments on commit e65c7dd

Please sign in to comment.