<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Igor, thanks for the patch! See my comments inline.<br>
    </p>
    <div class="moz-cite-prefix">On 11.08.2022 14:17, Igor Munkin wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:1d05bbecc9c9ae57250ec563e202a8e103f86fa4.1660216002.git.imun@tarantool.org">
      <pre class="moz-quote-pre" wrap="">While extending test suites 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.

After the years of struggling MakeLuaPath.cmake module is introduced 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.

Signed-off-by: Igor Munkin <a class="moz-txt-link-rfc2396E" href="mailto:imun@tarantool.org"><imun@tarantool.org></a>
---
 cmake/MakeLuaPath.cmake                   | 46 +++++++++++++++++++++++
 test/CMakeLists.txt                       |  2 +
 test/PUC-Rio-Lua-5.1-tests/CMakeLists.txt |  8 +++-
 test/lua-Harness-tests/CMakeLists.txt     | 16 +++++---
 test/tarantool-tests/CMakeLists.txt       | 28 ++++++++------
 5 files changed, 81 insertions(+), 19 deletions(-)
 create mode 100644 cmake/MakeLuaPath.cmake

diff --git a/cmake/MakeLuaPath.cmake b/cmake/MakeLuaPath.cmake
new file mode 100644
index 00000000..9a5a3bb8
--- /dev/null
+++ b/cmake/MakeLuaPath.cmake
@@ -0,0 +1,46 @@
+# make_lua_path provides a convenient way to define LUA_PATH and
+# LUA_CPATH variables.
+#
+# Example usage:
+#
+#   make_lua_path(LUA_PATH
+#     PATH
+#       ${CMAKE_CURRENT_SOURCE_DIR}/?.lua
+#       ${CMAKE_BINARY_DIR}/?.lua
+#       ./?.lua
+#   )
+#
+# This will give you the string:
+#    "./?.lua;${CMAKE_BINARY_DIR}/?.lua;${CMAKE_CURRENT_SOURCE_DIR}/?.lua;;"
+# XXX: Mind the reverse order of the entries in the result string.
+
+function(make_lua_path path)
+  set(prefix ARG)
+  set(noValues)
+  set(singleValues)
+  set(multiValues PATHS)
+
+  # FIXME: if we update to CMake >= 3.5, can remove this line.</pre>
    </blockquote>
    <p>I would replace comment with condition that will check CMake
      version</p>
    <p>and will fail when CMake >= 3.5:<br>
    </p>
    <pre style="margin-top: 0px; margin-right: 0px; margin-bottom: calc(var(--s-prose-spacing) + 0.4em); margin-left: 0px; padding: var(--su12); border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; line-height: var(--lh-md); font-family: var(--ff-mono); font-size: var(--fs-body1); vertical-align: baseline; box-sizing: inherit; width: auto; max-height: 600px; overflow: auto; background-color: var(--highlight-bg); border-radius: var(--br-md); overflow-wrap: normal; color: var(--highlight-color); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: var(--ff-mono); font-size: var(--fs-body1); vertical-align: baseline; box-sizing: inherit; background-color: transparent; white-space: inherit; color: var(--black-800); border-radius: 0px;">if (CMAKE_VERSION VERSION_GREATER_EQUAL 35)
