我有几个不同的建议
您可能想使用 MSBuild 而不是 NMake 进行研究
它更复杂,但可以直接从 .Net 控制,它是 VS 项目文件的格式,适用于从 VS 2010 开始的所有项目,以及 C#/VB/等。比这更早的项目
您可以使用一个小的帮助程序捕获环境并将其注入您的进程
这可能有点矫枉过正,但它会起作用。vsvarsall.bat 没有比设置一些环境变量更神奇的事情了,所以您所要做的就是记录运行它的结果,然后将其重播到您创建的进程的环境中。
帮助程序(envcapture.exe) 很简单。它只是列出其环境中的所有变量并将它们打印到标准输出。这是整个程序代码;坚持下去Main()
:
XElement documentElement = new XElement("Environment");
foreach (DictionaryEntry envVariable in Environment.GetEnvironmentVariables())
{
documentElement.Add(new XElement(
"Variable",
new XAttribute("Name", envVariable.Key),
envVariable.Value
));
}
Console.WriteLine(documentElement);
您可能只需调用而不是该程序并解析该输出即可逃脱set
,但是如果任何环境变量包含换行符,则可能会中断。
在您的主程序中:
首先,必须捕获由 vcvarsall.bat 初始化的环境。为此,我们将使用一个类似于cmd.exe /s /c " "...\vcvarsall.bat" x86 && "...\envcapture.exe" "
. vcvarsall.bat 修改环境,然后 envcapture.exe 打印出来。然后,主程序捕获该输出并将其解析为字典。(注意:vsVersion
这里可能是 90 或 100 或 110)
private static Dictionary<string, string> CaptureBuildEnvironment(
int vsVersion,
string architectureName
)
{
// assume the helper is in the same directory as this exe
string myExeDir = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location
);
string envCaptureExe = Path.Combine(myExeDir, "envcapture.exe");
string vsToolsVariableName = String.Format("VS{0}COMNTOOLS", vsVersion);
string envSetupScript = Path.Combine(
Environment.GetEnvironmentVariable(vsToolsVariableName),
@"..\..\VC\vcvarsall.bat"
);
using (Process envCaptureProcess = new Process())
{
envCaptureProcess.StartInfo.FileName = "cmd.exe";
// the /s and the extra quotes make sure that paths with
// spaces in the names are handled properly
envCaptureProcess.StartInfo.Arguments = String.Format(
"/s /c \" \"{0}\" {1} && \"{2}\" \"",
envSetupScript,
architectureName,
envCaptureExe
);
envCaptureProcess.StartInfo.RedirectStandardOutput = true;
envCaptureProcess.StartInfo.RedirectStandardError = true;
envCaptureProcess.StartInfo.UseShellExecute = false;
envCaptureProcess.StartInfo.CreateNoWindow = true;
envCaptureProcess.Start();
// read and discard standard error, or else we won't get output from
// envcapture.exe at all
envCaptureProcess.ErrorDataReceived += (sender, e) => { };
envCaptureProcess.BeginErrorReadLine();
string outputString = envCaptureProcess.StandardOutput.ReadToEnd();
// vsVersion < 110 prints out a line in vcvars*.bat. Ignore
// everything before the first '<'.
int xmlStartIndex = outputString.IndexOf('<');
if (xmlStartIndex == -1)
{
throw new Exception("No environment block was captured");
}
XElement documentElement = XElement.Parse(
outputString.Substring(xmlStartIndex)
);
Dictionary<string, string> capturedVars
= new Dictionary<string, string>();
foreach (XElement variable in documentElement.Elements("Variable"))
{
capturedVars.Add(
(string)variable.Attribute("Name"),
(string)variable
);
}
return capturedVars;
}
}
稍后,当你想在构建环境中运行命令时,只需将新进程中的环境变量替换为之前捕获的环境变量即可。CaptureBuildEnvironment
每次运行程序时,您应该只需要为每个参数组合调用一次。不要尝试在运行之间保存它,否则它会变得陈旧。
static void Main()
{
string command = "nmake";
string args = "";
Dictionary<string, string> buildEnvironment =
CaptureBuildEnvironment(100, "x86");
ProcessStartInfo info = new ProcessStartInfo();
// the search path from the adjusted environment doesn't seem
// to get used in Process.Start, but cmd will use it.
info.FileName = "cmd.exe";
info.Arguments = String.Format(
"/s /c \" \"{0}\" {1} \"",
command,
args
);
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
foreach (var i in buildEnvironment)
{
info.EnvironmentVariables[(string)i.Key] = (string)i.Value;
}
using (Process p = Process.Start(info))
{
// do something with your process. If you're capturing standard output,
// you'll also need to capture standard error. Be careful to avoid the
// deadlock bug mentioned in the docs for
// ProcessStartInfo.RedirectStandardOutput.
}
}
如果您使用它,请注意,如果 vcvarsall.bat 丢失或失败,它可能会严重死机,并且使用非 en-US 语言环境的系统可能会出现问题。