private static void BuildDotNet(TaskGraph graph, BuildData buildData, NativeCpp.BuildOptions buildOptions, string name, List <string> sourceFiles, HashSet <string> fileReferences = null) { // Setup build options var buildPlatform = Platform.BuildTargetPlatform; var outputPath = Path.GetDirectoryName(buildData.Target.GetOutputFilePath(buildOptions)); var outputFile = Path.Combine(outputPath, name + ".dll"); var outputDocFile = Path.Combine(outputPath, name + ".xml"); string monoRoot, monoPath, cscPath; switch (buildPlatform) { case TargetPlatform.Windows: { monoRoot = Path.Combine(Globals.EngineRoot, "Source", "Platforms", "Editor", "Windows", "Mono"); // Prefer installed Roslyn C# compiler over Mono one monoPath = null; cscPath = Path.Combine(Path.GetDirectoryName(VCEnvironment.MSBuildPath), "Roslyn", "csc.exe"); if (!File.Exists(cscPath)) { // Fallback to Mono binaries monoPath = Path.Combine(monoRoot, "bin", "mono.exe"); cscPath = Path.Combine(monoRoot, "lib", "mono", "4.5", "csc.exe"); } break; } case TargetPlatform.Linux: monoRoot = Path.Combine(Globals.EngineRoot, "Source", "Platforms", "Editor", "Linux", "Mono"); monoPath = Path.Combine(monoRoot, "bin", "mono"); cscPath = Path.Combine(monoRoot, "lib", "mono", "4.5", "csc.exe"); break; case TargetPlatform.Mac: monoRoot = Path.Combine(Globals.EngineRoot, "Source", "Platforms", "Editor", "Mac", "Mono"); monoPath = Path.Combine(monoRoot, "bin", "mono"); cscPath = Path.Combine(monoRoot, "lib", "mono", "4.5", "csc.exe"); break; default: throw new InvalidPlatformException(buildPlatform); } var referenceAssemblies = Path.Combine(monoRoot, "lib", "mono", "4.5-api"); if (fileReferences == null) { fileReferences = buildOptions.ScriptingAPI.FileReferences; } else { fileReferences.AddRange(buildOptions.ScriptingAPI.FileReferences); } // Setup C# compiler arguments var args = new List <string>(); args.Clear(); args.Add("/nologo"); args.Add("/target:library"); args.Add("/platform:AnyCPU"); args.Add("/debug+"); args.Add("/debug:portable"); args.Add("/errorreport:prompt"); args.Add("/preferreduilang:en-US"); args.Add("/highentropyva+"); args.Add("/deterministic"); args.Add("/nostdlib+"); args.Add("/errorendlocation"); args.Add("/utf8output"); args.Add("/warn:4"); args.Add("/unsafe"); args.Add("/fullpaths"); args.Add("/langversion:7.3"); if (buildOptions.ScriptingAPI.IgnoreMissingDocumentationWarnings) { args.Add("-nowarn:1591"); } args.Add(buildData.Configuration == TargetConfiguration.Debug ? "/optimize-" : "/optimize+"); args.Add(string.Format("/out:\"{0}\"", outputFile)); args.Add(string.Format("/doc:\"{0}\"", outputDocFile)); if (buildOptions.ScriptingAPI.Defines.Count != 0) { args.Add("/define:" + string.Join(";", buildOptions.ScriptingAPI.Defines)); } if (buildData.Configuration == TargetConfiguration.Debug) { args.Add("/define:DEBUG"); } args.Add(string.Format("/reference:\"{0}{1}mscorlib.dll\"", referenceAssemblies, Path.DirectorySeparatorChar)); foreach (var reference in buildOptions.ScriptingAPI.SystemReferences) { args.Add(string.Format("/reference:\"{0}{2}{1}.dll\"", referenceAssemblies, reference, Path.DirectorySeparatorChar)); } foreach (var reference in fileReferences) { args.Add(string.Format("/reference:\"{0}\"", reference)); } foreach (var sourceFile in sourceFiles) { args.Add("\"" + sourceFile + "\""); } // Generate response file with source files paths and compilation arguments string responseFile = Path.Combine(buildOptions.IntermediateFolder, name + ".response"); Utilities.WriteFileIfChanged(responseFile, string.Join(Environment.NewLine, args)); // Create C# compilation task var task = graph.Add <Task>(); task.PrerequisiteFiles.Add(responseFile); task.PrerequisiteFiles.AddRange(sourceFiles); task.PrerequisiteFiles.AddRange(fileReferences); task.ProducedFiles.Add(outputFile); task.WorkingDirectory = buildData.TargetOptions.WorkingDirectory; task.InfoMessage = "Compiling " + outputFile; task.Cost = task.PrerequisiteFiles.Count; if (monoPath != null) { task.CommandPath = monoPath; task.CommandArguments = $"\"{cscPath}\" /noconfig @\"{responseFile}\""; } else { // The "/shared" flag enables the compiler server support: // https://github.com/dotnet/roslyn/blob/main/docs/compilers/Compiler%20Server.md task.CommandPath = cscPath; task.CommandArguments = $"/noconfig /shared @\"{responseFile}\""; } // Copy referenced assemblies foreach (var srcFile in buildOptions.ScriptingAPI.FileReferences) { var dstFile = Path.Combine(outputPath, Path.GetFileName(srcFile)); if (dstFile == srcFile || graph.HasCopyTask(dstFile, srcFile)) { continue; } graph.AddCopyFile(dstFile, srcFile); var srcPdb = Path.ChangeExtension(srcFile, "pdb"); if (File.Exists(srcPdb)) { graph.AddCopyFile(Path.ChangeExtension(dstFile, "pdb"), srcPdb); } var srcXml = Path.ChangeExtension(srcFile, "xml"); if (File.Exists(srcXml)) { graph.AddCopyFile(Path.ChangeExtension(dstFile, "xml"), srcXml); } } }