cmSiteNameCommand.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSiteNameCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. // cmSiteNameCommand
  16. bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() != 1 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. std::vector<std::string> paths;
  24. paths.push_back("/usr/bsd");
  25. paths.push_back("/usr/sbin");
  26. paths.push_back("/usr/bin");
  27. paths.push_back("/bin");
  28. paths.push_back("/sbin");
  29. paths.push_back("/usr/local/bin");
  30. const char* cacheValue
  31. = m_Makefile->GetDefinition(args[0].c_str());
  32. if(cacheValue)
  33. {
  34. return true;
  35. }
  36. const char *temp = m_Makefile->GetDefinition("HOSTNAME");
  37. std::string hostname_cmd;
  38. if(temp)
  39. {
  40. hostname_cmd = temp;
  41. }
  42. else
  43. {
  44. hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
  45. }
  46. std::string siteName = "unknown";
  47. #if defined(_WIN32) && !defined(__CYGWIN__)
  48. std::string host;
  49. if(cmSystemTools::ReadRegistryValue(
  50. "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName;ComputerName",
  51. host))
  52. {
  53. siteName = host;
  54. }
  55. #else
  56. // try to find the hostname for this computer
  57. if (!cmSystemTools::IsOff(hostname_cmd.c_str()))
  58. {
  59. std::string host;
  60. cmSystemTools::RunSingleCommand(hostname_cmd.c_str(),
  61. &host, 0, 0, false);
  62. // got the hostname
  63. if (host.length())
  64. {
  65. // remove any white space from the host name
  66. std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
  67. cmsys::RegularExpression hostReg (hostRegExp.c_str());
  68. if (hostReg.find(host.c_str()))
  69. {
  70. // strip whitespace
  71. host = hostReg.match(1);
  72. }
  73. if(host.length())
  74. {
  75. siteName = host;
  76. }
  77. }
  78. }
  79. #endif
  80. m_Makefile->
  81. AddCacheDefinition(args[0].c_str(),
  82. siteName.c_str(),
  83. "Name of the computer/site where compile is being run",
  84. cmCacheManager::STRING);
  85. return true;
  86. }