1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
4 #include "cmBinUtilsMacOSMachOLinker.h"
14 #include "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h"
15 #include "cmRuntimeDependencyArchive.h"
16 #include "cmStringAlgorithms.h"
17 #include "cmSystemTools.h"
20 bool IsMissingSystemDylib(std::string const& path)
22 // Starting on macOS 11, the dynamic loader has a builtin cache of
23 // system-provided dylib files that do not exist on the filesystem.
24 // Tell our caller that these are expected to be missing.
25 return ((cmHasLiteralPrefix(path, "/System/Library/") ||
26 cmHasLiteralPrefix(path, "/usr/lib/")) &&
27 !cmSystemTools::PathExists(path));
31 cmBinUtilsMacOSMachOLinker::cmBinUtilsMacOSMachOLinker(
32 cmRuntimeDependencyArchive* archive)
33 : cmBinUtilsLinker(archive)
37 bool cmBinUtilsMacOSMachOLinker::Prepare()
39 std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
43 if (tool == "otool") {
45 cm::make_unique<cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool>(
49 e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
50 this->SetError(e.str());
57 auto cmBinUtilsMacOSMachOLinker::GetFileInfo(std::string const& file)
60 // Memoize processed rpaths and library dependencies to reduce the number
61 // of calls to otool, especially in the case of heavily recursive libraries
62 auto iter = ScannedFileInfo.find(file);
63 if (iter != ScannedFileInfo.end()) {
68 if (!this->Tool->GetFileInfo(file, file_info.libs, file_info.rpaths)) {
69 // Call to otool failed
73 auto iter_inserted = ScannedFileInfo.insert({ file, std::move(file_info) });
74 return &iter_inserted.first->second;
77 bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
78 std::string const& file, cmStateEnums::TargetType type)
80 std::string executableFile;
81 if (type == cmStateEnums::EXECUTABLE) {
82 executableFile = file;
84 executableFile = this->Archive->GetBundleExecutable();
86 std::string executablePath;
87 if (!executableFile.empty()) {
88 executablePath = cmSystemTools::GetFilenamePath(executableFile);
90 const FileInfo* file_info = this->GetFileInfo(file);
91 if (file_info == nullptr) {
94 return this->ScanDependencies(file, file_info->libs, file_info->rpaths,
98 bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
99 std::string const& file, std::vector<std::string> const& libs,
100 std::vector<std::string> const& rpaths, std::string const& executablePath)
102 std::string loaderPath = cmSystemTools::GetFilenamePath(file);
103 return this->GetFileDependencies(libs, executablePath, loaderPath, rpaths);
106 bool cmBinUtilsMacOSMachOLinker::GetFileDependencies(
107 std::vector<std::string> const& names, std::string const& executablePath,
108 std::string const& loaderPath, std::vector<std::string> const& rpaths)
110 for (std::string const& name : names) {
111 if (!this->Archive->IsPreExcluded(name)) {
114 if (!this->ResolveDependency(name, executablePath, loaderPath, rpaths,
119 if (!this->Archive->IsPostExcluded(path) &&
120 !IsMissingSystemDylib(path)) {
121 auto filename = cmSystemTools::GetFilenameName(path);
123 const FileInfo* dep_file_info = this->GetFileInfo(path);
124 if (dep_file_info == nullptr) {
128 this->Archive->AddResolvedPath(filename, path, unique,
129 dep_file_info->rpaths);
131 !this->ScanDependencies(path, dep_file_info->libs,
132 dep_file_info->rpaths, executablePath)) {
137 this->Archive->AddUnresolvedPath(name);
145 bool cmBinUtilsMacOSMachOLinker::ResolveDependency(
146 std::string const& name, std::string const& executablePath,
147 std::string const& loaderPath, std::vector<std::string> const& rpaths,
148 std::string& path, bool& resolved)
151 if (cmHasLiteralPrefix(name, "@rpath/")) {
152 if (!this->ResolveRPathDependency(name, executablePath, loaderPath, rpaths,
156 } else if (cmHasLiteralPrefix(name, "@loader_path/")) {
157 if (!this->ResolveLoaderPathDependency(name, loaderPath, path, resolved)) {
160 } else if (cmHasLiteralPrefix(name, "@executable_path/")) {
161 if (!this->ResolveExecutablePathDependency(name, executablePath, path,
170 if (resolved && !cmSystemTools::FileIsFullPath(path)) {
171 this->SetError("Resolved path is not absolute");
178 bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
179 std::string const& name, std::string const& executablePath,
180 std::string& path, bool& resolved)
182 if (executablePath.empty()) {
187 // 16 is == "@executable_path".length()
189 path.replace(0, 16, executablePath);
191 if (!cmSystemTools::PathExists(path)) {
200 bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
201 std::string const& name, std::string const& loaderPath, std::string& path,
204 if (loaderPath.empty()) {
209 // 12 is "@loader_path".length();
211 path.replace(0, 12, loaderPath);
213 if (!cmSystemTools::PathExists(path)) {
222 bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
223 std::string const& name, std::string const& executablePath,
224 std::string const& loaderPath, std::vector<std::string> const& rpaths,
225 std::string& path, bool& resolved)
227 for (std::string const& rpath : rpaths) {
228 std::string searchFile = name;
229 searchFile.replace(0, 6, rpath);
230 if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
231 if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
238 } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
239 if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
246 } else if (cmSystemTools::PathExists(searchFile)) {
248 * paraphrasing @ben.boeckel:
249 * if /b/libB.dylib is supposed to be used,
250 * /a/libbB.dylib will be found first if it exists. CMake tries to
251 * sort rpath directories to avoid this, but sometimes there is no
254 * I believe it is possible to resolve this using otools -l
255 * then checking the LC_LOAD_DYLIB command whose name is
256 * equal to the value of search_file, UNLESS the build
257 * specifically sets the RPath to paths that will match
258 * duplicate libs; at this point can we just point to
259 * user error, or is there a reason why the advantages
260 * to this scenario outweigh its disadvantages?
262 * Also priority seems to be the order as passed in when compiled
263 * so as long as this method's resolution guarantees priority
264 * in that manner further checking should not be necessary?