cmRuntimeDependencyArchive.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmRuntimeDependencyArchive.h"
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/memory>
  10. #include "cmBinUtilsLinuxELFLinker.h"
  11. #include "cmBinUtilsMacOSMachOLinker.h"
  12. #include "cmBinUtilsWindowsPELinker.h"
  13. #include "cmExecutionStatus.h"
  14. #include "cmList.h"
  15. #include "cmMakefile.h"
  16. #include "cmStateTypes.h"
  17. #include "cmSystemTools.h"
  18. #if defined(_WIN32)
  19. # include "cmGlobalGenerator.h"
  20. # ifndef CMAKE_BOOTSTRAP
  21. # include "cmGlobalVisualStudioVersionedGenerator.h"
  22. # endif
  23. # include "cmsys/Glob.hxx"
  24. # include "cmVSSetupHelper.h"
  25. #endif
  26. #if defined(_WIN32)
  27. static void AddVisualStudioPath(std::vector<std::string>& paths,
  28. std::string const& prefix,
  29. unsigned int version, cmGlobalGenerator* gg)
  30. {
  31. // If generating for the VS IDE, use the same instance.
  32. std::string vsloc;
  33. bool found = false;
  34. # ifndef CMAKE_BOOTSTRAP
  35. if (cmHasPrefix(gg->GetName(), prefix)) {
  36. cmGlobalVisualStudioVersionedGenerator* vsgen =
  37. static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
  38. if (vsgen->GetVSInstance(vsloc)) {
  39. found = true;
  40. }
  41. }
  42. # endif
  43. // Otherwise, find a VS instance ourselves.
  44. if (!found) {
  45. cmVSSetupAPIHelper vsSetupAPIHelper(version);
  46. if (vsSetupAPIHelper.GetVSInstanceInfo(vsloc)) {
  47. cmSystemTools::ConvertToUnixSlashes(vsloc);
  48. found = true;
  49. }
  50. }
  51. if (found) {
  52. cmsys::Glob glob;
  53. glob.SetListDirs(true);
  54. glob.FindFiles(vsloc + "/VC/Tools/MSVC/*");
  55. for (auto const& vcdir : glob.GetFiles()) {
  56. paths.push_back(vcdir + "/bin/Hostx64/x64");
  57. paths.push_back(vcdir + "/bin/Hostx86/x64");
  58. paths.push_back(vcdir + "/bin/Hostx64/x86");
  59. paths.push_back(vcdir + "/bin/Hostx86/x86");
  60. }
  61. }
  62. }
  63. static void AddRegistryPath(std::vector<std::string>& paths,
  64. std::string const& path, cmMakefile* mf)
  65. {
  66. // We should view the registry as the target application would view
  67. // it.
  68. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  69. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  70. if (mf->PlatformIs64Bit()) {
  71. view = cmSystemTools::KeyWOW64_64;
  72. other_view = cmSystemTools::KeyWOW64_32;
  73. }
  74. // Expand using the view of the target application.
  75. std::string expanded = path;
  76. cmSystemTools::ExpandRegistryValues(expanded, view);
  77. cmSystemTools::GlobDirs(expanded, paths);
  78. // Executables can be either 32-bit or 64-bit, so expand using the
  79. // alternative view.
  80. expanded = path;
  81. cmSystemTools::ExpandRegistryValues(expanded, other_view);
  82. cmSystemTools::GlobDirs(expanded, paths);
  83. }
  84. static void AddEnvPath(std::vector<std::string>& paths, std::string const& var,
  85. std::string const& suffix)
  86. {
  87. std::string value;
  88. if (cmSystemTools::GetEnv(var, value)) {
  89. paths.push_back(value + suffix);
  90. }
  91. }
  92. #endif
  93. static cmsys::RegularExpression TransformCompile(std::string const& str)
  94. {
  95. return cmsys::RegularExpression(str);
  96. }
  97. cmRuntimeDependencyArchive::cmRuntimeDependencyArchive(
  98. cmExecutionStatus& status, std::vector<std::string> searchDirectories,
  99. std::string bundleExecutable,
  100. std::vector<std::string> const& preIncludeRegexes,
  101. std::vector<std::string> const& preExcludeRegexes,
  102. std::vector<std::string> const& postIncludeRegexes,
  103. std::vector<std::string> const& postExcludeRegexes,
  104. std::vector<std::string> postIncludeFiles,
  105. std::vector<std::string> postExcludeFiles,
  106. std::vector<std::string> postExcludeFilesStrict)
  107. : Status(status)
  108. , SearchDirectories(std::move(searchDirectories))
  109. , BundleExecutable(std::move(bundleExecutable))
  110. , PreIncludeRegexes(preIncludeRegexes.size())
  111. , PreExcludeRegexes(preExcludeRegexes.size())
  112. , PostIncludeRegexes(postIncludeRegexes.size())
  113. , PostExcludeRegexes(postExcludeRegexes.size())
  114. , PostIncludeFiles(std::move(postIncludeFiles))
  115. , PostExcludeFiles(std::move(postExcludeFiles))
  116. , PostExcludeFilesStrict(std::move(postExcludeFilesStrict))
  117. {
  118. std::transform(preIncludeRegexes.begin(), preIncludeRegexes.end(),
  119. this->PreIncludeRegexes.begin(), TransformCompile);
  120. std::transform(preExcludeRegexes.begin(), preExcludeRegexes.end(),
  121. this->PreExcludeRegexes.begin(), TransformCompile);
  122. std::transform(postIncludeRegexes.begin(), postIncludeRegexes.end(),
  123. this->PostIncludeRegexes.begin(), TransformCompile);
  124. std::transform(postExcludeRegexes.begin(), postExcludeRegexes.end(),
  125. this->PostExcludeRegexes.begin(), TransformCompile);
  126. }
  127. bool cmRuntimeDependencyArchive::Prepare()
  128. {
  129. std::string platform = this->GetMakefile()->GetSafeDefinition(
  130. "CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM");
  131. if (platform.empty()) {
  132. std::string systemName =
  133. this->GetMakefile()->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  134. if (systemName == "Windows") {
  135. platform = "windows+pe";
  136. } else if (systemName == "Darwin") {
  137. platform = "macos+macho";
  138. } else if (systemName == "Linux") {
  139. platform = "linux+elf";
  140. }
  141. }
  142. if (platform == "linux+elf") {
  143. this->Linker = cm::make_unique<cmBinUtilsLinuxELFLinker>(this);
  144. } else if (platform == "windows+pe") {
  145. this->Linker = cm::make_unique<cmBinUtilsWindowsPELinker>(this);
  146. } else if (platform == "macos+macho") {
  147. this->Linker = cm::make_unique<cmBinUtilsMacOSMachOLinker>(this);
  148. } else {
  149. std::ostringstream e;
  150. e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM: "
  151. << platform;
  152. this->SetError(e.str());
  153. return false;
  154. }
  155. return this->Linker->Prepare();
  156. }
  157. bool cmRuntimeDependencyArchive::GetRuntimeDependencies(
  158. std::vector<std::string> const& executables,
  159. std::vector<std::string> const& libraries,
  160. std::vector<std::string> const& modules)
  161. {
  162. for (auto const& exe : executables) {
  163. if (!this->Linker->ScanDependencies(exe, cmStateEnums::EXECUTABLE)) {
  164. return false;
  165. }
  166. }
  167. for (auto const& lib : libraries) {
  168. if (!this->Linker->ScanDependencies(lib, cmStateEnums::SHARED_LIBRARY)) {
  169. return false;
  170. }
  171. }
  172. return std::all_of(
  173. modules.begin(), modules.end(), [this](std::string const& mod) -> bool {
  174. return this->Linker->ScanDependencies(mod, cmStateEnums::MODULE_LIBRARY);
  175. });
  176. }
  177. void cmRuntimeDependencyArchive::SetError(std::string const& e)
  178. {
  179. this->Status.SetError(e);
  180. }
  181. std::string const& cmRuntimeDependencyArchive::GetBundleExecutable() const
  182. {
  183. return this->BundleExecutable;
  184. }
  185. std::vector<std::string> const&
  186. cmRuntimeDependencyArchive::GetSearchDirectories() const
  187. {
  188. return this->SearchDirectories;
  189. }
  190. std::string const& cmRuntimeDependencyArchive::GetGetRuntimeDependenciesTool()
  191. const
  192. {
  193. return this->GetMakefile()->GetSafeDefinition(
  194. "CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL");
  195. }
  196. bool cmRuntimeDependencyArchive::GetGetRuntimeDependenciesCommand(
  197. std::string const& search, std::vector<std::string>& command) const
  198. {
  199. // First see if it was supplied by the user
  200. std::string toolCommand = this->GetMakefile()->GetSafeDefinition(
  201. "CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND");
  202. if (toolCommand.empty() && search == "objdump") {
  203. toolCommand = this->GetMakefile()->GetSafeDefinition("CMAKE_OBJDUMP");
  204. }
  205. if (!toolCommand.empty()) {
  206. cmExpandList(toolCommand, command);
  207. return true;
  208. }
  209. // Now go searching for it
  210. std::vector<std::string> paths;
  211. #ifdef _WIN32
  212. cmGlobalGenerator* gg = this->GetMakefile()->GetGlobalGenerator();
  213. // Add newer Visual Studio paths
  214. AddVisualStudioPath(paths, "Visual Studio 17 ", 17, gg);
  215. AddVisualStudioPath(paths, "Visual Studio 16 ", 16, gg);
  216. AddVisualStudioPath(paths, "Visual Studio 15 ", 15, gg);
  217. // Add older Visual Studio paths
  218. AddRegistryPath(
  219. paths,
  220. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\14.0;InstallDir]/"
  221. "../../VC/bin",
  222. this->GetMakefile());
  223. AddEnvPath(paths, "VS140COMNTOOLS", "/../../VC/bin");
  224. paths.push_back(
  225. "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin");
  226. AddRegistryPath(
  227. paths,
  228. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\12.0;InstallDir]/"
  229. "../../VC/bin",
  230. this->GetMakefile());
  231. AddEnvPath(paths, "VS120COMNTOOLS", "/../../VC/bin");
  232. paths.push_back(
  233. "C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin");
  234. AddRegistryPath(
  235. paths,
  236. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\11.0;InstallDir]/"
  237. "../../VC/bin",
  238. this->GetMakefile());
  239. AddEnvPath(paths, "VS110COMNTOOLS", "/../../VC/bin");
  240. paths.push_back(
  241. "C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin");
  242. AddRegistryPath(
  243. paths,
  244. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\10.0;InstallDir]/"
  245. "../../VC/bin",
  246. this->GetMakefile());
  247. AddEnvPath(paths, "VS100COMNTOOLS", "/../../VC/bin");
  248. paths.push_back(
  249. "C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/bin");
  250. AddRegistryPath(
  251. paths,
  252. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0;InstallDir]/"
  253. "../../VC/bin",
  254. this->GetMakefile());
  255. AddEnvPath(paths, "VS90COMNTOOLS", "/../../VC/bin");
  256. paths.push_back("C:/Program Files/Microsoft Visual Studio 9.0/VC/bin");
  257. paths.push_back("C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin");
  258. AddRegistryPath(
  259. paths,
  260. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0;InstallDir]/"
  261. "../../VC/bin",
  262. this->GetMakefile());
  263. AddEnvPath(paths, "VS80COMNTOOLS", "/../../VC/bin");
  264. paths.push_back("C:/Program Files/Microsoft Visual Studio 8/VC/BIN");
  265. paths.push_back("C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN");
  266. AddRegistryPath(
  267. paths,
  268. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir]/"
  269. "../../VC7/bin",
  270. this->GetMakefile());
  271. AddEnvPath(paths, "VS71COMNTOOLS", "/../../VC7/bin");
  272. paths.push_back(
  273. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN");
  274. paths.push_back(
  275. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN");
  276. #endif
  277. std::string program = cmSystemTools::FindProgram(search, paths);
  278. if (!program.empty()) {
  279. command = { program };
  280. return true;
  281. }
  282. // Couldn't find it
  283. return false;
  284. }
  285. bool cmRuntimeDependencyArchive::IsPreExcluded(std::string const& name) const
  286. {
  287. cmsys::RegularExpressionMatch match;
  288. auto const regexMatch =
  289. [&match, name](cmsys::RegularExpression const& regex) -> bool {
  290. return regex.find(name.c_str(), match);
  291. };
  292. auto const regexSearch =
  293. [&regexMatch](
  294. std::vector<cmsys::RegularExpression> const& regexes) -> bool {
  295. return std::any_of(regexes.begin(), regexes.end(), regexMatch);
  296. };
  297. return !regexSearch(this->PreIncludeRegexes) &&
  298. regexSearch(this->PreExcludeRegexes);
  299. }
  300. bool cmRuntimeDependencyArchive::IsPostExcluded(std::string const& name) const
  301. {
  302. cmsys::RegularExpressionMatch match;
  303. auto const regexMatch =
  304. [&match, name](cmsys::RegularExpression const& regex) -> bool {
  305. return regex.find(name.c_str(), match);
  306. };
  307. auto const regexSearch =
  308. [&regexMatch](
  309. std::vector<cmsys::RegularExpression> const& regexes) -> bool {
  310. return std::any_of(regexes.begin(), regexes.end(), regexMatch);
  311. };
  312. auto const fileMatch = [name](std::string const& file) -> bool {
  313. return cmSystemTools::SameFile(file, name);
  314. };
  315. auto const fileSearch =
  316. [&fileMatch](std::vector<std::string> const& files) -> bool {
  317. return std::any_of(files.begin(), files.end(), fileMatch);
  318. };
  319. return fileSearch(this->PostExcludeFilesStrict) ||
  320. (!(regexSearch(this->PostIncludeRegexes) ||
  321. fileSearch(this->PostIncludeFiles)) &&
  322. (regexSearch(this->PostExcludeRegexes) ||
  323. fileSearch(this->PostExcludeFiles)));
  324. }
  325. void cmRuntimeDependencyArchive::AddResolvedPath(
  326. std::string const& name, std::string const& path, bool& unique,
  327. std::vector<std::string> rpaths)
  328. {
  329. auto it = this->ResolvedPaths.emplace(name, std::set<std::string>{}).first;
  330. unique = true;
  331. for (auto const& other : it->second) {
  332. if (cmSystemTools::SameFile(path, other)) {
  333. unique = false;
  334. break;
  335. }
  336. }
  337. it->second.emplace(path);
  338. this->RPaths[path] = std::move(rpaths);
  339. }
  340. void cmRuntimeDependencyArchive::AddUnresolvedPath(std::string const& name)
  341. {
  342. this->UnresolvedPaths.insert(name);
  343. }
  344. cmMakefile* cmRuntimeDependencyArchive::GetMakefile() const
  345. {
  346. return &this->Status.GetMakefile();
  347. }
  348. std::map<std::string, std::set<std::string>> const&
  349. cmRuntimeDependencyArchive::GetResolvedPaths() const
  350. {
  351. return this->ResolvedPaths;
  352. }
  353. std::set<std::string> const& cmRuntimeDependencyArchive::GetUnresolvedPaths()
  354. const
  355. {
  356. return this->UnresolvedPaths;
  357. }
  358. std::map<std::string, std::vector<std::string>> const&
  359. cmRuntimeDependencyArchive::GetRPaths() const
  360. {
  361. return this->RPaths;
  362. }
  363. bool cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  364. std::string const& platform)
  365. {
  366. static std::set<std::string> const supportedPlatforms = { "Windows", "Linux",
  367. "Darwin" };
  368. return supportedPlatforms.count(platform);
  369. }