diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2023-02-24 15:01:13 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@qt.io> | 2023-03-15 13:11:40 +0000 |
commit | efcf7983df0bd07b20b17a9872b1106709117e6b (patch) | |
tree | fdda959e5831a0ac321ddd5f19a076620aae0490 | |
parent | 135f9894039b3b5c96ed6828fe31d3dc038fa1a4 (diff) |
Update function and fix possible null reference exception
Change-Id: I25ac7e3325526faf47adf5518d75b83a32406822
Reviewed-by: Miguel Costa <miguel.costa@qt.io>
-rw-r--r-- | QtVsTools.Core/HelperFunctions.cs | 42 | ||||
-rw-r--r-- | QtVsTools.Core/QMakeImport.cs | 2 |
2 files changed, 25 insertions, 19 deletions
diff --git a/QtVsTools.Core/HelperFunctions.cs b/QtVsTools.Core/HelperFunctions.cs index fdbde281..94704eb2 100644 --- a/QtVsTools.Core/HelperFunctions.cs +++ b/QtVsTools.Core/HelperFunctions.cs @@ -878,19 +878,18 @@ namespace QtVsTools.Core }); public static string VcPath { get; set; } - public static bool SetVCVars(VersionInformation VersionInfo, ProcessStartInfo startInfo) + public static bool SetVcVars(VersionInformation versionInfo, ProcessStartInfo startInfo) { var vm = QtVersionManager.The(); - VersionInfo ??= vm.GetVersionInfo(vm.GetDefaultVersion()); + versionInfo ??= vm.GetVersionInfo(vm.GetDefaultVersion()); if (string.IsNullOrEmpty(VcPath)) return false; // Select vcvars script according to host and target platforms - bool osIs64Bit = System.Environment.Is64BitOperatingSystem; - string comspecPath = Environment.GetEnvironmentVariable("COMSPEC"); - string vcVarsCmd = ""; - switch (VersionInfo.platform()) { + var osIs64Bit = System.Environment.Is64BitOperatingSystem; + var vcVarsCmd = ""; + switch (versionInfo.platform()) { case Platform.x86: vcVarsCmd = Path.Combine(VcPath, osIs64Bit ? @"Auxiliary\Build\vcvarsamd64_x86.bat" @@ -920,25 +919,32 @@ namespace QtVsTools.Core } // Run vcvars and print environment variables - StringBuilder stdOut = new StringBuilder(); - string command = $"/c \"{vcVarsCmd}\" && set"; - var vcVarsStartInfo = new ProcessStartInfo(comspecPath, command); - vcVarsStartInfo.CreateNoWindow = true; - vcVarsStartInfo.UseShellExecute = false; - vcVarsStartInfo.RedirectStandardError = true; - vcVarsStartInfo.RedirectStandardOutput = true; + var stdOut = new StringBuilder(); + var command = $"/c \"{vcVarsCmd}\" && set"; + var comspecPath = Environment.GetEnvironmentVariable("COMSPEC"); + var vcVarsStartInfo = new ProcessStartInfo(comspecPath, command) + { + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardError = true, + RedirectStandardOutput = true + }; + var process = Process.Start(vcVarsStartInfo); + if (process == null) + return false; + process.OutputDataReceived += (_, e) => { if (string.IsNullOrEmpty(e.Data)) return; - e.Data.TrimEnd('\r', '\n'); - if (!string.IsNullOrEmpty(e.Data)) - stdOut.Append($"{e.Data}\r\n"); + var data = e.Data.TrimEnd('\r', '\n'); + if (!string.IsNullOrEmpty(data)) + stdOut.Append($"{data}\r\n"); }; process.BeginOutputReadLine(); process.WaitForExit(); - bool ok = (process.ExitCode == 0); + var ok = (process.ExitCode == 0); process.Close(); if (!ok) return false; @@ -951,7 +957,7 @@ namespace QtVsTools.Core startInfo.Environment[vcVar.Key] = string.Join(";", vcVar.Value); // Warn if cl.exe is not in PATH - string clPath = envVars["PATH"] + var clPath = envVars["PATH"] .Select(path => Path.Combine(path, "cl.exe")) .FirstOrDefault(File.Exists); Messages.Print($"cl: {clPath ?? "NOT FOUND"}"); diff --git a/QtVsTools.Core/QMakeImport.cs b/QtVsTools.Core/QMakeImport.cs index 03d4fe5c..4a18ab92 100644 --- a/QtVsTools.Core/QMakeImport.cs +++ b/QtVsTools.Core/QMakeImport.cs @@ -53,7 +53,7 @@ namespace QtVsTools.Core if (setVcVars) { if (qtVersion is null) OutMsg("Error setting VC vars, Qt version may not be null"); - if (!HelperFunctions.SetVCVars(qtVersion, qmakeProc.StartInfo)) + if (!HelperFunctions.SetVcVars(qtVersion, qmakeProc.StartInfo)) OutMsg("Error setting VC vars"); } if (qmakeProc.Start()) { |