cmBinUtilsMacOSMachOLinker.cxx 8.2 KB

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