cmSiteNameCommand.cxx 2.6 KB

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