cmBinUtilsMacOSMachOLinker.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmBinUtilsMacOSMachOLinker.h"
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. #include <cm/memory>
  8. #include "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h"
  9. #include "cmRuntimeDependencyArchive.h"
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. namespace {
  13. bool IsMissingSystemDylib(std::string const& path)
  14. {
  15. // Starting on macOS 11, the dynamic loader has a builtin cache of
  16. // system-provided dylib files that do not exist on the filesystem.
  17. // Tell our caller that these are expected to be missing.
  18. return ((cmHasLiteralPrefix(path, "/System/Library/") ||
  19. cmHasLiteralPrefix(path, "/usr/lib/")) &&
  20. !cmSystemTools::PathExists(path));
  21. }
  22. }
  23. cmBinUtilsMacOSMachOLinker::cmBinUtilsMacOSMachOLinker(
  24. cmRuntimeDependencyArchive* archive)
  25. : cmBinUtilsLinker(archive)
  26. {
  27. }
  28. bool cmBinUtilsMacOSMachOLinker::Prepare()
  29. {
  30. std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
  31. if (tool.empty()) {
  32. tool = "otool";
  33. }
  34. if (tool == "otool") {
  35. this->Tool =
  36. cm::make_unique<cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool>(
  37. this->Archive);
  38. } else {
  39. std::ostringstream e;
  40. e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
  41. this->SetError(e.str());
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
  47. std::string const& file, cmStateEnums::TargetType type)
  48. {
  49. std::string executableFile;
  50. if (type == cmStateEnums::EXECUTABLE) {
  51. executableFile = file;
  52. } else {
  53. executableFile = this->Archive->GetBundleExecutable();
  54. }
  55. std::string executablePath;
  56. if (!executableFile.empty()) {
  57. executablePath = cmSystemTools::GetFilenamePath(executableFile);
  58. }
  59. return this->ScanDependencies(file, executablePath);
  60. }
  61. bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
  62. std::string const& file, std::string const& executablePath)
  63. {
  64. std::vector<std::string> libs;
  65. std::vector<std::string> rpaths;
  66. if (!this->Tool->GetFileInfo(file, libs, rpaths)) {
  67. return false;
  68. }
  69. std::string loaderPath = cmSystemTools::GetFilenamePath(file);
  70. return this->GetFileDependencies(libs, executablePath, loaderPath, rpaths);
  71. }
  72. bool cmBinUtilsMacOSMachOLinker::GetFileDependencies(
  73. std::vector<std::string> const& names, std::string const& executablePath,
  74. std::string const& loaderPath, std::vector<std::string> const& rpaths)
  75. {
  76. for (std::string const& name : names) {
  77. if (!this->Archive->IsPreExcluded(name)) {
  78. std::string path;
  79. bool resolved;
  80. if (!this->ResolveDependency(name, executablePath, loaderPath, rpaths,
  81. path, resolved)) {
  82. return false;
  83. }
  84. if (resolved) {
  85. if (!this->Archive->IsPostExcluded(path) &&
  86. !IsMissingSystemDylib(path)) {
  87. auto filename = cmSystemTools::GetFilenameName(path);
  88. bool unique;
  89. this->Archive->AddResolvedPath(filename, path, unique);
  90. if (unique && !this->ScanDependencies(path, executablePath)) {
  91. return false;
  92. }
  93. }
  94. } else {
  95. this->Archive->AddUnresolvedPath(name);
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. bool cmBinUtilsMacOSMachOLinker::ResolveDependency(
  102. std::string const& name, std::string const& executablePath,
  103. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  104. std::string& path, bool& resolved)
  105. {
  106. resolved = false;
  107. if (cmHasLiteralPrefix(name, "@rpath/")) {
  108. if (!this->ResolveRPathDependency(name, executablePath, loaderPath, rpaths,
  109. path, resolved)) {
  110. return false;
  111. }
  112. } else if (cmHasLiteralPrefix(name, "@loader_path/")) {
  113. if (!this->ResolveLoaderPathDependency(name, loaderPath, path, resolved)) {
  114. return false;
  115. }
  116. } else if (cmHasLiteralPrefix(name, "@executable_path/")) {
  117. if (!this->ResolveExecutablePathDependency(name, executablePath, path,
  118. resolved)) {
  119. return false;
  120. }
  121. } else {
  122. resolved = true;
  123. path = name;
  124. }
  125. if (resolved && !cmSystemTools::FileIsFullPath(path)) {
  126. this->SetError("Resolved path is not absolute");
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
  132. std::string const& name, std::string const& executablePath,
  133. std::string& path, bool& resolved)
  134. {
  135. if (executablePath.empty()) {
  136. resolved = false;
  137. return true;
  138. }
  139. // 16 is == "@executable_path".length()
  140. path = name;
  141. path.replace(0, 16, executablePath);
  142. if (!cmSystemTools::PathExists(path)) {
  143. resolved = false;
  144. return true;
  145. }
  146. resolved = true;
  147. return true;
  148. }
  149. bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
  150. std::string const& name, std::string const& loaderPath, std::string& path,
  151. bool& resolved)
  152. {
  153. if (loaderPath.empty()) {
  154. resolved = false;
  155. return true;
  156. }
  157. // 12 is "@loader_path".length();
  158. path = name;
  159. path.replace(0, 12, loaderPath);
  160. if (!cmSystemTools::PathExists(path)) {
  161. resolved = false;
  162. return true;
  163. }
  164. resolved = true;
  165. return true;
  166. }
  167. bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
  168. std::string const& name, std::string const& executablePath,
  169. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  170. std::string& path, bool& resolved)
  171. {
  172. for (std::string const& rpath : rpaths) {
  173. std::string searchFile = name;
  174. searchFile.replace(0, 6, rpath);
  175. if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
  176. if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
  177. resolved)) {
  178. return false;
  179. }
  180. if (resolved) {
  181. return true;
  182. }
  183. } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
  184. if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
  185. path, resolved)) {
  186. return false;
  187. }
  188. if (resolved) {
  189. return true;
  190. }
  191. } else if (cmSystemTools::PathExists(searchFile)) {
  192. /*
  193. * paraphrasing @ben.boeckel:
  194. * if /b/libB.dylib is supposed to be used,
  195. * /a/libbB.dylib will be found first if it exists. CMake tries to
  196. * sort rpath directories to avoid this, but sometimes there is no
  197. * right answer.
  198. *
  199. * I believe it is possible to resolve this using otools -l
  200. * then checking the LC_LOAD_DYLIB command whose name is
  201. * equal to the value of search_file, UNLESS the build
  202. * specifically sets the RPath to paths that will match
  203. * duplicate libs; at this point can we just point to
  204. * user error, or is there a reason why the advantages
  205. * to this scenario outweigh its disadvantages?
  206. *
  207. * Also priority seems to be the order as passed in when compiled
  208. * so as long as this method's resolution guarantees priority
  209. * in that manner further checking should not be necessary?
  210. */
  211. path = searchFile;
  212. resolved = true;
  213. return true;
  214. }
  215. }
  216. resolved = false;
  217. return true;
  218. }