cmRuntimeDependencyArchive.cxx 11 KB

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