cmLDConfigLDConfigTool.cxx 2.0 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 <istream>
  5. #include <string>
  6. #include <vector>
  7. #include "cmsys/RegularExpression.hxx"
  8. #include "cmList.h"
  9. #include "cmMakefile.h"
  10. #include "cmRuntimeDependencyArchive.h"
  11. #include "cmSystemTools.h"
  12. #include "cmUVProcessChain.h"
  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. cmList ldConfigCommand{ ldConfigPath };
  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() || process.GetStatus(0).SpawnResult != 0) {
  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. if (process.GetStatus(0).ExitStatus != 0) {
  55. this->Archive->SetError("Failed to run ldconfig");
  56. return false;
  57. }
  58. return true;
  59. }