cmBinUtilsMacOSMachOLinker.cxx 8.2 KB

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