如何在不禁用SIP的情况下在MacOS上使用CTest(未设置“ LC_PATH”)

时间:2019-06-10 10:38:30

标签: macos cmake intel-mkl ctest

我有使用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$ 

1 个答案:

答案 0 :(得分:0)

我通过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}")