cmSiteNameCommand.cxx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "cmSiteNameCommand.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include "cmMakefile.h"
  6. #include "cmStateTypes.h"
  7. #include "cmSystemTools.h"
  8. class cmExecutionStatus;
  9. // cmSiteNameCommand
  10. bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args,
  11. cmExecutionStatus&)
  12. {
  13. if (args.size() != 1) {
  14. this->SetError("called with incorrect number of arguments");
  15. return false;
  16. }
  17. std::vector<std::string> paths;
  18. paths.push_back("/usr/bsd");
  19. paths.push_back("/usr/sbin");
  20. paths.push_back("/usr/bin");
  21. paths.push_back("/bin");
  22. paths.push_back("/sbin");
  23. paths.push_back("/usr/local/bin");
  24. const char* cacheValue = this->Makefile->GetDefinition(args[0]);
  25. if (cacheValue) {
  26. return true;
  27. }
  28. const char* temp = this->Makefile->GetDefinition("HOSTNAME");
  29. std::string hostname_cmd;
  30. if (temp) {
  31. hostname_cmd = temp;
  32. } else {
  33. hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
  34. }
  35. std::string siteName = "unknown";
  36. #if defined(_WIN32) && !defined(__CYGWIN__)
  37. std::string host;
  38. if (cmSystemTools::ReadRegistryValue(
  39. "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
  40. "Control\\ComputerName\\ComputerName;ComputerName",
  41. host)) {
  42. siteName = host;
  43. }
  44. #else
  45. // try to find the hostname for this computer
  46. if (!cmSystemTools::IsOff(hostname_cmd.c_str())) {
  47. std::string host;
  48. cmSystemTools::RunSingleCommand(hostname_cmd.c_str(), &host, nullptr,
  49. nullptr, nullptr,
  50. cmSystemTools::OUTPUT_NONE);
  51. // got the hostname
  52. if (!host.empty()) {
  53. // remove any white space from the host name
  54. std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
  55. cmsys::RegularExpression hostReg(hostRegExp.c_str());
  56. if (hostReg.find(host.c_str())) {
  57. // strip whitespace
  58. host = hostReg.match(1);
  59. }
  60. if (!host.empty()) {
  61. siteName = host;
  62. }
  63. }
  64. }
  65. #endif
  66. this->Makefile->AddCacheDefinition(
  67. args[0], siteName.c_str(),
  68. "Name of the computer/site where compile is being run",
  69. cmStateEnums::STRING);
  70. return true;
  71. }