【问题标题】:How to use CTest on MacOS without disabling SIP (no `LC_PATH` is set)如何在不禁用 SIP 的情况下在 MacOS 上使用 CTest(未设置“LC_PATH”)
【发布时间】:2019-06-10 10:38:30
【问题描述】:

我有使用 CTest 编写的单元测试,这些测试依赖于英特尔的 MKL BLAS 实现。我使用find_package(BLAS) 并将测试可执行文件链接到${BLAS_LIBRARIES}。测试可执行文件依赖DYLD_LIBRARY_PATH 来查找mkl 库,而不是嵌入的LC_RPATH

不幸的是,由于SIPDYLD_LIBRARY_PATH 不会传播到ctest 环境,因此当它尝试运行测试时,它无法链接mkl 库。

有没有办法让CMake 为构建目录中的测试可执行文件嵌入外部LC_PATH

这是一个基本的测试可执行文件 (test.cpp)

#include <mkl_cblas.h>

int main() {
  return &cblas_dgemm != nullptr;
}

这里是关联的CMakeLists.txt

cmake_minimum_required(VERSION 3.14.5)

project(blas LANGUAGES CXX)
include(CTest)

set(BLA_VENDOR Intel10_64lp_seq)
find_package(BLAS REQUIRED)

add_executable(test_blas test.cpp)
target_link_libraries(test_blas ${BLAS_LIBRARIES})

add_test(NAME test_direct COMMAND test_blas)

我找到MKL 并构建没有任何问题。

Lukes-MacBook:test ldalessa$ cmake .
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE  
-- Looking for sgemm_
-- Looking for sgemm_ - found
-- Found BLAS: /opt/intel/compilers_and_libraries_2019.4.233/mac/mkl/lib/libmkl_intel_lp64.dylib;/opt/intel/compilers_and_libraries_2019.4.233/mac/mkl/lib/libmkl_sequential.dylib;/opt/intel/compilers_and_libraries_2019.4.233/mac/mkl/lib/libmkl_core.dylib;-lm;-ldl  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ldalessa/test
Lukes-MacBook:test ldalessa$ make VERBOSE=1
/usr/local/Cellar/cmake/3.14.5/bin/cmake -S/Users/ldalessa/test -B/Users/ldalessa/test --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_progress_start /Users/ldalessa/test/CMakeFiles /Users/ldalessa/test/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/test_blas.dir/build.make CMakeFiles/test_blas.dir/depend
cd /Users/ldalessa/test && /usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ldalessa/test /Users/ldalessa/test /Users/ldalessa/test /Users/ldalessa/test /Users/ldalessa/test/CMakeFiles/test_blas.dir/DependInfo.cmake --color=
Dependee "/Users/ldalessa/test/CMakeFiles/test_blas.dir/DependInfo.cmake" is newer than depender "/Users/ldalessa/test/CMakeFiles/test_blas.dir/depend.internal".
Dependee "/Users/ldalessa/test/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/ldalessa/test/CMakeFiles/test_blas.dir/depend.internal".
Scanning dependencies of target test_blas
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/test_blas.dir/build.make CMakeFiles/test_blas.dir/build
[ 50%] Building CXX object CMakeFiles/test_blas.dir/test.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk   -o CMakeFiles/test_blas.dir/test.cpp.o -c /Users/ldalessa/test/test.cpp
[100%] Linking CXX executable test_blas
/usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_link_script CMakeFiles/test_blas.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/test_blas.dir/test.cpp.o  -o test_blas /opt/intel/compilers_and_libraries_2019.4.233/mac/mkl/lib/libmkl_intel_lp64.dylib /opt/intel/compilers_and_libraries_2019.4.233/mac/mkl/lib/libmkl_sequential.dylib /opt/intel/compilers_and_libraries_2019.4.233/mac/mkl/lib/libmkl_core.dylib -lm -ldl 
[100%] Built target test_blas
/usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_progress_start /Users/ldalessa/test/CMakeFiles 0
Lukes-MacBook:test ldalessa$ ./test_blas 
Lukes-MacBook:test ldalessa$ 

但是当我使用CTest 运行时,我遇到了SIP 问题。

ukes-MacBook:test ldalessa$ make test
Running tests...
Test project /Users/ldalessa/test
    Start 1: test_direct
1/1 Test #1: test_direct ......................Child aborted***Exception:   0.01 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.01 sec

The following tests FAILED:
      1 - test_direct (Child aborted)
Errors while running CTest
make: *** [test] Error 8
Lukes-MacBook:test ldalessa$ cat Testing/Temporary/LastTest.log 
Start testing: Jun 10 03:35 PDT
----------------------------------------------------------
1/1 Testing: test_direct
1/1 Test: test_direct
Command: "/Users/ldalessa/test/test_blas"
Directory: /Users/ldalessa/test
"test_direct" start time: Jun 10 03:35 PDT
Output:
----------------------------------------------------------
dyld: Library not loaded: @rpath/libmkl_intel_lp64.dylib
  Referenced from: /Users/ldalessa/test/test_blas
  Reason: image not found
<end of output>
Test time =   0.01 sec
----------------------------------------------------------
Test Failed.
"test_direct" end time: Jun 10 03:35 PDT
"test_direct" time elapsed: 00:00:00
----------------------------------------------------------

End testing: Jun 10 03:35 PDT
Lukes-MacBook:test ldalessa$ 

【问题讨论】:

  • 我仍然需要并回答这个问题,但我目前的临时解决方法是使用 Apple 的 Accelerate 框架而不是 MKL,它可以正确链接。问题是这是侵入性的,因为英特尔的 MKL 使用不同的标头(mkl_blas.hmkl_lapack.h)和符号名称,所以我必须有一些额外的配置时间和配置的标头废话来适应代码库。

标签: macos cmake intel-mkl ctest


【解决方案1】:

我通过 cmake 邮件列表收到了this solution。基本上解决方案是使用一些 cmake 魔法提取 mkl 库目录的路径,然后手动将该路径添加到 BUILD_RPATH。

list(GET BLAS_LIBRARIES 0 _BLAS_FIRSTLIB)
get_filename_component(_BLAS_LIBDIR "${_BLAS_FIRSTLIB}" DIRECTORY)
set_target_properties(test_blas PROPERTIES BUILD_RPATH "${_BLAS_LIBDIR}")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2011-08-02
    • 2012-10-05
    • 2011-03-12
    • 2011-09-12
    • 2012-04-23
    • 2020-06-27
    相关资源
    最近更新 更多