3 // Main Command line interface for Mono ILasm Compiler
6 // Jackson Harper (Jackson@LatitudeGeo.com)
8 // (C) 2003 Jackson Harper, All rights reserved
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 using System
.Reflection
;
15 using System
.Collections
;
16 using System
.Security
.Cryptography
;
19 namespace Mono
.ILASM
{
28 public static int Main (string[] args
)
30 // Do everything in Invariant
31 System
.Threading
.Thread
.CurrentThread
.CurrentCulture
= System
.Globalization
.CultureInfo
.InvariantCulture
;
33 DriverMain driver
= new DriverMain (args
);
36 Report
.Message ("Operation completed successfully");
40 private class DriverMain
{
42 private ArrayList il_file_list
;
43 private string output_file
;
44 private Target target
= Target
.Exe
;
45 private string target_string
= "exe";
46 private bool show_tokens
= false;
47 // private bool show_method_def = false;
48 // private bool show_method_ref = false;
49 private bool show_parser
= false;
50 private bool scan_only
= false;
51 private bool debugging_info
= false;
52 private CodeGen codegen
;
53 private bool keycontainer
= false;
54 private string keyname
;
55 private StrongName sn
;
58 public DriverMain (string[] args
)
60 il_file_list
= new ArrayList ();
66 if (il_file_list
.Count
== 0)
68 if (output_file
== null)
69 output_file
= CreateOutputFilename ();
71 codegen
= new CodeGen (output_file
, target
== Target
.Dll
, debugging_info
, noautoinherit
);
72 foreach (string file_path
in il_file_list
) {
73 Report
.FilePath
= file_path
;
74 ProcessFile (file_path
);
79 if (Report
.ErrorCount
> 0)
82 if (target
!= Target
.Dll
&& !codegen
.HasEntryPoint
)
83 Report
.Error ("No entry point found.");
85 // if we have a key and aren't assembling a netmodule
86 if ((keyname
!= null) && !codegen
.IsThisAssembly (null)) {
88 // this overrides any attribute or .publickey directive in the source
89 codegen
.ThisAssembly
.SetPublicKey (sn
.PublicKey
);
95 File
.Delete (output_file
);
98 } catch (ILAsmException e
) {
99 Error (e
.ToString ());
101 } catch (PEAPI
.PEFileException pe
) {
102 Error ("Error : " + pe
.Message
);
108 Report
.Message ("Signing assembly with the specified strongname keypair");
109 return Sign (output_file
);
118 private void Error (string message
)
120 Console
.WriteLine (message
+ "\n");
121 Console
.WriteLine ("***** FAILURE *****\n");
124 private void LoadKey ()
127 CspParameters csp
= new CspParameters ();
128 csp
.KeyContainerName
= keyname
;
129 RSACryptoServiceProvider rsa
= new RSACryptoServiceProvider (csp
);
130 sn
= new StrongName (rsa
);
133 using (FileStream fs
= File
.OpenRead (keyname
)) {
134 data
= new byte [fs
.Length
];
135 fs
.Read (data
, 0, data
.Length
);
138 sn
= new StrongName (data
);
142 private bool Sign (string filename
)
144 // note: if the file cannot be signed (no public key in it) then
145 // we do not show an error, or a warning, if the key file doesn't
147 return sn
.Sign (filename
);
150 private void ProcessFile (string file_path
)
152 if (!File
.Exists (file_path
)) {
153 Console
.WriteLine ("File does not exist: {0}",
155 Environment
.Exit (2);
157 Report
.AssembleFile (file_path
, null,
158 target_string
, output_file
);
159 StreamReader reader
= File
.OpenText (file_path
);
160 ILTokenizer scanner
= new ILTokenizer (reader
);
163 scanner
.NewTokenEvent
+= new NewTokenEvent (ShowToken
);
164 //if (show_method_def)
165 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
166 //if (show_method_ref)
167 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
171 while ((tok
= scanner
.NextToken
) != ILToken
.EOF
) {
172 Console
.WriteLine (tok
);
177 ILParser parser
= new ILParser (codegen
, scanner
);
178 codegen
.BeginSourceFile (file_path
);
181 parser
.yyparse (new ScannerAdapter (scanner
),
182 new yydebug
.yyDebugSimple ());
184 parser
.yyparse (new ScannerAdapter (scanner
), null);
185 } catch (ILTokenizingException ilte
) {
186 Report
.Error (ilte
.Location
, "syntax error at token '" + ilte
.Token
+ "'");
187 } catch (Mono
.ILASM
.yyParser
.yyException ye
) {
188 Report
.Error (scanner
.Reader
.Location
, ye
.Message
);
189 } catch (ILAsmException ie
) {
190 ie
.FilePath
= file_path
;
191 ie
.Location
= scanner
.Reader
.Location
;
194 Console
.Write ("{0} ({1}, {2}): ",file_path
, scanner
.Reader
.Location
.line
, scanner
.Reader
.Location
.column
);
197 codegen
.EndSourceFile ();
201 public void ShowToken (object sender
, NewTokenEventArgs args
)
203 Console
.WriteLine ("token: '{0}'", args
.Token
);
206 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
208 Console.WriteLine ("***** Method defined *****");
209 Console.WriteLine ("-- signature: {0}", args.Signature);
210 Console.WriteLine ("-- name: {0}", args.Name);
211 Console.WriteLine ("-- return type: {0}", args.ReturnType);
212 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
213 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
214 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
215 Console.WriteLine ("-- call conv: {0}", args.CallConv);
218 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
220 Console.WriteLine ("***** Method referenced *****");
221 Console.WriteLine ("-- signature: {0}", args.Signature);
222 Console.WriteLine ("-- name: {0}", args.Name);
223 Console.WriteLine ("-- return type: {0}", args.ReturnType);
224 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
227 private void ParseArgs (string[] args
)
230 foreach (string str
in args
) {
231 if ((str
[0] != '-') && (str
[0] != '/')) {
232 il_file_list
.Add (str
);
235 switch (GetCommand (str
, out command_arg
)) {
238 output_file
= command_arg
;
242 target_string
= "exe";
246 target_string
= "dll";
253 debugging_info
= true;
255 // Stubs to stay commandline compatible with MS
267 if (command_arg
.Length
> 0)
268 keycontainer
= (command_arg
[0] == '@');
270 keyname
= command_arg
.Substring (1);
272 keyname
= command_arg
;
274 case "noautoinherit":
275 noautoinherit
= true;
283 case "show_method_def":
284 // show_method_def = true;
286 case "show_method_ref":
287 // show_method_ref = true;
305 il_file_list
.Add (str
);
311 private string GetCommand (string str
, out string command_arg
)
313 int end_index
= str
.IndexOfAny (new char[] {':', '='}
, 1);
314 string command
= str
.Substring (1,
315 end_index
== -1 ? str
.Length
- 1 : end_index
- 1);
317 if (end_index
!= -1) {
318 command_arg
= str
.Substring (end_index
+1);
323 return command
.ToLower ();
327 /// Get the first file name and makes it into an output file name
329 private string CreateOutputFilename ()
331 string file_name
= (string)il_file_list
[0];
332 int ext_index
= file_name
.LastIndexOf ('.');
335 ext_index
= file_name
.Length
;
337 return String
.Format ("{0}.{1}", file_name
.Substring (0, ext_index
),
341 private void Usage ()
343 Console
.WriteLine ("Mono IL assembler compiler\n" +
344 "ilasm [options] source-files\n" +
345 " --about About the Mono IL assembler compiler\n" +
346 " --version Print the version number of the compiler\n" +
347 " /output:file_name Specifies output file.\n" +
348 " /exe Compile to executable.\n" +
349 " /dll Compile to library.\n" +
350 " /debug Include debug information.\n" +
351 " /key:keyfile Strongname using the specified key file\n" +
352 " /key:@container Strongname using the specified key container\n" +
353 " /noautoinherit Disable inheriting from System.Object by default\n" +
354 "Options can be of the form -option or /option\n");
355 Environment
.Exit (1);
358 private void About ()
361 "For more information on Mono, visit the project Web site\n" +
362 " http://www.go-mono.com\n\n");
363 Environment
.Exit (0);
366 private void Version ()
368 string version
= System
.Reflection
.Assembly
.GetExecutingAssembly ().GetName ().Version
.ToString ();
369 Console
.WriteLine ("Mono IL assembler compiler version {0}", version
);
370 Environment
.Exit (0);