cmSiteNameCommand.cxx 2.2 KB

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