/// <summary> /// Compiles the given compilation to a file. /// </summary> /// <param name="compilation">Compilation</param> /// <param name="outputKind">OutputKind</param> /// <param name="outputPath">OutputPath</param> /// <returns>Output</returns> private static string ToFile(CodeAnalysis.Compilation compilation, OutputKind outputKind, string outputPath) { string assemblyFileName = null; if (outputKind == OutputKind.ConsoleApplication) { assemblyFileName = compilation.AssemblyName + ".exe"; } else if (outputKind == OutputKind.DynamicallyLinkedLibrary) { assemblyFileName = compilation.AssemblyName + ".dll"; } string fileName = null; if (!Configuration.OutputFilePath.Equals("")) { fileName = Configuration.OutputFilePath + Path.DirectorySeparatorChar + assemblyFileName; CompilationEngine.OutputDirectoryMap.Add(compilation.AssemblyName, Configuration.OutputFilePath); } else { fileName = Path.GetDirectoryName(outputPath) + Path.DirectorySeparatorChar + assemblyFileName; CompilationEngine.OutputDirectoryMap.Add(compilation.AssemblyName, Path.GetDirectoryName(outputPath)); } CompilationEngine.ProjectAssemblyPathMap.Add(compilation.AssemblyName, fileName); // Link external references. CompilationEngine.LinkExternalAssembliesToProject(compilation); EmitResult emitResult = null; using (var outputFile = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { emitResult = compilation.Emit(outputFile); if (emitResult.Success) { Output.Print("... Writing " + fileName); return(fileName); } } Output.Print("---"); Output.Print("Note: the errors below correspond to the intermediate C#-IR, " + "which can be printed using /debug."); Output.Print("---"); var message = string.Join("\r\n", emitResult.Diagnostics); throw new ApplicationException(message); }
/// <summary> /// Compiles the given P# project. /// </summary> /// <param name="project">Project</param> private static void CompileProject(Project project) { var runtimeDllPath = typeof(Dispatcher).Assembly.Location; var bugFindingRuntimeDllPath = typeof(BugFindingDispatcher).Assembly.Location; var runtimeDll = project.MetadataReferences.FirstOrDefault(val => val.Display.EndsWith( Path.DirectorySeparatorChar + "Microsoft.PSharp.Runtime.dll")); if (runtimeDll != null && (Configuration.RunStaticAnalysis || Configuration.RunDynamicAnalysis)) { project = project.RemoveMetadataReference(runtimeDll); } if ((Configuration.RunStaticAnalysis || Configuration.RunDynamicAnalysis) && !project.MetadataReferences.Any(val => val.Display.EndsWith( Path.DirectorySeparatorChar + "Microsoft.PSharp.BugFindingRuntime.dll"))) { project = project.AddMetadataReference(MetadataReference.CreateFromFile( bugFindingRuntimeDllPath)); } var compilation = project.GetCompilationAsync().Result; try { if (Configuration.RunDynamicAnalysis) { var dll = CompilationEngine.ToFile(compilation, OutputKind.DynamicallyLinkedLibrary, project.OutputFilePath); if (Configuration.ProjectName.Equals(project.Name)) { Configuration.AssembliesToBeAnalyzed.Add(dll); } } else if (Configuration.CompileForDistribution) { CompilationEngine.ToFile(compilation, OutputKind.DynamicallyLinkedLibrary, project.OutputFilePath); } else { CompilationEngine.ToFile(compilation, project.CompilationOptions.OutputKind, project.OutputFilePath); } } catch (ApplicationException ex) { ErrorReporter.ReportAndExit(ex.Message); } }
/// <summary> /// Runs the P# compilation engine. /// </summary> public static void Run() { CompilationEngine.ProjectAssemblyPathMap = new Dictionary <string, string>(); CompilationEngine.OutputDirectoryMap = new Dictionary <string, string>(); var graph = ProgramInfo.Solution.GetProjectDependencyGraph(); if (Configuration.ProjectName.Equals("")) { foreach (var projectId in graph.GetTopologicallySortedProjects()) { var project = ProgramInfo.Solution.GetProject(projectId); CompilationEngine.CompileProject(project); CompilationEngine.LinkSolutionAssembliesToProject(project, graph); } } else { // Find the project specified by the user. var targetProject = ProgramInfo.GetProjectWithName(Configuration.ProjectName); var projectDependencies = graph.GetProjectsThatThisProjectTransitivelyDependsOn(targetProject.Id); foreach (var projectId in graph.GetTopologicallySortedProjects()) { if (!projectDependencies.Contains(projectId) && !projectId.Equals(targetProject.Id)) { continue; } var project = ProgramInfo.Solution.GetProject(projectId); CompilationEngine.CompileProject(project); CompilationEngine.LinkSolutionAssembliesToProject(project, graph); } } // Links the P# core library. CompilationEngine.LinkAssembly(typeof(Machine).Assembly, "Microsoft.PSharp.dll"); // Links the P# runtime. if (Configuration.RunStaticAnalysis || Configuration.RunDynamicAnalysis) { CompilationEngine.LinkAssembly(typeof(BugFindingDispatcher).Assembly, "Microsoft.PSharp.BugFindingRuntime.dll"); } else { CompilationEngine.LinkAssembly(typeof(Dispatcher).Assembly, "Microsoft.PSharp.Runtime.dll"); } }