cmSiteNameCommand.cxx 2.2 KB

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