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