cmBinUtilsMacOSMachOLinker.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h"
  5. #include "cmRuntimeDependencyArchive.h"
  6. #include "cmStringAlgorithms.h"
  7. #include "cmSystemTools.h"
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11. #include "cm_memory.hxx"
  12. cmBinUtilsMacOSMachOLinker::cmBinUtilsMacOSMachOLinker(
  13. cmRuntimeDependencyArchive* archive)
  14. : cmBinUtilsLinker(archive)
  15. {
  16. }
  17. bool cmBinUtilsMacOSMachOLinker::Prepare()
  18. {
  19. std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
  20. if (tool.empty()) {
  21. tool = "otool";
  22. }
  23. if (tool == "otool") {
  24. this->Tool =
  25. cm::make_unique<cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool>(
  26. this->Archive);
  27. } else {
  28. std::ostringstream e;
  29. e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
  30. this->SetError(e.str());
  31. return false;
  32. }
  33. return true;
  34. }
  35. bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
  36. std::string const& file, cmStateEnums::TargetType type)
  37. {
  38. std::string executableFile;
  39. if (type == cmStateEnums::EXECUTABLE) {
  40. executableFile = file;
  41. } else {
  42. executableFile = this->Archive->GetBundleExecutable();
  43. }
  44. std::string executablePath;
  45. if (!executableFile.empty()) {
  46. executablePath = cmSystemTools::GetFilenamePath(executableFile);
  47. }
  48. return this->ScanDependencies(file, executablePath);
  49. }
  50. bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
  51. std::string const& file, std::string const& executablePath)
  52. {
  53. std::vector<std::string> libs;
  54. std::vector<std::string> rpaths;
  55. if (!this->Tool->GetFileInfo(file, libs, rpaths)) {
  56. return false;
  57. }
  58. std::string loaderPath = cmSystemTools::GetFilenamePath(file);
  59. return this->GetFileDependencies(libs, executablePath, loaderPath, rpaths);
  60. }
  61. bool cmBinUtilsMacOSMachOLinker::GetFileDependencies(
  62. std::vector<std::string> const& names, std::string const& executablePath,
  63. std::string const& loaderPath, std::vector<std::string> const& rpaths)
  64. {
  65. for (std::string const& name : names) {
  66. if (!this->Archive->IsPreExcluded(name)) {
  67. std::string path;
  68. bool resolved;
  69. if (!this->ResolveDependency(name, executablePath, loaderPath, rpaths,
  70. path, resolved)) {
  71. return false;
  72. }
  73. if (resolved) {
  74. if (!this->Archive->IsPostExcluded(path)) {
  75. auto filename = cmSystemTools::GetFilenameName(path);
  76. bool unique;
  77. this->Archive->AddResolvedPath(filename, path, unique);
  78. if (unique && !this->ScanDependencies(path, executablePath)) {
  79. return false;
  80. }
  81. }
  82. } else {
  83. this->Archive->AddUnresolvedPath(name);
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. bool cmBinUtilsMacOSMachOLinker::ResolveDependency(
  90. std::string const& name, std::string const& executablePath,
  91. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  92. std::string& path, bool& resolved)
  93. {
  94. resolved = false;
  95. if (cmHasLiteralPrefix(name, "@rpath/")) {
  96. if (!this->ResolveRPathDependency(name, executablePath, loaderPath, rpaths,
  97. path, resolved)) {
  98. return false;
  99. }
  100. } else if (cmHasLiteralPrefix(name, "@loader_path/")) {
  101. if (!this->ResolveLoaderPathDependency(name, loaderPath, path, resolved)) {
  102. return false;
  103. }
  104. } else if (cmHasLiteralPrefix(name, "@executable_path/")) {
  105. if (!this->ResolveExecutablePathDependency(name, executablePath, path,
  106. resolved)) {
  107. return false;
  108. }
  109. } else {
  110. resolved = true;
  111. path = name;
  112. }
  113. if (resolved && !cmSystemTools::FileIsFullPath(path)) {
  114. this->SetError("Resolved path is not absolute");
  115. return false;
  116. }
  117. return true;
  118. }
  119. bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
  120. std::string const& name, std::string const& executablePath,
  121. std::string& path, bool& resolved)
  122. {
  123. if (executablePath.empty()) {
  124. resolved = false;
  125. return true;
  126. }
  127. // 16 is == "@executable_path".length()
  128. path = name;
  129. path.replace(0, 16, executablePath);
  130. if (!cmSystemTools::PathExists(path)) {
  131. resolved = false;
  132. return true;
  133. }
  134. resolved = true;
  135. return true;
  136. }
  137. bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
  138. std::string const& name, std::string const& loaderPath, std::string& path,
  139. bool& resolved)
  140. {
  141. if (loaderPath.empty()) {
  142. resolved = false;
  143. return true;
  144. }
  145. // 12 is "@loader_path".length();
  146. path = name;
  147. path.replace(0, 12, loaderPath);
  148. if (!cmSystemTools::PathExists(path)) {
  149. resolved = false;
  150. return true;
  151. }
  152. resolved = true;
  153. return true;
  154. }
  155. bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
  156. std::string const& name, std::string const& executablePath,
  157. std::string const& loaderPath, std::vector<std::string> const& rpaths,
  158. std::string& path, bool& resolved)
  159. {
  160. for (std::string const& rpath : rpaths) {
  161. std::string searchFile = name;
  162. searchFile.replace(0, 6, rpath);
  163. if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
  164. if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
  165. resolved)) {
  166. return false;
  167. }
  168. if (resolved) {
  169. return true;
  170. }
  171. } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
  172. if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
  173. path, resolved)) {
  174. return false;
  175. }
  176. if (resolved) {
  177. return true;
  178. }
  179. } else if (cmSystemTools::PathExists(searchFile)) {
  180. /*
  181. * paraphrasing @ben.boeckel:
  182. * if /b/libB.dylib is supposed to be used,
  183. * /a/libbB.dylib will be found first if it exists. CMake tries to
  184. * sort rpath directories to avoid this, but sometimes there is no
  185. * right answer.
  186. *
  187. * I believe it is possible to resolve this using otools -l
  188. * then checking the LC_LOAD_DYLIB command whose name is
  189. * equal to the value of search_file, UNLESS the build
  190. * specifically sets the RPath to paths that will match
  191. * duplicate libs; at this point can we just point to
  192. * user error, or is there a reason why the advantages
  193. * to this scenario outweigh its disadvantages?
  194. *
  195. * Also priority seems to be the order as passed in when compiled
  196. * so as long as this method's resolution guarantees priority
  197. * in that manner further checking should not be necessary?
  198. */
  199. path = searchFile;
  200. resolved = true;
  201. return true;
  202. }
  203. }
  204. resolved = false;
  205. return true;
  206. }