2 // driver.cs: The compiler command line driver.
\r
4 // Author: Miguel de Icaza (miguel@gnu.org)
\r
6 // Licensed under the terms of the GNU GPL
\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
16 using System.Collections;
\r
22 /// The compiler driver.
\r
28 Library, Exe, Module, WinExe
\r
32 // Assemblies references to be linked. Initialized with
\r
33 // mscorlib.dll here.
\r
34 ArrayList references;
\r
37 ArrayList link_paths;
\r
39 RootContext context;
\r
41 bool yacc_verbose = false;
\r
43 int error_count = 0;
\r
45 string first_source;
\r
47 Target target = Target.Exe;
\r
49 public int parse (string input_file)
\r
51 CSharpParser parser;
\r
52 System.IO.Stream input;
\r
56 input = System.IO.File.OpenRead (input_file);
\r
61 parser = new CSharpParser (context, input_file, input);
\r
62 parser.yacc_verbose = yacc_verbose;
\r
64 errors = parser.parse ();
\r
65 } catch (Exception ex) {
\r
66 Console.WriteLine (ex);
\r
67 Console.WriteLine ("Compilation aborted");
\r
74 public void Usage ()
\r
77 "compiler [options] source-files\n\n" +
\r
78 "-v Verbose parsing\n"+
\r
79 "-o Specifies output file\n" +
\r
80 "-L Specifies path for loading assemblies\n" +
\r
81 "--nostdlib Does not load core libraries\n" +
\r
82 "--target Specifies the target (exe, winexe, library, module)\n" +
\r
83 "--dumper Specifies a tree dumper\n" +
\r
84 "-r References an assembly\n");
\r
88 public ITreeDump lookup_dumper (string name)
\r
91 return new Generator.TreeDump ();
\r
93 // if (name == "il")
\r
94 // return new MSIL.Generator ();
\r
99 public static void error (string msg)
\r
101 Console.WriteLine ("Error: " + msg);
\r
104 public static void notice (string msg)
\r
106 Console.WriteLine (msg);
\r
109 public static int Main(string[] args)
\r
111 Driver driver = new Driver (args);
\r
113 return driver.error_count;
\r
116 public int LoadAssembly (string assembly)
\r
120 foreach (string dir in link_paths){
\r
121 string full_path = dir + "/" + assembly;
\r
124 Console.WriteLine ("Loading: " + assembly);
\r
125 a = Assembly.Load (assembly);
\r
126 } catch (FileNotFoundException f) {
\r
127 error ("// File not found: " + full_path);
\r
128 error ("Log: " + f.FusionLog);
\r
130 } catch (BadImageFormatException) {
\r
131 error ("// Bad file format: " + full_path);
\r
133 } catch (FileLoadException f){
\r
134 error ("// File Load Exception: " + full_path);
\r
135 error ("Log: " + f.FusionLog);
\r
137 } catch (ArgumentNullException){
\r
138 error ("// Argument Null exception " + full_path);
\r
142 context.TypeManager.AddAssembly (a);
\r
148 // Loads all assemblies referenced on the command line
\r
150 public int LoadReferences ()
\r
154 foreach (string r in references){
\r
155 errors += LoadAssembly (r);
\r
162 // Parses the arguments, and drives the compilation
\r
165 // TODO: Mostly structured to debug the compiler
\r
166 // now, needs to be turned into a real driver soon.
\r
168 public Driver (string [] args)
\r
170 ITreeDump generator = null;
\r
172 string output_file = null;
\r
174 context = new RootContext ();
\r
175 references = new ArrayList ();
\r
176 link_paths = new ArrayList ();
\r
181 link_paths.Add ("file:///C:/WINNT/Microsoft.NET/Framework/v1.0.2914");
\r
183 for (i = 0; i < args.Length; i++){
\r
184 string arg = args [i];
\r
186 if (arg.StartsWith ("-")){
\r
187 if (arg.StartsWith ("-v")){
\r
188 yacc_verbose = true;
\r
192 if (arg.StartsWith ("--dumper")){
\r
193 generator = lookup_dumper (args [++i]);
\r
197 if (arg.StartsWith ("-z")){
\r
198 generator.ParseOptions (args [++i]);
\r
202 if (arg.StartsWith ("-o")){
\r
204 output_file = args [++i];
\r
205 } catch (Exception){
\r
206 error ("Could not write to `"+args [i]);
\r
213 if (arg.StartsWith ("--target")){
\r
214 string type = args [++i];
\r
218 target = Target.Library;
\r
222 target = Target.Exe;
\r
226 target = Target.WinExe;
\r
230 target = Target.Module;
\r
235 if (arg.StartsWith ("-r")){
\r
236 references.Add (args [++i]);
\r
240 if (arg.StartsWith ("-L")){
\r
241 link_paths.Add (args [++i]);
\r
245 if (arg == "--nostdlib"){
\r
246 context.StdLib = false;
\r
254 if (!arg.EndsWith (".cs")){
\r
256 error ("Do not know how to compile " + arg);
\r
261 if (first_source == null)
\r
262 first_source = arg;
\r
264 errors += parse (arg);
\r
267 if (first_source == null){
\r
268 context.Report.Error (2008, "No files to compile were specified");
\r
273 // Load Core Library for default compilation
\r
275 if (context.StdLib)
\r
276 references.Insert (0, "mscorlib");
\r
279 error ("Parsing failed");
\r
282 notice ("Parsing successful");
\r
285 // Load assemblies required
\r
287 errors += LoadReferences ();
\r
290 error ("Could not load one or more assemblies");
\r
296 // Dumping the parsed tree.
\r
298 // This code generation interface is only here
\r
299 // for debugging the parser.
\r
301 if (generator != null){
\r
302 if (output_file == null){
\r
303 error ("Error: no output file specified");
\r
307 Stream output_stream = File.Create (output_file);
\r
308 StreamWriter output = new StreamWriter (output_stream);
\r
310 errors += generator.Dump (context.Tree, output);
\r
313 error ("Compilation failed");
\r
316 notice ("Compilation successful");
\r
323 error_count = errors;
\r
328 if (output_file == null){
\r
329 int pos = first_source.LastIndexOf (".");
\r
331 output_file = first_source.Substring (0, pos) + ".exe";
\r
334 context.CodeGen = new CodeGen (output_file, output_file);
\r
337 // The second pass of the compiler
\r
339 context.ResolveTree ();
\r
340 context.PopulateTypes ();
\r
343 // Before emitting, we need to get the core
\r
344 // types emitted from the user defined types
\r
345 // or from the system ones.
\r
347 context.TypeManager.InitCoreTypes ();
\r
350 // The code generator
\r
352 context.EmitCode ();
\r
354 if (context.Report.Errors > 0){
\r
355 error ("Compilation failed");
\r
359 context.CloseTypes ();
\r
361 PEFileKinds k = PEFileKinds.ConsoleApplication;
\r
363 if (target == Target.Library || target == Target.Module)
\r
364 k = PEFileKinds.Dll;
\r
365 else if (target == Target.Exe)
\r
366 k = PEFileKinds.ConsoleApplication;
\r
367 else if (target == Target.WinExe)
\r
368 k = PEFileKinds.WindowApplication;
\r
370 if (target == Target.Exe || target == Target.WinExe){
\r
371 MethodInfo ep = context.EntryPoint;
\r
373 Console.WriteLine ("Setting entry point!");
\r
375 context.Report.Error (5001, "Program " + output_file +
\r
376 " does not have an entry point defined");
\r
380 context.CodeGen.AssemblyBuilder.SetEntryPoint (ep, k);
\r
383 context.CodeGen.Save (output_file);
\r
385 notice ("Success");
\r