</code></pre>
    <p>   message(FATAL_ERROR "Please remove snippet for CMake <
      3.5")</p>
    <p>endif()<br>
    </p>
    <br>
    <blockquote type="cite"
cite="mid:1d05bbecc9c9ae57250ec563e202a8e103f86fa4.1660216002.git.imun@tarantool.org">
      <pre class="moz-quote-pre" wrap="">
+  include(CMakeParseArguments)
+  cmake_parse_arguments(${prefix}
+                        "${noValues}"
+                        "${singleValues}"
+                        "${multiValues}"
+                        ${ARGN})
+
+  # XXX: This is the sentinel semicolon having special meaning
+  # for LUA_PATH and LUA_CPATH variables. For more info, see the
+  # link below:
+  # <a class="moz-txt-link-freetext" href="https://www.lua.org/manual/5.1/manual.html#pdf-LUA_PATH">https://www.lua.org/manual/5.1/manual.html#pdf-LUA_PATH</a>
+  set(result "\;")
+
+  foreach(inc ${ARG_PATHS})
+    # XXX: If one joins two strings with semicolon, the value
+    # automatically becomes a list. I found a single working
+    # solution to make result variable be a string via "escaping"
+    # the semicolon right in string interpolation.
+    set(result "${inc}\;${result}")
+  endforeach()
+
+  set(${path} "${result}" PARENT_SCOPE)
+endfunction()
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index ba25af54..a8262b12 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -3,6 +3,8 @@
 # See the rationale in the root CMakeLists.txt.
 cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
 
+include(MakeLuaPath)
+
 find_program(LUACHECK luacheck)
 if(LUACHECK)
   # XXX: The tweak below relates to luacheck problem with paths.
diff --git a/test/PUC-Rio-Lua-5.1-tests/CMakeLists.txt b/test/PUC-Rio-Lua-5.1-tests/CMakeLists.txt
index 8e825f55..b95e7852 100644
--- a/test/PUC-Rio-Lua-5.1-tests/CMakeLists.txt
+++ b/test/PUC-Rio-Lua-5.1-tests/CMakeLists.txt
@@ -14,7 +14,11 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
 # <a class="moz-txt-link-freetext" href="https://github.com/tarantool/tarantool/issues/5744">https://github.com/tarantool/tarantool/issues/5744</a>
 # Hence, there is no way other than set LUA_PATH environment
 # variable as proposed in the first case.
-set(LUA_PATH "?\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua")
+make_lua_path(LUA_PATH
+  PATHS
+    ${CMAKE_CURRENT_SOURCE_DIR}/?.lua
+    "?"
+)
 
 # Establish PUC-Rio-Lua-5.1-tests-prepare target that contains
 # rules for <libs/*> libraries compilation and creates <libs/P1>
@@ -38,7 +42,7 @@ add_custom_command(TARGET PUC-Rio-Lua-5.1-tests
   COMMENT "Running PUC-Rio Lua 5.1 tests"
   COMMAND
   env
-    LUA_PATH="${LUA_PATH}\;\;"
+    LUA_PATH="${LUA_PATH}"
     ${LUAJIT_TEST_COMMAND} ${CMAKE_CURRENT_SOURCE_DIR}/all.lua
   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
 )
diff --git a/test/lua-Harness-tests/CMakeLists.txt b/test/lua-Harness-tests/CMakeLists.txt
index 12476171..a57104a5 100644
--- a/test/lua-Harness-tests/CMakeLists.txt
+++ b/test/lua-Harness-tests/CMakeLists.txt
@@ -10,10 +10,16 @@ if(NOT PROVE)
   return()
 endif()
 
-# Tests create temporary files (see 303-package.t and 411-luajit.t for
-# example) to require. Also, they require some files from original test 
-# source directory.
-set(LUA_PATH "./?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;${LUAJIT_SOURCE_DIR}/?.lua\;${LUAJIT_BINARY_DIR}/?.lua")
+# Tests create temporary files (see 303-package.t and 411-luajit.t
+# for example) to be required. Also, they require some files from
+# the original test source directory.
+make_lua_path(LUA_PATH
+  PATHS
+    ${CMAKE_CURRENT_SOURCE_DIR}/?.lua
+    ${LUAJIT_BINARY_DIR}/?.lua
+    ${LUAJIT_SOURCE_DIR}/?.lua
+    ./?.lua
+)
 set(LUA_TEST_FLAGS --failures --shuffle)
 
 if(CMAKE_VERBOSE_MAKEFILE)
@@ -25,7 +31,7 @@ add_custom_command(TARGET lua-Harness-tests
   COMMENT "Running lua-Harness tests"
   COMMAND
   env
-    LUA_PATH="${LUA_PATH}\;"
+    LUA_PATH="${LUA_PATH}"
     ${PROVE} ${CMAKE_CURRENT_SOURCE_DIR}
       --exec '${LUAJIT_TEST_COMMAND} -l profile_luajit21'
       --jobs ${CMAKE_BUILD_PARALLEL_LEVEL}
diff --git a/test/tarantool-tests/CMakeLists.txt b/test/tarantool-tests/CMakeLists.txt
index ecda2e63..27866869 100644
--- a/test/tarantool-tests/CMakeLists.txt
+++ b/test/tarantool-tests/CMakeLists.txt
@@ -43,14 +43,11 @@ macro(BuildTestCLib lib sources)
   # semicolon. If one finds the normal way to make it work, feel
   # free to reach me.
   set(TESTLIBS "${lib};${TESTLIBS}" PARENT_SCOPE)
-  # Add the directory where the lib is built to the LUA_CPATH
-  # environment variable, so LuaJIT can find and load it.
-  # XXX: Here we see the other side of the coin. If one joins two
-  # strings with semicolon, the value automatically becomes a
-  # list. I found a single working solution to make LUA_CPATH be
-  # a string via "escaping" the semicolon right in string
-  # interpolation.
-  set(LUA_CPATH "${CMAKE_CURRENT_BINARY_DIR}/?${CMAKE_SHARED_LIBRARY_SUFFIX}\;${LUA_CPATH}" PARENT_SCOPE)
+  # Add the directory where the lib is built to the list with
+  # entries for LUA_CPATH environment variable, so LuaJIT can find
+  # and load it. See the comment about extending the list in the
+  # parent scope few lines above.
+  set(LUA_CPATHS "${CMAKE_CURRENT_BINARY_DIR}/?${CMAKE_SHARED_LIBRARY_SUFFIX};${LUA_CPATHS}" PARENT_SCOPE)
   # Also add this directory to LD_LIBRARY_PATH environment
   # variable, so FFI machinery can find and load it.
   set(LD_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}:${LD_LIBRARY_PATH}" PARENT_SCOPE)
@@ -76,14 +73,21 @@ add_subdirectory(misclib-sysprof-capi)
 # in src/ directory and auxiliary tests-related modules are
 # located in the current directory (but tests are run in the
 # binary directory), so LUA_PATH need to be updated.
-set(LUA_PATH
-  "${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;${PROJECT_SOURCE_DIR}/tools/?.lua\;${PROJECT_SOURCE_DIR}/src/?.lua"
+make_lua_path(LUA_PATH
+  PATHS
+    ${CMAKE_CURRENT_SOURCE_DIR}/?.lua
+    ${PROJECT_SOURCE_DIR}/tools/?.lua
+    ${PROJECT_SOURCE_DIR}/src/?.lua
 )
+# Update LUA_CPATH with the library paths collected within
+# <BuildTestLib> macro.
+make_lua_path(LUA_CPATH PATHS ${LUA_CPATHS})
+
 set(LUA_TEST_SUFFIX .test.lua)
 set(LUA_TEST_FLAGS --failures --shuffle)
 set(LUA_TEST_ENV
-  "LUA_PATH=\"${LUA_PATH}\;\;\""
-  "LUA_CPATH=\"${LUA_CPATH}\;\;\""
+  "LUA_PATH=\"${LUA_PATH}\""
+  "LUA_CPATH=\"${LUA_CPATH}\""
 )
 
 if(CMAKE_VERBOSE_MAKEFILE)
</pre>
    </blockquote>
  </body>
</html>