file-GET_RUNTIME_DEPENDENCIES-linux.cmake 7.3 KB

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