BuildSystem.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. using System.Security;
  9. using Microsoft.Build.Framework;
  10. namespace GodotSharpTools.Build
  11. {
  12. public class BuildInstance : IDisposable
  13. {
  14. [MethodImpl(MethodImplOptions.InternalCall)]
  15. private extern static void godot_icall_BuildInstance_ExitCallback(string solution, string config, int exitCode);
  16. [MethodImpl(MethodImplOptions.InternalCall)]
  17. private extern static string godot_icall_BuildInstance_get_MSBuildPath();
  18. [MethodImpl(MethodImplOptions.InternalCall)]
  19. private extern static string godot_icall_BuildInstance_get_MonoWindowsBinDir();
  20. [MethodImpl(MethodImplOptions.InternalCall)]
  21. private extern static bool godot_icall_BuildInstance_get_UsingMonoMSBuildOnWindows();
  22. [MethodImpl(MethodImplOptions.InternalCall)]
  23. private extern static bool godot_icall_BuildInstance_get_PrintBuildOutput();
  24. private static string GetMSBuildPath()
  25. {
  26. string msbuildPath = godot_icall_BuildInstance_get_MSBuildPath();
  27. if (msbuildPath == null)
  28. throw new FileNotFoundException("Cannot find the MSBuild executable.");
  29. return msbuildPath;
  30. }
  31. private static string MonoWindowsBinDir
  32. {
  33. get
  34. {
  35. string monoWinBinDir = godot_icall_BuildInstance_get_MonoWindowsBinDir();
  36. if (monoWinBinDir == null)
  37. throw new FileNotFoundException("Cannot find the Windows Mono binaries directory.");
  38. return monoWinBinDir;
  39. }
  40. }
  41. private static bool UsingMonoMSBuildOnWindows
  42. {
  43. get
  44. {
  45. return godot_icall_BuildInstance_get_UsingMonoMSBuildOnWindows();
  46. }
  47. }
  48. private static bool PrintBuildOutput
  49. {
  50. get
  51. {
  52. return godot_icall_BuildInstance_get_PrintBuildOutput();
  53. }
  54. }
  55. private string solution;
  56. private string config;
  57. private Process process;
  58. private int exitCode;
  59. public int ExitCode { get { return exitCode; } }
  60. public bool IsRunning { get { return process != null && !process.HasExited; } }
  61. public BuildInstance(string solution, string config)
  62. {
  63. this.solution = solution;
  64. this.config = config;
  65. }
  66. public bool Build(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties = null)
  67. {
  68. List<string> customPropertiesList = new List<string>();
  69. if (customProperties != null)
  70. customPropertiesList.AddRange(customProperties);
  71. string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customPropertiesList);
  72. ProcessStartInfo startInfo = new ProcessStartInfo(GetMSBuildPath(), compilerArgs);
  73. bool redirectOutput = !IsDebugMSBuildRequested() && !PrintBuildOutput;
  74. if (!redirectOutput) // TODO: or if stdout verbose
  75. Console.WriteLine($"Running: \"{startInfo.FileName}\" {startInfo.Arguments}");
  76. startInfo.RedirectStandardOutput = redirectOutput;
  77. startInfo.RedirectStandardError = redirectOutput;
  78. startInfo.UseShellExecute = false;
  79. if (UsingMonoMSBuildOnWindows)
  80. {
  81. // These environment variables are required for Mono's MSBuild to find the compilers.
  82. // We use the batch files in Mono's bin directory to make sure the compilers are executed with mono.
  83. string monoWinBinDir = MonoWindowsBinDir;
  84. startInfo.EnvironmentVariables.Add("CscToolExe", Path.Combine(monoWinBinDir, "csc.bat"));
  85. startInfo.EnvironmentVariables.Add("VbcToolExe", Path.Combine(monoWinBinDir, "vbc.bat"));
  86. startInfo.EnvironmentVariables.Add("FscToolExe", Path.Combine(monoWinBinDir, "fsharpc.bat"));
  87. }
  88. // Needed when running from Developer Command Prompt for VS
  89. RemovePlatformVariable(startInfo.EnvironmentVariables);
  90. using (Process process = new Process())
  91. {
  92. process.StartInfo = startInfo;
  93. process.Start();
  94. if (redirectOutput)
  95. {
  96. process.BeginOutputReadLine();
  97. process.BeginErrorReadLine();
  98. }
  99. process.WaitForExit();
  100. exitCode = process.ExitCode;
  101. }
  102. return true;
  103. }
  104. public bool BuildAsync(string loggerAssemblyPath, string loggerOutputDir, string[] customProperties = null)
  105. {
  106. if (process != null)
  107. throw new InvalidOperationException("Already in use");
  108. List<string> customPropertiesList = new List<string>();
  109. if (customProperties != null)
  110. customPropertiesList.AddRange(customProperties);
  111. string compilerArgs = BuildArguments(loggerAssemblyPath, loggerOutputDir, customPropertiesList);
  112. ProcessStartInfo startInfo = new ProcessStartInfo(GetMSBuildPath(), compilerArgs);
  113. bool redirectOutput = !IsDebugMSBuildRequested() && !PrintBuildOutput;
  114. if (!redirectOutput) // TODO: or if stdout verbose
  115. Console.WriteLine($"Running: \"{startInfo.FileName}\" {startInfo.Arguments}");
  116. startInfo.RedirectStandardOutput = redirectOutput;
  117. startInfo.RedirectStandardError = redirectOutput;
  118. startInfo.UseShellExecute = false;
  119. if (UsingMonoMSBuildOnWindows)
  120. {
  121. // These environment variables are required for Mono's MSBuild to find the compilers.
  122. // We use the batch files in Mono's bin directory to make sure the compilers are executed with mono.
  123. string monoWinBinDir = MonoWindowsBinDir;
  124. startInfo.EnvironmentVariables.Add("CscToolExe", Path.Combine(monoWinBinDir, "csc.bat"));
  125. startInfo.EnvironmentVariables.Add("VbcToolExe", Path.Combine(monoWinBinDir, "vbc.bat"));
  126. startInfo.EnvironmentVariables.Add("FscToolExe", Path.Combine(monoWinBinDir, "fsharpc.bat"));
  127. }
  128. // Needed when running from Developer Command Prompt for VS
  129. RemovePlatformVariable(startInfo.EnvironmentVariables);
  130. process = new Process();
  131. process.StartInfo = startInfo;
  132. process.EnableRaisingEvents = true;
  133. process.Exited += new EventHandler(BuildProcess_Exited);
  134. process.Start();
  135. if (redirectOutput)
  136. {
  137. process.BeginOutputReadLine();
  138. process.BeginErrorReadLine();
  139. }
  140. return true;
  141. }
  142. private string BuildArguments(string loggerAssemblyPath, string loggerOutputDir, List<string> customProperties)
  143. {
  144. string arguments = string.Format(@"""{0}"" /v:normal /t:Rebuild ""/p:{1}"" ""/l:{2},{3};{4}""",
  145. solution,
  146. "Configuration=" + config,
  147. typeof(GodotBuildLogger).FullName,
  148. loggerAssemblyPath,
  149. loggerOutputDir
  150. );
  151. foreach (string customProperty in customProperties)
  152. {
  153. arguments += " /p:" + customProperty;
  154. }
  155. return arguments;
  156. }
  157. private void RemovePlatformVariable(StringDictionary environmentVariables)
  158. {
  159. // EnvironmentVariables is case sensitive? Seriously?
  160. List<string> platformEnvironmentVariables = new List<string>();
  161. foreach (string env in environmentVariables.Keys)
  162. {
  163. if (env.ToUpper() == "PLATFORM")
  164. platformEnvironmentVariables.Add(env);
  165. }
  166. foreach (string env in platformEnvironmentVariables)
  167. environmentVariables.Remove(env);
  168. }
  169. private void BuildProcess_Exited(object sender, System.EventArgs e)
  170. {
  171. exitCode = process.ExitCode;
  172. godot_icall_BuildInstance_ExitCallback(solution, config, exitCode);
  173. Dispose();
  174. }
  175. private static bool IsDebugMSBuildRequested()
  176. {
  177. return Environment.GetEnvironmentVariable("GODOT_DEBUG_MSBUILD")?.Trim() == "1";
  178. }
  179. public void Dispose()
  180. {
  181. if (process != null)
  182. {
  183. process.Dispose();
  184. process = null;
  185. }
  186. }
  187. }
  188. public class GodotBuildLogger : ILogger
  189. {
  190. public string Parameters { get; set; }
  191. public LoggerVerbosity Verbosity { get; set; }
  192. public void Initialize(IEventSource eventSource)
  193. {
  194. if (null == Parameters)
  195. throw new LoggerException("Log directory was not set.");
  196. string[] parameters = Parameters.Split(new[] { ';' });
  197. string logDir = parameters[0];
  198. if (String.IsNullOrEmpty(logDir))
  199. throw new LoggerException("Log directory was not set.");
  200. if (parameters.Length > 1)
  201. throw new LoggerException("Too many parameters passed.");
  202. string logFile = Path.Combine(logDir, "msbuild_log.txt");
  203. string issuesFile = Path.Combine(logDir, "msbuild_issues.csv");
  204. try
  205. {
  206. if (!Directory.Exists(logDir))
  207. Directory.CreateDirectory(logDir);
  208. this.logStreamWriter = new StreamWriter(logFile);
  209. this.issuesStreamWriter = new StreamWriter(issuesFile);
  210. }
  211. catch (Exception ex)
  212. {
  213. if
  214. (
  215. ex is UnauthorizedAccessException
  216. || ex is ArgumentNullException
  217. || ex is PathTooLongException
  218. || ex is DirectoryNotFoundException
  219. || ex is NotSupportedException
  220. || ex is ArgumentException
  221. || ex is SecurityException
  222. || ex is IOException
  223. )
  224. {
  225. throw new LoggerException("Failed to create log file: " + ex.Message);
  226. }
  227. else
  228. {
  229. // Unexpected failure
  230. throw;
  231. }
  232. }
  233. eventSource.ProjectStarted += new ProjectStartedEventHandler(eventSource_ProjectStarted);
  234. eventSource.TaskStarted += new TaskStartedEventHandler(eventSource_TaskStarted);
  235. eventSource.MessageRaised += new BuildMessageEventHandler(eventSource_MessageRaised);
  236. eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised);
  237. eventSource.ErrorRaised += new BuildErrorEventHandler(eventSource_ErrorRaised);
  238. eventSource.ProjectFinished += new ProjectFinishedEventHandler(eventSource_ProjectFinished);
  239. }
  240. void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e)
  241. {
  242. string line = String.Format("{0}({1},{2}): error {3}: {4}", e.File, e.LineNumber, e.ColumnNumber, e.Code, e.Message);
  243. if (e.ProjectFile.Length > 0)
  244. line += string.Format(" [{0}]", e.ProjectFile);
  245. WriteLine(line);
  246. string errorLine = String.Format(@"error,{0},{1},{2},{3},{4},{5}",
  247. e.File.CsvEscape(), e.LineNumber, e.ColumnNumber,
  248. e.Code.CsvEscape(), e.Message.CsvEscape(), e.ProjectFile.CsvEscape());
  249. issuesStreamWriter.WriteLine(errorLine);
  250. }
  251. void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
  252. {
  253. string line = String.Format("{0}({1},{2}): warning {3}: {4}", e.File, e.LineNumber, e.ColumnNumber, e.Code, e.Message, e.ProjectFile);
  254. if (e.ProjectFile != null && e.ProjectFile.Length > 0)
  255. line += string.Format(" [{0}]", e.ProjectFile);
  256. WriteLine(line);
  257. string warningLine = String.Format(@"warning,{0},{1},{2},{3},{4},{5}",
  258. e.File.CsvEscape(), e.LineNumber, e.ColumnNumber,
  259. e.Code.CsvEscape(), e.Message.CsvEscape(), e.ProjectFile != null ? e.ProjectFile.CsvEscape() : string.Empty);
  260. issuesStreamWriter.WriteLine(warningLine);
  261. }
  262. void eventSource_MessageRaised(object sender, BuildMessageEventArgs e)
  263. {
  264. // BuildMessageEventArgs adds Importance to BuildEventArgs
  265. // Let's take account of the verbosity setting we've been passed in deciding whether to log the message
  266. if ((e.Importance == MessageImportance.High && IsVerbosityAtLeast(LoggerVerbosity.Minimal))
  267. || (e.Importance == MessageImportance.Normal && IsVerbosityAtLeast(LoggerVerbosity.Normal))
  268. || (e.Importance == MessageImportance.Low && IsVerbosityAtLeast(LoggerVerbosity.Detailed))
  269. )
  270. {
  271. WriteLineWithSenderAndMessage(String.Empty, e);
  272. }
  273. }
  274. void eventSource_TaskStarted(object sender, TaskStartedEventArgs e)
  275. {
  276. // TaskStartedEventArgs adds ProjectFile, TaskFile, TaskName
  277. // To keep this log clean, this logger will ignore these events.
  278. }
  279. void eventSource_ProjectStarted(object sender, ProjectStartedEventArgs e)
  280. {
  281. WriteLine(e.Message);
  282. indent++;
  283. }
  284. void eventSource_ProjectFinished(object sender, ProjectFinishedEventArgs e)
  285. {
  286. indent--;
  287. WriteLine(e.Message);
  288. }
  289. /// <summary>
  290. /// Write a line to the log, adding the SenderName
  291. /// </summary>
  292. private void WriteLineWithSender(string line, BuildEventArgs e)
  293. {
  294. if (0 == String.Compare(e.SenderName, "MSBuild", true /*ignore case*/))
  295. {
  296. // Well, if the sender name is MSBuild, let's leave it out for prettiness
  297. WriteLine(line);
  298. }
  299. else
  300. {
  301. WriteLine(e.SenderName + ": " + line);
  302. }
  303. }
  304. /// <summary>
  305. /// Write a line to the log, adding the SenderName and Message
  306. /// (these parameters are on all MSBuild event argument objects)
  307. /// </summary>
  308. private void WriteLineWithSenderAndMessage(string line, BuildEventArgs e)
  309. {
  310. if (0 == String.Compare(e.SenderName, "MSBuild", true /*ignore case*/))
  311. {
  312. // Well, if the sender name is MSBuild, let's leave it out for prettiness
  313. WriteLine(line + e.Message);
  314. }
  315. else
  316. {
  317. WriteLine(e.SenderName + ": " + line + e.Message);
  318. }
  319. }
  320. private void WriteLine(string line)
  321. {
  322. for (int i = indent; i > 0; i--)
  323. {
  324. logStreamWriter.Write("\t");
  325. }
  326. logStreamWriter.WriteLine(line);
  327. }
  328. public void Shutdown()
  329. {
  330. logStreamWriter.Close();
  331. issuesStreamWriter.Close();
  332. }
  333. public bool IsVerbosityAtLeast(LoggerVerbosity checkVerbosity)
  334. {
  335. return this.Verbosity >= checkVerbosity;
  336. }
  337. private StreamWriter logStreamWriter;
  338. private StreamWriter issuesStreamWriter;
  339. private int indent;
  340. }
  341. }