Mono.CSharp.Driver.Compile C# (CSharp) Method

Compile() public method

public Compile ( ) : bool
return bool
		public bool Compile ()
		{
			var module = new ModuleContainer (ctx);
			RootContext.ToplevelTypes = module;

			if (timestamps) {
				stopwatch = Stopwatch.StartNew ();
				first_time = DateTime.Now;
			}

			Parse (module);
			ShowTime ("Parsing source files");

			if (Report.Errors > 0)
				return false;

			if (RootContext.TokenizeOnly || RootContext.ParseOnly)
				return true;

			if (RootContext.ToplevelTypes.NamespaceEntry != null)
				throw new InternalErrorException ("who set it?");

			//
			// Quick hack
			//
			var output_file = RootContext.OutputFile;
			string output_file_name;
			if (output_file == null) {
				if (first_source == null) {
					Report.Error (1562, "If no source files are specified you must specify the output file with -out:");
					return false;
				}

				int pos = first_source.LastIndexOf ('.');

				if (pos > 0)
					output_file = first_source.Substring (0, pos) + RootContext.TargetExt;
				else
					output_file = first_source + RootContext.TargetExt;

				output_file_name = output_file;
			} else {
				output_file_name = Path.GetFileName (output_file);
			}

			//
			// Load assemblies required
			//
			if (timestamps)
				stopwatch = Stopwatch.StartNew ();

#if STATIC
			var importer = new StaticImporter ();
			var references_loader = new StaticLoader (importer, ctx);

			var assembly = new AssemblyDefinitionStatic (module, references_loader, output_file_name, output_file);
			assembly.Create (references_loader.Domain);

			// Create compiler types first even before any referenced
			// assembly is loaded to allow forward referenced types from
			// loaded assembly into compiled builder to be resolved
			// correctly
			module.CreateType ();
			ShowTime ("Creating compiled types");

			importer.AddCompiledAssembly (assembly);
			references_loader.LoadReferences (module);
			ShowTime ("Imporing referenced assemblies");

			if (!ctx.BuildinTypes.CheckDefinitions (module))
				return false;

			ShowTime ("Initializing predefined types");

			references_loader.LoadModules (assembly, module.GlobalRootNamespace);
#else
			var assembly = new AssemblyDefinitionDynamic (module, output_file_name, output_file);
			module.SetDeclaringAssembly (assembly);

			var importer = new ReflectionImporter (ctx.BuildinTypes);
			assembly.Importer = importer;

			var loader = new DynamicLoader (importer, ctx);
			loader.LoadReferences (module);

			ShowTime ("Imporing referenced assemblies");

			if (!ctx.BuildinTypes.CheckDefinitions (module))
				return false;

			ShowTime ("Initializing predefined types");

			if (!assembly.Create (AppDomain.CurrentDomain, AssemblyBuilderAccess.Save))
				return false;

			loader.LoadModules (assembly, module.GlobalRootNamespace);
#endif
			module.Define ();

			ShowTime ("Types definition");

			if (Report.Errors > 0)
				return false;

			if (Report.Errors == 0 &&
				RootContext.Documentation != null &&
				!RootContext.Documentation.OutputDocComment (
					output_file, Report))
				return false;

			//
			// Verify using aliases now
			//
			NamespaceEntry.VerifyAllUsing ();
			
			if (Report.Errors > 0){
				return false;
			}

			assembly.Resolve ();
			
			if (Report.Errors > 0)
				return false;
			
			//
			// The code generator
			//
			if (timestamps)
				stopwatch = Stopwatch.StartNew ();

			assembly.Emit ();

			ShowTime ("Resolving and emitting members blocks");

			if (Report.Errors > 0){
				return false;
			}

			module.CloseType ();

			ShowTime ("Closing types");

			if (timestamps)
				stopwatch = Stopwatch.StartNew ();

			assembly.EmbedResources ();
			ShowTime ("Embedding resources");

			if (Report.Errors > 0)
				return false;

			if (timestamps)
				stopwatch = Stopwatch.StartNew ();
			
			assembly.Save ();

#if STATIC
			references_loader.Dispose ();
#endif

			ShowTime ("Saving output assembly");

			ShowTotalTime ("Total");

			Timer.ShowTimers ();

			return (Report.Errors == 0);
		}
	}

Usage Example

Example #1
0
        public static int Main(string[] args)
        {
            Location.InEmacs = Environment.GetEnvironmentVariable("EMACS") == "t";

            CommandLineParser cmd = new CommandLineParser(Console.Out);
            var settings          = cmd.ParseArguments(args);

            if (settings == null)
            {
                return(1);
            }

            if (cmd.HasBeenStopped)
            {
                return(0);
            }

            Driver d = new Driver(new CompilerContext(settings, new ConsoleReportPrinter()));

            if (d.Compile() && d.Report.Errors == 0)
            {
                if (d.Report.Warnings > 0)
                {
                    Console.WriteLine("Compilation succeeded - {0} warning(s)", d.Report.Warnings);
                }
                Environment.Exit(0);
                return(0);
            }


            Console.WriteLine("Compilation failed: {0} error(s), {1} warnings",
                              d.Report.Errors, d.Report.Warnings);
            Environment.Exit(1);
            return(1);
        }
All Usage Examples Of Mono.CSharp.Driver::Compile