[Tarantool-patches] [PATCH luajit 5/7] test: shrink LUA_CPATH and {DY}LD_LIBRARY_PATH

Sergey Kaplun skaplun at tarantool.org
Mon Sep 23 10:18:51 MSK 2024


This patch sets the unique value of each of these variables for each of
the tests and only where they are needed. Also, it drops the comment
about SIP [1] tricks since it is obsolete after
29897567ee5ed57e961c730432c056a3dbaa8f09 ("test: stop using
utils.selfrun in tests").

[1]: https://support.apple.com/en-us/HT204899

Resolves tarantool/tarantool#9898
---
 test/tarantool-tests/CMakeLists.txt | 110 ++++++++++++++++------------
 1 file changed, 63 insertions(+), 47 deletions(-)

diff --git a/test/tarantool-tests/CMakeLists.txt b/test/tarantool-tests/CMakeLists.txt
index a1339100..6f1b1ae1 100644
--- a/test/tarantool-tests/CMakeLists.txt
+++ b/test/tarantool-tests/CMakeLists.txt
@@ -8,14 +8,13 @@ add_custom_target(tarantool-tests-libs
 macro(BuildTestCLib lib sources)
   AddTestLib(${lib} ${sources})
   add_dependencies(tarantool-tests-libs ${lib})
-  # 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)
+  # Add libraries to the list to be proceeded with after test
+  # targets are created.
+  # XXX: Workaround for profilers c-related tests, see the comment
+  # below.
+  if(NOT "${CMAKE_CURRENT_BINARY_DIR}" MATCHES profilers)
+    set(CLIB_DIRS "${CMAKE_CURRENT_BINARY_DIR};${CLIB_DIRS}" PARENT_SCOPE)
+  endif()
 endmacro()
 
 # FIXME: This is used only due to ancient CMake requirements.
@@ -76,46 +75,8 @@ make_lua_path(LUA_PATH
     ${LUAJIT_BINARY_DIR}/?.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)
 
-# XXX: Since the auxiliary libraries are built as a dynamically
-# loaded modules on MacOS instead of shared libraries as it is
-# done on Linux and BSD, another environment variable should be
-# used to guide <ffi.load> while searching the extension.
-# XXX: Be noticed that we shouldn't use `"` here to wrap
-# the variable's content. If we do this, the variable value will
-# contain `"` at the beginning and the end, so this `"` at the
-# beginning will be treated as the directory for the first entry
-# (the last subdirectory added).
-if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
-  # XXX: Apple tries their best to "protect their users from
-  # malware". As a result SIP (see the link[1] below) has been
-  # designed and released. Now, Apple developers are so protected,
-  # that they can load nothing being not installed in the system,
-  # since some programs sanitize the environment before they start
-  # child processes. Specifically, environment variables starting
-  # with DYLD_ and LD_ are unset for child process started by
-  # other programs (like `ctest` using for launching tests).
-  # For more info, see the docs[2] below.
-  #
-  # These environment variables are used by FFI machinery to find
-  # the proper shared library, hence we can still tweak testing
-  # environment before calling <ffi.load>. However, the value
-  # can't be passed via the standard environment variable, so we
-  # use ENVIRONMENT property in `set_tests_properties` to get
-  # around SIP magic tricks.
-  #
-  # [1]: https://support.apple.com/en-us/HT204899
-  # [2]: https://developer.apple.com/library/archive/documentation/Security/Conceptual/System_Integrity_Protection_Guide/RuntimeProtections/RuntimeProtections.html
-  list(APPEND LUA_TEST_ENV_MORE DYLD_LIBRARY_PATH=${LD_LIBRARY_PATH})
-else()
-  list(APPEND LUA_TEST_ENV_MORE LD_LIBRARY_PATH=${LD_LIBRARY_PATH})
-endif()
-
 # Some tests use `LD_PRELOAD` to mock system calls (like
 # <lj-802-panic-at-mcode-protfail.test.lua> overwrites
 # `mprotect()`. When compiling with ASan support under GCC, it is
@@ -152,12 +113,67 @@ foreach(test_path ${tests})
     # LUA_CPATH and LD_LIBRARY_PATH variables and also
     # dependencies list with libraries are set in scope of
     # BuildTestLib macro.
-    ENVIRONMENT "LUA_PATH=${LUA_PATH};LUA_CPATH=${LUA_CPATH};${LUA_TEST_ENV_MORE}"
+    ENVIRONMENT "LUA_PATH=${LUA_PATH};${LUA_TEST_ENV_MORE}"
     LABELS ${TEST_SUITE_NAME}
     DEPENDS tarantool-tests-deps
   )
 endforeach()
 
+# We use the following naming convention: the name of the
+# directory containing the C library to be loaded should match the
+# prefix of the test itself. The same library may be used in
+# several tests. See <lj-1166-error-stitch*>, for example.
+foreach(clib_dir ${CLIB_DIRS})
+  get_filename_component(clib_dir_prefix ${clib_dir} NAME)
+  file(GLOB_RECURSE tests_using_clib
+    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
+    "${clib_dir_prefix}*${LUA_TEST_SUFFIX}"
+  )
+  foreach(test_name ${tests_using_clib})
+    set(test_title "test/${TEST_SUITE_NAME}/${test_name}")
+    # Add the directory where the library is built to the list
+    # with entries for the LUA_CPATH environment variable, so
+    # LuaJIT can find and load it.
+    AppendTestEnvVar(${test_title}
+      LUA_CPATH "${clib_dir}/?${CMAKE_SHARED_LIBRARY_SUFFIX}\;"
+    )
+    # Also, add this directory to the LD_LIBRARY_PATH environment
+    # variable so FFI machinery can find and load it.
+    # XXX: Be noticed that we shouldn't use `"` here to wrap the
+    # variable's content. If we do this, the variable value will
+    # contain `"` at the beginning and the end, so this `"` at the
+    # beginning will be treated as the directory for the entry.
+    # XXX: Since the auxiliary libraries are built as dynamically
+    # loaded modules on MacOS instead of shared libraries as it is
+    # done on Linux and BSD, another environment variable should
+    # be used to guide <ffi.load> while searching the extension.
+    if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
+      AppendTestEnvVar(${test_title} DYLD_LIBRARY_PATH ${clib_dir}:)
+    else()
+      AppendTestEnvVar(${test_title} LD_LIBRARY_PATH ${clib_dir}:)
+    endif()
+  endforeach()
+endforeach()
+
+# XXX: Special workaround for several different loaded libraries
+# in the <gh-5813-resolving-of-c-symbols.test.lua>. Since the libs
+# can't be flatterned to one directory (see the comment in their
+# <CMakeLists.txt>), just set them manually here.
+set(CSYMBOLS_DIR
+  ${CMAKE_CURRENT_BINARY_DIR}/profilers/gh-5813-resolving-of-c-symbols
+)
+make_lua_path(CSYMBOLS_CPATHS
+  PATHS
+    ${CSYMBOLS_DIR}/both/?${CMAKE_SHARED_LIBRARY_SUFFIX}
+    ${CSYMBOLS_DIR}/gnuhash/?${CMAKE_SHARED_LIBRARY_SUFFIX}
+    ${CSYMBOLS_DIR}/hash/?${CMAKE_SHARED_LIBRARY_SUFFIX}
+    ${CSYMBOLS_DIR}/stripped/?${CMAKE_SHARED_LIBRARY_SUFFIX}
+)
+AppendTestEnvVar(
+  test/${TEST_SUITE_NAME}/profilers/gh-5813-resolving-of-c-symbols.test.lua
+  LUA_CPATH "${CSYMBOLS_CPATHS}"
+)
+
 # The part of the profilers toolchain is located in the <tools/>
 # directory, so LUA_PATH needs to be updated.
 file(GLOB_RECURSE profilers_tests
-- 
2.46.0



More information about the Tarantool-patches mailing list