cmLDConfigLDConfigTool.cxx 2.1 KB

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