cmSiteNameCommand.cxx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmStateTypes.h"
  8. #include "cmStringAlgorithms.h"
  9. #include "cmSystemTools.h"
  10. // cmSiteNameCommand
  11. bool cmSiteNameCommand(std::vector<std::string> const& args,
  12. cmExecutionStatus& status)
  13. {
  14. if (args.size() != 1) {
  15. status.SetError("called with incorrect number of arguments");
  16. return false;
  17. }
  18. std::vector<std::string> paths;
  19. paths.emplace_back("/usr/bsd");
  20. paths.emplace_back("/usr/sbin");
  21. paths.emplace_back("/usr/bin");
  22. paths.emplace_back("/bin");
  23. paths.emplace_back("/sbin");
  24. paths.emplace_back("/usr/local/bin");
  25. const char* cacheValue = status.GetMakefile().GetDefinition(args[0]);
  26. if (cacheValue) {
  27. return true;
  28. }
  29. const char* temp = status.GetMakefile().GetDefinition("HOSTNAME");
  30. std::string hostname_cmd;
  31. if (temp) {
  32. hostname_cmd = temp;
  33. } else {
  34. hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
  35. }
  36. std::string siteName = "unknown";
  37. #if defined(_WIN32) && !defined(__CYGWIN__)
  38. std::string host;
  39. if (cmSystemTools::ReadRegistryValue(
  40. "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
  41. "Control\\ComputerName\\ComputerName;ComputerName",
  42. host)) {
  43. siteName = host;
  44. }
  45. #else
  46. // try to find the hostname for this computer
  47. if (!cmIsOff(hostname_cmd)) {
  48. std::string host;
  49. cmSystemTools::RunSingleCommand(hostname_cmd, &host, nullptr, nullptr,
  50. nullptr, 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. status.GetMakefile().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. }