Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 8 additions & 43 deletions test/Mono.Linker.Tests/TestCasesRunner/TestCaseCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected virtual NPath CompileCSharpAssemblyWithDefaultCompiler (CompilerOption
#if NETCOREAPP
return CompileCSharpAssemblyWithRoslyn (options);
#else
return CompileCSharpAssemblyWithCodeDom (options);
return CompileCSharpAssemblyWithCsc (options);
#endif
}

Expand Down Expand Up @@ -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<string> ();
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;
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -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;
}
}
}