cmBinUtilsMacOSMachOLinker.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. std::vector<std::string> libs;
  60. std::vector<std::string> rpaths;
  61. if (!this->Tool->GetFileInfo(file, libs, rpaths)) {
  62. return false;
  63. }
  64. return this->ScanDependencies(file, libs, rpaths, executablePath);
  65. }
  66. bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
  67. std::string const& file, std::vector<std::string> const& libs,
  68. std::vector<std::string> const& rpaths, std::string const& executablePath)
  69. {
  70. std::string loaderPath = cmSystemTools::GetFilenamePath(file);
  71. return this->GetFileDependencies(libs, executablePath, loaderPath, rpaths);
  72. }
  73. bool cmBinUtilsMacOSMachOLinker::GetFileDependencies(
  74. std::vector<std::string> const& names, std::string const& executablePath,
  75. std::string const& loaderPath, std::vector<std::string> const& rpaths)
  76. {
  77. for (std::string const& name : names) {
  78. if (!this->Archive->IsPreExcluded(name)) {
  79. std::string path;
  80. bool resolved;
  81. if (!this->ResolveDependency(name, executablePath, loaderPath, rpaths,
  82. path, resolved)) {
  83. return false;
  84. }
  85. if (resolved) {
  86. if (!this->Archive->IsPostExcluded(path) &&
  87. !IsMissingSystemDylib(path)) {
  88. auto filename = cmSystemTools::GetFilenameName(path);
  89. bool unique;
  90. std::vector<std::string> libs;
  91. std::vector<std::string> depRpaths;
  92. if (!this->Tool->GetFileInfo(path, libs, depRpaths)) {
  93. return false;
  94. }
  95. this->Archive->AddResolvedPath(filename, path, unique, depRpaths);
  96. if (unique &&
  97. !this->ScanDependencies(path, libs, depRpaths, executablePath)) {
  98. return false;
  99. }
  100. }
  101. } else {
  102. this->Archive->AddUnresolvedPath(name);
  103. }
  104. }
  105. }
  106. return true;
  107. }
  108. bool cmBinUtilsMacOSMachOLinker::ResolveDependency(
  109. std::string const& name, std::string const& executablePath,
  110. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  111. std::string& path, bool& resolved)
  112. {
  113. resolved = false;
  114. if (cmHasLiteralPrefix(name, "@rpath/")) {
  115. if (!this->ResolveRPathDependency(name, executablePath, loaderPath, rpaths,
  116. path, resolved)) {
  117. return false;
  118. }
  119. } else if (cmHasLiteralPrefix(name, "@loader_path/")) {
  120. if (!this->ResolveLoaderPathDependency(name, loaderPath, path, resolved)) {
  121. return false;
  122. }
  123. } else if (cmHasLiteralPrefix(name, "@executable_path/")) {
  124. if (!this->ResolveExecutablePathDependency(name, executablePath, path,
  125. resolved)) {
  126. return false;
  127. }
  128. } else {
  129. resolved = true;
  130. path = name;
  131. }
  132. if (resolved && !cmSystemTools::FileIsFullPath(path)) {
  133. this->SetError("Resolved path is not absolute");
  134. return false;
  135. }
  136. return true;
  137. }
  138. bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
  139. std::string const& name, std::string const& executablePath,
  140. std::string& path, bool& resolved)
  141. {
  142. if (executablePath.empty()) {
  143. resolved = false;
  144. return true;
  145. }
  146. // 16 is == "@executable_path".length()
  147. path = name;
  148. path.replace(0, 16, executablePath);
  149. if (!cmSystemTools::PathExists(path)) {
  150. resolved = false;
  151. return true;
  152. }
  153. resolved = true;
  154. return true;
  155. }
  156. bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
  157. std::string const& name, std::string const& loaderPath, std::string& path,
  158. bool& resolved)
  159. {
  160. if (loaderPath.empty()) {
  161. resolved = false;
  162. return true;
  163. }
  164. // 12 is "@loader_path".length();
  165. path = name;
  166. path.replace(0, 12, loaderPath);
  167. if (!cmSystemTools::PathExists(path)) {
  168. resolved = false;
  169. return true;
  170. }
  171. resolved = true;
  172. return true;
  173. }
  174. bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
  175. std::string const& name, std::string const& executablePath,
  176. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  177. std::string& path, bool& resolved)
  178. {
  179. for (std::string const& rpath : rpaths) {
  180. std::string searchFile = name;
  181. searchFile.replace(0, 6, rpath);
  182. if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
  183. if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
  184. resolved)) {
  185. return false;
  186. }
  187. if (resolved) {
  188. return true;
  189. }
  190. } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
  191. if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
  192. path, resolved)) {
  193. return false;
  194. }
  195. if (resolved) {
  196. return true;
  197. }
  198. } else if (cmSystemTools::PathExists(searchFile)) {
  199. /*
  200. * paraphrasing @ben.boeckel:
  201. * if /b/libB.dylib is supposed to be used,
  202. * /a/libbB.dylib will be found first if it exists. CMake tries to
  203. * sort rpath directories to avoid this, but sometimes there is no
  204. * right answer.
  205. *
  206. * I believe it is possible to resolve this using otools -l
  207. * then checking the LC_LOAD_DYLIB command whose name is
  208. * equal to the value of search_file, UNLESS the build
  209. * specifically sets the RPath to paths that will match
  210. * duplicate libs; at this point can we just point to
  211. * user error, or is there a reason why the advantages
  212. * to this scenario outweigh its disadvantages?
  213. *
  214. * Also priority seems to be the order as passed in when compiled
  215. * so as long as this method's resolution guarantees priority
  216. * in that manner further checking should not be necessary?
  217. */
  218. path = searchFile;
  219. resolved = true;
  220. return true;
  221. }
  222. }
  223. resolved = false;
  224. return true;
  225. }