Skip to content

Commit c98e665

Browse files
victorhoraFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Improvements on LUA build scripts and support for LUA 5.2
1 parent de36fca commit c98e665

File tree

3 files changed

+99
-16
lines changed

3 files changed

+99
-16
lines changed

CHANGES

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
v3.0.????? - ?
33
---------------------------
44

5+
- Improvements on LUA build scripts and support for LUA 5.2.
6+
[Issue #1617 and #1622 - @victorhora, @zimmerle]
57
- Fix compilation error with disable_debug_log flag
68
[0xfd84e - Izik Abramov]
7-
- Improvements on the benchmark tool.
9+
- Improvements on the benchmark tool.
810
[Issue #1615 - @zimmerle]
911
- Fix lua headers on the build scripts
1012
[Issue #1621 - @Minasu]

build/lua.m4

+92-14
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ AC_DEFUN([CHECK_LUA],
66
[dnl
77
88
# Possible names for the lua library/package (pkg-config)
9-
LUA_POSSIBLE_LIB_NAMES="lua"
9+
LUA_POSSIBLE_LIB_NAMES="lua lua53 lua5.3 lua52 lua5.2"
1010
1111
# Possible extensions for the library
1212
LUA_POSSIBLE_EXTENSIONS="so so0 la sl dll dylib so.0.0.0"
1313
1414
# Possible paths (if pkg-config was not found, proceed with the file lookup)
15-
LUA_POSSIBLE_PATHS="/usr/lib /usr/local/lib /usr/local/lua /usr/local/liblua /usr/local /opt /usr /usr/lib64 /opt/local"
15+
LUA_POSSIBLE_PATHS="/usr/lib /usr/local/lib /usr/local/lib64 /usr/local/lua /usr/local/liblua /usr/local /opt /usr /usr/lib64 /opt/local"
1616
1717
# Variables to be set by this very own script.
1818
LUA_CFLAGS=""
@@ -46,6 +46,32 @@ else
4646
break
4747
fi
4848
done
49+
if test -z "${LUA_CFLAGS}"; then
50+
#Trying to figure out the version using pkg-config...
51+
if test -n "${PKG_CONFIG}"; then
52+
LUA_PKG_NAME=""
53+
for x in ${LUA_POSSIBLE_LIB_NAMES}; do
54+
if ${PKG_CONFIG} --exists ${x}; then
55+
LUA_PKG_NAME="$x"
56+
LUA_PKG_VERSION="`${PKG_CONFIG} ${LUA_PKG_NAME} --modversion`"
57+
break
58+
fi
59+
done
60+
fi
61+
if test -n "${LUA_PKG_NAME}"; then
62+
# Package was found using the pkg-config scripts
63+
LUA_PKG_VERSION="`${PKG_CONFIG} ${LUA_PKG_NAME} --modversion`"
64+
LUA_CFLAGS="`${PKG_CONFIG} ${LUA_PKG_NAME} --cflags`"
65+
LUA_LDADD="`${PKG_CONFIG} ${LUA_PKG_NAME} --libs-only-l`"
66+
LUA_LDFLAGS="`${PKG_CONFIG} ${LUA_PKG_NAME} --libs-only-L --libs-only-other`"
67+
LUA_DISPLAY="${LUA_LDADD}, ${LUA_CFLAGS}"
68+
case $LUA_PKG_VERSION in
69+
(5.1*) LUA_CFLAGS="-DWITH_LUA_5_1 ${LUA_CFLAGS}" ; lua_5_1=1 ;;
70+
(5.2*) LUA_CFLAGS="-DWITH_LUA_5_2 ${LUA_CFLAGS}" ; lua_5_2=1 ;;
71+
esac
72+
AC_MSG_NOTICE([LUA pkg-config version: ${LUA_PKG_VERSION}])
73+
fi
74+
fi
4975
fi
5076
5177
@@ -58,13 +84,17 @@ if test -z "${LUA_CFLAGS}"; then
5884
LUA_FOUND=2
5985
fi
6086
else
61-
AC_MSG_ERROR([LUA was explicitly referenced but it was not found])
62-
LUA_FOUND=-1
87+
AC_MSG_ERROR([LUA was explicitly referenced but it was not found])
88+
LUA_FOUND=-1
6389
fi
6490
else
91+
if test "${lua_5_1}" = 1 && test "x${LUA_MANDATORY}" == "xyes" ; then
92+
AC_MSG_ERROR([LUA was explicitly referenced but LUA v5.1 was found and it is not currently supported on libModSecurity. LUA_VERSION: ${LUA_VERSION}])
93+
LUA_FOUND=-1
94+
fi
6595
if test -z "${LUA_MANDATORY}" || test "x${LUA_MANDATORY}" == "xno"; then
6696
LUA_FOUND=1
67-
AC_MSG_NOTICE([using LUA v${LUA_VERSION}])
97+
AC_MSG_NOTICE([using LUA ${LUA_LDADD}])
6898
LUA_CFLAGS="-DWITH_LUA ${LUA_CFLAGS}"
6999
LUA_DISPLAY="${LUA_LDADD} ${LUA_LDFLAGS}, ${LUA_CFLAGS}"
70100
AC_SUBST(LUA_LDFLAGS)
@@ -73,7 +103,7 @@ else
73103
AC_SUBST(LUA_DISPLAY)
74104
else
75105
LUA_FOUND=1
76-
AC_MSG_NOTICE([using LUA v${LUA_VERSION}])
106+
AC_MSG_NOTICE([using LUA ${LUA_LDADD}])
77107
LUA_CFLAGS="-DWITH_LUA ${LUA_CFLAGS}"
78108
LUA_DISPLAY="${LUA_LDADD} ${LUA_LDFLAGS}, ${LUA_CFLAGS}"
79109
AC_SUBST(LUA_LDFLAGS)
@@ -83,6 +113,10 @@ else
83113
fi
84114
fi
85115
116+
if test "${lua_5_1}" = 1 ; then
117+
AC_MSG_NOTICE([LUA 5.1 was found and it is not currently supported on libModSecurity. LUA_VERSION: ${LUA_VERSION}. LUA build disabled.])
118+
LUA_FOUND=2
119+
fi
86120
87121
AC_SUBST(LUA_FOUND)
88122
@@ -132,9 +166,15 @@ AC_DEFUN([CHECK_FOR_LUA_AT], [
132166
if test -e "${path}/include/lua.h"; then
133167
lua_inc_path="${path}/include"
134168
elif test -e "${path}/lua.h"; then
135-
lua_inc_path="${path}"
169+
lua_inc_path="${path}"
136170
elif test -e "${path}/include/lua/lua.h"; then
137-
lua_inc_path="${path}/include"
171+
lua_inc_path="${path}/include/lua"
172+
elif test -e "${path}/include/lua5.3/lua.h"; then
173+
lua_inc_path="${path}/include/lua5.3"
174+
LUA_VERSION=503
175+
elif test -e "${path}/include/lua5.2/lua.h"; then
176+
lua_inc_path="${path}/include/lua5.2"
177+
LUA_VERSION=502
138178
fi
139179
140180
if test -n "${lua_lib_path}"; then
@@ -144,14 +184,52 @@ AC_DEFUN([CHECK_FOR_LUA_AT], [
144184
if test -n "${lua_inc_path}"; then
145185
AC_MSG_NOTICE([LUA headers found at: ${lua_inc_path}])
146186
fi
147-
148187
if test -n "${lua_lib_path}" -a -n "${lua_inc_path}"; then
149-
# TODO: Compile a piece of code to check the version.
150-
LUA_CFLAGS="-I${lua_inc_path}"
151-
LUA_LDADD="-l${lua_lib_name}"
152-
LUA_LDFLAGS="-L${lua_lib_path}"
153-
LUA_DISPLAY="${lua_lib_file}, ${lua_inc_path}"
188+
LUA_CFLAGS="-I${lua_inc_path}"
189+
LUA_LDADD="-l${lua_lib_name}"
190+
LUA_LDFLAGS="-L${lua_lib_path}"
191+
LUA_DISPLAY="${lua_lib_file}, ${lua_inc_path}"
192+
193+
# Double checking version from lua.h...
194+
AC_TRY_COMPILE([ #include <lua.h>> ],
195+
[ #if (LUA_VERSION_NUM < 502)
196+
return 0;
197+
#else
198+
#error Lua 5.1 not detected
199+
#endif ],
200+
[ LUA_VERSION=501 ], [ lua_5_1=0 ]
201+
)
202+
203+
AC_TRY_COMPILE([ #include <lua.h> ],
204+
[ #if (LUA_VERSION_NUM == 502)
205+
return 0;
206+
#else
207+
#error Lua 5.2 not detected
208+
#endif ],
209+
[ LUA_VERSION=502 ], [ lua_5_2=0 ]
210+
)
211+
212+
if test -z "${LUA_VERSION}" ; then
213+
# As a last resort, try to find LUA version from $lua_inc_path
214+
while read -r line
215+
do
216+
case "$line" in
217+
(\#define\ LUA_VERSION_NUM*501*) LUA_VERSION=501 ;;
218+
(\#define\ LUA_VERSION_NUM*502*) LUA_VERSION=501 ;;
219+
(\#define\ LUA_VERSION_NUM*503*) LUA_VERSION=503
220+
esac
221+
done <"${lua_inc_path}/lua.h"
222+
AC_MSG_NOTICE([LUA_VERSION is ${LUA_VERSION} found at: ${lua_inc_path}])
223+
else
224+
AC_MSG_NOTICE([LUA version from includes: ${LUA_VERSION}])
225+
fi
226+
227+
case $LUA_VERSION in
228+
(501) LUA_CFLAGS="-DWITH_LUA_5_1 ${LUA_CFLAGS}" ; lua_5_1=1 ;;
229+
(502) LUA_CFLAGS="-DWITH_LUA_5_2 ${LUA_CFLAGS}" ; lua_5_2=1 ;;
230+
esac
154231
fi
232+
155233
]) # AC_DEFUN [CHECK_FOR_LUA_AT]
156234

157235

src/engine/lua.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ bool Lua::load(std::string script, std::string *err) {
8383
return false;
8484
}
8585

86+
#ifdef WITH_LUA_5_2
87+
if (lua_dump(L, Lua::blob_keeper, reinterpret_cast<void *>(&m_blob))) {
88+
#else
8689
if (lua_dump(L, Lua::blob_keeper, reinterpret_cast<void *>(&m_blob), 0)) {
90+
#endif
8791
const char *luaerr = lua_tostring(L, -1);
8892
err->assign("Failed to compile script '" + script + "");
8993
if (luaerr) {
@@ -95,7 +99,6 @@ bool Lua::load(std::string script, std::string *err) {
9599
return false;
96100
}
97101

98-
99102
lua_close(L);
100103
return true;
101104
#else

0 commit comments

Comments
 (0)