cmBinUtilsMacOSMachOLinker.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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. -> FileInfo const*
  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. FileInfo const* file_info = this->GetFileInfo(file);
  78. if (!file_info) {
  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. FileInfo const* dep_file_info = this->GetFileInfo(path);
  109. if (!dep_file_info) {
  110. return false;
  111. }
  112. this->Archive->AddResolvedPath(filename, path, unique,
  113. dep_file_info->rpaths);
  114. if (unique) {
  115. std::vector<std::string> combinedParentRpaths =
  116. dep_file_info->rpaths;
  117. combinedParentRpaths.insert(combinedParentRpaths.end(),
  118. rpaths.begin(), rpaths.end());
  119. if (!this->ScanDependencies(path, dep_file_info->libs,
  120. combinedParentRpaths,
  121. executablePath)) {
  122. return false;
  123. }
  124. }
  125. }
  126. } else {
  127. this->Archive->AddUnresolvedPath(name);
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. bool cmBinUtilsMacOSMachOLinker::ResolveDependency(
  134. std::string const& name, std::string const& executablePath,
  135. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  136. std::string& path, bool& resolved)
  137. {
  138. resolved = false;
  139. if (cmHasLiteralPrefix(name, "@rpath/")) {
  140. if (!this->ResolveRPathDependency(name, executablePath, loaderPath, rpaths,
  141. path, resolved)) {
  142. return false;
  143. }
  144. } else if (cmHasLiteralPrefix(name, "@loader_path/")) {
  145. if (!this->ResolveLoaderPathDependency(name, loaderPath, path, resolved)) {
  146. return false;
  147. }
  148. } else if (cmHasLiteralPrefix(name, "@executable_path/")) {
  149. if (!this->ResolveExecutablePathDependency(name, executablePath, path,
  150. resolved)) {
  151. return false;
  152. }
  153. } else {
  154. resolved = true;
  155. path = name;
  156. this->NormalizePath(path);
  157. }
  158. if (resolved && !cmSystemTools::FileIsFullPath(path)) {
  159. this->SetError("Resolved path is not absolute");
  160. return false;
  161. }
  162. return true;
  163. }
  164. bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
  165. std::string const& name, std::string const& executablePath,
  166. std::string& path, bool& resolved)
  167. {
  168. if (executablePath.empty()) {
  169. resolved = false;
  170. return true;
  171. }
  172. // 16 is == "@executable_path".length()
  173. path = name;
  174. path.replace(0, 16, executablePath);
  175. if (!cmSystemTools::PathExists(path)) {
  176. resolved = false;
  177. return true;
  178. }
  179. this->NormalizePath(path);
  180. resolved = true;
  181. return true;
  182. }
  183. bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
  184. std::string const& name, std::string const& loaderPath, std::string& path,
  185. bool& resolved)
  186. {
  187. if (loaderPath.empty()) {
  188. resolved = false;
  189. return true;
  190. }
  191. // 12 is "@loader_path".length();
  192. path = name;
  193. path.replace(0, 12, loaderPath);
  194. if (!cmSystemTools::PathExists(path)) {
  195. resolved = false;
  196. return true;
  197. }
  198. this->NormalizePath(path);
  199. resolved = true;
  200. return true;
  201. }
  202. bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
  203. std::string const& name, std::string const& executablePath,
  204. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  205. std::string& path, bool& resolved)
  206. {
  207. for (std::string const& rpath : rpaths) {
  208. std::string searchFile = name;
  209. searchFile.replace(0, 6, rpath);
  210. if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
  211. if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
  212. resolved)) {
  213. return false;
  214. }
  215. if (resolved) {
  216. return true;
  217. }
  218. } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
  219. if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
  220. path, resolved)) {
  221. return false;
  222. }
  223. if (resolved) {
  224. return true;
  225. }
  226. } else if (cmSystemTools::PathExists(searchFile)) {
  227. /*
  228. * paraphrasing @ben.boeckel:
  229. * if /b/libB.dylib is supposed to be used,
  230. * /a/libB.dylib will be found first if it exists. CMake tries to
  231. * sort rpath directories to avoid this, but sometimes there is no
  232. * right answer.
  233. *
  234. * I believe it is possible to resolve this using otools -l
  235. * then checking the LC_LOAD_DYLIB command whose name is
  236. * equal to the value of search_file, UNLESS the build
  237. * specifically sets the RPath to paths that will match
  238. * duplicate libs; at this point can we just point to
  239. * user error, or is there a reason why the advantages
  240. * to this scenario outweigh its disadvantages?
  241. *
  242. * Also priority seems to be the order as passed in when compiled
  243. * so as long as this method's resolution guarantees priority
  244. * in that manner further checking should not be necessary?
  245. */
  246. path = std::move(searchFile);
  247. this->NormalizePath(path);
  248. resolved = true;
  249. return true;
  250. }
  251. }
  252. resolved = false;
  253. return true;
  254. }