cmBinUtilsMacOSMachOLinker.cxx 6.7 KB

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