Browse Source

cmSystemTools: Add GetComspec method to get cmd on Windows

The function attempts to read the path to cmd executable from the
COMSPEC environment variable. Falls back to cmd.exe if the respective
environment variable is not set or path doesn't exist.
Alexey Edelev 1 year ago
parent
commit
9ab270f47d
3 changed files with 15 additions and 7 deletions
  1. 1 7
      Source/cmGlobalNinjaGenerator.cxx
  2. 11 0
      Source/cmSystemTools.cxx
  3. 3 0
      Source/cmSystemTools.h

+ 1 - 7
Source/cmGlobalNinjaGenerator.cxx

@@ -573,13 +573,7 @@ cmGlobalNinjaGenerator::cmGlobalNinjaGenerator(cmake* cm)
   cm->GetState()->SetWindowsShell(true);
   cm->GetState()->SetWindowsShell(true);
 
 
   // Attempt to use full path to COMSPEC, default "cmd.exe"
   // Attempt to use full path to COMSPEC, default "cmd.exe"
-  std::string comspec;
-  if (cmSystemTools::GetEnv("COMSPEC", comspec) &&
-      cmSystemTools::FileIsFullPath(comspec)) {
-    this->Comspec = comspec;
-  } else {
-    this->Comspec = "cmd.exe";
-  }
+  this->Comspec = cmSystemTools::GetComspec();
 #endif
 #endif
   cm->GetState()->SetNinja(true);
   cm->GetState()->SetNinja(true);
   this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
   this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";

+ 11 - 0
Source/cmSystemTools.cxx

@@ -970,6 +970,17 @@ cmSystemTools::WindowsVersion cmSystemTools::GetWindowsVersion()
   result.dwBuildNumber = osviex.dwBuildNumber;
   result.dwBuildNumber = osviex.dwBuildNumber;
   return result;
   return result;
 }
 }
+
+std::string cmSystemTools::GetComspec()
+{
+  std::string comspec;
+  if (!cmSystemTools::GetEnv("COMSPEC", comspec) ||
+      !cmSystemTools::FileIsFullPath(comspec)) {
+    comspec = "cmd.exe";
+  }
+  return comspec;
+}
+
 #endif
 #endif
 
 
 std::string cmSystemTools::GetRealPathResolvingWindowsSubst(
 std::string cmSystemTools::GetRealPathResolvingWindowsSubst(

+ 3 - 0
Source/cmSystemTools.h

@@ -585,6 +585,9 @@ public:
     unsigned int dwBuildNumber;
     unsigned int dwBuildNumber;
   };
   };
   static WindowsVersion GetWindowsVersion();
   static WindowsVersion GetWindowsVersion();
+
+  /** Attempt to get full path to COMSPEC, default "cmd.exe" */
+  static std::string GetComspec();
 #endif
 #endif
 
 
   /** Get the real path for a given path, removing all symlinks.
   /** Get the real path for a given path, removing all symlinks.