From 3d576bf685bf5cd8c0af5622437390b9a60c16af Mon Sep 17 00:00:00 2001 From: Michael Voorhees Date: Thu, 12 Mar 2020 10:52:38 -0400 Subject: [PATCH] Make roslyn the default compiler for test cases Using the CodeDomCompiler has been a constant source of windows only test failures being introduced. I believe the reason CodeDomCompiler was still sticking around was because it was faster than roslyn but with server mode now widely supported and so many of our tests simply depending on roslyn now I don't think there is any advantage to using CodeDomCompiler anymore. We switch to using roslyn by default a long time ago and it's been seamless so I expect making this same change upstream will be seamless as well. Note that I did add /shared to our csc command line now. Without it, roslyn is noticeably slowing than CodeDomCompiler. --- .../TestCasesRunner/TestCaseCompiler.cs | 51 +++---------------- 1 file changed, 8 insertions(+), 43 deletions(-) diff --git a/test/Mono.Linker.Tests/TestCasesRunner/TestCaseCompiler.cs b/test/Mono.Linker.Tests/TestCasesRunner/TestCaseCompiler.cs index 57e092286ac3..782fcbcd196a 100644 --- a/test/Mono.Linker.Tests/TestCasesRunner/TestCaseCompiler.cs +++ b/test/Mono.Linker.Tests/TestCasesRunner/TestCaseCompiler.cs @@ -194,7 +194,7 @@ protected virtual NPath CompileCSharpAssemblyWithDefaultCompiler (CompilerOption #if NETCOREAPP return CompileCSharpAssemblyWithRoslyn (options); #else - return CompileCSharpAssemblyWithCodeDom (options); + return CompileCSharpAssemblyWithCsc (options); #endif } @@ -286,43 +286,29 @@ protected virtual NPath CompileCSharpAssemblyWithRoslyn (CompilerOptions options } #endif - protected virtual NPath CompileCSharpAssemblyWithCodeDom (CompilerOptions options) - { - var compilerOptions = CreateCodeDomCompilerOptions (options); - var provider = CodeDomProvider.CreateProvider ("C#"); - var result = provider.CompileAssemblyFromFile (compilerOptions, options.SourceFiles.Select (p => p.ToString ()).ToArray ()); - if (!result.Errors.HasErrors) - return compilerOptions.OutputAssembly.ToNPath (); - - var errors = new StringBuilder (); - foreach (var error in result.Errors) - errors.AppendLine (error.ToString ()); - throw new Exception ("Compilation errors: " + errors); - } - protected virtual NPath CompileCSharpAssemblyWithCsc (CompilerOptions options) { #if NETCOREAPP return CompileCSharpAssemblyWithRoslyn (options); #else - return CompileCSharpAssemblyWithExternalCompiler (LocateCscExecutable (), options); + return CompileCSharpAssemblyWithExternalCompiler (LocateCscExecutable (), options, "/shared "); #endif } protected virtual NPath CompileCSharpAssemblyWithMcs(CompilerOptions options) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) - CompileCSharpAssemblyWithExternalCompiler (LocateMcsExecutable (), options); + CompileCSharpAssemblyWithExternalCompiler (LocateMcsExecutable (), options, string.Empty); return CompileCSharpAssemblyWithDefaultCompiler (options); } - protected NPath CompileCSharpAssemblyWithExternalCompiler (string executable, CompilerOptions options) + protected NPath CompileCSharpAssemblyWithExternalCompiler (string executable, CompilerOptions options, string compilerSpecificArguments) { var capturedOutput = new List (); var process = new Process (); process.StartInfo.FileName = executable; - process.StartInfo.Arguments = OptionsToCompilerCommandLineArguments (options); + process.StartInfo.Arguments = OptionsToCompilerCommandLineArguments (options, compilerSpecificArguments); process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; @@ -397,9 +383,11 @@ static string LocateMcsExecutable () return "mcs"; } - protected string OptionsToCompilerCommandLineArguments (CompilerOptions options) + protected string OptionsToCompilerCommandLineArguments (CompilerOptions options, string compilerSpecificArguments) { var builder = new StringBuilder (); + if (!string.IsNullOrEmpty(compilerSpecificArguments)) + builder.Append (compilerSpecificArguments); builder.Append ($"/out:{options.OutputPath}"); var target = options.OutputPath.ExtensionWithDot == ".exe" ? "exe" : "library"; builder.Append ($" /target:{target}"); @@ -437,28 +425,5 @@ protected NPath CompileIlAssembly (CompilerOptions options) { return _ilCompiler.Compile (options); } - - private CompilerParameters CreateCodeDomCompilerOptions (CompilerOptions options) - { - var compilerParameters = new CompilerParameters - { - OutputAssembly = options.OutputPath.ToString (), - GenerateExecutable = options.OutputPath.FileName.EndsWith (".exe") - }; - - compilerParameters.CompilerOptions = options.Defines?.Aggregate (string.Empty, (buff, arg) => $"{buff} /define:{arg}"); - - compilerParameters.ReferencedAssemblies.AddRange (options.References.Select (r => r.ToString ()).ToArray ()); - - if (options.Resources != null) - compilerParameters.EmbeddedResources.AddRange (options.Resources.Select (r => r.ToString ()).ToArray ()); - - if (options.AdditionalArguments != null) { - var combinedValues = options.AdditionalArguments.Aggregate (string.Empty, (buff, arg) => $"{buff} {arg}"); - compilerParameters.CompilerOptions = $"{compilerParameters.CompilerOptions} {combinedValues}"; - } - - return compilerParameters; - } } } \ No newline at end of file