Skip to content

Commit 9be0f3b

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 <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 5fc378a commit 9be0f3b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

cmake/MakeLuaPath.cmake

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

0 commit comments

Comments
 (0)