cmBinUtilsMacOSMachOLinker.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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) {
  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) {
  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. }
  157. if (resolved && !cmSystemTools::FileIsFullPath(path)) {
  158. this->SetError("Resolved path is not absolute");
  159. return false;
  160. }
  161. return true;
  162. }
  163. bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
  164. std::string const& name, std::string const& executablePath,
  165. std::string& path, bool& resolved)
  166. {
  167. if (executablePath.empty()) {
  168. resolved = false;
  169. return true;
  170. }
  171. // 16 is == "@executable_path".length()
  172. path = name;
  173. path.replace(0, 16, executablePath);
  174. if (!cmSystemTools::PathExists(path)) {
  175. resolved = false;
  176. return true;
  177. }
  178. resolved = true;
  179. return true;
  180. }
  181. bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
  182. std::string const& name, std::string const& loaderPath, std::string& path,
  183. bool& resolved)
  184. {
  185. if (loaderPath.empty()) {
  186. resolved = false;
  187. return true;
  188. }
  189. // 12 is "@loader_path".length();
  190. path = name;
  191. path.replace(0, 12, loaderPath);
  192. if (!cmSystemTools::PathExists(path)) {
  193. resolved = false;
  194. return true;
  195. }
  196. resolved = true;
  197. return true;
  198. }
  199. bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
  200. std::string const& name, std::string const& executablePath,
  201. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  202. std::string& path, bool& resolved)
  203. {
  204. for (std::string const& rpath : rpaths) {
  205. std::string searchFile = name;
  206. searchFile.replace(0, 6, rpath);
  207. if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
  208. if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
  209. resolved)) {
  210. return false;
  211. }
  212. if (resolved) {
  213. return true;
  214. }
  215. } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
  216. if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
  217. path, resolved)) {
  218. return false;
  219. }
  220. if (resolved) {
  221. return true;
  222. }
  223. } else if (cmSystemTools::PathExists(searchFile)) {
  224. /*
  225. * paraphrasing @ben.boeckel:
  226. * if /b/libB.dylib is supposed to be used,
  227. * /a/libbB.dylib will be found first if it exists. CMake tries to
  228. * sort rpath directories to avoid this, but sometimes there is no
  229. * right answer.
  230. *
  231. * I believe it is possible to resolve this using otools -l
  232. * then checking the LC_LOAD_DYLIB command whose name is
  233. * equal to the value of search_file, UNLESS the build
  234. * specifically sets the RPath to paths that will match
  235. * duplicate libs; at this point can we just point to
  236. * user error, or is there a reason why the advantages
  237. * to this scenario outweigh its disadvantages?
  238. *
  239. * Also priority seems to be the order as passed in when compiled
  240. * so as long as this method's resolution guarantees priority
  241. * in that manner further checking should not be necessary?
  242. */
  243. path = searchFile;
  244. resolved = true;
  245. return true;
  246. }
  247. }
  248. resolved = false;
  249. return true;
  250. }