file-GET_RUNTIME_DEPENDENCIES-linux.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. enable_language(C)
  2. cmake_policy(SET CMP0095 NEW)
  3. set(test_rpath_names
  4. preexcluded
  5. rpath_postexcluded
  6. rpath
  7. rpath_parent_postexcluded
  8. rpath_parent
  9. rpath_origin_postexcluded
  10. rpath_origin
  11. rpath_search_postexcluded
  12. rpath_search
  13. rpath_unresolved
  14. conflict
  15. )
  16. set(test_runpath_names
  17. runpath_postexcluded
  18. runpath
  19. runpath_origin_postexcluded
  20. runpath_origin
  21. runpath_parent_unresolved
  22. runpath_search_postexcluded
  23. runpath_search
  24. runpath_unresolved
  25. )
  26. file(REMOVE "${CMAKE_BINARY_DIR}/test_rpath.c")
  27. add_library(test_rpath SHARED "${CMAKE_BINARY_DIR}/test_rpath.c")
  28. foreach(name ${test_rpath_names})
  29. file(WRITE "${CMAKE_BINARY_DIR}/${name}.c" "void ${name}(void) {}\n")
  30. add_library(${name} SHARED "${CMAKE_BINARY_DIR}/${name}.c")
  31. file(APPEND "${CMAKE_BINARY_DIR}/test_rpath.c" "extern void ${name}(void);\n")
  32. endforeach()
  33. file(APPEND "${CMAKE_BINARY_DIR}/test_rpath.c" "void test_rpath(void)\n{\n")
  34. foreach(name ${test_rpath_names})
  35. file(APPEND "${CMAKE_BINARY_DIR}/test_rpath.c" " ${name}();\n")
  36. endforeach()
  37. file(APPEND "${CMAKE_BINARY_DIR}/test_rpath.c" "}\n")
  38. install(TARGETS rpath_postexcluded DESTINATION lib/rpath_postexcluded)
  39. install(TARGETS rpath DESTINATION lib/rpath)
  40. install(TARGETS rpath_origin_postexcluded DESTINATION lib/rpath_origin_postexcluded)
  41. install(TARGETS rpath_origin DESTINATION lib/rpath_origin)
  42. install(TARGETS rpath_parent_postexcluded DESTINATION lib/rpath_parent_postexcluded)
  43. install(TARGETS rpath rpath_origin rpath_parent DESTINATION lib/rpath_parent)
  44. install(TARGETS rpath_search_postexcluded DESTINATION lib/rpath_search_postexcluded)
  45. install(TARGETS rpath rpath_origin rpath_parent rpath_search DESTINATION lib/rpath_search)
  46. install(TARGETS conflict DESTINATION lib/conflict)
  47. target_link_libraries(test_rpath PRIVATE ${test_rpath_names})
  48. set_property(TARGET test_rpath PROPERTY INSTALL_RPATH
  49. "${CMAKE_BINARY_DIR}/root-all/lib/rpath_postexcluded"
  50. "${CMAKE_BINARY_DIR}/root-all/lib/rpath"
  51. "\$ORIGIN/rpath_origin_postexcluded"
  52. "\${ORIGIN}/rpath_origin"
  53. "${CMAKE_BINARY_DIR}/root-all/lib/conflict"
  54. )
  55. target_link_options(test_rpath PRIVATE -Wl,--disable-new-dtags)
  56. file(REMOVE "${CMAKE_BINARY_DIR}/test_runpath.c")
  57. add_library(test_runpath SHARED "${CMAKE_BINARY_DIR}/test_runpath.c")
  58. foreach(name ${test_runpath_names} rpath conflict)
  59. file(WRITE "${CMAKE_BINARY_DIR}/${name}.c" "void ${name}(void) {}\n")
  60. if(NOT name MATCHES "^(rpath|conflict)$")
  61. add_library(${name} SHARED "${CMAKE_BINARY_DIR}/${name}.c")
  62. endif()
  63. file(APPEND "${CMAKE_BINARY_DIR}/test_runpath.c" "extern void ${name}(void);\n")
  64. endforeach()
  65. file(APPEND "${CMAKE_BINARY_DIR}/test_runpath.c" "void test_runpath(void)\n{\n")
  66. foreach(name ${test_runpath_names} rpath conflict)
  67. file(APPEND "${CMAKE_BINARY_DIR}/test_runpath.c" " ${name}();\n")
  68. endforeach()
  69. file(APPEND "${CMAKE_BINARY_DIR}/test_runpath.c" "}\n")
  70. install(TARGETS runpath_postexcluded DESTINATION lib/runpath_postexcluded)
  71. install(TARGETS runpath DESTINATION lib/runpath)
  72. install(TARGETS runpath_origin_postexcluded DESTINATION lib/runpath_origin_postexcluded)
  73. install(TARGETS runpath_origin DESTINATION lib/runpath_origin)
  74. install(TARGETS runpath_parent_unresolved DESTINATION lib/runpath_parent_unresolved)
  75. install(TARGETS runpath_search_postexcluded DESTINATION lib/runpath_search_postexcluded)
  76. install(TARGETS runpath runpath_origin runpath_search DESTINATION lib/runpath_search)
  77. install(TARGETS conflict DESTINATION lib/conflict2)
  78. target_link_libraries(test_runpath PRIVATE ${test_runpath_names} rpath conflict)
  79. set_property(TARGET test_runpath PROPERTY INSTALL_RPATH
  80. "${CMAKE_BINARY_DIR}/root-all/lib/runpath/../rpath" # Ensure that files that don't conflict are treated correctly
  81. "${CMAKE_BINARY_DIR}/root-all/lib/runpath_postexcluded"
  82. "${CMAKE_BINARY_DIR}/root-all/lib/runpath"
  83. "\${ORIGIN}/runpath_origin_postexcluded"
  84. "\$ORIGIN/runpath_origin"
  85. "${CMAKE_BINARY_DIR}/root-all/lib/conflict2"
  86. )
  87. target_link_options(test_runpath PRIVATE -Wl,--enable-new-dtags)
  88. set_property(TARGET test_rpath ${test_rpath_names} test_runpath ${test_runpath_names} PROPERTY LIBRARY_OUTPUT_DIRECTORY lib)
  89. install(TARGETS test_rpath test_runpath DESTINATION lib)
  90. add_executable(topexe file-GET_RUNTIME_DEPENDENCIES-linux/topexe.c)
  91. add_library(toplib SHARED file-GET_RUNTIME_DEPENDENCIES-linux/toplib.c)
  92. add_library(topmod MODULE file-GET_RUNTIME_DEPENDENCIES-linux/toplib.c)
  93. target_link_libraries(topexe PRIVATE test_rpath test_runpath)
  94. target_link_libraries(toplib PRIVATE test_rpath test_runpath)
  95. target_link_libraries(topmod PRIVATE test_rpath test_runpath)
  96. set_property(TARGET topexe toplib topmod PROPERTY INSTALL_RPATH
  97. "${CMAKE_BINARY_DIR}/root-all/lib"
  98. "${CMAKE_BINARY_DIR}/root-all/lib/rpath_parent_postexcluded"
  99. "${CMAKE_BINARY_DIR}/root-all/lib/rpath_parent"
  100. "${CMAKE_BINARY_DIR}/root-all/lib/runpath_parent_unresolved"
  101. )
  102. target_link_options(topexe PRIVATE -Wl,--disable-new-dtags)
  103. target_link_options(toplib PRIVATE -Wl,--disable-new-dtags)
  104. target_link_options(topmod PRIVATE -Wl,--disable-new-dtags)
  105. install(TARGETS topexe toplib RUNTIME DESTINATION bin LIBRARY DESTINATION lib)
  106. install(TARGETS topmod LIBRARY DESTINATION lib/modules)
  107. install(CODE [[
  108. function(exec_get_runtime_dependencies depsfile udepsfile cdepsfile)
  109. file(GET_RUNTIME_DEPENDENCIES
  110. RESOLVED_DEPENDENCIES_VAR deps
  111. UNRESOLVED_DEPENDENCIES_VAR udeps
  112. CONFLICTING_DEPENDENCIES_PREFIX cdeps
  113. PRE_INCLUDE_REGEXES
  114. "^lib(test_rpath|rpath_postexcluded|rpath|rpath_parent_postexcluded|rpath_parent|rpath_origin_postexcluded|rpath_origin|rpath_search_postexcluded|rpath_search|rpath_unresolved|test_runpath|runpath_postexcluded|runpath|runpath_origin_postexcluded|runpath_origin|runpath_parent_unresolved|runpath_search_postexcluded|runpath_search|runpath_unresolved|conflict)\\.so$"
  115. "^libc\\.so"
  116. PRE_EXCLUDE_REGEXES ".*"
  117. POST_INCLUDE_REGEXES "^.*/(libtest_rpath|rpath/librpath|rpath_parent/librpath_parent|rpath_search/librpath_search|libtest_runpath|runpath/librunpath|runpath_origin_postexcluded|runpath_origin|runpath_search/librunpath_search|conflict2?/libconflict)\\.so$"
  118. POST_EXCLUDE_REGEXES ".*"
  119. DIRECTORIES
  120. "${CMAKE_INSTALL_PREFIX}/lib/rpath_search_postexcluded"
  121. "${CMAKE_INSTALL_PREFIX}/lib/rpath_search"
  122. "${CMAKE_INSTALL_PREFIX}/lib/runpath_search_postexcluded"
  123. "${CMAKE_INSTALL_PREFIX}/lib/runpath_search"
  124. ${ARGN}
  125. )
  126. list(SORT deps)
  127. list(SORT udeps)
  128. list(SORT cdeps_FILENAMES)
  129. file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${depsfile}" "${deps}")
  130. file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${udepsfile}" "${udeps}")
  131. file(WRITE "${CMAKE_INSTALL_PREFIX}/deps/${cdepsfile}" "")
  132. foreach(cdep IN LISTS cdeps_FILENAMES)
  133. set(cdep_values ${cdeps_${cdep}})
  134. list(SORT cdep_values)
  135. file(APPEND "${CMAKE_INSTALL_PREFIX}/deps/${cdepsfile}" "${cdep}:${cdep_values}\n")
  136. endforeach()
  137. endfunction()
  138. exec_get_runtime_dependencies(
  139. deps1.txt udeps1.txt cdeps1.txt
  140. EXECUTABLES
  141. "${CMAKE_INSTALL_PREFIX}/bin/$<TARGET_FILE_NAME:topexe>"
  142. )
  143. exec_get_runtime_dependencies(
  144. deps2.txt udeps2.txt cdeps2.txt
  145. LIBRARIES
  146. "${CMAKE_INSTALL_PREFIX}/lib/$<TARGET_FILE_NAME:toplib>"
  147. )
  148. exec_get_runtime_dependencies(
  149. deps3.txt udeps3.txt cdeps3.txt
  150. MODULES
  151. "${CMAKE_INSTALL_PREFIX}/lib/modules/$<TARGET_FILE_NAME:topmod>"
  152. )
  153. ]])