Skip to content

Commit 2c7e71e

Browse files
committed
cmake: introduce MakeLuaPath.cmake helper
While extending tests it is often required to append additional path where Lua or Lua C auxiliary modules are located to LUA_PATH or LUA_CPATH environment variables. Due to insane semicolon interpolation in CMake strings (that converts such string to a list as a result), we need to escape semicolon in LUA_PATH/LUA_CPATH strings while building the resulting value. The patch introduce MakeLuaPath.cmake module to make LUA_PATH and LUA_CPATH definition convenient with <lapi_tests_make_lua_path> helper. This function takes all paths given as a variable list argument, joins them in a reverse order by a semicolon and yields the resulting string to a specified CMake variable. Needed for the following commit.
1 parent 5fd07ec commit 2c7e71e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

cmake/MakeLuaPath.cmake

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# lapi_tests_make_lua_path provides a convenient way to define
2+
# LUA_PATH and LUA_CPATH variables.
3+
#
4+
# Example usage:
5+
#
6+
# lapi_tests_make_lua_path(LUA_PATH
7+
# PATH
8+
# ./?.lua
9+
# ${CMAKE_BINARY_DIR}/?.lua
10+
# ${CMAKE_CURRENT_SOURCE_DIR}/?.lua
11+
# )
12+
#
13+
# This will give you the string:
14+
# "./?.lua;${CMAKE_BINARY_DIR}/?.lua;${CMAKE_CURRENT_SOURCE_DIR}/?.lua;;"
15+
#
16+
# Source: https://github.com/tarantool/luajit/blob/441405c398d625fda304b16bb10f119bfd822696/cmake/MakeLuaPath.cmake
17+
18+
function(lapi_tests_make_lua_path path)
19+
set(prefix ARG)
20+
set(noValues)
21+
set(singleValues)
22+
set(multiValues PATHS)
23+
24+
include(CMakeParseArguments)
25+
cmake_parse_arguments(${prefix}
26+
"${noValues}"
27+
"${singleValues}"
28+
"${multiValues}"
29+
${ARGN})
30+
31+
set(_MAKE_LUA_PATH_RESULT "")
32+
33+
foreach(inc ${ARG_PATHS})
34+
# XXX: If one joins two strings with the semicolon, the value
35+
# automatically becomes a list. I found a single working
36+
# solution to make result variable be a string via "escaping"
37+
# the semicolon right in string interpolation.
38+
set(_MAKE_LUA_PATH_RESULT "${_MAKE_LUA_PATH_RESULT}${inc}\;")
39+
endforeach()
40+
41+
if("${_MAKE_LUA_PATH_RESULT}" STREQUAL "")
42+
message(FATAL_ERROR
43+
"No paths are given to <lapi_tests_make_lua_path> helper.")
44+
endif()
45+
46+
# XXX: This is the sentinel semicolon having special meaning
47+
# for LUA_PATH and LUA_CPATH variables. For more info, see the
48+
# link below:
49+
# https://www.lua.org/manual/5.1/manual.html#pdf-LUA_PATH
50+
set(${path} "${_MAKE_LUA_PATH_RESULT}\;" PARENT_SCOPE)
51+
# XXX: Unset the internal variable to not spoil CMake cache.
52+
# Study the case in CheckIPOSupported.cmake, that affected this
53+
# module: https://gitlab.kitware.com/cmake/cmake/-/commit/4b82977
54+
unset(_MAKE_LUA_PATH_RESULT)
55+
endfunction()

0 commit comments

Comments
 (0)