cmLDConfigLDConfigTool.cxx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "cmLDConfigLDConfigTool.h"
  4. #include "cmMakefile.h"
  5. #include "cmRuntimeDependencyArchive.h"
  6. #include "cmStringAlgorithms.h"
  7. #include "cmSystemTools.h"
  8. #include "cmUVProcessChain.h"
  9. #include "cmsys/RegularExpression.hxx"
  10. #include <istream>
  11. #include <string>
  12. #include <vector>
  13. cmLDConfigLDConfigTool::cmLDConfigLDConfigTool(
  14. cmRuntimeDependencyArchive* archive)
  15. : cmLDConfigTool(archive)
  16. {
  17. }
  18. bool cmLDConfigLDConfigTool::GetLDConfigPaths(std::vector<std::string>& paths)
  19. {
  20. std::string ldConfigPath =
  21. this->Archive->GetMakefile()->GetSafeDefinition("CMAKE_LDCONFIG_COMMAND");
  22. if (ldConfigPath.empty()) {
  23. ldConfigPath = cmSystemTools::FindProgram(
  24. "ldconfig", { "/sbin", "/usr/sbin", "/usr/local/sbin" });
  25. if (ldConfigPath.empty()) {
  26. this->Archive->SetError("Could not find ldconfig");
  27. return false;
  28. }
  29. }
  30. std::vector<std::string> ldConfigCommand;
  31. cmExpandList(ldConfigPath, ldConfigCommand);
  32. ldConfigCommand.emplace_back("-v");
  33. ldConfigCommand.emplace_back("-N"); // Don't rebuild the cache.
  34. ldConfigCommand.emplace_back("-X"); // Don't update links.
  35. cmUVProcessChainBuilder builder;
  36. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
  37. .AddCommand(ldConfigCommand);
  38. auto process = builder.Start();
  39. if (!process.Valid()) {
  40. this->Archive->SetError("Failed to start ldconfig process");
  41. return false;
  42. }
  43. std::string line;
  44. static const cmsys::RegularExpression regex("^([^\t:]*):");
  45. while (std::getline(*process.OutputStream(), line)) {
  46. cmsys::RegularExpressionMatch match;
  47. if (regex.find(line.c_str(), match)) {
  48. paths.push_back(match.match(1));
  49. }
  50. }
  51. if (!process.Wait()) {
  52. this->Archive->SetError("Failed to wait on ldconfig process");
  53. return false;
  54. }
  55. auto status = process.GetStatus();
  56. if (!status[0] || status[0]->ExitStatus != 0) {
  57. this->Archive->SetError("Failed to run ldconfig");
  58. return false;
  59. }
  60. return true;
  61. }