1enable_language(C) 2 3set(test1_names rpath) 4set(test2_names rpath) 5 6file(WRITE "${CMAKE_BINARY_DIR}/rpath.c" "void rpath(void) {}\n") 7add_library(rpath SHARED "${CMAKE_BINARY_DIR}/rpath.c") 8set_property(TARGET rpath PROPERTY INSTALL_NAME_DIR @rpath) 9install(TARGETS rpath DESTINATION lib/rpath1) 10install(TARGETS rpath DESTINATION lib/rpath2) 11 12file(REMOVE "${CMAKE_BINARY_DIR}/test1.c") 13add_library(test1 SHARED "${CMAKE_BINARY_DIR}/test1.c") 14foreach(name ${test1_names}) 15 file(APPEND "${CMAKE_BINARY_DIR}/test1.c" "extern void ${name}(void);\n") 16endforeach() 17file(APPEND "${CMAKE_BINARY_DIR}/test1.c" "void test1(void)\n{\n") 18foreach(name ${test1_names}) 19 file(APPEND "${CMAKE_BINARY_DIR}/test1.c" " ${name}();\n") 20endforeach() 21file(APPEND "${CMAKE_BINARY_DIR}/test1.c" "}\n") 22 23target_link_libraries(test1 PRIVATE ${test1_names}) 24set_property(TARGET test1 PROPERTY INSTALL_RPATH 25 "${CMAKE_BINARY_DIR}/root-all/lib/rpath1" 26 ) 27 28file(REMOVE "${CMAKE_BINARY_DIR}/test2.c") 29add_library(test2 SHARED "${CMAKE_BINARY_DIR}/test2.c") 30foreach(name ${test2_names}) 31 file(APPEND "${CMAKE_BINARY_DIR}/test2.c" "extern void ${name}(void);\n") 32endforeach() 33file(APPEND "${CMAKE_BINARY_DIR}/test2.c" "void test2(void)\n{\n") 34foreach(name ${test2_names}) 35 file(APPEND "${CMAKE_BINARY_DIR}/test2.c" " ${name}();\n") 36endforeach() 37file(APPEND "${CMAKE_BINARY_DIR}/test2.c" "}\n") 38 39target_link_libraries(test2 PRIVATE ${test2_names}) 40set_property(TARGET test2 PROPERTY INSTALL_RPATH 41 "${CMAKE_BINARY_DIR}/root-all/lib/rpath2" 42 ) 43 44install(TARGETS test1 test2 DESTINATION lib) 45 46install(CODE [[ 47 file(GET_RUNTIME_DEPENDENCIES 48 LIBRARIES 49 "${CMAKE_INSTALL_PREFIX}/lib/$<TARGET_FILE_NAME:test1>" 50 "${CMAKE_INSTALL_PREFIX}/lib/$<TARGET_FILE_NAME:test2>" 51 PRE_INCLUDE_REGEXES "^@rpath/librpath\\.dylib$" 52 PRE_EXCLUDE_REGEXES ".*" 53 ) 54 message(FATAL_ERROR "This message should not be displayed") 55 ]]) 56