123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include "cmBinUtilsMacOSMachOLinker.h"
- #include "cmAlgorithms.h"
- #include "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h"
- #include "cmRuntimeDependencyArchive.h"
- #include "cmSystemTools.h"
- #include <sstream>
- #include <string>
- #include <vector>
- #include "cm_memory.hxx"
- cmBinUtilsMacOSMachOLinker::cmBinUtilsMacOSMachOLinker(
- cmRuntimeDependencyArchive* archive)
- : cmBinUtilsLinker(archive)
- {
- }
- bool cmBinUtilsMacOSMachOLinker::Prepare()
- {
- std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
- if (tool.empty()) {
- tool = "otool";
- }
- if (tool == "otool") {
- this->Tool =
- cm::make_unique<cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool>(
- this->Archive);
- } else {
- std::ostringstream e;
- e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
- this->SetError(e.str());
- return false;
- }
- return true;
- }
- bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
- std::string const& file, cmStateEnums::TargetType type)
- {
- std::string executableFile;
- if (type == cmStateEnums::EXECUTABLE) {
- executableFile = file;
- } else {
- executableFile = this->Archive->GetBundleExecutable();
- }
- std::string executablePath;
- if (!executableFile.empty()) {
- executablePath = cmSystemTools::GetFilenamePath(executableFile);
- }
- return this->ScanDependencies(file, executablePath);
- }
- bool cmBinUtilsMacOSMachOLinker::ScanDependencies(
- std::string const& file, std::string const& executablePath)
- {
- std::vector<std::string> libs, rpaths;
- if (!this->Tool->GetFileInfo(file, libs, rpaths)) {
- return false;
- }
- std::string loaderPath = cmSystemTools::GetFilenamePath(file);
- return this->GetFileDependencies(libs, executablePath, loaderPath, rpaths);
- }
- bool cmBinUtilsMacOSMachOLinker::GetFileDependencies(
- std::vector<std::string> const& names, std::string const& executablePath,
- std::string const& loaderPath, std::vector<std::string> const& rpaths)
- {
- for (std::string const& name : names) {
- if (!this->Archive->IsPreExcluded(name)) {
- std::string path;
- bool resolved;
- if (!this->ResolveDependency(name, executablePath, loaderPath, rpaths,
- path, resolved)) {
- return false;
- }
- if (resolved) {
- if (!this->Archive->IsPostExcluded(path)) {
- auto filename = cmSystemTools::GetFilenameName(path);
- bool unique;
- this->Archive->AddResolvedPath(filename, path, unique);
- if (unique && !this->ScanDependencies(path, executablePath)) {
- return false;
- }
- }
- } else {
- this->Archive->AddUnresolvedPath(name);
- }
- }
- }
- return true;
- }
- bool cmBinUtilsMacOSMachOLinker::ResolveDependency(
- std::string const& name, std::string const& executablePath,
- std::string const& loaderPath, std::vector<std::string> const& rpaths,
- std::string& path, bool& resolved)
- {
- resolved = false;
- if (cmHasLiteralPrefix(name, "@rpath/")) {
- if (!this->ResolveRPathDependency(name, executablePath, loaderPath, rpaths,
- path, resolved)) {
- return false;
- }
- } else if (cmHasLiteralPrefix(name, "@loader_path/")) {
- if (!this->ResolveLoaderPathDependency(name, loaderPath, path, resolved)) {
- return false;
- }
- } else if (cmHasLiteralPrefix(name, "@executable_path/")) {
- if (!this->ResolveExecutablePathDependency(name, executablePath, path,
- resolved)) {
- return false;
- }
- } else {
- resolved = true;
- path = name;
- }
- if (resolved && !cmSystemTools::FileIsFullPath(path)) {
- this->SetError("Resolved path is not absolute");
- return false;
- }
- return true;
- }
- bool cmBinUtilsMacOSMachOLinker::ResolveExecutablePathDependency(
- std::string const& name, std::string const& executablePath,
- std::string& path, bool& resolved)
- {
- if (executablePath.empty()) {
- resolved = false;
- return true;
- }
- // 16 is == "@executable_path".length()
- path = name;
- path.replace(0, 16, executablePath);
- if (!cmSystemTools::PathExists(path)) {
- resolved = false;
- return true;
- }
- resolved = true;
- return true;
- }
- bool cmBinUtilsMacOSMachOLinker::ResolveLoaderPathDependency(
- std::string const& name, std::string const& loaderPath, std::string& path,
- bool& resolved)
- {
- if (loaderPath.empty()) {
- resolved = false;
- return true;
- }
- // 12 is "@loader_path".length();
- path = name;
- path.replace(0, 12, loaderPath);
- if (!cmSystemTools::PathExists(path)) {
- resolved = false;
- return true;
- }
- resolved = true;
- return true;
- }
- bool cmBinUtilsMacOSMachOLinker::ResolveRPathDependency(
- std::string const& name, std::string const& executablePath,
- std::string const& loaderPath, std::vector<std::string> const& rpaths,
- std::string& path, bool& resolved)
- {
- for (std::string const& rpath : rpaths) {
- std::string searchFile = name;
- searchFile.replace(0, 6, rpath);
- if (cmHasLiteralPrefix(searchFile, "@loader_path/")) {
- if (!this->ResolveLoaderPathDependency(searchFile, loaderPath, path,
- resolved)) {
- return false;
- }
- if (resolved) {
- return true;
- }
- } else if (cmHasLiteralPrefix(searchFile, "@executable_path/")) {
- if (!this->ResolveExecutablePathDependency(searchFile, executablePath,
- path, resolved)) {
- return false;
- }
- if (resolved) {
- return true;
- }
- } else if (cmSystemTools::PathExists(searchFile)) {
- /*
- * paraphrasing @ben.boeckel:
- * if /b/libB.dylib is supposed to be used,
- * /a/libbB.dylib will be found first if it exists. CMake tries to
- * sort rpath directories to avoid this, but sometimes there is no
- * right answer.
- *
- * I believe it is possible to resolve this using otools -l
- * then checking the LC_LOAD_DYLIB command whose name is
- * equal to the value of search_file, UNLESS the build
- * specifically sets the RPath to paths that will match
- * duplicate libs; at this point can we just point to
- * user error, or is there a reason why the advantages
- * to this scenario outweigh its disadvantages?
- *
- * Also priority seems to be the order as passed in when compiled
- * so as long as this method's resolution guarantees priority
- * in that manner further checking should not be necessary?
- */
- path = searchFile;
- resolved = true;
- return true;
- }
- }
- resolved = false;
- return true;
- }
|