// // Main compilation method // public bool Compile(out AssemblyBuilder outAssembly, AppDomain domain, bool generateInMemory) { var settings = ctx.Settings; outAssembly = null; // // If we are an exe, require a source file for the entry point or // if there is nothing to put in the assembly, and we are not a library // if (settings.FirstSourceFile == null && ((settings.Target == Target.Exe || settings.Target == Target.WinExe || settings.Target == Target.Module) || settings.Resources == null)) { Report.Error(2008, "No files to compile were specified"); return(false); } if (settings.Platform == Platform.AnyCPU32Preferred && (settings.Target == Target.Library || settings.Target == Target.Module)) { Report.Error(4023, "Platform option `anycpu32bitpreferred' is valid only for executables"); return(false); } TimeReporter tr = new TimeReporter(settings.Timestamps); ctx.TimeReporter = tr; tr.StartTotal(); var module = new ModuleContainer(ctx); RootContext.ToplevelTypes = module; tr.Start(TimeReporter.TimerType.ParseTotal); Parse(module); tr.Stop(TimeReporter.TimerType.ParseTotal); if (Report.Errors > 0) { return(false); } if (settings.TokenizeOnly || settings.ParseOnly) { tr.StopTotal(); tr.ShowStats(); return(true); } var output_file = settings.OutputFile; string output_file_name; /* if (output_file == null) * { * var source_file = settings.FirstSourceFile; * * if (source_file == null) * { * Report.Error(1562, "If no source files are specified you must specify the output file with -out:"); * return false; * } * * output_file_name = source_file.Name; * int pos = output_file_name.LastIndexOf('.'); * * if (pos > 0) * output_file_name = output_file_name.Substring(0, pos); * * output_file_name += settings.TargetExt; * output_file = output_file_name; * } * else * {*/ output_file_name = Path.GetFileName(output_file); /* if (string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(output_file_name)) || * output_file_name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) * { * Report.Error(2021, "Output file name is not valid"); * return false; * } * }*/ var assembly = new AssemblyDefinitionDynamic(module, output_file_name, output_file); module.SetDeclaringAssembly(assembly); var importer = new ReflectionImporter(module, ctx.BuiltinTypes); assembly.Importer = importer; var loader = new DynamicLoader(importer, ctx); loader.LoadReferences(module); if (!ctx.BuiltinTypes.CheckDefinitions(module)) { return(false); } if (!assembly.Create(domain, AssemblyBuilderAccess.RunAndSave)) { return(false); } module.CreateContainer(); loader.LoadModules(assembly, module.GlobalRootNamespace); module.InitializePredefinedTypes(); if (settings.GetResourceStrings != null) { module.LoadGetResourceStrings(settings.GetResourceStrings); } tr.Start(TimeReporter.TimerType.ModuleDefinitionTotal); module.Define(); tr.Stop(TimeReporter.TimerType.ModuleDefinitionTotal); if (Report.Errors > 0) { return(false); } if (settings.DocumentationFile != null) { var doc = new DocumentationBuilder(module); doc.OutputDocComment(output_file, settings.DocumentationFile); } assembly.Resolve(); if (Report.Errors > 0) { return(false); } tr.Start(TimeReporter.TimerType.EmitTotal); assembly.Emit(); tr.Stop(TimeReporter.TimerType.EmitTotal); if (Report.Errors > 0) { return(false); } tr.Start(TimeReporter.TimerType.CloseTypes); module.CloseContainer(); tr.Stop(TimeReporter.TimerType.CloseTypes); tr.Start(TimeReporter.TimerType.Resouces); if (!settings.WriteMetadataOnly) { assembly.EmbedResources(); } tr.Stop(TimeReporter.TimerType.Resouces); if (Report.Errors > 0) { return(false); } if (!generateInMemory) { assembly.Save(); } outAssembly = assembly.Builder; tr.StopTotal(); tr.ShowStats(); return(Report.Errors == 0); }
public bool Compile(out AssemblyBuilder assembly, AppDomain domain, bool generateInMemory) { // Get the current settings CompilerSettings settings = context.Settings; // Set the result for quick exit assembly = null; // Check if any source files were supplied if (settings.FirstSourceFile == null && (((MCSTarget)settings.Target == MCSTarget.Exe || (MCSTarget)settings.Target == MCSTarget.WinExe || (MCSTarget)settings.Target == MCSTarget.Module) || settings.Resources == null)) { Report.Error(2008, "No source files specified"); return(false); } // Check for any invalid settings if (settings.Platform == Platform.AnyCPU32Preferred && ((MCSTarget)settings.Target == MCSTarget.Library || (MCSTarget)settings.Target == MCSTarget.Module)) { Report.Error(4023, "The preferred platform '{0}' is only valid on executable outputs", Platform.AnyCPU32Preferred.ToString()); return(false); } // Create the time reporter TimeReporter time = new TimeReporter(settings.Timestamps); context.TimeReporter = time; time.StartTotal(); // Create the module ModuleContainer module = new ModuleContainer(context); RootContext.ToplevelTypes = module; // Start timing the parse stage time.Start(TimeReporter.TimerType.ParseTotal); { // Begin parse Parse(module); } time.Stop(TimeReporter.TimerType.ParseTotal); // Check for any errors if (Report.Errors > 0) { return(false); } // Check for partial compilation if (settings.TokenizeOnly == true || settings.ParseOnly == true) { time.StopTotal(); time.ShowStats(); return(true); } // Get the output file string output = settings.OutputFile; string outputName = Path.GetFileName(output); // Create an assembly defenition AssemblyDefinitionDynamic defenition = new AssemblyDefinitionDynamic(module, outputName, output); module.SetDeclaringAssembly(defenition); ReflectionImporter importer = new ReflectionImporter(module, context.BuiltinTypes); defenition.Importer = importer; DynamicLoader loader = new DynamicLoader(importer, context); loader.LoadReferences(module); // Validate built in types if (context.BuiltinTypes.CheckDefinitions(module) == false) { return(false); } // Create the assmbly in domain if (defenition.Create(domain, AssemblyBuilderAccess.RunAndSave) == false) { return(false); } module.CreateContainer(); loader.LoadModules(defenition, module.GlobalRootNamespace); module.InitializePredefinedTypes(); // Check for any resource strings if (settings.GetResourceStrings != null) { module.LoadGetResourceStrings(settings.GetResourceStrings); } // Time the module defenition time.Start(TimeReporter.TimerType.ModuleDefinitionTotal); { try { // Begin defining module.Define(); } catch { // Failed to define module return(false); } } time.Stop(TimeReporter.TimerType.ModuleDefinitionTotal); // Check for any errors if (Report.Errors > 0) { return(false); } // Check for documentation if (settings.DocumentationFile != null) { // Build the xml docs file DocumentationBuilder docs = new DocumentationBuilder(module); docs.OutputDocComment(output, settings.DocumentationFile); } defenition.Resolve(); // Check for documentation errors if (Report.Errors > 0) { return(false); } // Finally emit the defenition into something useful time.Start(TimeReporter.TimerType.EmitTotal); { // Emit assembly defenition.Emit(); } time.Stop(TimeReporter.TimerType.EmitTotal); // Check for any emit errors if (Report.Errors > 0) { return(false); } // Module cleanup time.Start(TimeReporter.TimerType.CloseTypes); { module.CloseContainer(); } time.Stop(TimeReporter.TimerType.CloseTypes); // Check for embedded resources time.Start(TimeReporter.TimerType.Resouces); { if (settings.WriteMetadataOnly == false) { defenition.EmbedResources(); } } time.Stop(TimeReporter.TimerType.Resouces); // Embedd errors if (Report.Errors > 0) { return(false); } // Check for generate in memory if (generateInMemory == false) { defenition.Save(); } // Store the result assembly = defenition.Builder; time.StopTotal(); time.ShowStats(); // Check for errors return(Report.Errors == 0); }